vite-node 3.0.9 → 3.1.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/utils.cjs CHANGED
@@ -8,195 +8,180 @@ var pathe = require('pathe');
8
8
  const isWindows = process.platform === "win32";
9
9
  const drive = isWindows ? process.cwd()[0] : null;
10
10
  const driveOpposite = drive ? drive === drive.toUpperCase() ? drive.toLowerCase() : drive.toUpperCase() : null;
11
- const driveRegexp = drive ? new RegExp(`(?:^|/@fs/)${drive}(:[\\/])`) : null;
12
- const driveOppositeRegext = driveOpposite ? new RegExp(`(?:^|/@fs/)${driveOpposite}(:[\\/])`) : null;
11
+ const driveRegexp = drive ? new RegExp(`(?:^|/@fs/)${drive}(\:[\\/])`) : null;
12
+ const driveOppositeRegext = driveOpposite ? new RegExp(`(?:^|/@fs/)${driveOpposite}(\:[\\/])`) : null;
13
13
  function slash(str) {
14
- return str.replace(/\\/g, "/");
14
+ return str.replace(/\\/g, "/");
15
15
  }
16
16
  const VALID_ID_PREFIX = "/@id/";
17
17
  function normalizeRequestId(id, base) {
18
- if (base && id.startsWith(withTrailingSlash(base))) {
19
- id = `/${id.slice(base.length)}`;
20
- }
21
- if (driveRegexp && !(driveRegexp == null ? void 0 : driveRegexp.test(id)) && (driveOppositeRegext == null ? void 0 : driveOppositeRegext.test(id))) {
22
- id = id.replace(driveOppositeRegext, `${drive}$1`);
23
- }
24
- if (id.startsWith("file://")) {
25
- const { file, postfix } = splitFileAndPostfix(id);
26
- return node_url.fileURLToPath(file) + postfix;
27
- }
28
- return id.replace(/^\/@id\/__x00__/, "\0").replace(/^\/@id\//, "").replace(/^__vite-browser-external:/, "").replace(/\?v=\w+/, "?").replace(/&v=\w+/, "").replace(/\?t=\w+/, "?").replace(/&t=\w+/, "").replace(/\?import/, "?").replace(/&import/, "").replace(/\?&/, "?").replace(/\?+$/, "");
18
+ if (base && id.startsWith(withTrailingSlash(base))) id = `/${id.slice(base.length)}`;
19
+ if (driveRegexp && !(driveRegexp === null || driveRegexp === void 0 ? void 0 : driveRegexp.test(id)) && (driveOppositeRegext === null || driveOppositeRegext === void 0 ? void 0 : driveOppositeRegext.test(id))) id = id.replace(driveOppositeRegext, `${drive}$1`);
20
+ if (id.startsWith("file://")) {
21
+ const { file, postfix } = splitFileAndPostfix(id);
22
+ return node_url.fileURLToPath(file) + postfix;
23
+ }
24
+ return id.replace(/^\/@id\/__x00__/, "\0").replace(/^\/@id\//, "").replace(/^__vite-browser-external:/, "").replace(/\?v=\w+/, "?").replace(/&v=\w+/, "").replace(/\?t=\w+/, "?").replace(/&t=\w+/, "").replace(/\?import/, "?").replace(/&import/, "").replace(/\?&/, "?").replace(/\?+$/, "");
29
25
  }
30
26
  const postfixRE = /[?#].*$/;
31
27
  function cleanUrl(url) {
32
- return url.replace(postfixRE, "");
28
+ return url.replace(postfixRE, "");
33
29
  }
34
30
  function splitFileAndPostfix(path) {
35
- const file = cleanUrl(path);
36
- return { file, postfix: path.slice(file.length) };
31
+ const file = cleanUrl(path);
32
+ return {
33
+ file,
34
+ postfix: path.slice(file.length)
35
+ };
37
36
  }
38
37
  const internalRequests = ["@vite/client", "@vite/env"];
39
- const internalRequestRegexp = new RegExp(
40
- `^/?(?:${internalRequests.join("|")})$`
41
- );
38
+ const internalRequestRegexp = new RegExp(`^/?(?:${internalRequests.join("|")})$`);
42
39
  function isInternalRequest(id) {
43
- return internalRequestRegexp.test(id);
40
+ return internalRequestRegexp.test(id);
44
41
  }
45
- const prefixedBuiltins = /* @__PURE__ */ new Set([
46
- "node:sea",
47
- "node:sqlite",
48
- "node:test",
49
- "node:test/reporters"
42
+ const prefixedBuiltins = new Set([
43
+ "node:sea",
44
+ "node:sqlite",
45
+ "node:test",
46
+ "node:test/reporters"
50
47
  ]);
51
- const builtins = /* @__PURE__ */ new Set([
52
- ...node_module.builtinModules,
53
- "assert/strict",
54
- "diagnostics_channel",
55
- "dns/promises",
56
- "fs/promises",
57
- "path/posix",
58
- "path/win32",
59
- "readline/promises",
60
- "stream/consumers",
61
- "stream/promises",
62
- "stream/web",
63
- "timers/promises",
64
- "util/types",
65
- "wasi"
48
+ const builtins = new Set([
49
+ ...node_module.builtinModules,
50
+ "assert/strict",
51
+ "diagnostics_channel",
52
+ "dns/promises",
53
+ "fs/promises",
54
+ "path/posix",
55
+ "path/win32",
56
+ "readline/promises",
57
+ "stream/consumers",
58
+ "stream/promises",
59
+ "stream/web",
60
+ "timers/promises",
61
+ "util/types",
62
+ "wasi"
66
63
  ]);
67
64
  function normalizeModuleId(id) {
68
- if (prefixedBuiltins.has(id)) {
69
- return id;
70
- }
71
- if (id.startsWith("file://")) {
72
- return node_url.fileURLToPath(id);
73
- }
74
- return id.replace(/\\/g, "/").replace(/^\/@fs\//, isWindows ? "" : "/").replace(/^node:/, "").replace(/^\/+/, "/");
65
+ if (prefixedBuiltins.has(id)) return id;
66
+ if (id.startsWith("file://")) return node_url.fileURLToPath(id);
67
+ return id.replace(/\\/g, "/").replace(/^\/@fs\//, isWindows ? "" : "/").replace(/^node:/, "").replace(/^\/+/, "/");
75
68
  }
76
69
  function isPrimitive(v) {
77
- return v !== Object(v);
70
+ return v !== Object(v);
78
71
  }
79
72
  function toFilePath(id, root) {
80
- let { absolute, exists } = (() => {
81
- if (id.startsWith("/@fs/")) {
82
- return { absolute: id.slice(4), exists: true };
83
- }
84
- if (!id.startsWith(withTrailingSlash(root)) && id.startsWith("/")) {
85
- const resolved = pathe.resolve(root, id.slice(1));
86
- if (fs.existsSync(cleanUrl(resolved))) {
87
- return { absolute: resolved, exists: true };
88
- }
89
- } else if (id.startsWith(withTrailingSlash(root)) && fs.existsSync(cleanUrl(id))) {
90
- return { absolute: id, exists: true };
91
- }
92
- return { absolute: id, exists: false };
93
- })();
94
- if (absolute.startsWith("//")) {
95
- absolute = absolute.slice(1);
96
- }
97
- return {
98
- path: isWindows && absolute.startsWith("/") ? slash(node_url.fileURLToPath(node_url.pathToFileURL(absolute.slice(1)).href)) : absolute,
99
- exists
100
- };
73
+ let { absolute, exists } = (() => {
74
+ if (id.startsWith("/@fs/")) return {
75
+ absolute: id.slice(4),
76
+ exists: true
77
+ };
78
+ if (!id.startsWith(withTrailingSlash(root)) && id.startsWith("/")) {
79
+ const resolved = pathe.resolve(root, id.slice(1));
80
+ if (fs.existsSync(cleanUrl(resolved))) return {
81
+ absolute: resolved,
82
+ exists: true
83
+ };
84
+ } else if (id.startsWith(withTrailingSlash(root)) && fs.existsSync(cleanUrl(id))) return {
85
+ absolute: id,
86
+ exists: true
87
+ };
88
+ return {
89
+ absolute: id,
90
+ exists: false
91
+ };
92
+ })();
93
+ if (absolute.startsWith("//")) absolute = absolute.slice(1);
94
+ return {
95
+ path: isWindows && absolute.startsWith("/") ? slash(node_url.fileURLToPath(node_url.pathToFileURL(absolute.slice(1)).href)) : absolute,
96
+ exists
97
+ };
101
98
  }
102
99
  const NODE_BUILTIN_NAMESPACE = "node:";
103
100
  function isNodeBuiltin(id) {
104
- if (prefixedBuiltins.has(id)) {
105
- return true;
106
- }
107
- return builtins.has(
108
- id.startsWith(NODE_BUILTIN_NAMESPACE) ? id.slice(NODE_BUILTIN_NAMESPACE.length) : id
109
- );
110
- }
101
+ if (prefixedBuiltins.has(id)) return true;
102
+ return builtins.has(id.startsWith(NODE_BUILTIN_NAMESPACE) ? id.slice(NODE_BUILTIN_NAMESPACE.length) : id);
103
+ }
104
+ /**
105
+ * Convert `Arrayable<T>` to `Array<T>`
106
+ *
107
+ * @category Array
108
+ */
111
109
  function toArray(array) {
112
- if (array === null || array === void 0) {
113
- array = [];
114
- }
115
- if (Array.isArray(array)) {
116
- return array;
117
- }
118
- return [array];
110
+ if (array === null || array === void 0) array = [];
111
+ if (Array.isArray(array)) return array;
112
+ return [array];
119
113
  }
120
114
  function getCachedData(cache, basedir, originalBasedir) {
121
- const pkgData = cache.get(getFnpdCacheKey(basedir));
122
- if (pkgData) {
123
- traverseBetweenDirs(originalBasedir, basedir, (dir) => {
124
- cache.set(getFnpdCacheKey(dir), pkgData);
125
- });
126
- return pkgData;
127
- }
115
+ const pkgData = cache.get(getFnpdCacheKey(basedir));
116
+ if (pkgData) {
117
+ traverseBetweenDirs(originalBasedir, basedir, (dir) => {
118
+ cache.set(getFnpdCacheKey(dir), pkgData);
119
+ });
120
+ return pkgData;
121
+ }
128
122
  }
129
123
  function setCacheData(cache, data, basedir, originalBasedir) {
130
- cache.set(getFnpdCacheKey(basedir), data);
131
- traverseBetweenDirs(originalBasedir, basedir, (dir) => {
132
- cache.set(getFnpdCacheKey(dir), data);
133
- });
124
+ cache.set(getFnpdCacheKey(basedir), data);
125
+ traverseBetweenDirs(originalBasedir, basedir, (dir) => {
126
+ cache.set(getFnpdCacheKey(dir), data);
127
+ });
134
128
  }
135
129
  function getFnpdCacheKey(basedir) {
136
- return `fnpd_${basedir}`;
130
+ return `fnpd_${basedir}`;
137
131
  }
132
+ /**
133
+ * Traverse between `longerDir` (inclusive) and `shorterDir` (exclusive) and call `cb` for each dir.
134
+ * @param longerDir Longer dir path, e.g. `/User/foo/bar/baz`
135
+ * @param shorterDir Shorter dir path, e.g. `/User/foo`
136
+ */
138
137
  function traverseBetweenDirs(longerDir, shorterDir, cb) {
139
- while (longerDir !== shorterDir) {
140
- cb(longerDir);
141
- longerDir = pathe.dirname(longerDir);
142
- }
138
+ while (longerDir !== shorterDir) {
139
+ cb(longerDir);
140
+ longerDir = pathe.dirname(longerDir);
141
+ }
143
142
  }
144
143
  function withTrailingSlash(path) {
145
- if (path[path.length - 1] !== "/") {
146
- return `${path}/`;
147
- }
148
- return path;
144
+ if (path[path.length - 1] !== "/") return `${path}/`;
145
+ return path;
149
146
  }
150
147
  function createImportMetaEnvProxy() {
151
- const booleanKeys = ["DEV", "PROD", "SSR"];
152
- return new Proxy(process.env, {
153
- get(_, key) {
154
- if (typeof key !== "string") {
155
- return void 0;
156
- }
157
- if (booleanKeys.includes(key)) {
158
- return !!process.env[key];
159
- }
160
- return process.env[key];
161
- },
162
- set(_, key, value) {
163
- if (typeof key !== "string") {
164
- return true;
165
- }
166
- if (booleanKeys.includes(key)) {
167
- process.env[key] = value ? "1" : "";
168
- } else {
169
- process.env[key] = value;
170
- }
171
- return true;
172
- }
173
- });
174
- }
175
- const packageCache = /* @__PURE__ */ new Map();
148
+ const booleanKeys = [
149
+ "DEV",
150
+ "PROD",
151
+ "SSR"
152
+ ];
153
+ return new Proxy(process.env, {
154
+ get(_, key) {
155
+ if (typeof key !== "string") return void 0;
156
+ if (booleanKeys.includes(key)) return !!process.env[key];
157
+ return process.env[key];
158
+ },
159
+ set(_, key, value) {
160
+ if (typeof key !== "string") return true;
161
+ if (booleanKeys.includes(key)) process.env[key] = value ? "1" : "";
162
+ else process.env[key] = value;
163
+ return true;
164
+ }
165
+ });
166
+ }
167
+ const packageCache = new Map();
176
168
  async function findNearestPackageData(basedir) {
177
- var _a;
178
- const originalBasedir = basedir;
179
- while (basedir) {
180
- const cached = getCachedData(packageCache, basedir, originalBasedir);
181
- if (cached) {
182
- return cached;
183
- }
184
- const pkgPath = pathe.join(basedir, "package.json");
185
- if ((_a = await fs.promises.stat(pkgPath).catch(() => {
186
- })) == null ? void 0 : _a.isFile()) {
187
- const pkgData = JSON.parse(await fs.promises.readFile(pkgPath, "utf8"));
188
- if (packageCache) {
189
- setCacheData(packageCache, pkgData, basedir, originalBasedir);
190
- }
191
- return pkgData;
192
- }
193
- const nextBasedir = pathe.dirname(basedir);
194
- if (nextBasedir === basedir) {
195
- break;
196
- }
197
- basedir = nextBasedir;
198
- }
199
- return {};
169
+ const originalBasedir = basedir;
170
+ while (basedir) {
171
+ var _await$fsp$stat$catch;
172
+ const cached = getCachedData(packageCache, basedir, originalBasedir);
173
+ if (cached) return cached;
174
+ const pkgPath = pathe.join(basedir, "package.json");
175
+ if ((_await$fsp$stat$catch = await fs.promises.stat(pkgPath).catch(() => {})) === null || _await$fsp$stat$catch === void 0 ? void 0 : _await$fsp$stat$catch.isFile()) {
176
+ const pkgData = JSON.parse(await fs.promises.readFile(pkgPath, "utf8"));
177
+ if (packageCache) setCacheData(packageCache, pkgData, basedir, originalBasedir);
178
+ return pkgData;
179
+ }
180
+ const nextBasedir = pathe.dirname(basedir);
181
+ if (nextBasedir === basedir) break;
182
+ basedir = nextBasedir;
183
+ }
184
+ return {};
200
185
  }
201
186
 
202
187
  exports.VALID_ID_PREFIX = VALID_ID_PREFIX;