vite-node 2.0.0-beta.8 → 2.0.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/README.md +5 -2
- package/dist/chunk-hmr.cjs +35 -18
- package/dist/chunk-hmr.mjs +35 -18
- package/dist/cli.cjs +23 -11
- package/dist/cli.d.ts +1 -1
- package/dist/cli.mjs +24 -12
- package/dist/client.cjs +78 -35
- package/dist/client.d.ts +1 -1
- package/dist/client.mjs +78 -35
- package/dist/constants.cjs +4 -1
- package/dist/constants.mjs +4 -1
- package/dist/hmr.d.ts +1 -1
- package/dist/{index-D1EszD4V.d.ts → index-CCsqCcr7.d.ts} +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/server.cjs +186 -104
- package/dist/server.d.ts +1 -7
- package/dist/server.mjs +189 -107
- package/dist/source-map.cjs +66 -31
- package/dist/source-map.mjs +66 -31
- package/dist/types.d.ts +1 -1
- package/dist/utils.cjs +41 -31
- package/dist/utils.d.ts +1 -1
- package/dist/utils.mjs +41 -31
- package/package.json +3 -3
package/dist/server.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { performance } from 'node:perf_hooks';
|
|
2
2
|
import { existsSync, promises } from 'node:fs';
|
|
3
3
|
import assert from 'node:assert';
|
|
4
|
-
import { join, extname, dirname, resolve, relative, normalize
|
|
4
|
+
import { join, extname, dirname, resolve, relative, normalize } from 'pathe';
|
|
5
5
|
import createDebug from 'debug';
|
|
6
|
-
import { isNodeBuiltin, slash, findNearestPackageData, toArray, withTrailingSlash,
|
|
7
|
-
import {
|
|
6
|
+
import { isNodeBuiltin, slash, findNearestPackageData, toArray, withTrailingSlash, normalizeModuleId, toFilePath } from './utils.mjs';
|
|
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';
|
|
@@ -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,37 +173,49 @@ 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@\-]/g, "_").replace(/_+/g, "_")}-${hashCode(
|
|
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
221
|
code: code.replace(/^\/\/.*\n/, ""),
|
|
@@ -182,12 +223,17 @@ ${result.code}`, "utf-8");
|
|
|
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,35 +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, {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
const id = (resolved == null ? void 0 : resolved.id) || rawId;
|
|
287
|
-
const external = !isAbsolute(id) || this.isModuleDirectory(id) ? rawId : null;
|
|
288
|
-
return {
|
|
289
|
-
id,
|
|
290
|
-
fsPath: cleanUrl(id),
|
|
291
|
-
external
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
isModuleDirectory(path) {
|
|
295
|
-
var _a;
|
|
296
|
-
const moduleDirectories = ((_a = this.options.deps) == null ? void 0 : _a.moduleDirectories) || ["/node_modules/"];
|
|
297
|
-
return moduleDirectories.some((dir) => path.includes(dir));
|
|
348
|
+
return this.server.pluginContainer.resolveId(id, importer, {
|
|
349
|
+
ssr: mode === "ssr"
|
|
350
|
+
});
|
|
298
351
|
}
|
|
299
352
|
getSourceMap(source) {
|
|
300
353
|
var _a, _b;
|
|
301
354
|
const fetchResult = (_a = this.fetchCache.get(source)) == null ? void 0 : _a.result;
|
|
302
|
-
if (fetchResult == null ? void 0 : fetchResult.map)
|
|
355
|
+
if (fetchResult == null ? void 0 : fetchResult.map) {
|
|
303
356
|
return fetchResult.map;
|
|
357
|
+
}
|
|
304
358
|
const ssrTransformResult = (_b = this.server.moduleGraph.getModuleById(source)) == null ? void 0 : _b.ssrTransformResult;
|
|
305
359
|
return (ssrTransformResult == null ? void 0 : ssrTransformResult.map) || null;
|
|
306
360
|
}
|
|
307
361
|
assertMode(mode) {
|
|
308
|
-
assert(
|
|
362
|
+
assert(
|
|
363
|
+
mode === "web" || mode === "ssr",
|
|
364
|
+
`"transformMode" can only be "web" or "ssr", received "${mode}".`
|
|
365
|
+
);
|
|
309
366
|
}
|
|
310
367
|
async fetchModule(id, transformMode) {
|
|
311
368
|
const mode = transformMode || this.getTransformMode(id);
|
|
@@ -318,9 +375,12 @@ class ViteNodeServer {
|
|
|
318
375
|
this.assertMode(mode);
|
|
319
376
|
const promiseMap = this.fetchPromiseMap[mode];
|
|
320
377
|
if (!promiseMap.has(moduleId)) {
|
|
321
|
-
promiseMap.set(
|
|
322
|
-
|
|
323
|
-
|
|
378
|
+
promiseMap.set(
|
|
379
|
+
moduleId,
|
|
380
|
+
this._fetchModule(moduleId, mode).finally(() => {
|
|
381
|
+
promiseMap.delete(moduleId);
|
|
382
|
+
})
|
|
383
|
+
);
|
|
324
384
|
}
|
|
325
385
|
return promiseMap.get(moduleId);
|
|
326
386
|
}
|
|
@@ -329,15 +389,21 @@ class ViteNodeServer {
|
|
|
329
389
|
this.assertMode(mode);
|
|
330
390
|
const promiseMap = this.transformPromiseMap[mode];
|
|
331
391
|
if (!promiseMap.has(id)) {
|
|
332
|
-
promiseMap.set(
|
|
333
|
-
|
|
334
|
-
|
|
392
|
+
promiseMap.set(
|
|
393
|
+
id,
|
|
394
|
+
this._transformRequest(id, filepath, mode).finally(() => {
|
|
395
|
+
promiseMap.delete(id);
|
|
396
|
+
})
|
|
397
|
+
);
|
|
335
398
|
}
|
|
336
399
|
return promiseMap.get(id);
|
|
337
400
|
}
|
|
338
401
|
async transformModule(id, transformMode) {
|
|
339
|
-
if (transformMode !== "web")
|
|
340
|
-
throw new Error(
|
|
402
|
+
if (transformMode !== "web") {
|
|
403
|
+
throw new Error(
|
|
404
|
+
'`transformModule` only supports `transformMode: "web"`.'
|
|
405
|
+
);
|
|
406
|
+
}
|
|
341
407
|
const normalizedId = normalizeModuleId(id);
|
|
342
408
|
const mod = this.server.moduleGraph.getModuleById(normalizedId);
|
|
343
409
|
const result = (mod == null ? void 0 : mod.transformResult) || await this.server.transformRequest(normalizedId);
|
|
@@ -348,26 +414,34 @@ class ViteNodeServer {
|
|
|
348
414
|
getTransformMode(id) {
|
|
349
415
|
var _a, _b, _c, _d;
|
|
350
416
|
const withoutQuery = id.split("?")[0];
|
|
351
|
-
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))) {
|
|
352
418
|
return "web";
|
|
353
|
-
|
|
419
|
+
}
|
|
420
|
+
if ((_d = (_c = this.options.transformMode) == null ? void 0 : _c.ssr) == null ? void 0 : _d.some((r) => withoutQuery.match(r))) {
|
|
354
421
|
return "ssr";
|
|
355
|
-
|
|
422
|
+
}
|
|
423
|
+
if (withoutQuery.match(/\.([cm]?[jt]sx?|json)$/)) {
|
|
356
424
|
return "ssr";
|
|
425
|
+
}
|
|
357
426
|
return "web";
|
|
358
427
|
}
|
|
359
428
|
getChangedModule(id, file) {
|
|
360
429
|
const module = this.server.moduleGraph.getModuleById(id) || this.server.moduleGraph.getModuleById(file);
|
|
361
|
-
if (module)
|
|
430
|
+
if (module) {
|
|
362
431
|
return module;
|
|
432
|
+
}
|
|
363
433
|
const _modules = this.server.moduleGraph.getModulesByFile(file);
|
|
364
|
-
if (!_modules || !_modules.size)
|
|
434
|
+
if (!_modules || !_modules.size) {
|
|
365
435
|
return null;
|
|
436
|
+
}
|
|
366
437
|
const modules = [..._modules];
|
|
367
438
|
let mod = modules[0];
|
|
368
439
|
let latestMax = -1;
|
|
369
440
|
for (const m of _modules) {
|
|
370
|
-
const timestamp = Math.max(
|
|
441
|
+
const timestamp = Math.max(
|
|
442
|
+
m.lastHMRTimestamp,
|
|
443
|
+
m.lastInvalidationTimestamp
|
|
444
|
+
);
|
|
371
445
|
if (timestamp > latestMax) {
|
|
372
446
|
latestMax = timestamp;
|
|
373
447
|
mod = m;
|
|
@@ -380,10 +454,13 @@ class ViteNodeServer {
|
|
|
380
454
|
let result;
|
|
381
455
|
const cacheDir = (_a = this.options.deps) == null ? void 0 : _a.cacheDir;
|
|
382
456
|
if (cacheDir && id.includes(cacheDir)) {
|
|
383
|
-
if (!id.startsWith(withTrailingSlash(this.server.config.root)))
|
|
457
|
+
if (!id.startsWith(withTrailingSlash(this.server.config.root))) {
|
|
384
458
|
id = join(this.server.config.root, id);
|
|
459
|
+
}
|
|
385
460
|
const timeout = setTimeout(() => {
|
|
386
|
-
throw new Error(
|
|
461
|
+
throw new Error(
|
|
462
|
+
`ViteNodeServer: ${id} not found. This is a bug, please report it.`
|
|
463
|
+
);
|
|
387
464
|
}, 5e3);
|
|
388
465
|
await this.ensureExists(id);
|
|
389
466
|
clearTimeout(timeout);
|
|
@@ -391,9 +468,13 @@ class ViteNodeServer {
|
|
|
391
468
|
const { path: filePath } = toFilePath(id, this.server.config.root);
|
|
392
469
|
const moduleNode = this.getChangedModule(id, filePath);
|
|
393
470
|
const cache = this.fetchCaches[transformMode].get(filePath);
|
|
394
|
-
const timestamp = moduleNode ? Math.max(
|
|
395
|
-
|
|
471
|
+
const timestamp = moduleNode ? Math.max(
|
|
472
|
+
moduleNode.lastHMRTimestamp,
|
|
473
|
+
moduleNode.lastInvalidationTimestamp
|
|
474
|
+
) : 0;
|
|
475
|
+
if (cache && (timestamp === 0 || cache.timestamp >= timestamp)) {
|
|
396
476
|
return cache.result;
|
|
477
|
+
}
|
|
397
478
|
const time = Date.now();
|
|
398
479
|
const externalize = await this.shouldExternalize(filePath);
|
|
399
480
|
let duration;
|
|
@@ -412,10 +493,7 @@ class ViteNodeServer {
|
|
|
412
493
|
result
|
|
413
494
|
};
|
|
414
495
|
const durations = this.durations[transformMode].get(filePath) || [];
|
|
415
|
-
this.durations[transformMode].set(
|
|
416
|
-
filePath,
|
|
417
|
-
[...durations, duration ?? 0]
|
|
418
|
-
);
|
|
496
|
+
this.durations[transformMode].set(filePath, [...durations, duration ?? 0]);
|
|
419
497
|
this.fetchCaches[transformMode].set(filePath, cacheEntry);
|
|
420
498
|
this.fetchCache.set(filePath, cacheEntry);
|
|
421
499
|
return result;
|
|
@@ -433,21 +511,25 @@ class ViteNodeServer {
|
|
|
433
511
|
let result = null;
|
|
434
512
|
if ((_a = this.options.debug) == null ? void 0 : _a.loadDumppedModules) {
|
|
435
513
|
result = await ((_b = this.debugger) == null ? void 0 : _b.loadDump(id)) ?? null;
|
|
436
|
-
if (result)
|
|
514
|
+
if (result) {
|
|
437
515
|
return result;
|
|
516
|
+
}
|
|
438
517
|
}
|
|
439
518
|
if (transformMode === "web") {
|
|
440
519
|
result = await this.server.transformRequest(id);
|
|
441
|
-
if (result)
|
|
520
|
+
if (result) {
|
|
442
521
|
result = await this.server.ssrTransform(result.code, result.map, id);
|
|
522
|
+
}
|
|
443
523
|
} else {
|
|
444
524
|
result = await this.server.transformRequest(id, { ssr: true });
|
|
445
525
|
}
|
|
446
526
|
const sourcemap = this.options.sourcemap ?? "inline";
|
|
447
|
-
if (sourcemap === "inline" && result && !id.includes("node_modules"))
|
|
527
|
+
if (sourcemap === "inline" && result && !id.includes("node_modules")) {
|
|
448
528
|
result = await this.processTransformResult(filepath, result);
|
|
449
|
-
|
|
529
|
+
}
|
|
530
|
+
if ((_c = this.options.debug) == null ? void 0 : _c.dumpModules) {
|
|
450
531
|
await ((_d = this.debugger) == null ? void 0 : _d.dumpFile(id, result));
|
|
532
|
+
}
|
|
451
533
|
return result;
|
|
452
534
|
}
|
|
453
535
|
}
|