vite-node 0.17.1 → 0.19.0
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/cli.d.ts +8 -0
- package/client.d.ts +37 -0
- package/dist/cli.cjs +5 -5
- package/dist/cli.d.ts +1 -1
- package/dist/cli.mjs +5 -5
- package/dist/client.cjs +278 -11
- package/dist/client.d.ts +1 -1
- package/dist/client.mjs +256 -8
- package/dist/hmr.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/server.cjs +193 -9
- package/dist/server.d.ts +1 -1
- package/dist/server.mjs +190 -6
- package/dist/{types-b59fb161.d.ts → types-de894284.d.ts} +8 -16
- package/dist/types.d.ts +1 -1
- package/dist/utils.cjs +54 -12
- package/dist/utils.d.ts +1 -1
- package/dist/utils.mjs +49 -3
- package/index.d.ts +1 -0
- package/package.json +3 -3
- package/server.d.ts +26 -0
- package/types.d.ts +68 -0
- package/utils.d.ts +10 -0
- package/dist/chunk-client.cjs +0 -272
- package/dist/chunk-client.mjs +0 -245
- package/dist/chunk-server.cjs +0 -197
- package/dist/chunk-server.mjs +0 -189
- package/dist/chunk-utils.cjs +0 -59
- package/dist/chunk-utils.mjs +0 -49
package/cli.d.ts
ADDED
package/client.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ViteNodeRunnerOptions, ModuleCache } from './types.js';
|
|
2
|
+
|
|
3
|
+
declare const DEFAULT_REQUEST_STUBS: {
|
|
4
|
+
'/@vite/client': {
|
|
5
|
+
injectQuery: (id: string) => string;
|
|
6
|
+
createHotContext(): {
|
|
7
|
+
accept: () => void;
|
|
8
|
+
prune: () => void;
|
|
9
|
+
};
|
|
10
|
+
updateStyle(): void;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
declare class ViteNodeRunner {
|
|
14
|
+
options: ViteNodeRunnerOptions;
|
|
15
|
+
root: string;
|
|
16
|
+
moduleCache: Map<string, ModuleCache>;
|
|
17
|
+
constructor(options: ViteNodeRunnerOptions);
|
|
18
|
+
executeFile(file: string): Promise<any>;
|
|
19
|
+
executeId(id: string): Promise<any>;
|
|
20
|
+
cachedRequest(rawId: string, callstack: string[]): Promise<any>;
|
|
21
|
+
directRequest(id: string, fsPath: string, callstack: string[]): Promise<any>;
|
|
22
|
+
prepareContext(context: Record<string, any>): Record<string, any>;
|
|
23
|
+
setCache(id: string, mod: Partial<ModuleCache>): void;
|
|
24
|
+
shouldResolveId(dep: string): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Define if a module should be interop-ed
|
|
27
|
+
* This function mostly for the ability to override by subclass
|
|
28
|
+
*/
|
|
29
|
+
shouldInterop(path: string, mod: any): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Import a module and interop it
|
|
32
|
+
*/
|
|
33
|
+
interopedImport(path: string): Promise<any>;
|
|
34
|
+
hasNestedDefault(target: any): any;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { DEFAULT_REQUEST_STUBS, ViteNodeRunner };
|
package/dist/cli.cjs
CHANGED
|
@@ -3,16 +3,16 @@
|
|
|
3
3
|
var events = require('events');
|
|
4
4
|
var kolorist = require('kolorist');
|
|
5
5
|
var vite = require('vite');
|
|
6
|
-
var server = require('./
|
|
7
|
-
var client = require('./
|
|
8
|
-
var utils = require('./
|
|
6
|
+
var server = require('./server.cjs');
|
|
7
|
+
var client = require('./client.cjs');
|
|
8
|
+
var utils = require('./utils.cjs');
|
|
9
9
|
var hmr = require('./chunk-hmr.cjs');
|
|
10
10
|
require('pathe');
|
|
11
11
|
require('debug');
|
|
12
12
|
require('fs');
|
|
13
13
|
require('mlly');
|
|
14
|
-
require('module');
|
|
15
14
|
require('url');
|
|
15
|
+
require('module');
|
|
16
16
|
require('vm');
|
|
17
17
|
|
|
18
18
|
function toArr(any) {
|
|
@@ -628,7 +628,7 @@ class CAC extends events.EventEmitter {
|
|
|
628
628
|
|
|
629
629
|
const cac = (name = "") => new CAC(name);
|
|
630
630
|
|
|
631
|
-
var version = "0.
|
|
631
|
+
var version = "0.19.0";
|
|
632
632
|
|
|
633
633
|
const cli = cac("vite-node");
|
|
634
634
|
cli.version(version).option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--options <options>", "Use specified Vite server options").help();
|
package/dist/cli.d.ts
CHANGED
package/dist/cli.mjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
2
|
import { red } from 'kolorist';
|
|
3
3
|
import { createServer } from 'vite';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { ViteNodeServer } from './server.mjs';
|
|
5
|
+
import { ViteNodeRunner } from './client.mjs';
|
|
6
|
+
import { toArray } from './utils.mjs';
|
|
7
7
|
import { v as viteNodeHmrPlugin, c as createHotContext, h as handleMessage } from './chunk-hmr.mjs';
|
|
8
8
|
import 'pathe';
|
|
9
9
|
import 'debug';
|
|
10
10
|
import 'fs';
|
|
11
11
|
import 'mlly';
|
|
12
|
-
import 'module';
|
|
13
12
|
import 'url';
|
|
13
|
+
import 'module';
|
|
14
14
|
import 'vm';
|
|
15
15
|
|
|
16
16
|
function toArr(any) {
|
|
@@ -626,7 +626,7 @@ class CAC extends EventEmitter {
|
|
|
626
626
|
|
|
627
627
|
const cac = (name = "") => new CAC(name);
|
|
628
628
|
|
|
629
|
-
var version = "0.
|
|
629
|
+
var version = "0.19.0";
|
|
630
630
|
|
|
631
631
|
const cli = cac("vite-node");
|
|
632
632
|
cli.version(version).option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--options <options>", "Use specified Vite server options").help();
|
package/dist/client.cjs
CHANGED
|
@@ -2,17 +2,284 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
require('
|
|
7
|
-
require('
|
|
8
|
-
require('
|
|
9
|
-
require('
|
|
10
|
-
require('
|
|
11
|
-
require('
|
|
12
|
-
require('./chunk-utils.cjs');
|
|
5
|
+
var module$1 = require('module');
|
|
6
|
+
var url = require('url');
|
|
7
|
+
var vm = require('vm');
|
|
8
|
+
var pathe = require('pathe');
|
|
9
|
+
var mlly = require('mlly');
|
|
10
|
+
var createDebug = require('debug');
|
|
11
|
+
var utils = require('./utils.cjs');
|
|
13
12
|
|
|
13
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
14
|
|
|
15
|
+
function _interopNamespace(e) {
|
|
16
|
+
if (e && e.__esModule) return e;
|
|
17
|
+
var n = Object.create(null);
|
|
18
|
+
if (e) {
|
|
19
|
+
Object.keys(e).forEach(function (k) {
|
|
20
|
+
if (k !== 'default') {
|
|
21
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
22
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function () { return e[k]; }
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
n["default"] = e;
|
|
30
|
+
return Object.freeze(n);
|
|
31
|
+
}
|
|
15
32
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
33
|
+
var vm__default = /*#__PURE__*/_interopDefaultLegacy(vm);
|
|
34
|
+
var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
|
|
35
|
+
|
|
36
|
+
const debugExecute = createDebug__default["default"]("vite-node:client:execute");
|
|
37
|
+
const debugNative = createDebug__default["default"]("vite-node:client:native");
|
|
38
|
+
const DEFAULT_REQUEST_STUBS = {
|
|
39
|
+
"/@vite/client": {
|
|
40
|
+
injectQuery: (id) => id,
|
|
41
|
+
createHotContext() {
|
|
42
|
+
return {
|
|
43
|
+
accept: () => {
|
|
44
|
+
},
|
|
45
|
+
prune: () => {
|
|
46
|
+
},
|
|
47
|
+
dispose: () => {
|
|
48
|
+
},
|
|
49
|
+
decline: () => {
|
|
50
|
+
},
|
|
51
|
+
invalidate: () => {
|
|
52
|
+
},
|
|
53
|
+
on: () => {
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
updateStyle(id, css) {
|
|
58
|
+
if (typeof document === "undefined")
|
|
59
|
+
return;
|
|
60
|
+
const element = document.getElementById(id);
|
|
61
|
+
if (element)
|
|
62
|
+
element.remove();
|
|
63
|
+
const head = document.querySelector("head");
|
|
64
|
+
const style = document.createElement("style");
|
|
65
|
+
style.setAttribute("type", "text/css");
|
|
66
|
+
style.id = id;
|
|
67
|
+
style.innerHTML = css;
|
|
68
|
+
head == null ? void 0 : head.appendChild(style);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
class ModuleCacheMap extends Map {
|
|
73
|
+
normalizePath(fsPath) {
|
|
74
|
+
return utils.normalizeModuleId(fsPath);
|
|
75
|
+
}
|
|
76
|
+
set(fsPath, mod) {
|
|
77
|
+
fsPath = this.normalizePath(fsPath);
|
|
78
|
+
if (!super.has(fsPath))
|
|
79
|
+
super.set(fsPath, mod);
|
|
80
|
+
else
|
|
81
|
+
Object.assign(super.get(fsPath), mod);
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
get(fsPath) {
|
|
85
|
+
fsPath = this.normalizePath(fsPath);
|
|
86
|
+
return super.get(fsPath);
|
|
87
|
+
}
|
|
88
|
+
delete(fsPath) {
|
|
89
|
+
fsPath = this.normalizePath(fsPath);
|
|
90
|
+
return super.delete(fsPath);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
class ViteNodeRunner {
|
|
94
|
+
constructor(options) {
|
|
95
|
+
this.options = options;
|
|
96
|
+
this.root = options.root ?? process.cwd();
|
|
97
|
+
this.moduleCache = options.moduleCache ?? new ModuleCacheMap();
|
|
98
|
+
this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG : false);
|
|
99
|
+
}
|
|
100
|
+
async executeFile(file) {
|
|
101
|
+
return await this.cachedRequest(`/@fs/${utils.slash(pathe.resolve(file))}`, []);
|
|
102
|
+
}
|
|
103
|
+
async executeId(id) {
|
|
104
|
+
return await this.cachedRequest(id, []);
|
|
105
|
+
}
|
|
106
|
+
async cachedRequest(rawId, callstack) {
|
|
107
|
+
var _a, _b, _c, _d;
|
|
108
|
+
const id = utils.normalizeRequestId(rawId, this.options.base);
|
|
109
|
+
const fsPath = utils.toFilePath(id, this.root);
|
|
110
|
+
if (callstack.includes(fsPath) && ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.exports))
|
|
111
|
+
return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.exports;
|
|
112
|
+
if ((_c = this.moduleCache.get(fsPath)) == null ? void 0 : _c.promise)
|
|
113
|
+
return (_d = this.moduleCache.get(fsPath)) == null ? void 0 : _d.promise;
|
|
114
|
+
const promise = this.directRequest(id, fsPath, callstack);
|
|
115
|
+
this.moduleCache.set(fsPath, { promise });
|
|
116
|
+
return await promise;
|
|
117
|
+
}
|
|
118
|
+
async directRequest(id, fsPath, _callstack) {
|
|
119
|
+
const callstack = [..._callstack, fsPath];
|
|
120
|
+
const request = async (dep) => {
|
|
121
|
+
var _a;
|
|
122
|
+
const fsPath2 = utils.toFilePath(utils.normalizeRequestId(dep, this.options.base), this.root);
|
|
123
|
+
const getStack = () => {
|
|
124
|
+
return `stack:
|
|
125
|
+
${[...callstack, fsPath2].reverse().map((p) => `- ${p}`).join("\n")}`;
|
|
126
|
+
};
|
|
127
|
+
let debugTimer;
|
|
128
|
+
if (this.debug)
|
|
129
|
+
debugTimer = setTimeout(() => this.debugLog(() => `module ${fsPath2} takes over 2s to load.
|
|
130
|
+
${getStack()}`), 2e3);
|
|
131
|
+
try {
|
|
132
|
+
if (callstack.includes(fsPath2)) {
|
|
133
|
+
this.debugLog(() => `circular dependency, ${getStack()}`);
|
|
134
|
+
const depExports = (_a = this.moduleCache.get(fsPath2)) == null ? void 0 : _a.exports;
|
|
135
|
+
if (depExports)
|
|
136
|
+
return depExports;
|
|
137
|
+
throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`);
|
|
138
|
+
}
|
|
139
|
+
const mod = await this.cachedRequest(dep, callstack);
|
|
140
|
+
return mod;
|
|
141
|
+
} finally {
|
|
142
|
+
if (debugTimer)
|
|
143
|
+
clearTimeout(debugTimer);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
Object.defineProperty(request, "callstack", { get: () => callstack });
|
|
147
|
+
const resolveId = async (dep, callstackPosition = 1) => {
|
|
148
|
+
if (this.options.resolveId && this.shouldResolveId(dep)) {
|
|
149
|
+
let importer = callstack[callstack.length - callstackPosition];
|
|
150
|
+
if (importer && importer.startsWith("mock:"))
|
|
151
|
+
importer = importer.slice(5);
|
|
152
|
+
const { id: id2 } = await this.options.resolveId(dep, importer) || {};
|
|
153
|
+
dep = id2 && pathe.isAbsolute(id2) ? utils.mergeSlashes(`/@fs/${id2}`) : id2 || dep;
|
|
154
|
+
}
|
|
155
|
+
return dep;
|
|
156
|
+
};
|
|
157
|
+
id = await resolveId(id, 2);
|
|
158
|
+
const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
|
|
159
|
+
if (id in requestStubs)
|
|
160
|
+
return requestStubs[id];
|
|
161
|
+
let { code: transformed, externalize } = await this.options.fetchModule(id);
|
|
162
|
+
if (externalize) {
|
|
163
|
+
debugNative(externalize);
|
|
164
|
+
const mod = await this.interopedImport(externalize);
|
|
165
|
+
this.moduleCache.set(fsPath, { exports: mod });
|
|
166
|
+
return mod;
|
|
167
|
+
}
|
|
168
|
+
if (transformed == null)
|
|
169
|
+
throw new Error(`[vite-node] Failed to load ${id}`);
|
|
170
|
+
const url$1 = url.pathToFileURL(fsPath).href;
|
|
171
|
+
const meta = { url: url$1 };
|
|
172
|
+
const exports = /* @__PURE__ */ Object.create(null);
|
|
173
|
+
exports[Symbol.toStringTag] = "Module";
|
|
174
|
+
this.moduleCache.set(fsPath, { code: transformed, exports });
|
|
175
|
+
const __filename = url.fileURLToPath(url$1);
|
|
176
|
+
const moduleProxy = {
|
|
177
|
+
set exports(value) {
|
|
178
|
+
exportAll(exports, value);
|
|
179
|
+
exports.default = value;
|
|
180
|
+
},
|
|
181
|
+
get exports() {
|
|
182
|
+
return exports;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
let hotContext;
|
|
186
|
+
if (this.options.createHotContext) {
|
|
187
|
+
Object.defineProperty(meta, "hot", {
|
|
188
|
+
enumerable: true,
|
|
189
|
+
get: () => {
|
|
190
|
+
var _a, _b;
|
|
191
|
+
hotContext || (hotContext = (_b = (_a = this.options).createHotContext) == null ? void 0 : _b.call(_a, this, `/@fs/${fsPath}`));
|
|
192
|
+
return hotContext;
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
const context = this.prepareContext({
|
|
197
|
+
__vite_ssr_import__: request,
|
|
198
|
+
__vite_ssr_dynamic_import__: request,
|
|
199
|
+
__vite_ssr_exports__: exports,
|
|
200
|
+
__vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
|
|
201
|
+
__vite_ssr_import_meta__: meta,
|
|
202
|
+
__vitest_resolve_id__: resolveId,
|
|
203
|
+
require: module$1.createRequire(url$1),
|
|
204
|
+
exports,
|
|
205
|
+
module: moduleProxy,
|
|
206
|
+
__filename,
|
|
207
|
+
__dirname: pathe.dirname(__filename)
|
|
208
|
+
});
|
|
209
|
+
debugExecute(__filename);
|
|
210
|
+
if (transformed[0] === "#")
|
|
211
|
+
transformed = transformed.replace(/^\#\!.*/, (s) => " ".repeat(s.length));
|
|
212
|
+
const fn = vm__default["default"].runInThisContext(`'use strict';async (${Object.keys(context).join(",")})=>{{${transformed}
|
|
213
|
+
}}`, {
|
|
214
|
+
filename: fsPath,
|
|
215
|
+
lineOffset: 0
|
|
216
|
+
});
|
|
217
|
+
await fn(...Object.values(context));
|
|
218
|
+
return exports;
|
|
219
|
+
}
|
|
220
|
+
prepareContext(context) {
|
|
221
|
+
return context;
|
|
222
|
+
}
|
|
223
|
+
shouldResolveId(dep) {
|
|
224
|
+
if (mlly.isNodeBuiltin(dep) || dep in (this.options.requestStubs || DEFAULT_REQUEST_STUBS) || dep.startsWith("/@vite"))
|
|
225
|
+
return false;
|
|
226
|
+
return !pathe.isAbsolute(dep) || !pathe.extname(dep);
|
|
227
|
+
}
|
|
228
|
+
shouldInterop(path, mod) {
|
|
229
|
+
if (this.options.interopDefault === false)
|
|
230
|
+
return false;
|
|
231
|
+
return !path.endsWith(".mjs") && "default" in mod;
|
|
232
|
+
}
|
|
233
|
+
async interopedImport(path) {
|
|
234
|
+
const mod = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path);
|
|
235
|
+
if (this.shouldInterop(path, mod)) {
|
|
236
|
+
const tryDefault = this.hasNestedDefault(mod);
|
|
237
|
+
return new Proxy(mod, {
|
|
238
|
+
get: proxyMethod("get", tryDefault),
|
|
239
|
+
set: proxyMethod("set", tryDefault),
|
|
240
|
+
has: proxyMethod("has", tryDefault),
|
|
241
|
+
deleteProperty: proxyMethod("deleteProperty", tryDefault)
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
return mod;
|
|
245
|
+
}
|
|
246
|
+
hasNestedDefault(target) {
|
|
247
|
+
return "__esModule" in target && target.__esModule && "default" in target.default;
|
|
248
|
+
}
|
|
249
|
+
debugLog(msg) {
|
|
250
|
+
if (this.debug)
|
|
251
|
+
console.log(`[vite-node] ${msg()}`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function proxyMethod(name, tryDefault) {
|
|
255
|
+
return function(target, key, ...args) {
|
|
256
|
+
const result = Reflect[name](target, key, ...args);
|
|
257
|
+
if (utils.isPrimitive(target.default))
|
|
258
|
+
return result;
|
|
259
|
+
if (tryDefault && key === "default" || typeof result === "undefined")
|
|
260
|
+
return Reflect[name](target.default, key, ...args);
|
|
261
|
+
return result;
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function exportAll(exports, sourceModule) {
|
|
265
|
+
if (exports === sourceModule)
|
|
266
|
+
return;
|
|
267
|
+
for (const key in sourceModule) {
|
|
268
|
+
if (key !== "default") {
|
|
269
|
+
try {
|
|
270
|
+
Object.defineProperty(exports, key, {
|
|
271
|
+
enumerable: true,
|
|
272
|
+
configurable: true,
|
|
273
|
+
get() {
|
|
274
|
+
return sourceModule[key];
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
} catch (_err) {
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
exports.DEFAULT_REQUEST_STUBS = DEFAULT_REQUEST_STUBS;
|
|
284
|
+
exports.ModuleCacheMap = ModuleCacheMap;
|
|
285
|
+
exports.ViteNodeRunner = ViteNodeRunner;
|
package/dist/client.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { i as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, g as ViteNodeRunner } from './types-
|
|
1
|
+
export { i as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, g as ViteNodeRunner } from './types-de894284.js';
|
package/dist/client.mjs
CHANGED
|
@@ -1,8 +1,256 @@
|
|
|
1
|
-
|
|
2
|
-
import '
|
|
3
|
-
import '
|
|
4
|
-
import '
|
|
5
|
-
import '
|
|
6
|
-
import '
|
|
7
|
-
import '
|
|
8
|
-
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
import { pathToFileURL, fileURLToPath } from 'url';
|
|
3
|
+
import vm from 'vm';
|
|
4
|
+
import { resolve, dirname, isAbsolute, extname } from 'pathe';
|
|
5
|
+
import { isNodeBuiltin } from 'mlly';
|
|
6
|
+
import createDebug from 'debug';
|
|
7
|
+
import { normalizeModuleId, slash, normalizeRequestId, toFilePath, isPrimitive, mergeSlashes } from './utils.mjs';
|
|
8
|
+
|
|
9
|
+
const debugExecute = createDebug("vite-node:client:execute");
|
|
10
|
+
const debugNative = createDebug("vite-node:client:native");
|
|
11
|
+
const DEFAULT_REQUEST_STUBS = {
|
|
12
|
+
"/@vite/client": {
|
|
13
|
+
injectQuery: (id) => id,
|
|
14
|
+
createHotContext() {
|
|
15
|
+
return {
|
|
16
|
+
accept: () => {
|
|
17
|
+
},
|
|
18
|
+
prune: () => {
|
|
19
|
+
},
|
|
20
|
+
dispose: () => {
|
|
21
|
+
},
|
|
22
|
+
decline: () => {
|
|
23
|
+
},
|
|
24
|
+
invalidate: () => {
|
|
25
|
+
},
|
|
26
|
+
on: () => {
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
updateStyle(id, css) {
|
|
31
|
+
if (typeof document === "undefined")
|
|
32
|
+
return;
|
|
33
|
+
const element = document.getElementById(id);
|
|
34
|
+
if (element)
|
|
35
|
+
element.remove();
|
|
36
|
+
const head = document.querySelector("head");
|
|
37
|
+
const style = document.createElement("style");
|
|
38
|
+
style.setAttribute("type", "text/css");
|
|
39
|
+
style.id = id;
|
|
40
|
+
style.innerHTML = css;
|
|
41
|
+
head == null ? void 0 : head.appendChild(style);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
class ModuleCacheMap extends Map {
|
|
46
|
+
normalizePath(fsPath) {
|
|
47
|
+
return normalizeModuleId(fsPath);
|
|
48
|
+
}
|
|
49
|
+
set(fsPath, mod) {
|
|
50
|
+
fsPath = this.normalizePath(fsPath);
|
|
51
|
+
if (!super.has(fsPath))
|
|
52
|
+
super.set(fsPath, mod);
|
|
53
|
+
else
|
|
54
|
+
Object.assign(super.get(fsPath), mod);
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
get(fsPath) {
|
|
58
|
+
fsPath = this.normalizePath(fsPath);
|
|
59
|
+
return super.get(fsPath);
|
|
60
|
+
}
|
|
61
|
+
delete(fsPath) {
|
|
62
|
+
fsPath = this.normalizePath(fsPath);
|
|
63
|
+
return super.delete(fsPath);
|
|
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 ModuleCacheMap();
|
|
71
|
+
this.debug = options.debug ?? (typeof process !== "undefined" ? !!process.env.VITE_NODE_DEBUG : false);
|
|
72
|
+
}
|
|
73
|
+
async executeFile(file) {
|
|
74
|
+
return await this.cachedRequest(`/@fs/${slash(resolve(file))}`, []);
|
|
75
|
+
}
|
|
76
|
+
async executeId(id) {
|
|
77
|
+
return await this.cachedRequest(id, []);
|
|
78
|
+
}
|
|
79
|
+
async cachedRequest(rawId, callstack) {
|
|
80
|
+
var _a, _b, _c, _d;
|
|
81
|
+
const id = normalizeRequestId(rawId, this.options.base);
|
|
82
|
+
const fsPath = toFilePath(id, this.root);
|
|
83
|
+
if (callstack.includes(fsPath) && ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.exports))
|
|
84
|
+
return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.exports;
|
|
85
|
+
if ((_c = this.moduleCache.get(fsPath)) == null ? void 0 : _c.promise)
|
|
86
|
+
return (_d = this.moduleCache.get(fsPath)) == null ? void 0 : _d.promise;
|
|
87
|
+
const promise = this.directRequest(id, fsPath, callstack);
|
|
88
|
+
this.moduleCache.set(fsPath, { promise });
|
|
89
|
+
return await promise;
|
|
90
|
+
}
|
|
91
|
+
async directRequest(id, fsPath, _callstack) {
|
|
92
|
+
const callstack = [..._callstack, fsPath];
|
|
93
|
+
const request = async (dep) => {
|
|
94
|
+
var _a;
|
|
95
|
+
const fsPath2 = toFilePath(normalizeRequestId(dep, this.options.base), this.root);
|
|
96
|
+
const getStack = () => {
|
|
97
|
+
return `stack:
|
|
98
|
+
${[...callstack, fsPath2].reverse().map((p) => `- ${p}`).join("\n")}`;
|
|
99
|
+
};
|
|
100
|
+
let debugTimer;
|
|
101
|
+
if (this.debug)
|
|
102
|
+
debugTimer = setTimeout(() => this.debugLog(() => `module ${fsPath2} takes over 2s to load.
|
|
103
|
+
${getStack()}`), 2e3);
|
|
104
|
+
try {
|
|
105
|
+
if (callstack.includes(fsPath2)) {
|
|
106
|
+
this.debugLog(() => `circular dependency, ${getStack()}`);
|
|
107
|
+
const depExports = (_a = this.moduleCache.get(fsPath2)) == null ? void 0 : _a.exports;
|
|
108
|
+
if (depExports)
|
|
109
|
+
return depExports;
|
|
110
|
+
throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`);
|
|
111
|
+
}
|
|
112
|
+
const mod = await this.cachedRequest(dep, callstack);
|
|
113
|
+
return mod;
|
|
114
|
+
} finally {
|
|
115
|
+
if (debugTimer)
|
|
116
|
+
clearTimeout(debugTimer);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
Object.defineProperty(request, "callstack", { get: () => callstack });
|
|
120
|
+
const resolveId = async (dep, callstackPosition = 1) => {
|
|
121
|
+
if (this.options.resolveId && this.shouldResolveId(dep)) {
|
|
122
|
+
let importer = callstack[callstack.length - callstackPosition];
|
|
123
|
+
if (importer && importer.startsWith("mock:"))
|
|
124
|
+
importer = importer.slice(5);
|
|
125
|
+
const { id: id2 } = await this.options.resolveId(dep, importer) || {};
|
|
126
|
+
dep = id2 && isAbsolute(id2) ? mergeSlashes(`/@fs/${id2}`) : id2 || dep;
|
|
127
|
+
}
|
|
128
|
+
return dep;
|
|
129
|
+
};
|
|
130
|
+
id = await resolveId(id, 2);
|
|
131
|
+
const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
|
|
132
|
+
if (id in requestStubs)
|
|
133
|
+
return requestStubs[id];
|
|
134
|
+
let { code: transformed, externalize } = await this.options.fetchModule(id);
|
|
135
|
+
if (externalize) {
|
|
136
|
+
debugNative(externalize);
|
|
137
|
+
const mod = await this.interopedImport(externalize);
|
|
138
|
+
this.moduleCache.set(fsPath, { exports: mod });
|
|
139
|
+
return mod;
|
|
140
|
+
}
|
|
141
|
+
if (transformed == null)
|
|
142
|
+
throw new Error(`[vite-node] Failed to load ${id}`);
|
|
143
|
+
const url = pathToFileURL(fsPath).href;
|
|
144
|
+
const meta = { url };
|
|
145
|
+
const exports = /* @__PURE__ */ Object.create(null);
|
|
146
|
+
exports[Symbol.toStringTag] = "Module";
|
|
147
|
+
this.moduleCache.set(fsPath, { code: transformed, exports });
|
|
148
|
+
const __filename = fileURLToPath(url);
|
|
149
|
+
const moduleProxy = {
|
|
150
|
+
set exports(value) {
|
|
151
|
+
exportAll(exports, value);
|
|
152
|
+
exports.default = value;
|
|
153
|
+
},
|
|
154
|
+
get exports() {
|
|
155
|
+
return exports;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
let hotContext;
|
|
159
|
+
if (this.options.createHotContext) {
|
|
160
|
+
Object.defineProperty(meta, "hot", {
|
|
161
|
+
enumerable: true,
|
|
162
|
+
get: () => {
|
|
163
|
+
var _a, _b;
|
|
164
|
+
hotContext || (hotContext = (_b = (_a = this.options).createHotContext) == null ? void 0 : _b.call(_a, this, `/@fs/${fsPath}`));
|
|
165
|
+
return hotContext;
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const context = this.prepareContext({
|
|
170
|
+
__vite_ssr_import__: request,
|
|
171
|
+
__vite_ssr_dynamic_import__: request,
|
|
172
|
+
__vite_ssr_exports__: exports,
|
|
173
|
+
__vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
|
|
174
|
+
__vite_ssr_import_meta__: meta,
|
|
175
|
+
__vitest_resolve_id__: resolveId,
|
|
176
|
+
require: createRequire(url),
|
|
177
|
+
exports,
|
|
178
|
+
module: moduleProxy,
|
|
179
|
+
__filename,
|
|
180
|
+
__dirname: dirname(__filename)
|
|
181
|
+
});
|
|
182
|
+
debugExecute(__filename);
|
|
183
|
+
if (transformed[0] === "#")
|
|
184
|
+
transformed = transformed.replace(/^\#\!.*/, (s) => " ".repeat(s.length));
|
|
185
|
+
const fn = vm.runInThisContext(`'use strict';async (${Object.keys(context).join(",")})=>{{${transformed}
|
|
186
|
+
}}`, {
|
|
187
|
+
filename: fsPath,
|
|
188
|
+
lineOffset: 0
|
|
189
|
+
});
|
|
190
|
+
await fn(...Object.values(context));
|
|
191
|
+
return exports;
|
|
192
|
+
}
|
|
193
|
+
prepareContext(context) {
|
|
194
|
+
return context;
|
|
195
|
+
}
|
|
196
|
+
shouldResolveId(dep) {
|
|
197
|
+
if (isNodeBuiltin(dep) || dep in (this.options.requestStubs || DEFAULT_REQUEST_STUBS) || dep.startsWith("/@vite"))
|
|
198
|
+
return false;
|
|
199
|
+
return !isAbsolute(dep) || !extname(dep);
|
|
200
|
+
}
|
|
201
|
+
shouldInterop(path, mod) {
|
|
202
|
+
if (this.options.interopDefault === false)
|
|
203
|
+
return false;
|
|
204
|
+
return !path.endsWith(".mjs") && "default" in mod;
|
|
205
|
+
}
|
|
206
|
+
async interopedImport(path) {
|
|
207
|
+
const mod = await import(path);
|
|
208
|
+
if (this.shouldInterop(path, mod)) {
|
|
209
|
+
const tryDefault = this.hasNestedDefault(mod);
|
|
210
|
+
return new Proxy(mod, {
|
|
211
|
+
get: proxyMethod("get", tryDefault),
|
|
212
|
+
set: proxyMethod("set", tryDefault),
|
|
213
|
+
has: proxyMethod("has", tryDefault),
|
|
214
|
+
deleteProperty: proxyMethod("deleteProperty", tryDefault)
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
return mod;
|
|
218
|
+
}
|
|
219
|
+
hasNestedDefault(target) {
|
|
220
|
+
return "__esModule" in target && target.__esModule && "default" in target.default;
|
|
221
|
+
}
|
|
222
|
+
debugLog(msg) {
|
|
223
|
+
if (this.debug)
|
|
224
|
+
console.log(`[vite-node] ${msg()}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function proxyMethod(name, tryDefault) {
|
|
228
|
+
return function(target, key, ...args) {
|
|
229
|
+
const result = Reflect[name](target, key, ...args);
|
|
230
|
+
if (isPrimitive(target.default))
|
|
231
|
+
return result;
|
|
232
|
+
if (tryDefault && key === "default" || typeof result === "undefined")
|
|
233
|
+
return Reflect[name](target.default, key, ...args);
|
|
234
|
+
return result;
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
function exportAll(exports, sourceModule) {
|
|
238
|
+
if (exports === sourceModule)
|
|
239
|
+
return;
|
|
240
|
+
for (const key in sourceModule) {
|
|
241
|
+
if (key !== "default") {
|
|
242
|
+
try {
|
|
243
|
+
Object.defineProperty(exports, key, {
|
|
244
|
+
enumerable: true,
|
|
245
|
+
configurable: true,
|
|
246
|
+
get() {
|
|
247
|
+
return sourceModule[key];
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
} catch (_err) {
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export { DEFAULT_REQUEST_STUBS, ModuleCacheMap, ViteNodeRunner };
|
package/dist/hmr.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
2
|
import { HMRPayload, Plugin } from 'vite';
|
|
3
|
-
import { U as UpdatePayload, P as PrunePayload, f as FullReloadPayload, E as ErrorPayload, g as ViteNodeRunner, h as HMRPayload$1, H as HotContext } from './types-
|
|
3
|
+
import { U as UpdatePayload, P as PrunePayload, f as FullReloadPayload, E as ErrorPayload, g as ViteNodeRunner, h as HMRPayload$1, H as HotContext } from './types-de894284.js';
|
|
4
4
|
|
|
5
5
|
declare type EventType = string | symbol;
|
|
6
6
|
declare type Handler<T = unknown> = (event: T) => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { A as Arrayable, C as CreateHotContextFunction, D as DepsHandlingOptions, a as FetchFunction, F as FetchResult, H as HotContext, c as ModuleCache, M as ModuleCacheMap, N as Nullable, R as RawSourceMap, b as ResolveIdFunction, S as StartOfSourceMap, d as ViteNodeResolveId, V as ViteNodeRunnerOptions, e as ViteNodeServerOptions } from './types-
|
|
1
|
+
export { A as Arrayable, C as CreateHotContextFunction, D as DepsHandlingOptions, a as FetchFunction, F as FetchResult, H as HotContext, c as ModuleCache, M as ModuleCacheMap, N as Nullable, R as RawSourceMap, b as ResolveIdFunction, S as StartOfSourceMap, d as ViteNodeResolveId, V as ViteNodeRunnerOptions, e as ViteNodeServerOptions } from './types-de894284.js';
|