vite-node 0.1.18 → 0.1.22
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/client.d.ts +31 -3
- package/dist/cli.cjs +424 -0
- package/dist/cli.js +330 -9
- package/dist/client.cjs +204 -0
- package/dist/client.js +72 -36
- package/dist/index.cjs +2 -0
- package/dist/server.cjs +174 -0
- package/dist/server.js +22 -9
- package/dist/utils.cjs +31 -0
- package/dist/utils.js +1 -1
- package/index.d.ts +28 -4
- package/package.json +8 -4
- package/server.d.ts +26 -3
package/dist/cli.js
CHANGED
|
@@ -1,15 +1,333 @@
|
|
|
1
1
|
import minimist from 'minimist';
|
|
2
2
|
import { red, dim } from 'kolorist';
|
|
3
3
|
import { createServer } from 'vite';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import '
|
|
7
|
-
import '
|
|
8
|
-
import '
|
|
9
|
-
import '
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
import { existsSync } from 'fs';
|
|
5
|
+
import { isNodeBuiltin, isValidNodeImport } from 'mlly';
|
|
6
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
7
|
+
import { dirname, resolve } from 'pathe';
|
|
8
|
+
import { createRequire } from 'module';
|
|
9
|
+
import vm from 'vm';
|
|
10
|
+
|
|
11
|
+
const isWindows = process.platform === "win32";
|
|
12
|
+
function slash(str) {
|
|
13
|
+
return str.replace(/\\/g, "/");
|
|
14
|
+
}
|
|
15
|
+
function normalizeId(id, base) {
|
|
16
|
+
if (base && id.startsWith(base))
|
|
17
|
+
id = `/${id.slice(base.length)}`;
|
|
18
|
+
return id.replace(/^\/@id\/__x00__/, "\0").replace(/^\/@id\//, "").replace(/^__vite-browser-external:/, "").replace(/^node:/, "").replace(/[?&]v=\w+/, "?").replace(/\?$/, "");
|
|
19
|
+
}
|
|
20
|
+
function isPrimitive(v) {
|
|
21
|
+
return v !== Object(v);
|
|
22
|
+
}
|
|
23
|
+
function toFilePath(id, root) {
|
|
24
|
+
let absolute = slash(id).startsWith("/@fs/") ? id.slice(4) : id.startsWith(dirname(root)) && dirname(root) !== "/" ? id : id.startsWith("/") ? slash(resolve(root, id.slice(1))) : id;
|
|
25
|
+
if (absolute.startsWith("//"))
|
|
26
|
+
absolute = absolute.slice(1);
|
|
27
|
+
return isWindows && absolute.startsWith("/") ? fileURLToPath(pathToFileURL(absolute.slice(1)).href) : absolute;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ESM_EXT_RE = /\.(es|esm|esm-browser|esm-bundler|es6|module)\.js$/;
|
|
31
|
+
const ESM_FOLDER_RE = /\/esm\/(.*\.js)$/;
|
|
32
|
+
const defaultInline = [
|
|
33
|
+
/\/vitest\/dist\//,
|
|
34
|
+
/vitest-virtual-\w+\/dist/,
|
|
35
|
+
/virtual:/,
|
|
36
|
+
/\.ts$/,
|
|
37
|
+
ESM_EXT_RE,
|
|
38
|
+
ESM_FOLDER_RE
|
|
39
|
+
];
|
|
40
|
+
const depsExternal = [
|
|
41
|
+
/\.cjs.js$/,
|
|
42
|
+
/\.mjs$/
|
|
43
|
+
];
|
|
44
|
+
function guessCJSversion(id) {
|
|
45
|
+
if (id.match(ESM_EXT_RE)) {
|
|
46
|
+
for (const i of [
|
|
47
|
+
id.replace(ESM_EXT_RE, ".mjs"),
|
|
48
|
+
id.replace(ESM_EXT_RE, ".umd.js"),
|
|
49
|
+
id.replace(ESM_EXT_RE, ".cjs.js"),
|
|
50
|
+
id.replace(ESM_EXT_RE, ".js")
|
|
51
|
+
]) {
|
|
52
|
+
if (existsSync(i))
|
|
53
|
+
return i;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (id.match(ESM_FOLDER_RE)) {
|
|
57
|
+
for (const i of [
|
|
58
|
+
id.replace(ESM_FOLDER_RE, "/umd/$1"),
|
|
59
|
+
id.replace(ESM_FOLDER_RE, "/cjs/$1"),
|
|
60
|
+
id.replace(ESM_FOLDER_RE, "/$1")
|
|
61
|
+
]) {
|
|
62
|
+
if (existsSync(i))
|
|
63
|
+
return i;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function shouldExternalize(id, options, cache = new Map()) {
|
|
68
|
+
if (!cache.has(id))
|
|
69
|
+
cache.set(id, _shouldExternalize(id, options));
|
|
70
|
+
return cache.get(id);
|
|
71
|
+
}
|
|
72
|
+
async function _shouldExternalize(id, options) {
|
|
73
|
+
if (isNodeBuiltin(id))
|
|
74
|
+
return id;
|
|
75
|
+
id = patchWindowsImportPath(id);
|
|
76
|
+
if (matchExternalizePattern(id, options == null ? void 0 : options.inline))
|
|
77
|
+
return false;
|
|
78
|
+
if (matchExternalizePattern(id, options == null ? void 0 : options.external))
|
|
79
|
+
return id;
|
|
80
|
+
const isNodeModule = id.includes("/node_modules/");
|
|
81
|
+
id = isNodeModule ? guessCJSversion(id) || id : id;
|
|
82
|
+
if (matchExternalizePattern(id, defaultInline))
|
|
83
|
+
return false;
|
|
84
|
+
if (matchExternalizePattern(id, depsExternal))
|
|
85
|
+
return id;
|
|
86
|
+
if (isNodeModule && await isValidNodeImport(id))
|
|
87
|
+
return id;
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
function matchExternalizePattern(id, patterns) {
|
|
91
|
+
if (!patterns)
|
|
92
|
+
return false;
|
|
93
|
+
for (const ex of patterns) {
|
|
94
|
+
if (typeof ex === "string") {
|
|
95
|
+
if (id.includes(`/node_modules/${ex}/`))
|
|
96
|
+
return true;
|
|
97
|
+
} else {
|
|
98
|
+
if (ex.test(id))
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
function patchWindowsImportPath(path) {
|
|
105
|
+
if (path.match(/^\w:\\/))
|
|
106
|
+
return `file:///${slash(path)}`;
|
|
107
|
+
else if (path.match(/^\w:\//))
|
|
108
|
+
return `file:///${path}`;
|
|
109
|
+
else
|
|
110
|
+
return path;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let SOURCEMAPPING_URL = "sourceMa";
|
|
114
|
+
SOURCEMAPPING_URL += "ppingURL";
|
|
115
|
+
class ViteNodeServer {
|
|
116
|
+
constructor(server, options = {}) {
|
|
117
|
+
this.server = server;
|
|
118
|
+
this.options = options;
|
|
119
|
+
this.promiseMap = new Map();
|
|
120
|
+
}
|
|
121
|
+
shouldExternalize(id) {
|
|
122
|
+
return shouldExternalize(id, this.options.deps);
|
|
123
|
+
}
|
|
124
|
+
async fetchModule(id) {
|
|
125
|
+
const externalize = await this.shouldExternalize(toFilePath(id, this.server.config.root));
|
|
126
|
+
if (externalize)
|
|
127
|
+
return { externalize };
|
|
128
|
+
const r = await this.transformRequest(id);
|
|
129
|
+
return { code: r == null ? void 0 : r.code };
|
|
130
|
+
}
|
|
131
|
+
async resolveId(id, importer) {
|
|
132
|
+
return this.server.pluginContainer.resolveId(id, importer, { ssr: true });
|
|
133
|
+
}
|
|
134
|
+
async transformRequest(id) {
|
|
135
|
+
if (!this.promiseMap.has(id)) {
|
|
136
|
+
this.promiseMap.set(id, this._transformRequest(id).finally(() => {
|
|
137
|
+
this.promiseMap.delete(id);
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
return this.promiseMap.get(id);
|
|
141
|
+
}
|
|
142
|
+
getTransformMode(id) {
|
|
143
|
+
var _a, _b, _c, _d;
|
|
144
|
+
const withoutQuery = id.split("?")[0];
|
|
145
|
+
if ((_b = (_a = this.options.transformMode) == null ? void 0 : _a.web) == null ? void 0 : _b.some((r) => withoutQuery.match(r)))
|
|
146
|
+
return "web";
|
|
147
|
+
if ((_d = (_c = this.options.transformMode) == null ? void 0 : _c.ssr) == null ? void 0 : _d.some((r) => withoutQuery.match(r)))
|
|
148
|
+
return "ssr";
|
|
149
|
+
if (withoutQuery.match(/\.([cm]?[jt]sx?|json)$/))
|
|
150
|
+
return "ssr";
|
|
151
|
+
return "web";
|
|
152
|
+
}
|
|
153
|
+
async _transformRequest(id) {
|
|
154
|
+
let result = null;
|
|
155
|
+
const mode = this.getTransformMode(id);
|
|
156
|
+
if (mode === "web") {
|
|
157
|
+
result = await this.server.transformRequest(id);
|
|
158
|
+
if (result)
|
|
159
|
+
result = await this.server.ssrTransform(result.code, result.map, id);
|
|
160
|
+
} else {
|
|
161
|
+
result = await this.server.transformRequest(id, { ssr: true });
|
|
162
|
+
}
|
|
163
|
+
if (this.options.sourcemap !== false && result && !id.includes("node_modules"))
|
|
164
|
+
withInlineSourcemap(result);
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async function withInlineSourcemap(result) {
|
|
169
|
+
const { code, map } = result;
|
|
170
|
+
if (code.includes(`${SOURCEMAPPING_URL}=`))
|
|
171
|
+
return result;
|
|
172
|
+
if (map)
|
|
173
|
+
result.code = `${code}
|
|
174
|
+
|
|
175
|
+
//# ${SOURCEMAPPING_URL}=data:application/json;charset=utf-8;base64,${Buffer.from(JSON.stringify(map), "utf-8").toString("base64")}
|
|
176
|
+
`;
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const DEFAULT_REQUEST_STUBS = {
|
|
181
|
+
"/@vite/client": {
|
|
182
|
+
injectQuery: (id) => id,
|
|
183
|
+
createHotContext() {
|
|
184
|
+
return {
|
|
185
|
+
accept: () => {
|
|
186
|
+
},
|
|
187
|
+
prune: () => {
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
},
|
|
191
|
+
updateStyle() {
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
class ViteNodeRunner {
|
|
196
|
+
constructor(options) {
|
|
197
|
+
this.options = options;
|
|
198
|
+
this.root = options.root || process.cwd();
|
|
199
|
+
this.moduleCache = options.moduleCache || new Map();
|
|
200
|
+
}
|
|
201
|
+
async executeFile(file) {
|
|
202
|
+
return await this.cachedRequest(`/@fs/${slash(resolve(file))}`, []);
|
|
203
|
+
}
|
|
204
|
+
async executeId(id) {
|
|
205
|
+
return await this.cachedRequest(id, []);
|
|
206
|
+
}
|
|
207
|
+
async cachedRequest(rawId, callstack) {
|
|
208
|
+
var _a, _b;
|
|
209
|
+
const id = normalizeId(rawId, this.options.base);
|
|
210
|
+
if ((_a = this.moduleCache.get(id)) == null ? void 0 : _a.promise)
|
|
211
|
+
return (_b = this.moduleCache.get(id)) == null ? void 0 : _b.promise;
|
|
212
|
+
const fsPath = toFilePath(id, this.root);
|
|
213
|
+
const promise = this.directRequest(id, fsPath, callstack);
|
|
214
|
+
this.setCache(id, { promise });
|
|
215
|
+
return await promise;
|
|
216
|
+
}
|
|
217
|
+
async directRequest(id, fsPath, callstack) {
|
|
218
|
+
callstack = [...callstack, id];
|
|
219
|
+
const request = async (dep) => {
|
|
220
|
+
var _a;
|
|
221
|
+
if (callstack.includes(dep)) {
|
|
222
|
+
if (!((_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports))
|
|
223
|
+
throw new Error(`[vite-node] Circular dependency detected
|
|
224
|
+
Stack:
|
|
225
|
+
${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
|
|
226
|
+
return this.moduleCache.get(dep).exports;
|
|
227
|
+
}
|
|
228
|
+
return this.cachedRequest(dep, callstack);
|
|
229
|
+
};
|
|
230
|
+
const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
|
|
231
|
+
if (id in requestStubs)
|
|
232
|
+
return requestStubs[id];
|
|
233
|
+
const { code: transformed, externalize } = await this.options.fetchModule(id);
|
|
234
|
+
if (externalize) {
|
|
235
|
+
const mod = await this.interopedImport(externalize);
|
|
236
|
+
this.setCache(id, { exports: mod });
|
|
237
|
+
return mod;
|
|
238
|
+
}
|
|
239
|
+
if (transformed == null)
|
|
240
|
+
throw new Error(`[vite-node] Failed to load ${id}`);
|
|
241
|
+
const url = pathToFileURL(fsPath).href;
|
|
242
|
+
const exports = {};
|
|
243
|
+
this.setCache(id, { code: transformed, exports });
|
|
244
|
+
const __filename = fileURLToPath(url);
|
|
245
|
+
const moduleProxy = {
|
|
246
|
+
set exports(value) {
|
|
247
|
+
exportAll(exports, value);
|
|
248
|
+
exports.default = value;
|
|
249
|
+
},
|
|
250
|
+
get exports() {
|
|
251
|
+
return exports.default;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
const context = this.prepareContext({
|
|
255
|
+
__vite_ssr_import__: request,
|
|
256
|
+
__vite_ssr_dynamic_import__: request,
|
|
257
|
+
__vite_ssr_exports__: exports,
|
|
258
|
+
__vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
|
|
259
|
+
__vite_ssr_import_meta__: { url },
|
|
260
|
+
require: createRequire(url),
|
|
261
|
+
exports,
|
|
262
|
+
module: moduleProxy,
|
|
263
|
+
__filename,
|
|
264
|
+
__dirname: dirname(__filename)
|
|
265
|
+
});
|
|
266
|
+
const fn = vm.runInThisContext(`async (${Object.keys(context).join(",")})=>{{${transformed}
|
|
267
|
+
}}`, {
|
|
268
|
+
filename: fsPath,
|
|
269
|
+
lineOffset: 0
|
|
270
|
+
});
|
|
271
|
+
await fn(...Object.values(context));
|
|
272
|
+
return exports;
|
|
273
|
+
}
|
|
274
|
+
prepareContext(context) {
|
|
275
|
+
return context;
|
|
276
|
+
}
|
|
277
|
+
setCache(id, mod) {
|
|
278
|
+
if (!this.moduleCache.has(id))
|
|
279
|
+
this.moduleCache.set(id, mod);
|
|
280
|
+
else
|
|
281
|
+
Object.assign(this.moduleCache.get(id), mod);
|
|
282
|
+
}
|
|
283
|
+
shouldInterop(path, mod) {
|
|
284
|
+
if (this.options.interopDefault === false)
|
|
285
|
+
return false;
|
|
286
|
+
return !path.endsWith(".mjs") && "default" in mod;
|
|
287
|
+
}
|
|
288
|
+
async interopedImport(path) {
|
|
289
|
+
const mod = await import(path);
|
|
290
|
+
if (this.shouldInterop(path, mod)) {
|
|
291
|
+
const tryDefault = this.hasNestedDefault(mod);
|
|
292
|
+
return new Proxy(mod, {
|
|
293
|
+
get: proxyMethod("get", tryDefault),
|
|
294
|
+
set: proxyMethod("set", tryDefault),
|
|
295
|
+
has: proxyMethod("has", tryDefault),
|
|
296
|
+
deleteProperty: proxyMethod("deleteProperty", tryDefault)
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
return mod;
|
|
300
|
+
}
|
|
301
|
+
hasNestedDefault(target) {
|
|
302
|
+
return "__esModule" in target && target.__esModule && "default" in target.default;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function proxyMethod(name, tryDefault) {
|
|
306
|
+
return function(target, key, ...args) {
|
|
307
|
+
const result = Reflect[name](target, key, ...args);
|
|
308
|
+
if (isPrimitive(target.default))
|
|
309
|
+
return result;
|
|
310
|
+
if (tryDefault && key === "default" || typeof result === "undefined")
|
|
311
|
+
return Reflect[name](target.default, key, ...args);
|
|
312
|
+
return result;
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
function exportAll(exports, sourceModule) {
|
|
316
|
+
for (const key in sourceModule) {
|
|
317
|
+
if (key !== "default") {
|
|
318
|
+
try {
|
|
319
|
+
Object.defineProperty(exports, key, {
|
|
320
|
+
enumerable: true,
|
|
321
|
+
configurable: true,
|
|
322
|
+
get() {
|
|
323
|
+
return sourceModule[key];
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
} catch (_err) {
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
13
331
|
|
|
14
332
|
const argv = minimist(process.argv.slice(2), {
|
|
15
333
|
"alias": {
|
|
@@ -70,6 +388,9 @@ async function run(options = {}) {
|
|
|
70
388
|
base: server.config.base,
|
|
71
389
|
fetchModule(id) {
|
|
72
390
|
return node.fetchModule(id);
|
|
391
|
+
},
|
|
392
|
+
resolveId(id, importer) {
|
|
393
|
+
return node.resolveId(id, importer);
|
|
73
394
|
}
|
|
74
395
|
});
|
|
75
396
|
for (const file of files)
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var module$1 = require('module');
|
|
6
|
+
var url = require('url');
|
|
7
|
+
var vm = require('vm');
|
|
8
|
+
var pathe = require('pathe');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
function _interopNamespace(e) {
|
|
13
|
+
if (e && e.__esModule) return e;
|
|
14
|
+
var n = Object.create(null);
|
|
15
|
+
if (e) {
|
|
16
|
+
Object.keys(e).forEach(function (k) {
|
|
17
|
+
if (k !== 'default') {
|
|
18
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return e[k]; }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
n["default"] = e;
|
|
27
|
+
return Object.freeze(n);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var vm__default = /*#__PURE__*/_interopDefaultLegacy(vm);
|
|
31
|
+
|
|
32
|
+
const isWindows = process.platform === "win32";
|
|
33
|
+
function slash(str) {
|
|
34
|
+
return str.replace(/\\/g, "/");
|
|
35
|
+
}
|
|
36
|
+
function normalizeId(id, base) {
|
|
37
|
+
if (base && id.startsWith(base))
|
|
38
|
+
id = `/${id.slice(base.length)}`;
|
|
39
|
+
return id.replace(/^\/@id\/__x00__/, "\0").replace(/^\/@id\//, "").replace(/^__vite-browser-external:/, "").replace(/^node:/, "").replace(/[?&]v=\w+/, "?").replace(/\?$/, "");
|
|
40
|
+
}
|
|
41
|
+
function isPrimitive(v) {
|
|
42
|
+
return v !== Object(v);
|
|
43
|
+
}
|
|
44
|
+
function toFilePath(id, root) {
|
|
45
|
+
let absolute = slash(id).startsWith("/@fs/") ? id.slice(4) : id.startsWith(pathe.dirname(root)) && pathe.dirname(root) !== "/" ? id : id.startsWith("/") ? slash(pathe.resolve(root, id.slice(1))) : id;
|
|
46
|
+
if (absolute.startsWith("//"))
|
|
47
|
+
absolute = absolute.slice(1);
|
|
48
|
+
return isWindows && absolute.startsWith("/") ? url.fileURLToPath(url.pathToFileURL(absolute.slice(1)).href) : absolute;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const DEFAULT_REQUEST_STUBS = {
|
|
52
|
+
"/@vite/client": {
|
|
53
|
+
injectQuery: (id) => id,
|
|
54
|
+
createHotContext() {
|
|
55
|
+
return {
|
|
56
|
+
accept: () => {
|
|
57
|
+
},
|
|
58
|
+
prune: () => {
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
updateStyle() {
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
class ViteNodeRunner {
|
|
67
|
+
constructor(options) {
|
|
68
|
+
this.options = options;
|
|
69
|
+
this.root = options.root || process.cwd();
|
|
70
|
+
this.moduleCache = options.moduleCache || new Map();
|
|
71
|
+
}
|
|
72
|
+
async executeFile(file) {
|
|
73
|
+
return await this.cachedRequest(`/@fs/${slash(pathe.resolve(file))}`, []);
|
|
74
|
+
}
|
|
75
|
+
async executeId(id) {
|
|
76
|
+
return await this.cachedRequest(id, []);
|
|
77
|
+
}
|
|
78
|
+
async cachedRequest(rawId, callstack) {
|
|
79
|
+
var _a, _b;
|
|
80
|
+
const id = normalizeId(rawId, this.options.base);
|
|
81
|
+
if ((_a = this.moduleCache.get(id)) == null ? void 0 : _a.promise)
|
|
82
|
+
return (_b = this.moduleCache.get(id)) == null ? void 0 : _b.promise;
|
|
83
|
+
const fsPath = toFilePath(id, this.root);
|
|
84
|
+
const promise = this.directRequest(id, fsPath, callstack);
|
|
85
|
+
this.setCache(id, { promise });
|
|
86
|
+
return await promise;
|
|
87
|
+
}
|
|
88
|
+
async directRequest(id, fsPath, callstack) {
|
|
89
|
+
callstack = [...callstack, id];
|
|
90
|
+
const request = async (dep) => {
|
|
91
|
+
var _a;
|
|
92
|
+
if (callstack.includes(dep)) {
|
|
93
|
+
if (!((_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports))
|
|
94
|
+
throw new Error(`[vite-node] Circular dependency detected
|
|
95
|
+
Stack:
|
|
96
|
+
${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
|
|
97
|
+
return this.moduleCache.get(dep).exports;
|
|
98
|
+
}
|
|
99
|
+
return this.cachedRequest(dep, callstack);
|
|
100
|
+
};
|
|
101
|
+
const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
|
|
102
|
+
if (id in requestStubs)
|
|
103
|
+
return requestStubs[id];
|
|
104
|
+
const { code: transformed, externalize } = await this.options.fetchModule(id);
|
|
105
|
+
if (externalize) {
|
|
106
|
+
const mod = await this.interopedImport(externalize);
|
|
107
|
+
this.setCache(id, { exports: mod });
|
|
108
|
+
return mod;
|
|
109
|
+
}
|
|
110
|
+
if (transformed == null)
|
|
111
|
+
throw new Error(`[vite-node] Failed to load ${id}`);
|
|
112
|
+
const url$1 = url.pathToFileURL(fsPath).href;
|
|
113
|
+
const exports = {};
|
|
114
|
+
this.setCache(id, { code: transformed, exports });
|
|
115
|
+
const __filename = url.fileURLToPath(url$1);
|
|
116
|
+
const moduleProxy = {
|
|
117
|
+
set exports(value) {
|
|
118
|
+
exportAll(exports, value);
|
|
119
|
+
exports.default = value;
|
|
120
|
+
},
|
|
121
|
+
get exports() {
|
|
122
|
+
return exports.default;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const context = this.prepareContext({
|
|
126
|
+
__vite_ssr_import__: request,
|
|
127
|
+
__vite_ssr_dynamic_import__: request,
|
|
128
|
+
__vite_ssr_exports__: exports,
|
|
129
|
+
__vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
|
|
130
|
+
__vite_ssr_import_meta__: { url: url$1 },
|
|
131
|
+
require: module$1.createRequire(url$1),
|
|
132
|
+
exports,
|
|
133
|
+
module: moduleProxy,
|
|
134
|
+
__filename,
|
|
135
|
+
__dirname: pathe.dirname(__filename)
|
|
136
|
+
});
|
|
137
|
+
const fn = vm__default["default"].runInThisContext(`async (${Object.keys(context).join(",")})=>{{${transformed}
|
|
138
|
+
}}`, {
|
|
139
|
+
filename: fsPath,
|
|
140
|
+
lineOffset: 0
|
|
141
|
+
});
|
|
142
|
+
await fn(...Object.values(context));
|
|
143
|
+
return exports;
|
|
144
|
+
}
|
|
145
|
+
prepareContext(context) {
|
|
146
|
+
return context;
|
|
147
|
+
}
|
|
148
|
+
setCache(id, mod) {
|
|
149
|
+
if (!this.moduleCache.has(id))
|
|
150
|
+
this.moduleCache.set(id, mod);
|
|
151
|
+
else
|
|
152
|
+
Object.assign(this.moduleCache.get(id), mod);
|
|
153
|
+
}
|
|
154
|
+
shouldInterop(path, mod) {
|
|
155
|
+
if (this.options.interopDefault === false)
|
|
156
|
+
return false;
|
|
157
|
+
return !path.endsWith(".mjs") && "default" in mod;
|
|
158
|
+
}
|
|
159
|
+
async interopedImport(path) {
|
|
160
|
+
const mod = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path);
|
|
161
|
+
if (this.shouldInterop(path, mod)) {
|
|
162
|
+
const tryDefault = this.hasNestedDefault(mod);
|
|
163
|
+
return new Proxy(mod, {
|
|
164
|
+
get: proxyMethod("get", tryDefault),
|
|
165
|
+
set: proxyMethod("set", tryDefault),
|
|
166
|
+
has: proxyMethod("has", tryDefault),
|
|
167
|
+
deleteProperty: proxyMethod("deleteProperty", tryDefault)
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return mod;
|
|
171
|
+
}
|
|
172
|
+
hasNestedDefault(target) {
|
|
173
|
+
return "__esModule" in target && target.__esModule && "default" in target.default;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function proxyMethod(name, tryDefault) {
|
|
177
|
+
return function(target, key, ...args) {
|
|
178
|
+
const result = Reflect[name](target, key, ...args);
|
|
179
|
+
if (isPrimitive(target.default))
|
|
180
|
+
return result;
|
|
181
|
+
if (tryDefault && key === "default" || typeof result === "undefined")
|
|
182
|
+
return Reflect[name](target.default, key, ...args);
|
|
183
|
+
return result;
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function exportAll(exports, sourceModule) {
|
|
187
|
+
for (const key in sourceModule) {
|
|
188
|
+
if (key !== "default") {
|
|
189
|
+
try {
|
|
190
|
+
Object.defineProperty(exports, key, {
|
|
191
|
+
enumerable: true,
|
|
192
|
+
configurable: true,
|
|
193
|
+
get() {
|
|
194
|
+
return sourceModule[key];
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
} catch (_err) {
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
exports.DEFAULT_REQUEST_STUBS = DEFAULT_REQUEST_STUBS;
|
|
204
|
+
exports.ViteNodeRunner = ViteNodeRunner;
|