Skip to content

Commit 466ba9a

Browse files
committed
Fix resolve imports
1 parent 5ea241b commit 466ba9a

File tree

6 files changed

+836
-1054
lines changed

6 files changed

+836
-1054
lines changed

.changeset/tidy-ligers-matter.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@code-hike/lighter": patch
3+
---
4+
5+
Fix resolve imports

lib/dist/index.cjs.js

+334-449
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dist/index.esm.mjs

+334-443
Large diffs are not rendered by default.

lib/rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import json from "@rollup/plugin-json";
77
const pkg = require("./package.json");
88

99
export default {
10-
inlineDynamicImports: true,
1110
input: "src/index.js",
1211
output: [
1312
{
@@ -22,6 +21,7 @@ export default {
2221
plugins: [
2322
replace({
2423
__LIGHTER_VERSION__: `"${pkg.version}"`,
24+
preventAssignment: false,
2525
}),
2626
json(),
2727
nodeResolve(),

lib/src/file-system.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import resolveSync from "./resolve";
2+
13
export async function readJSON(folder, filename) {
24
const fs = await import("fs").then((m) => m.promises);
35
const path = await import("path");
@@ -13,7 +15,6 @@ export async function readJSON(folder, filename) {
1315
return JSON.parse(result);
1416
} catch (e) {
1517
console.log("using resolve");
16-
const { default: resolveSync } = await import("./resolve");
1718
const indexFile = resolveSync("@code-hike/lighter", { basedir: __dirname });
1819
let folderPath = path.resolve(indexFile, "..", "..", folder);
1920
let filepath = path.resolve(folderPath, filename);

lib/src/resolve.js

+160-160
Original file line numberDiff line numberDiff line change
@@ -1,185 +1,185 @@
11
// from https://github.com/browserify/resolve
22
// removed the "is-core-module" dependency because it fails on edge runtime
33

4-
var fs = require("fs");
5-
var path = require("path");
6-
var parse = path.parse || require("path-parse"); // eslint-disable-line global-require
4+
export default function resolveSync(x, options) {
5+
var fs = require("fs");
6+
var path = require("path");
7+
var parse = path.parse || require("path-parse"); // eslint-disable-line global-require
8+
9+
var os = require("os");
10+
11+
function normalizeOptions(x, opts) {
12+
/**
13+
* This file is purposefully a passthrough. It's expected that third-party
14+
* environments will override it at runtime in order to inject special logic
15+
* into `resolve` (by manipulating the options). One such example is the PnP
16+
* code path in Yarn.
17+
*/
18+
19+
return opts || {};
20+
}
721

8-
var os = require("os");
22+
var getNodeModulesDirs = function getNodeModulesDirs(absoluteStart, modules) {
23+
var prefix = "/";
24+
if (/^([A-Za-z]:)/.test(absoluteStart)) {
25+
prefix = "";
26+
} else if (/^\\\\/.test(absoluteStart)) {
27+
prefix = "\\\\";
28+
}
929

10-
function normalizeOptions(x, opts) {
11-
/**
12-
* This file is purposefully a passthrough. It's expected that third-party
13-
* environments will override it at runtime in order to inject special logic
14-
* into `resolve` (by manipulating the options). One such example is the PnP
15-
* code path in Yarn.
16-
*/
30+
var paths = [absoluteStart];
31+
var parsed = parse(absoluteStart);
32+
while (parsed.dir !== paths[paths.length - 1]) {
33+
paths.push(parsed.dir);
34+
parsed = parse(parsed.dir);
35+
}
1736

18-
return opts || {};
19-
}
37+
return paths.reduce(function (dirs, aPath) {
38+
return dirs.concat(
39+
modules.map(function (moduleDir) {
40+
return path.resolve(prefix, aPath, moduleDir);
41+
})
42+
);
43+
}, []);
44+
};
2045

21-
var getNodeModulesDirs = function getNodeModulesDirs(absoluteStart, modules) {
22-
var prefix = "/";
23-
if (/^([A-Za-z]:)/.test(absoluteStart)) {
24-
prefix = "";
25-
} else if (/^\\\\/.test(absoluteStart)) {
26-
prefix = "\\\\";
27-
}
46+
function nodeModulesPaths(start, opts, request) {
47+
var modules =
48+
opts && opts.moduleDirectory
49+
? [].concat(opts.moduleDirectory)
50+
: ["node_modules"];
51+
52+
if (opts && typeof opts.paths === "function") {
53+
return opts.paths(
54+
request,
55+
start,
56+
function () {
57+
return getNodeModulesDirs(start, modules);
58+
},
59+
opts
60+
);
61+
}
2862

29-
var paths = [absoluteStart];
30-
var parsed = parse(absoluteStart);
31-
while (parsed.dir !== paths[paths.length - 1]) {
32-
paths.push(parsed.dir);
33-
parsed = parse(parsed.dir);
63+
var dirs = getNodeModulesDirs(start, modules);
64+
return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
3465
}
3566

36-
return paths.reduce(function (dirs, aPath) {
37-
return dirs.concat(
38-
modules.map(function (moduleDir) {
39-
return path.resolve(prefix, aPath, moduleDir);
40-
})
41-
);
42-
}, []);
43-
};
44-
45-
function nodeModulesPaths(start, opts, request) {
46-
var modules =
47-
opts && opts.moduleDirectory
48-
? [].concat(opts.moduleDirectory)
49-
: ["node_modules"];
50-
51-
if (opts && typeof opts.paths === "function") {
52-
return opts.paths(
53-
request,
54-
start,
55-
function () {
56-
return getNodeModulesDirs(start, modules);
57-
},
58-
opts
59-
);
60-
}
67+
const getHomedir =
68+
os.homedir ||
69+
function homedir() {
70+
var home = process.env.HOME;
71+
var user =
72+
process.env.LOGNAME ||
73+
process.env.USER ||
74+
process.env.LNAME ||
75+
process.env.USERNAME;
76+
77+
if (process.platform === "win32") {
78+
return (
79+
process.env.USERPROFILE ||
80+
process.env.HOMEDRIVE + process.env.HOMEPATH ||
81+
home ||
82+
null
83+
);
84+
}
6185

62-
var dirs = getNodeModulesDirs(start, modules);
63-
return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
64-
}
86+
if (process.platform === "darwin") {
87+
return home || (user ? "/Users/" + user : null);
88+
}
6589

66-
const getHomedir =
67-
os.homedir ||
68-
function homedir() {
69-
var home = process.env.HOME;
70-
var user =
71-
process.env.LOGNAME ||
72-
process.env.USER ||
73-
process.env.LNAME ||
74-
process.env.USERNAME;
75-
76-
if (process.platform === "win32") {
77-
return (
78-
process.env.USERPROFILE ||
79-
process.env.HOMEDRIVE + process.env.HOMEPATH ||
80-
home ||
81-
null
82-
);
90+
if (process.platform === "linux") {
91+
return (
92+
home ||
93+
(process.getuid() === 0 ? "/root" : user ? "/home/" + user : null)
94+
); // eslint-disable-line no-extra-parens
95+
}
96+
97+
return home || null;
98+
};
99+
100+
var caller = function () {
101+
// see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
102+
var origPrepareStackTrace = Error.prepareStackTrace;
103+
Error.prepareStackTrace = function (_, stack) {
104+
return stack;
105+
};
106+
var stack = new Error().stack;
107+
Error.prepareStackTrace = origPrepareStackTrace;
108+
return stack[2].getFileName();
109+
};
110+
111+
var realpathFS =
112+
process.platform !== "win32" &&
113+
fs.realpathSync &&
114+
typeof fs.realpathSync.native === "function"
115+
? fs.realpathSync.native
116+
: fs.realpathSync;
117+
118+
var homedir = getHomedir();
119+
var defaultPaths = function () {
120+
return [
121+
path.join(homedir, ".node_modules"),
122+
path.join(homedir, ".node_libraries"),
123+
];
124+
};
125+
126+
var defaultIsFile = function isFile(file) {
127+
try {
128+
var stat = fs.statSync(file, { throwIfNoEntry: false });
129+
} catch (e) {
130+
if (e && (e.code === "ENOENT" || e.code === "ENOTDIR")) return false;
131+
throw e;
83132
}
133+
return !!stat && (stat.isFile() || stat.isFIFO());
134+
};
84135

85-
if (process.platform === "darwin") {
86-
return home || (user ? "/Users/" + user : null);
136+
var defaultIsDir = function isDirectory(dir) {
137+
try {
138+
var stat = fs.statSync(dir, { throwIfNoEntry: false });
139+
} catch (e) {
140+
if (e && (e.code === "ENOENT" || e.code === "ENOTDIR")) return false;
141+
throw e;
87142
}
143+
return !!stat && stat.isDirectory();
144+
};
88145

89-
if (process.platform === "linux") {
90-
return (
91-
home ||
92-
(process.getuid() === 0 ? "/root" : user ? "/home/" + user : null)
93-
); // eslint-disable-line no-extra-parens
146+
var defaultRealpathSync = function realpathSync(x) {
147+
try {
148+
return realpathFS(x);
149+
} catch (realpathErr) {
150+
if (realpathErr.code !== "ENOENT") {
151+
throw realpathErr;
152+
}
94153
}
154+
return x;
155+
};
95156

96-
return home || null;
157+
var maybeRealpathSync = function maybeRealpathSync(realpathSync, x, opts) {
158+
if (opts && opts.preserveSymlinks === false) {
159+
return realpathSync(x);
160+
}
161+
return x;
97162
};
98163

99-
var caller = function () {
100-
// see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
101-
var origPrepareStackTrace = Error.prepareStackTrace;
102-
Error.prepareStackTrace = function (_, stack) {
103-
return stack;
164+
var defaultReadPackageSync = function defaultReadPackageSync(
165+
readFileSync,
166+
pkgfile
167+
) {
168+
var body = readFileSync(pkgfile);
169+
try {
170+
var pkg = JSON.parse(body);
171+
return pkg;
172+
} catch (jsonErr) {}
104173
};
105-
var stack = new Error().stack;
106-
Error.prepareStackTrace = origPrepareStackTrace;
107-
return stack[2].getFileName();
108-
};
109-
110-
var realpathFS =
111-
process.platform !== "win32" &&
112-
fs.realpathSync &&
113-
typeof fs.realpathSync.native === "function"
114-
? fs.realpathSync.native
115-
: fs.realpathSync;
116-
117-
var homedir = getHomedir();
118-
var defaultPaths = function () {
119-
return [
120-
path.join(homedir, ".node_modules"),
121-
path.join(homedir, ".node_libraries"),
122-
];
123-
};
124-
125-
var defaultIsFile = function isFile(file) {
126-
try {
127-
var stat = fs.statSync(file, { throwIfNoEntry: false });
128-
} catch (e) {
129-
if (e && (e.code === "ENOENT" || e.code === "ENOTDIR")) return false;
130-
throw e;
131-
}
132-
return !!stat && (stat.isFile() || stat.isFIFO());
133-
};
134-
135-
var defaultIsDir = function isDirectory(dir) {
136-
try {
137-
var stat = fs.statSync(dir, { throwIfNoEntry: false });
138-
} catch (e) {
139-
if (e && (e.code === "ENOENT" || e.code === "ENOTDIR")) return false;
140-
throw e;
141-
}
142-
return !!stat && stat.isDirectory();
143-
};
144-
145-
var defaultRealpathSync = function realpathSync(x) {
146-
try {
147-
return realpathFS(x);
148-
} catch (realpathErr) {
149-
if (realpathErr.code !== "ENOENT") {
150-
throw realpathErr;
151-
}
152-
}
153-
return x;
154-
};
155174

156-
var maybeRealpathSync = function maybeRealpathSync(realpathSync, x, opts) {
157-
if (opts && opts.preserveSymlinks === false) {
158-
return realpathSync(x);
159-
}
160-
return x;
161-
};
162-
163-
var defaultReadPackageSync = function defaultReadPackageSync(
164-
readFileSync,
165-
pkgfile
166-
) {
167-
var body = readFileSync(pkgfile);
168-
try {
169-
var pkg = JSON.parse(body);
170-
return pkg;
171-
} catch (jsonErr) {}
172-
};
173-
174-
var getPackageCandidates = function getPackageCandidates(x, start, opts) {
175-
var dirs = nodeModulesPaths(start, opts, x);
176-
for (var i = 0; i < dirs.length; i++) {
177-
dirs[i] = path.join(dirs[i], x);
178-
}
179-
return dirs;
180-
};
175+
var getPackageCandidates = function getPackageCandidates(x, start, opts) {
176+
var dirs = nodeModulesPaths(start, opts, x);
177+
for (var i = 0; i < dirs.length; i++) {
178+
dirs[i] = path.join(dirs[i], x);
179+
}
180+
return dirs;
181+
};
181182

182-
module.exports = function resolveSync(x, options) {
183183
if (typeof x !== "string") {
184184
throw new TypeError("Path must be a string.");
185185
}
@@ -330,4 +330,4 @@ module.exports = function resolveSync(x, options) {
330330
}
331331
}
332332
}
333-
};
333+
}

0 commit comments

Comments
 (0)