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/client.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ declare type FetchFunction = (id: string) => Promise<{
|
|
|
2
2
|
code?: string;
|
|
3
3
|
externalize?: string;
|
|
4
4
|
}>;
|
|
5
|
+
declare type ResolveIdFunction = (id: string, importer?: string) => Promise<ViteNodeResolveId | null>;
|
|
5
6
|
interface ModuleCache {
|
|
6
7
|
promise?: Promise<any>;
|
|
7
8
|
exports?: any;
|
|
@@ -9,17 +10,34 @@ interface ModuleCache {
|
|
|
9
10
|
}
|
|
10
11
|
interface ViteNodeRunnerOptions {
|
|
11
12
|
fetchModule: FetchFunction;
|
|
13
|
+
resolveId: ResolveIdFunction;
|
|
12
14
|
root: string;
|
|
13
15
|
base?: string;
|
|
14
16
|
moduleCache?: Map<string, ModuleCache>;
|
|
15
|
-
|
|
17
|
+
interopDefault?: boolean;
|
|
16
18
|
requestStubs?: Record<string, any>;
|
|
17
19
|
}
|
|
20
|
+
interface ViteNodeResolveId {
|
|
21
|
+
external?: boolean | 'absolute' | 'relative';
|
|
22
|
+
id: string;
|
|
23
|
+
meta?: Record<string, any> | null;
|
|
24
|
+
moduleSideEffects?: boolean | 'no-treeshake' | null;
|
|
25
|
+
syntheticNamedExports?: boolean | string | null;
|
|
26
|
+
}
|
|
18
27
|
|
|
28
|
+
declare const DEFAULT_REQUEST_STUBS: {
|
|
29
|
+
'/@vite/client': {
|
|
30
|
+
injectQuery: (id: string) => string;
|
|
31
|
+
createHotContext(): {
|
|
32
|
+
accept: () => void;
|
|
33
|
+
prune: () => void;
|
|
34
|
+
};
|
|
35
|
+
updateStyle(): void;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
19
38
|
declare class ViteNodeRunner {
|
|
20
39
|
options: ViteNodeRunnerOptions;
|
|
21
40
|
root: string;
|
|
22
|
-
externalCache: Map<string, string | Promise<false | string>>;
|
|
23
41
|
moduleCache: Map<string, ModuleCache>;
|
|
24
42
|
constructor(options: ViteNodeRunnerOptions);
|
|
25
43
|
executeFile(file: string): Promise<any>;
|
|
@@ -28,6 +46,16 @@ declare class ViteNodeRunner {
|
|
|
28
46
|
directRequest(id: string, fsPath: string, callstack: string[]): Promise<any>;
|
|
29
47
|
prepareContext(context: Record<string, any>): Record<string, any>;
|
|
30
48
|
setCache(id: string, mod: Partial<ModuleCache>): void;
|
|
49
|
+
/**
|
|
50
|
+
* Define if a module should be interop-ed
|
|
51
|
+
* This function mostly for the ability to override by subclass
|
|
52
|
+
*/
|
|
53
|
+
shouldInterop(path: string, mod: any): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Import a module and interop it
|
|
56
|
+
*/
|
|
57
|
+
interopedImport(path: string): Promise<any>;
|
|
58
|
+
hasNestedDefault(target: any): any;
|
|
31
59
|
}
|
|
32
60
|
|
|
33
|
-
export { ViteNodeRunner };
|
|
61
|
+
export { DEFAULT_REQUEST_STUBS, ViteNodeRunner };
|
package/dist/cli.cjs
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var minimist = require('minimist');
|
|
4
|
+
var kolorist = require('kolorist');
|
|
5
|
+
var vite = require('vite');
|
|
6
|
+
var fs = require('fs');
|
|
7
|
+
var mlly = require('mlly');
|
|
8
|
+
var url = require('url');
|
|
9
|
+
var pathe = require('pathe');
|
|
10
|
+
var module$1 = require('module');
|
|
11
|
+
var vm = require('vm');
|
|
12
|
+
|
|
13
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
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
|
+
}
|
|
32
|
+
|
|
33
|
+
var minimist__default = /*#__PURE__*/_interopDefaultLegacy(minimist);
|
|
34
|
+
var vm__default = /*#__PURE__*/_interopDefaultLegacy(vm);
|
|
35
|
+
|
|
36
|
+
const isWindows = process.platform === "win32";
|
|
37
|
+
function slash(str) {
|
|
38
|
+
return str.replace(/\\/g, "/");
|
|
39
|
+
}
|
|
40
|
+
function normalizeId(id, base) {
|
|
41
|
+
if (base && id.startsWith(base))
|
|
42
|
+
id = `/${id.slice(base.length)}`;
|
|
43
|
+
return id.replace(/^\/@id\/__x00__/, "\0").replace(/^\/@id\//, "").replace(/^__vite-browser-external:/, "").replace(/^node:/, "").replace(/[?&]v=\w+/, "?").replace(/\?$/, "");
|
|
44
|
+
}
|
|
45
|
+
function isPrimitive(v) {
|
|
46
|
+
return v !== Object(v);
|
|
47
|
+
}
|
|
48
|
+
function toFilePath(id, root) {
|
|
49
|
+
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;
|
|
50
|
+
if (absolute.startsWith("//"))
|
|
51
|
+
absolute = absolute.slice(1);
|
|
52
|
+
return isWindows && absolute.startsWith("/") ? url.fileURLToPath(url.pathToFileURL(absolute.slice(1)).href) : absolute;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const ESM_EXT_RE = /\.(es|esm|esm-browser|esm-bundler|es6|module)\.js$/;
|
|
56
|
+
const ESM_FOLDER_RE = /\/esm\/(.*\.js)$/;
|
|
57
|
+
const defaultInline = [
|
|
58
|
+
/\/vitest\/dist\//,
|
|
59
|
+
/vitest-virtual-\w+\/dist/,
|
|
60
|
+
/virtual:/,
|
|
61
|
+
/\.ts$/,
|
|
62
|
+
ESM_EXT_RE,
|
|
63
|
+
ESM_FOLDER_RE
|
|
64
|
+
];
|
|
65
|
+
const depsExternal = [
|
|
66
|
+
/\.cjs.js$/,
|
|
67
|
+
/\.mjs$/
|
|
68
|
+
];
|
|
69
|
+
function guessCJSversion(id) {
|
|
70
|
+
if (id.match(ESM_EXT_RE)) {
|
|
71
|
+
for (const i of [
|
|
72
|
+
id.replace(ESM_EXT_RE, ".mjs"),
|
|
73
|
+
id.replace(ESM_EXT_RE, ".umd.js"),
|
|
74
|
+
id.replace(ESM_EXT_RE, ".cjs.js"),
|
|
75
|
+
id.replace(ESM_EXT_RE, ".js")
|
|
76
|
+
]) {
|
|
77
|
+
if (fs.existsSync(i))
|
|
78
|
+
return i;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (id.match(ESM_FOLDER_RE)) {
|
|
82
|
+
for (const i of [
|
|
83
|
+
id.replace(ESM_FOLDER_RE, "/umd/$1"),
|
|
84
|
+
id.replace(ESM_FOLDER_RE, "/cjs/$1"),
|
|
85
|
+
id.replace(ESM_FOLDER_RE, "/$1")
|
|
86
|
+
]) {
|
|
87
|
+
if (fs.existsSync(i))
|
|
88
|
+
return i;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async function shouldExternalize(id, options, cache = new Map()) {
|
|
93
|
+
if (!cache.has(id))
|
|
94
|
+
cache.set(id, _shouldExternalize(id, options));
|
|
95
|
+
return cache.get(id);
|
|
96
|
+
}
|
|
97
|
+
async function _shouldExternalize(id, options) {
|
|
98
|
+
if (mlly.isNodeBuiltin(id))
|
|
99
|
+
return id;
|
|
100
|
+
id = patchWindowsImportPath(id);
|
|
101
|
+
if (matchExternalizePattern(id, options == null ? void 0 : options.inline))
|
|
102
|
+
return false;
|
|
103
|
+
if (matchExternalizePattern(id, options == null ? void 0 : options.external))
|
|
104
|
+
return id;
|
|
105
|
+
const isNodeModule = id.includes("/node_modules/");
|
|
106
|
+
id = isNodeModule ? guessCJSversion(id) || id : id;
|
|
107
|
+
if (matchExternalizePattern(id, defaultInline))
|
|
108
|
+
return false;
|
|
109
|
+
if (matchExternalizePattern(id, depsExternal))
|
|
110
|
+
return id;
|
|
111
|
+
if (isNodeModule && await mlly.isValidNodeImport(id))
|
|
112
|
+
return id;
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
function matchExternalizePattern(id, patterns) {
|
|
116
|
+
if (!patterns)
|
|
117
|
+
return false;
|
|
118
|
+
for (const ex of patterns) {
|
|
119
|
+
if (typeof ex === "string") {
|
|
120
|
+
if (id.includes(`/node_modules/${ex}/`))
|
|
121
|
+
return true;
|
|
122
|
+
} else {
|
|
123
|
+
if (ex.test(id))
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
function patchWindowsImportPath(path) {
|
|
130
|
+
if (path.match(/^\w:\\/))
|
|
131
|
+
return `file:///${slash(path)}`;
|
|
132
|
+
else if (path.match(/^\w:\//))
|
|
133
|
+
return `file:///${path}`;
|
|
134
|
+
else
|
|
135
|
+
return path;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let SOURCEMAPPING_URL = "sourceMa";
|
|
139
|
+
SOURCEMAPPING_URL += "ppingURL";
|
|
140
|
+
class ViteNodeServer {
|
|
141
|
+
constructor(server, options = {}) {
|
|
142
|
+
this.server = server;
|
|
143
|
+
this.options = options;
|
|
144
|
+
this.promiseMap = new Map();
|
|
145
|
+
}
|
|
146
|
+
shouldExternalize(id) {
|
|
147
|
+
return shouldExternalize(id, this.options.deps);
|
|
148
|
+
}
|
|
149
|
+
async fetchModule(id) {
|
|
150
|
+
const externalize = await this.shouldExternalize(toFilePath(id, this.server.config.root));
|
|
151
|
+
if (externalize)
|
|
152
|
+
return { externalize };
|
|
153
|
+
const r = await this.transformRequest(id);
|
|
154
|
+
return { code: r == null ? void 0 : r.code };
|
|
155
|
+
}
|
|
156
|
+
async resolveId(id, importer) {
|
|
157
|
+
return this.server.pluginContainer.resolveId(id, importer, { ssr: true });
|
|
158
|
+
}
|
|
159
|
+
async transformRequest(id) {
|
|
160
|
+
if (!this.promiseMap.has(id)) {
|
|
161
|
+
this.promiseMap.set(id, this._transformRequest(id).finally(() => {
|
|
162
|
+
this.promiseMap.delete(id);
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
return this.promiseMap.get(id);
|
|
166
|
+
}
|
|
167
|
+
getTransformMode(id) {
|
|
168
|
+
var _a, _b, _c, _d;
|
|
169
|
+
const withoutQuery = id.split("?")[0];
|
|
170
|
+
if ((_b = (_a = this.options.transformMode) == null ? void 0 : _a.web) == null ? void 0 : _b.some((r) => withoutQuery.match(r)))
|
|
171
|
+
return "web";
|
|
172
|
+
if ((_d = (_c = this.options.transformMode) == null ? void 0 : _c.ssr) == null ? void 0 : _d.some((r) => withoutQuery.match(r)))
|
|
173
|
+
return "ssr";
|
|
174
|
+
if (withoutQuery.match(/\.([cm]?[jt]sx?|json)$/))
|
|
175
|
+
return "ssr";
|
|
176
|
+
return "web";
|
|
177
|
+
}
|
|
178
|
+
async _transformRequest(id) {
|
|
179
|
+
let result = null;
|
|
180
|
+
const mode = this.getTransformMode(id);
|
|
181
|
+
if (mode === "web") {
|
|
182
|
+
result = await this.server.transformRequest(id);
|
|
183
|
+
if (result)
|
|
184
|
+
result = await this.server.ssrTransform(result.code, result.map, id);
|
|
185
|
+
} else {
|
|
186
|
+
result = await this.server.transformRequest(id, { ssr: true });
|
|
187
|
+
}
|
|
188
|
+
if (this.options.sourcemap !== false && result && !id.includes("node_modules"))
|
|
189
|
+
withInlineSourcemap(result);
|
|
190
|
+
return result;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async function withInlineSourcemap(result) {
|
|
194
|
+
const { code, map } = result;
|
|
195
|
+
if (code.includes(`${SOURCEMAPPING_URL}=`))
|
|
196
|
+
return result;
|
|
197
|
+
if (map)
|
|
198
|
+
result.code = `${code}
|
|
199
|
+
|
|
200
|
+
//# ${SOURCEMAPPING_URL}=data:application/json;charset=utf-8;base64,${Buffer.from(JSON.stringify(map), "utf-8").toString("base64")}
|
|
201
|
+
`;
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const DEFAULT_REQUEST_STUBS = {
|
|
206
|
+
"/@vite/client": {
|
|
207
|
+
injectQuery: (id) => id,
|
|
208
|
+
createHotContext() {
|
|
209
|
+
return {
|
|
210
|
+
accept: () => {
|
|
211
|
+
},
|
|
212
|
+
prune: () => {
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
},
|
|
216
|
+
updateStyle() {
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
class ViteNodeRunner {
|
|
221
|
+
constructor(options) {
|
|
222
|
+
this.options = options;
|
|
223
|
+
this.root = options.root || process.cwd();
|
|
224
|
+
this.moduleCache = options.moduleCache || new Map();
|
|
225
|
+
}
|
|
226
|
+
async executeFile(file) {
|
|
227
|
+
return await this.cachedRequest(`/@fs/${slash(pathe.resolve(file))}`, []);
|
|
228
|
+
}
|
|
229
|
+
async executeId(id) {
|
|
230
|
+
return await this.cachedRequest(id, []);
|
|
231
|
+
}
|
|
232
|
+
async cachedRequest(rawId, callstack) {
|
|
233
|
+
var _a, _b;
|
|
234
|
+
const id = normalizeId(rawId, this.options.base);
|
|
235
|
+
if ((_a = this.moduleCache.get(id)) == null ? void 0 : _a.promise)
|
|
236
|
+
return (_b = this.moduleCache.get(id)) == null ? void 0 : _b.promise;
|
|
237
|
+
const fsPath = toFilePath(id, this.root);
|
|
238
|
+
const promise = this.directRequest(id, fsPath, callstack);
|
|
239
|
+
this.setCache(id, { promise });
|
|
240
|
+
return await promise;
|
|
241
|
+
}
|
|
242
|
+
async directRequest(id, fsPath, callstack) {
|
|
243
|
+
callstack = [...callstack, id];
|
|
244
|
+
const request = async (dep) => {
|
|
245
|
+
var _a;
|
|
246
|
+
if (callstack.includes(dep)) {
|
|
247
|
+
if (!((_a = this.moduleCache.get(dep)) == null ? void 0 : _a.exports))
|
|
248
|
+
throw new Error(`[vite-node] Circular dependency detected
|
|
249
|
+
Stack:
|
|
250
|
+
${[...callstack, dep].reverse().map((p) => `- ${p}`).join("\n")}`);
|
|
251
|
+
return this.moduleCache.get(dep).exports;
|
|
252
|
+
}
|
|
253
|
+
return this.cachedRequest(dep, callstack);
|
|
254
|
+
};
|
|
255
|
+
const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS;
|
|
256
|
+
if (id in requestStubs)
|
|
257
|
+
return requestStubs[id];
|
|
258
|
+
const { code: transformed, externalize } = await this.options.fetchModule(id);
|
|
259
|
+
if (externalize) {
|
|
260
|
+
const mod = await this.interopedImport(externalize);
|
|
261
|
+
this.setCache(id, { exports: mod });
|
|
262
|
+
return mod;
|
|
263
|
+
}
|
|
264
|
+
if (transformed == null)
|
|
265
|
+
throw new Error(`[vite-node] Failed to load ${id}`);
|
|
266
|
+
const url$1 = url.pathToFileURL(fsPath).href;
|
|
267
|
+
const exports = {};
|
|
268
|
+
this.setCache(id, { code: transformed, exports });
|
|
269
|
+
const __filename = url.fileURLToPath(url$1);
|
|
270
|
+
const moduleProxy = {
|
|
271
|
+
set exports(value) {
|
|
272
|
+
exportAll(exports, value);
|
|
273
|
+
exports.default = value;
|
|
274
|
+
},
|
|
275
|
+
get exports() {
|
|
276
|
+
return exports.default;
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
const context = this.prepareContext({
|
|
280
|
+
__vite_ssr_import__: request,
|
|
281
|
+
__vite_ssr_dynamic_import__: request,
|
|
282
|
+
__vite_ssr_exports__: exports,
|
|
283
|
+
__vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
|
|
284
|
+
__vite_ssr_import_meta__: { url: url$1 },
|
|
285
|
+
require: module$1.createRequire(url$1),
|
|
286
|
+
exports,
|
|
287
|
+
module: moduleProxy,
|
|
288
|
+
__filename,
|
|
289
|
+
__dirname: pathe.dirname(__filename)
|
|
290
|
+
});
|
|
291
|
+
const fn = vm__default["default"].runInThisContext(`async (${Object.keys(context).join(",")})=>{{${transformed}
|
|
292
|
+
}}`, {
|
|
293
|
+
filename: fsPath,
|
|
294
|
+
lineOffset: 0
|
|
295
|
+
});
|
|
296
|
+
await fn(...Object.values(context));
|
|
297
|
+
return exports;
|
|
298
|
+
}
|
|
299
|
+
prepareContext(context) {
|
|
300
|
+
return context;
|
|
301
|
+
}
|
|
302
|
+
setCache(id, mod) {
|
|
303
|
+
if (!this.moduleCache.has(id))
|
|
304
|
+
this.moduleCache.set(id, mod);
|
|
305
|
+
else
|
|
306
|
+
Object.assign(this.moduleCache.get(id), mod);
|
|
307
|
+
}
|
|
308
|
+
shouldInterop(path, mod) {
|
|
309
|
+
if (this.options.interopDefault === false)
|
|
310
|
+
return false;
|
|
311
|
+
return !path.endsWith(".mjs") && "default" in mod;
|
|
312
|
+
}
|
|
313
|
+
async interopedImport(path) {
|
|
314
|
+
const mod = await (function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(t)); }); })(path);
|
|
315
|
+
if (this.shouldInterop(path, mod)) {
|
|
316
|
+
const tryDefault = this.hasNestedDefault(mod);
|
|
317
|
+
return new Proxy(mod, {
|
|
318
|
+
get: proxyMethod("get", tryDefault),
|
|
319
|
+
set: proxyMethod("set", tryDefault),
|
|
320
|
+
has: proxyMethod("has", tryDefault),
|
|
321
|
+
deleteProperty: proxyMethod("deleteProperty", tryDefault)
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
return mod;
|
|
325
|
+
}
|
|
326
|
+
hasNestedDefault(target) {
|
|
327
|
+
return "__esModule" in target && target.__esModule && "default" in target.default;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function proxyMethod(name, tryDefault) {
|
|
331
|
+
return function(target, key, ...args) {
|
|
332
|
+
const result = Reflect[name](target, key, ...args);
|
|
333
|
+
if (isPrimitive(target.default))
|
|
334
|
+
return result;
|
|
335
|
+
if (tryDefault && key === "default" || typeof result === "undefined")
|
|
336
|
+
return Reflect[name](target.default, key, ...args);
|
|
337
|
+
return result;
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
function exportAll(exports, sourceModule) {
|
|
341
|
+
for (const key in sourceModule) {
|
|
342
|
+
if (key !== "default") {
|
|
343
|
+
try {
|
|
344
|
+
Object.defineProperty(exports, key, {
|
|
345
|
+
enumerable: true,
|
|
346
|
+
configurable: true,
|
|
347
|
+
get() {
|
|
348
|
+
return sourceModule[key];
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
} catch (_err) {
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const argv = minimist__default["default"](process.argv.slice(2), {
|
|
358
|
+
"alias": {
|
|
359
|
+
r: "root",
|
|
360
|
+
c: "config",
|
|
361
|
+
h: "help",
|
|
362
|
+
w: "watch",
|
|
363
|
+
s: "silent"
|
|
364
|
+
},
|
|
365
|
+
"--": true,
|
|
366
|
+
"string": ["root", "config"],
|
|
367
|
+
"boolean": ["help", "watch", "silent"],
|
|
368
|
+
unknown(name) {
|
|
369
|
+
if (name[0] === "-") {
|
|
370
|
+
console.error(kolorist.red(`Unknown argument: ${name}`));
|
|
371
|
+
help();
|
|
372
|
+
process.exit(1);
|
|
373
|
+
}
|
|
374
|
+
return true;
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
if (argv.help) {
|
|
378
|
+
help();
|
|
379
|
+
process.exit(0);
|
|
380
|
+
}
|
|
381
|
+
if (!argv._.length) {
|
|
382
|
+
console.error(kolorist.red("No files specified."));
|
|
383
|
+
help();
|
|
384
|
+
process.exit(1);
|
|
385
|
+
}
|
|
386
|
+
process.argv = [...process.argv.slice(0, 2), ...argv["--"] || []];
|
|
387
|
+
run(argv);
|
|
388
|
+
function help() {
|
|
389
|
+
console.log(`
|
|
390
|
+
Usage:
|
|
391
|
+
$ vite-node [options] [files]
|
|
392
|
+
|
|
393
|
+
Options:
|
|
394
|
+
-r, --root <path> ${kolorist.dim("[string]")} use specified root directory
|
|
395
|
+
-c, --config <file> ${kolorist.dim("[string]")} use specified config file
|
|
396
|
+
-w, --watch ${kolorist.dim("[boolean]")} restart on file changes, similar to "nodemon"
|
|
397
|
+
-s, --silent ${kolorist.dim("[boolean]")} do not emit errors and logs
|
|
398
|
+
--vue ${kolorist.dim("[boolean]")} support for importing Vue component
|
|
399
|
+
`);
|
|
400
|
+
}
|
|
401
|
+
async function run(options = {}) {
|
|
402
|
+
const files = options.files || options._ || [];
|
|
403
|
+
const server = await vite.createServer({
|
|
404
|
+
logLevel: "error",
|
|
405
|
+
clearScreen: false,
|
|
406
|
+
configFile: options.config,
|
|
407
|
+
root: options.root
|
|
408
|
+
});
|
|
409
|
+
await server.pluginContainer.buildStart({});
|
|
410
|
+
const node = new ViteNodeServer(server);
|
|
411
|
+
const runner = new ViteNodeRunner({
|
|
412
|
+
root: server.config.root,
|
|
413
|
+
base: server.config.base,
|
|
414
|
+
fetchModule(id) {
|
|
415
|
+
return node.fetchModule(id);
|
|
416
|
+
},
|
|
417
|
+
resolveId(id, importer) {
|
|
418
|
+
return node.resolveId(id, importer);
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
for (const file of files)
|
|
422
|
+
await runner.executeFile(file);
|
|
423
|
+
await server.close();
|
|
424
|
+
}
|