vite-node 2.0.0-beta.1 → 2.0.0-beta.11
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/README.md +5 -2
- package/dist/chunk-hmr.cjs +35 -18
- package/dist/chunk-hmr.mjs +35 -18
- package/dist/cli.cjs +17 -9
- package/dist/cli.d.ts +1 -1
- package/dist/cli.mjs +17 -9
- package/dist/client.cjs +79 -36
- package/dist/client.d.ts +1 -1
- package/dist/client.mjs +79 -36
- package/dist/constants.cjs +4 -1
- package/dist/constants.mjs +4 -1
- package/dist/hmr.d.ts +1 -1
- package/dist/{index-C38RQo3J.d.ts → index-D1EszD4V.d.ts} +6 -1
- package/dist/index.d.ts +1 -1
- package/dist/server.cjs +188 -92
- package/dist/server.d.ts +1 -1
- package/dist/server.mjs +189 -93
- package/dist/source-map.cjs +70 -35
- package/dist/source-map.mjs +70 -35
- package/dist/types.d.ts +1 -1
- package/dist/utils.cjs +43 -36
- package/dist/utils.d.ts +2 -4
- package/dist/utils.mjs +44 -35
- package/package.json +3 -3
package/dist/server.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import assert from 'node:assert';
|
|
|
4
4
|
import { join, extname, dirname, resolve, relative, normalize } from 'pathe';
|
|
5
5
|
import createDebug from 'debug';
|
|
6
6
|
import { isNodeBuiltin, slash, findNearestPackageData, toArray, withTrailingSlash, normalizeModuleId, toFilePath } from './utils.mjs';
|
|
7
|
-
import {
|
|
7
|
+
import { KNOWN_ASSET_RE } from './constants.mjs';
|
|
8
8
|
import c from 'picocolors';
|
|
9
9
|
import { withInlineSourcemap } from './source-map.mjs';
|
|
10
10
|
import 'node:url';
|
|
@@ -12,7 +12,7 @@ import 'node:module';
|
|
|
12
12
|
import 'node:path';
|
|
13
13
|
|
|
14
14
|
const BUILTIN_EXTENSIONS = /* @__PURE__ */ new Set([".mjs", ".cjs", ".node", ".wasm"]);
|
|
15
|
-
const ESM_SYNTAX_RE = /([\s;]|^)(import[\s\w*,{}]*from|import\s*["'*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m;
|
|
15
|
+
const ESM_SYNTAX_RE = /(?:[\s;]|^)(?:import[\s\w*,{}]*from|import\s*["'*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m;
|
|
16
16
|
const ESM_EXT_RE = /\.(es|esm|esm-browser|esm-bundler|es6|module)\.js$/;
|
|
17
17
|
const ESM_FOLDER_RE = /\/(es|esm)\/(.*\.js)$/;
|
|
18
18
|
const defaultInline = [
|
|
@@ -21,7 +21,7 @@ const defaultInline = [
|
|
|
21
21
|
// special Vite query strings
|
|
22
22
|
/[?&](init|raw|url|inline)\b/,
|
|
23
23
|
// Vite returns a string for assets imports, even if it's inside "node_modules"
|
|
24
|
-
|
|
24
|
+
KNOWN_ASSET_RE
|
|
25
25
|
];
|
|
26
26
|
const depsExternal = [
|
|
27
27
|
/\/node_modules\/.*\.cjs\.js$/,
|
|
@@ -35,8 +35,9 @@ function guessCJSversion(id) {
|
|
|
35
35
|
id.replace(ESM_EXT_RE, ".cjs.js"),
|
|
36
36
|
id.replace(ESM_EXT_RE, ".js")
|
|
37
37
|
]) {
|
|
38
|
-
if (existsSync(i))
|
|
38
|
+
if (existsSync(i)) {
|
|
39
39
|
return i;
|
|
40
|
+
}
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
43
|
if (id.match(ESM_FOLDER_RE)) {
|
|
@@ -46,79 +47,98 @@ function guessCJSversion(id) {
|
|
|
46
47
|
id.replace(ESM_FOLDER_RE, "/lib/$1"),
|
|
47
48
|
id.replace(ESM_FOLDER_RE, "/$1")
|
|
48
49
|
]) {
|
|
49
|
-
if (existsSync(i))
|
|
50
|
+
if (existsSync(i)) {
|
|
50
51
|
return i;
|
|
52
|
+
}
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
async function isValidNodeImport(id) {
|
|
55
57
|
const extension = extname(id);
|
|
56
|
-
if (BUILTIN_EXTENSIONS.has(extension))
|
|
58
|
+
if (BUILTIN_EXTENSIONS.has(extension)) {
|
|
57
59
|
return true;
|
|
58
|
-
|
|
60
|
+
}
|
|
61
|
+
if (extension !== ".js") {
|
|
59
62
|
return false;
|
|
63
|
+
}
|
|
60
64
|
id = id.replace("file:///", "");
|
|
61
65
|
const package_ = await findNearestPackageData(dirname(id));
|
|
62
|
-
if (package_.type === "module")
|
|
66
|
+
if (package_.type === "module") {
|
|
63
67
|
return true;
|
|
64
|
-
|
|
68
|
+
}
|
|
69
|
+
if (/\.(?:\w+-)?esm?(?:-\w+)?\.js$|\/esm?\//.test(id)) {
|
|
65
70
|
return false;
|
|
71
|
+
}
|
|
66
72
|
const code = await promises.readFile(id, "utf8").catch(() => "");
|
|
67
73
|
return !ESM_SYNTAX_RE.test(code);
|
|
68
74
|
}
|
|
69
75
|
const _defaultExternalizeCache = /* @__PURE__ */ new Map();
|
|
70
76
|
async function shouldExternalize(id, options, cache = _defaultExternalizeCache) {
|
|
71
|
-
if (!cache.has(id))
|
|
77
|
+
if (!cache.has(id)) {
|
|
72
78
|
cache.set(id, _shouldExternalize(id, options));
|
|
79
|
+
}
|
|
73
80
|
return cache.get(id);
|
|
74
81
|
}
|
|
75
82
|
async function _shouldExternalize(id, options) {
|
|
76
|
-
if (isNodeBuiltin(id))
|
|
83
|
+
if (isNodeBuiltin(id)) {
|
|
77
84
|
return id;
|
|
78
|
-
|
|
85
|
+
}
|
|
86
|
+
if (id.startsWith("data:") || /^(?:https?:)?\/\//.test(id)) {
|
|
79
87
|
return id;
|
|
88
|
+
}
|
|
80
89
|
id = patchWindowsImportPath(id);
|
|
81
|
-
if ((options == null ? void 0 : options.cacheDir) && id.includes(options.cacheDir))
|
|
90
|
+
if ((options == null ? void 0 : options.cacheDir) && id.includes(options.cacheDir)) {
|
|
82
91
|
return id;
|
|
92
|
+
}
|
|
83
93
|
const moduleDirectories = (options == null ? void 0 : options.moduleDirectories) || ["/node_modules/"];
|
|
84
|
-
if (matchExternalizePattern(id, moduleDirectories, options == null ? void 0 : options.inline))
|
|
94
|
+
if (matchExternalizePattern(id, moduleDirectories, options == null ? void 0 : options.inline)) {
|
|
85
95
|
return false;
|
|
86
|
-
|
|
96
|
+
}
|
|
97
|
+
if (matchExternalizePattern(id, moduleDirectories, options == null ? void 0 : options.external)) {
|
|
87
98
|
return id;
|
|
99
|
+
}
|
|
88
100
|
const isLibraryModule = moduleDirectories.some((dir) => id.includes(dir));
|
|
89
101
|
const guessCJS = isLibraryModule && (options == null ? void 0 : options.fallbackCJS);
|
|
90
102
|
id = guessCJS ? guessCJSversion(id) || id : id;
|
|
91
|
-
if (matchExternalizePattern(id, moduleDirectories, defaultInline))
|
|
103
|
+
if (matchExternalizePattern(id, moduleDirectories, defaultInline)) {
|
|
92
104
|
return false;
|
|
93
|
-
|
|
105
|
+
}
|
|
106
|
+
if (matchExternalizePattern(id, moduleDirectories, depsExternal)) {
|
|
94
107
|
return id;
|
|
95
|
-
|
|
108
|
+
}
|
|
109
|
+
if (isLibraryModule && await isValidNodeImport(id)) {
|
|
96
110
|
return id;
|
|
111
|
+
}
|
|
97
112
|
return false;
|
|
98
113
|
}
|
|
99
114
|
function matchExternalizePattern(id, moduleDirectories, patterns) {
|
|
100
|
-
if (patterns == null)
|
|
115
|
+
if (patterns == null) {
|
|
101
116
|
return false;
|
|
102
|
-
|
|
117
|
+
}
|
|
118
|
+
if (patterns === true) {
|
|
103
119
|
return true;
|
|
120
|
+
}
|
|
104
121
|
for (const ex of patterns) {
|
|
105
122
|
if (typeof ex === "string") {
|
|
106
|
-
if (moduleDirectories.some((dir) => id.includes(join(dir, ex))))
|
|
123
|
+
if (moduleDirectories.some((dir) => id.includes(join(dir, ex)))) {
|
|
107
124
|
return true;
|
|
125
|
+
}
|
|
108
126
|
} else {
|
|
109
|
-
if (ex.test(id))
|
|
127
|
+
if (ex.test(id)) {
|
|
110
128
|
return true;
|
|
129
|
+
}
|
|
111
130
|
}
|
|
112
131
|
}
|
|
113
132
|
return false;
|
|
114
133
|
}
|
|
115
134
|
function patchWindowsImportPath(path) {
|
|
116
|
-
if (path.match(/^\w:\\/))
|
|
135
|
+
if (path.match(/^\w:\\/)) {
|
|
117
136
|
return `file:///${slash(path)}`;
|
|
118
|
-
else if (path.match(/^\w:\//))
|
|
137
|
+
} else if (path.match(/^\w:\//)) {
|
|
119
138
|
return `file:///${path}`;
|
|
120
|
-
else
|
|
139
|
+
} else {
|
|
121
140
|
return path;
|
|
141
|
+
}
|
|
122
142
|
}
|
|
123
143
|
|
|
124
144
|
function hashCode(s) {
|
|
@@ -130,13 +150,22 @@ function hashCode(s) {
|
|
|
130
150
|
class Debugger {
|
|
131
151
|
constructor(root, options) {
|
|
132
152
|
this.options = options;
|
|
133
|
-
if (options.dumpModules)
|
|
134
|
-
this.dumpDir = resolve(
|
|
153
|
+
if (options.dumpModules) {
|
|
154
|
+
this.dumpDir = resolve(
|
|
155
|
+
root,
|
|
156
|
+
options.dumpModules === true ? ".vite-node/dump" : options.dumpModules
|
|
157
|
+
);
|
|
158
|
+
}
|
|
135
159
|
if (this.dumpDir) {
|
|
136
|
-
if (options.loadDumppedModules)
|
|
137
|
-
console.info(
|
|
138
|
-
|
|
139
|
-
|
|
160
|
+
if (options.loadDumppedModules) {
|
|
161
|
+
console.info(
|
|
162
|
+
c.gray(`[vite-node] [debug] load modules from ${this.dumpDir}`)
|
|
163
|
+
);
|
|
164
|
+
} else {
|
|
165
|
+
console.info(
|
|
166
|
+
c.gray(`[vite-node] [debug] dump modules to ${this.dumpDir}`)
|
|
167
|
+
);
|
|
168
|
+
}
|
|
140
169
|
}
|
|
141
170
|
this.initPromise = this.clearDump();
|
|
142
171
|
}
|
|
@@ -144,50 +173,67 @@ class Debugger {
|
|
|
144
173
|
initPromise;
|
|
145
174
|
externalizeMap = /* @__PURE__ */ new Map();
|
|
146
175
|
async clearDump() {
|
|
147
|
-
if (!this.dumpDir)
|
|
176
|
+
if (!this.dumpDir) {
|
|
148
177
|
return;
|
|
149
|
-
|
|
178
|
+
}
|
|
179
|
+
if (!this.options.loadDumppedModules && existsSync(this.dumpDir)) {
|
|
150
180
|
await promises.rm(this.dumpDir, { recursive: true, force: true });
|
|
181
|
+
}
|
|
151
182
|
await promises.mkdir(this.dumpDir, { recursive: true });
|
|
152
183
|
}
|
|
153
184
|
encodeId(id) {
|
|
154
|
-
return `${id.replace(/[^\w
|
|
185
|
+
return `${id.replace(/[^\w@\-]/g, "_").replace(/_+/g, "_")}-${hashCode(
|
|
186
|
+
id
|
|
187
|
+
)}.js`;
|
|
155
188
|
}
|
|
156
189
|
async recordExternalize(id, path) {
|
|
157
|
-
if (!this.dumpDir)
|
|
190
|
+
if (!this.dumpDir) {
|
|
158
191
|
return;
|
|
192
|
+
}
|
|
159
193
|
this.externalizeMap.set(id, path);
|
|
160
194
|
await this.writeInfo();
|
|
161
195
|
}
|
|
162
196
|
async dumpFile(id, result) {
|
|
163
|
-
if (!result || !this.dumpDir)
|
|
197
|
+
if (!result || !this.dumpDir) {
|
|
164
198
|
return;
|
|
199
|
+
}
|
|
165
200
|
await this.initPromise;
|
|
166
201
|
const name = this.encodeId(id);
|
|
167
|
-
return await promises.writeFile(
|
|
168
|
-
|
|
202
|
+
return await promises.writeFile(
|
|
203
|
+
join(this.dumpDir, name),
|
|
204
|
+
`// ${id.replace(/\0/g, "\\0")}
|
|
205
|
+
${result.code}`,
|
|
206
|
+
"utf-8"
|
|
207
|
+
);
|
|
169
208
|
}
|
|
170
209
|
async loadDump(id) {
|
|
171
|
-
if (!this.dumpDir)
|
|
210
|
+
if (!this.dumpDir) {
|
|
172
211
|
return null;
|
|
212
|
+
}
|
|
173
213
|
await this.initPromise;
|
|
174
214
|
const name = this.encodeId(id);
|
|
175
215
|
const path = join(this.dumpDir, name);
|
|
176
|
-
if (!existsSync(path))
|
|
216
|
+
if (!existsSync(path)) {
|
|
177
217
|
return null;
|
|
218
|
+
}
|
|
178
219
|
const code = await promises.readFile(path, "utf-8");
|
|
179
220
|
return {
|
|
180
|
-
code: code.replace(
|
|
221
|
+
code: code.replace(/^\/\/.*\n/, ""),
|
|
181
222
|
map: void 0
|
|
182
223
|
};
|
|
183
224
|
}
|
|
184
225
|
async writeInfo() {
|
|
185
|
-
if (!this.dumpDir)
|
|
226
|
+
if (!this.dumpDir) {
|
|
186
227
|
return;
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
228
|
+
}
|
|
229
|
+
const info = JSON.stringify(
|
|
230
|
+
{
|
|
231
|
+
time: (/* @__PURE__ */ new Date()).toLocaleString(),
|
|
232
|
+
externalize: Object.fromEntries(this.externalizeMap.entries())
|
|
233
|
+
},
|
|
234
|
+
null,
|
|
235
|
+
2
|
|
236
|
+
);
|
|
191
237
|
return promises.writeFile(join(this.dumpDir, "info.json"), info, "utf-8");
|
|
192
238
|
}
|
|
193
239
|
}
|
|
@@ -200,38 +246,55 @@ class ViteNodeServer {
|
|
|
200
246
|
var _a, _b, _c;
|
|
201
247
|
const ssrOptions = server.config.ssr;
|
|
202
248
|
options.deps ?? (options.deps = {});
|
|
203
|
-
options.deps.cacheDir = relative(
|
|
249
|
+
options.deps.cacheDir = relative(
|
|
250
|
+
server.config.root,
|
|
251
|
+
options.deps.cacheDir || server.config.cacheDir
|
|
252
|
+
);
|
|
204
253
|
if (ssrOptions) {
|
|
205
254
|
if (ssrOptions.noExternal === true) {
|
|
206
255
|
(_a = options.deps).inline ?? (_a.inline = true);
|
|
207
256
|
} else if (options.deps.inline !== true) {
|
|
208
257
|
(_b = options.deps).inline ?? (_b.inline = []);
|
|
209
258
|
const inline = options.deps.inline;
|
|
210
|
-
options.deps.inline.push(
|
|
259
|
+
options.deps.inline.push(
|
|
260
|
+
...toArray(ssrOptions.noExternal).filter(
|
|
261
|
+
(dep) => !inline.includes(dep)
|
|
262
|
+
)
|
|
263
|
+
);
|
|
211
264
|
}
|
|
212
265
|
}
|
|
213
266
|
if (process.env.VITE_NODE_DEBUG_DUMP) {
|
|
214
|
-
options.debug = Object.assign(
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
267
|
+
options.debug = Object.assign(
|
|
268
|
+
{
|
|
269
|
+
dumpModules: !!process.env.VITE_NODE_DEBUG_DUMP,
|
|
270
|
+
loadDumppedModules: process.env.VITE_NODE_DEBUG_DUMP === "load"
|
|
271
|
+
},
|
|
272
|
+
options.debug ?? {}
|
|
273
|
+
);
|
|
218
274
|
}
|
|
219
|
-
if (options.debug)
|
|
275
|
+
if (options.debug) {
|
|
220
276
|
this.debugger = new Debugger(server.config.root, options.debug);
|
|
277
|
+
}
|
|
221
278
|
(_c = options.deps).moduleDirectories ?? (_c.moduleDirectories = []);
|
|
222
279
|
const envValue = process.env.VITE_NODE_DEPS_MODULE_DIRECTORIES || process.env.npm_config_VITE_NODE_DEPS_MODULE_DIRECTORIES;
|
|
223
280
|
const customModuleDirectories = envValue == null ? void 0 : envValue.split(",");
|
|
224
|
-
if (customModuleDirectories)
|
|
281
|
+
if (customModuleDirectories) {
|
|
225
282
|
options.deps.moduleDirectories.push(...customModuleDirectories);
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
283
|
+
}
|
|
284
|
+
options.deps.moduleDirectories = options.deps.moduleDirectories.map(
|
|
285
|
+
(dir) => {
|
|
286
|
+
if (!dir.startsWith("/")) {
|
|
287
|
+
dir = `/${dir}`;
|
|
288
|
+
}
|
|
289
|
+
if (!dir.endsWith("/")) {
|
|
290
|
+
dir += "/";
|
|
291
|
+
}
|
|
292
|
+
return normalize(dir);
|
|
293
|
+
}
|
|
294
|
+
);
|
|
295
|
+
if (!options.deps.moduleDirectories.includes("/node_modules/")) {
|
|
234
296
|
options.deps.moduleDirectories.push("/node_modules/");
|
|
297
|
+
}
|
|
235
298
|
}
|
|
236
299
|
fetchPromiseMap = {
|
|
237
300
|
ssr: /* @__PURE__ */ new Map(),
|
|
@@ -262,8 +325,9 @@ class ViteNodeServer {
|
|
|
262
325
|
return [...ssrDurations, ...webDurations].reduce((a, b) => a + b, 0);
|
|
263
326
|
}
|
|
264
327
|
async ensureExists(id) {
|
|
265
|
-
if (this.existingOptimizedDeps.has(id))
|
|
328
|
+
if (this.existingOptimizedDeps.has(id)) {
|
|
266
329
|
return true;
|
|
330
|
+
}
|
|
267
331
|
if (existsSync(id)) {
|
|
268
332
|
this.existingOptimizedDeps.add(id);
|
|
269
333
|
return true;
|
|
@@ -277,21 +341,28 @@ class ViteNodeServer {
|
|
|
277
341
|
});
|
|
278
342
|
}
|
|
279
343
|
async resolveId(id, importer, transformMode) {
|
|
280
|
-
if (importer && !importer.startsWith(withTrailingSlash(this.server.config.root)))
|
|
344
|
+
if (importer && !importer.startsWith(withTrailingSlash(this.server.config.root))) {
|
|
281
345
|
importer = resolve(this.server.config.root, importer);
|
|
346
|
+
}
|
|
282
347
|
const mode = transformMode ?? (importer && this.getTransformMode(importer) || "ssr");
|
|
283
|
-
return this.server.pluginContainer.resolveId(id, importer, {
|
|
348
|
+
return this.server.pluginContainer.resolveId(id, importer, {
|
|
349
|
+
ssr: mode === "ssr"
|
|
350
|
+
});
|
|
284
351
|
}
|
|
285
352
|
getSourceMap(source) {
|
|
286
353
|
var _a, _b;
|
|
287
354
|
const fetchResult = (_a = this.fetchCache.get(source)) == null ? void 0 : _a.result;
|
|
288
|
-
if (fetchResult == null ? void 0 : fetchResult.map)
|
|
355
|
+
if (fetchResult == null ? void 0 : fetchResult.map) {
|
|
289
356
|
return fetchResult.map;
|
|
357
|
+
}
|
|
290
358
|
const ssrTransformResult = (_b = this.server.moduleGraph.getModuleById(source)) == null ? void 0 : _b.ssrTransformResult;
|
|
291
359
|
return (ssrTransformResult == null ? void 0 : ssrTransformResult.map) || null;
|
|
292
360
|
}
|
|
293
361
|
assertMode(mode) {
|
|
294
|
-
assert(
|
|
362
|
+
assert(
|
|
363
|
+
mode === "web" || mode === "ssr",
|
|
364
|
+
`"transformMode" can only be "web" or "ssr", received "${mode}".`
|
|
365
|
+
);
|
|
295
366
|
}
|
|
296
367
|
async fetchModule(id, transformMode) {
|
|
297
368
|
const mode = transformMode || this.getTransformMode(id);
|
|
@@ -304,9 +375,12 @@ class ViteNodeServer {
|
|
|
304
375
|
this.assertMode(mode);
|
|
305
376
|
const promiseMap = this.fetchPromiseMap[mode];
|
|
306
377
|
if (!promiseMap.has(moduleId)) {
|
|
307
|
-
promiseMap.set(
|
|
308
|
-
|
|
309
|
-
|
|
378
|
+
promiseMap.set(
|
|
379
|
+
moduleId,
|
|
380
|
+
this._fetchModule(moduleId, mode).finally(() => {
|
|
381
|
+
promiseMap.delete(moduleId);
|
|
382
|
+
})
|
|
383
|
+
);
|
|
310
384
|
}
|
|
311
385
|
return promiseMap.get(moduleId);
|
|
312
386
|
}
|
|
@@ -315,15 +389,21 @@ class ViteNodeServer {
|
|
|
315
389
|
this.assertMode(mode);
|
|
316
390
|
const promiseMap = this.transformPromiseMap[mode];
|
|
317
391
|
if (!promiseMap.has(id)) {
|
|
318
|
-
promiseMap.set(
|
|
319
|
-
|
|
320
|
-
|
|
392
|
+
promiseMap.set(
|
|
393
|
+
id,
|
|
394
|
+
this._transformRequest(id, filepath, mode).finally(() => {
|
|
395
|
+
promiseMap.delete(id);
|
|
396
|
+
})
|
|
397
|
+
);
|
|
321
398
|
}
|
|
322
399
|
return promiseMap.get(id);
|
|
323
400
|
}
|
|
324
401
|
async transformModule(id, transformMode) {
|
|
325
|
-
if (transformMode !== "web")
|
|
326
|
-
throw new Error(
|
|
402
|
+
if (transformMode !== "web") {
|
|
403
|
+
throw new Error(
|
|
404
|
+
'`transformModule` only supports `transformMode: "web"`.'
|
|
405
|
+
);
|
|
406
|
+
}
|
|
327
407
|
const normalizedId = normalizeModuleId(id);
|
|
328
408
|
const mod = this.server.moduleGraph.getModuleById(normalizedId);
|
|
329
409
|
const result = (mod == null ? void 0 : mod.transformResult) || await this.server.transformRequest(normalizedId);
|
|
@@ -334,26 +414,34 @@ class ViteNodeServer {
|
|
|
334
414
|
getTransformMode(id) {
|
|
335
415
|
var _a, _b, _c, _d;
|
|
336
416
|
const withoutQuery = id.split("?")[0];
|
|
337
|
-
if ((_b = (_a = this.options.transformMode) == null ? void 0 : _a.web) == null ? void 0 : _b.some((r) => withoutQuery.match(r)))
|
|
417
|
+
if ((_b = (_a = this.options.transformMode) == null ? void 0 : _a.web) == null ? void 0 : _b.some((r) => withoutQuery.match(r))) {
|
|
338
418
|
return "web";
|
|
339
|
-
|
|
419
|
+
}
|
|
420
|
+
if ((_d = (_c = this.options.transformMode) == null ? void 0 : _c.ssr) == null ? void 0 : _d.some((r) => withoutQuery.match(r))) {
|
|
340
421
|
return "ssr";
|
|
341
|
-
|
|
422
|
+
}
|
|
423
|
+
if (withoutQuery.match(/\.([cm]?[jt]sx?|json)$/)) {
|
|
342
424
|
return "ssr";
|
|
425
|
+
}
|
|
343
426
|
return "web";
|
|
344
427
|
}
|
|
345
428
|
getChangedModule(id, file) {
|
|
346
429
|
const module = this.server.moduleGraph.getModuleById(id) || this.server.moduleGraph.getModuleById(file);
|
|
347
|
-
if (module)
|
|
430
|
+
if (module) {
|
|
348
431
|
return module;
|
|
432
|
+
}
|
|
349
433
|
const _modules = this.server.moduleGraph.getModulesByFile(file);
|
|
350
|
-
if (!_modules || !_modules.size)
|
|
434
|
+
if (!_modules || !_modules.size) {
|
|
351
435
|
return null;
|
|
436
|
+
}
|
|
352
437
|
const modules = [..._modules];
|
|
353
438
|
let mod = modules[0];
|
|
354
439
|
let latestMax = -1;
|
|
355
440
|
for (const m of _modules) {
|
|
356
|
-
const timestamp = Math.max(
|
|
441
|
+
const timestamp = Math.max(
|
|
442
|
+
m.lastHMRTimestamp,
|
|
443
|
+
m.lastInvalidationTimestamp
|
|
444
|
+
);
|
|
357
445
|
if (timestamp > latestMax) {
|
|
358
446
|
latestMax = timestamp;
|
|
359
447
|
mod = m;
|
|
@@ -366,10 +454,13 @@ class ViteNodeServer {
|
|
|
366
454
|
let result;
|
|
367
455
|
const cacheDir = (_a = this.options.deps) == null ? void 0 : _a.cacheDir;
|
|
368
456
|
if (cacheDir && id.includes(cacheDir)) {
|
|
369
|
-
if (!id.startsWith(withTrailingSlash(this.server.config.root)))
|
|
457
|
+
if (!id.startsWith(withTrailingSlash(this.server.config.root))) {
|
|
370
458
|
id = join(this.server.config.root, id);
|
|
459
|
+
}
|
|
371
460
|
const timeout = setTimeout(() => {
|
|
372
|
-
throw new Error(
|
|
461
|
+
throw new Error(
|
|
462
|
+
`ViteNodeServer: ${id} not found. This is a bug, please report it.`
|
|
463
|
+
);
|
|
373
464
|
}, 5e3);
|
|
374
465
|
await this.ensureExists(id);
|
|
375
466
|
clearTimeout(timeout);
|
|
@@ -377,9 +468,13 @@ class ViteNodeServer {
|
|
|
377
468
|
const { path: filePath } = toFilePath(id, this.server.config.root);
|
|
378
469
|
const moduleNode = this.getChangedModule(id, filePath);
|
|
379
470
|
const cache = this.fetchCaches[transformMode].get(filePath);
|
|
380
|
-
const timestamp = moduleNode ? Math.max(
|
|
381
|
-
|
|
471
|
+
const timestamp = moduleNode ? Math.max(
|
|
472
|
+
moduleNode.lastHMRTimestamp,
|
|
473
|
+
moduleNode.lastInvalidationTimestamp
|
|
474
|
+
) : 0;
|
|
475
|
+
if (cache && (timestamp === 0 || cache.timestamp >= timestamp)) {
|
|
382
476
|
return cache.result;
|
|
477
|
+
}
|
|
383
478
|
const time = Date.now();
|
|
384
479
|
const externalize = await this.shouldExternalize(filePath);
|
|
385
480
|
let duration;
|
|
@@ -398,10 +493,7 @@ class ViteNodeServer {
|
|
|
398
493
|
result
|
|
399
494
|
};
|
|
400
495
|
const durations = this.durations[transformMode].get(filePath) || [];
|
|
401
|
-
this.durations[transformMode].set(
|
|
402
|
-
filePath,
|
|
403
|
-
[...durations, duration ?? 0]
|
|
404
|
-
);
|
|
496
|
+
this.durations[transformMode].set(filePath, [...durations, duration ?? 0]);
|
|
405
497
|
this.fetchCaches[transformMode].set(filePath, cacheEntry);
|
|
406
498
|
this.fetchCache.set(filePath, cacheEntry);
|
|
407
499
|
return result;
|
|
@@ -419,21 +511,25 @@ class ViteNodeServer {
|
|
|
419
511
|
let result = null;
|
|
420
512
|
if ((_a = this.options.debug) == null ? void 0 : _a.loadDumppedModules) {
|
|
421
513
|
result = await ((_b = this.debugger) == null ? void 0 : _b.loadDump(id)) ?? null;
|
|
422
|
-
if (result)
|
|
514
|
+
if (result) {
|
|
423
515
|
return result;
|
|
516
|
+
}
|
|
424
517
|
}
|
|
425
518
|
if (transformMode === "web") {
|
|
426
519
|
result = await this.server.transformRequest(id);
|
|
427
|
-
if (result)
|
|
520
|
+
if (result) {
|
|
428
521
|
result = await this.server.ssrTransform(result.code, result.map, id);
|
|
522
|
+
}
|
|
429
523
|
} else {
|
|
430
524
|
result = await this.server.transformRequest(id, { ssr: true });
|
|
431
525
|
}
|
|
432
526
|
const sourcemap = this.options.sourcemap ?? "inline";
|
|
433
|
-
if (sourcemap === "inline" && result && !id.includes("node_modules"))
|
|
527
|
+
if (sourcemap === "inline" && result && !id.includes("node_modules")) {
|
|
434
528
|
result = await this.processTransformResult(filepath, result);
|
|
435
|
-
|
|
529
|
+
}
|
|
530
|
+
if ((_c = this.options.debug) == null ? void 0 : _c.dumpModules) {
|
|
436
531
|
await ((_d = this.debugger) == null ? void 0 : _d.dumpFile(id, result));
|
|
532
|
+
}
|
|
437
533
|
return result;
|
|
438
534
|
}
|
|
439
535
|
}
|