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