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