vite 5.1.4 → 5.1.5

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.
@@ -1,1617 +1,1021 @@
1
- class HMRContext {
2
- hmrClient;
3
- ownerPath;
4
- newListeners;
5
- constructor(hmrClient, ownerPath) {
6
- this.hmrClient = hmrClient;
7
- this.ownerPath = ownerPath;
8
- if (!hmrClient.dataMap.has(ownerPath)) {
9
- hmrClient.dataMap.set(ownerPath, {});
10
- }
11
- // when a file is hot updated, a new context is created
12
- // clear its stale callbacks
13
- const mod = hmrClient.hotModulesMap.get(ownerPath);
14
- if (mod) {
15
- mod.callbacks = [];
16
- }
17
- // clear stale custom event listeners
18
- const staleListeners = hmrClient.ctxToListenersMap.get(ownerPath);
19
- if (staleListeners) {
20
- for (const [event, staleFns] of staleListeners) {
21
- const listeners = hmrClient.customListenersMap.get(event);
22
- if (listeners) {
23
- hmrClient.customListenersMap.set(event, listeners.filter((l) => !staleFns.includes(l)));
24
- }
25
- }
26
- }
27
- this.newListeners = new Map();
28
- hmrClient.ctxToListenersMap.set(ownerPath, this.newListeners);
29
- }
30
- get data() {
31
- return this.hmrClient.dataMap.get(this.ownerPath);
32
- }
33
- accept(deps, callback) {
34
- if (typeof deps === 'function' || !deps) {
35
- // self-accept: hot.accept(() => {})
36
- this.acceptDeps([this.ownerPath], ([mod]) => deps?.(mod));
37
- }
38
- else if (typeof deps === 'string') {
39
- // explicit deps
40
- this.acceptDeps([deps], ([mod]) => callback?.(mod));
41
- }
42
- else if (Array.isArray(deps)) {
43
- this.acceptDeps(deps, callback);
44
- }
45
- else {
46
- throw new Error(`invalid hot.accept() usage.`);
47
- }
48
- }
49
- // export names (first arg) are irrelevant on the client side, they're
50
- // extracted in the server for propagation
51
- acceptExports(_, callback) {
52
- this.acceptDeps([this.ownerPath], ([mod]) => callback?.(mod));
53
- }
54
- dispose(cb) {
55
- this.hmrClient.disposeMap.set(this.ownerPath, cb);
56
- }
57
- prune(cb) {
58
- this.hmrClient.pruneMap.set(this.ownerPath, cb);
59
- }
60
- // Kept for backward compatibility (#11036)
61
- // eslint-disable-next-line @typescript-eslint/no-empty-function
62
- decline() { }
63
- invalidate(message) {
64
- this.hmrClient.notifyListeners('vite:invalidate', {
65
- path: this.ownerPath,
66
- message,
67
- });
68
- this.send('vite:invalidate', { path: this.ownerPath, message });
69
- this.hmrClient.logger.debug(`[vite] invalidate ${this.ownerPath}${message ? `: ${message}` : ''}`);
70
- }
71
- on(event, cb) {
72
- const addToMap = (map) => {
73
- const existing = map.get(event) || [];
74
- existing.push(cb);
75
- map.set(event, existing);
76
- };
77
- addToMap(this.hmrClient.customListenersMap);
78
- addToMap(this.newListeners);
79
- }
80
- off(event, cb) {
81
- const removeFromMap = (map) => {
82
- const existing = map.get(event);
83
- if (existing === undefined) {
84
- return;
85
- }
86
- const pruned = existing.filter((l) => l !== cb);
87
- if (pruned.length === 0) {
88
- map.delete(event);
89
- return;
90
- }
91
- map.set(event, pruned);
92
- };
93
- removeFromMap(this.hmrClient.customListenersMap);
94
- removeFromMap(this.newListeners);
95
- }
96
- send(event, data) {
97
- this.hmrClient.messenger.send(JSON.stringify({ type: 'custom', event, data }));
98
- }
99
- acceptDeps(deps, callback = () => { }) {
100
- const mod = this.hmrClient.hotModulesMap.get(this.ownerPath) || {
101
- id: this.ownerPath,
102
- callbacks: [],
103
- };
104
- mod.callbacks.push({
105
- deps,
106
- fn: callback,
107
- });
108
- this.hmrClient.hotModulesMap.set(this.ownerPath, mod);
109
- }
110
- }
111
- class HMRMessenger {
112
- connection;
113
- constructor(connection) {
114
- this.connection = connection;
115
- }
116
- queue = [];
117
- send(message) {
118
- this.queue.push(message);
119
- this.flush();
120
- }
121
- flush() {
122
- if (this.connection.isReady()) {
123
- this.queue.forEach((msg) => this.connection.send(msg));
124
- this.queue = [];
125
- }
126
- }
127
- }
128
- class HMRClient {
129
- logger;
130
- importUpdatedModule;
131
- hotModulesMap = new Map();
132
- disposeMap = new Map();
133
- pruneMap = new Map();
134
- dataMap = new Map();
135
- customListenersMap = new Map();
136
- ctxToListenersMap = new Map();
137
- messenger;
138
- constructor(logger, connection,
139
- // This allows implementing reloading via different methods depending on the environment
140
- importUpdatedModule) {
141
- this.logger = logger;
142
- this.importUpdatedModule = importUpdatedModule;
143
- this.messenger = new HMRMessenger(connection);
144
- }
145
- async notifyListeners(event, data) {
146
- const cbs = this.customListenersMap.get(event);
147
- if (cbs) {
148
- await Promise.allSettled(cbs.map((cb) => cb(data)));
149
- }
150
- }
151
- clear() {
152
- this.hotModulesMap.clear();
153
- this.disposeMap.clear();
154
- this.pruneMap.clear();
155
- this.dataMap.clear();
156
- this.customListenersMap.clear();
157
- this.ctxToListenersMap.clear();
158
- }
159
- // After an HMR update, some modules are no longer imported on the page
160
- // but they may have left behind side effects that need to be cleaned up
161
- // (.e.g style injections)
162
- // TODO Trigger their dispose callbacks.
163
- prunePaths(paths) {
164
- paths.forEach((path) => {
165
- const fn = this.pruneMap.get(path);
166
- if (fn) {
167
- fn(this.dataMap.get(path));
168
- }
169
- });
170
- }
171
- warnFailedUpdate(err, path) {
172
- if (!err.message.includes('fetch')) {
173
- this.logger.error(err);
174
- }
175
- this.logger.error(`[hmr] Failed to reload ${path}. ` +
176
- `This could be due to syntax errors or importing non-existent ` +
177
- `modules. (see errors above)`);
178
- }
179
- updateQueue = [];
180
- pendingUpdateQueue = false;
181
- /**
182
- * buffer multiple hot updates triggered by the same src change
183
- * so that they are invoked in the same order they were sent.
184
- * (otherwise the order may be inconsistent because of the http request round trip)
185
- */
186
- async queueUpdate(payload) {
187
- this.updateQueue.push(this.fetchUpdate(payload));
188
- if (!this.pendingUpdateQueue) {
189
- this.pendingUpdateQueue = true;
190
- await Promise.resolve();
191
- this.pendingUpdateQueue = false;
192
- const loading = [...this.updateQueue];
193
- this.updateQueue = [];
194
- (await Promise.all(loading)).forEach((fn) => fn && fn());
195
- }
196
- }
197
- async fetchUpdate(update) {
198
- const { path, acceptedPath } = update;
199
- const mod = this.hotModulesMap.get(path);
200
- if (!mod) {
201
- // In a code-splitting project,
202
- // it is common that the hot-updating module is not loaded yet.
203
- // https://github.com/vitejs/vite/issues/721
204
- return;
205
- }
206
- let fetchedModule;
207
- const isSelfUpdate = path === acceptedPath;
208
- // determine the qualified callbacks before we re-import the modules
209
- const qualifiedCallbacks = mod.callbacks.filter(({ deps }) => deps.includes(acceptedPath));
210
- if (isSelfUpdate || qualifiedCallbacks.length > 0) {
211
- const disposer = this.disposeMap.get(acceptedPath);
212
- if (disposer)
213
- await disposer(this.dataMap.get(acceptedPath));
214
- try {
215
- fetchedModule = await this.importUpdatedModule(update);
216
- }
217
- catch (e) {
218
- this.warnFailedUpdate(e, acceptedPath);
219
- }
220
- }
221
- return () => {
222
- for (const { deps, fn } of qualifiedCallbacks) {
223
- fn(deps.map((dep) => (dep === acceptedPath ? fetchedModule : undefined)));
224
- }
225
- const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`;
226
- this.logger.debug(`[vite] hot updated: ${loggedPath}`);
227
- };
228
- }
229
- }
230
-
231
- const isWindows = typeof process !== 'undefined' && process.platform === 'win32';
232
- const decodeBase64 = typeof atob !== 'undefined'
233
- ? atob
234
- : (str) => Buffer.from(str, 'base64').toString('utf-8');
235
- // currently we copy this from '../../constants' - maybe we can inline it somewhow?
236
- const NULL_BYTE_PLACEHOLDER = `__x00__`;
237
- const VALID_ID_PREFIX = `/@id/`;
1
+ const VALID_ID_PREFIX = "/@id/", NULL_BYTE_PLACEHOLDER = "__x00__";
2
+ let SOURCEMAPPING_URL = "sourceMa";
3
+ SOURCEMAPPING_URL += "ppingURL";
4
+ const VITE_RUNTIME_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`, isWindows = typeof process < "u" && process.platform === "win32";
238
5
  function wrapId(id) {
239
- return id.startsWith(VALID_ID_PREFIX)
240
- ? id
241
- : VALID_ID_PREFIX + id.replace('\0', NULL_BYTE_PLACEHOLDER);
6
+ return id.startsWith(VALID_ID_PREFIX) ? id : VALID_ID_PREFIX + id.replace("\0", NULL_BYTE_PLACEHOLDER);
242
7
  }
243
8
  function unwrapId(id) {
244
- return id.startsWith(VALID_ID_PREFIX)
245
- ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0')
246
- : id;
247
- }
248
- const windowsSlashRE = /\\/g;
249
- function slash(p) {
250
- return p.replace(windowsSlashRE, '/');
9
+ return id.startsWith(VALID_ID_PREFIX) ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, "\0") : id;
251
10
  }
252
11
  const postfixRE = /[?#].*$/;
253
12
  function cleanUrl(url) {
254
- return url.replace(postfixRE, '');
13
+ return url.replace(postfixRE, "");
255
14
  }
256
15
  function isPrimitive(value) {
257
- return !value || (typeof value !== 'object' && typeof value !== 'function');
16
+ return !value || typeof value != "object" && typeof value != "function";
258
17
  }
259
- const CHAR_FORWARD_SLASH = 47;
260
- const CHAR_BACKWARD_SLASH = 92;
261
- const percentRegEx = /%/g;
262
- const backslashRegEx = /\\/g;
263
- const newlineRegEx = /\n/g;
264
- const carriageReturnRegEx = /\r/g;
265
- const tabRegEx = /\t/g;
266
- const questionRegex = /\?/g;
267
- const hashRegex = /#/g;
268
- function encodePathChars(filepath) {
269
- if (filepath.indexOf('%') !== -1)
270
- filepath = filepath.replace(percentRegEx, '%25');
271
- // In posix, backslash is a valid character in paths:
272
- if (!isWindows && filepath.indexOf('\\') !== -1)
273
- filepath = filepath.replace(backslashRegEx, '%5C');
274
- if (filepath.indexOf('\n') !== -1)
275
- filepath = filepath.replace(newlineRegEx, '%0A');
276
- if (filepath.indexOf('\r') !== -1)
277
- filepath = filepath.replace(carriageReturnRegEx, '%0D');
278
- if (filepath.indexOf('\t') !== -1)
279
- filepath = filepath.replace(tabRegEx, '%09');
280
- return filepath;
281
- }
282
- function posixPathToFileHref(posixPath) {
283
- let resolved = posixResolve(posixPath);
284
- // path.resolve strips trailing slashes so we must add them back
285
- const filePathLast = posixPath.charCodeAt(posixPath.length - 1);
286
- if ((filePathLast === CHAR_FORWARD_SLASH ||
287
- (isWindows && filePathLast === CHAR_BACKWARD_SLASH)) &&
288
- resolved[resolved.length - 1] !== '/')
289
- resolved += '/';
290
- // Call encodePathChars first to avoid encoding % again for ? and #.
291
- resolved = encodePathChars(resolved);
292
- // Question and hash character should be included in pathname.
293
- // Therefore, encoding is required to eliminate parsing them in different states.
294
- // This is done as an optimization to not creating a URL instance and
295
- // later triggering pathname setter, which impacts performance
296
- if (resolved.indexOf('?') !== -1)
297
- resolved = resolved.replace(questionRegex, '%3F');
298
- if (resolved.indexOf('#') !== -1)
299
- resolved = resolved.replace(hashRegex, '%23');
300
- return new URL(`file://${resolved}`).href;
301
- }
302
- function posixDirname(filepath) {
303
- const normalizedPath = filepath.endsWith('/')
304
- ? filepath.substring(0, filepath.length - 1)
305
- : filepath;
306
- return normalizedPath.substring(0, normalizedPath.lastIndexOf('/')) || '/';
18
+ function withTrailingSlash(path) {
19
+ return path[path.length - 1] !== "/" ? `${path}/` : path;
307
20
  }
308
- function toWindowsPath(path) {
309
- return path.replace(/\//g, '\\');
21
+ const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
22
+ function normalizeWindowsPath(input = "") {
23
+ return input && input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
310
24
  }
311
- // inlined from pathe to support environments without access to node:path
25
+ const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/, _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
312
26
  function cwd() {
313
- if (typeof process !== 'undefined' && typeof process.cwd === 'function') {
314
- return slash(process.cwd());
315
- }
316
- return '/';
317
- }
318
- function posixResolve(...segments) {
319
- // Normalize windows arguments
320
- segments = segments.map((argument) => slash(argument));
321
- let resolvedPath = '';
322
- let resolvedAbsolute = false;
323
- for (let index = segments.length - 1; index >= -1 && !resolvedAbsolute; index--) {
324
- const path = index >= 0 ? segments[index] : cwd();
325
- // Skip empty entries
326
- if (!path || path.length === 0) {
327
- continue;
328
- }
329
- resolvedPath = `${path}/${resolvedPath}`;
330
- resolvedAbsolute = isAbsolute(path);
331
- }
332
- // At this point the path should be resolved to a full absolute path, but
333
- // handle relative paths to be safe (might happen when process.cwd() fails)
334
- // Normalize the path
335
- resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
336
- if (resolvedAbsolute && !isAbsolute(resolvedPath)) {
337
- return `/${resolvedPath}`;
338
- }
339
- return resolvedPath.length > 0 ? resolvedPath : '.';
340
- }
341
- const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
342
- function isAbsolute(p) {
343
- return _IS_ABSOLUTE_RE.test(p);
344
- }
345
- // Resolves . and .. elements in a path with directory names
27
+ return typeof process < "u" && typeof process.cwd == "function" ? process.cwd().replace(/\\/g, "/") : "/";
28
+ }
29
+ const resolve = function(...arguments_) {
30
+ arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument));
31
+ let resolvedPath = "", resolvedAbsolute = !1;
32
+ for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) {
33
+ const path = index >= 0 ? arguments_[index] : cwd();
34
+ !path || path.length === 0 || (resolvedPath = `${path}/${resolvedPath}`, resolvedAbsolute = isAbsolute(path));
35
+ }
36
+ return resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute), resolvedAbsolute && !isAbsolute(resolvedPath) ? `/${resolvedPath}` : resolvedPath.length > 0 ? resolvedPath : ".";
37
+ };
346
38
  function normalizeString(path, allowAboveRoot) {
347
- let res = '';
348
- let lastSegmentLength = 0;
349
- let lastSlash = -1;
350
- let dots = 0;
351
- let char = null;
352
- for (let index = 0; index <= path.length; ++index) {
353
- if (index < path.length) {
354
- char = path[index];
355
- }
356
- else if (char === '/') {
357
- break;
358
- }
359
- else {
360
- char = '/';
361
- }
362
- if (char === '/') {
363
- if (lastSlash === index - 1 || dots === 1) ;
364
- else if (dots === 2) {
365
- if (res.length < 2 ||
366
- lastSegmentLength !== 2 ||
367
- res[res.length - 1] !== '.' ||
368
- res[res.length - 2] !== '.') {
369
- if (res.length > 2) {
370
- const lastSlashIndex = res.lastIndexOf('/');
371
- if (lastSlashIndex === -1) {
372
- res = '';
373
- lastSegmentLength = 0;
374
- }
375
- else {
376
- res = res.slice(0, lastSlashIndex);
377
- lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
378
- }
379
- lastSlash = index;
380
- dots = 0;
381
- continue;
382
- }
383
- else if (res.length > 0) {
384
- res = '';
385
- lastSegmentLength = 0;
386
- lastSlash = index;
387
- dots = 0;
388
- continue;
389
- }
390
- }
391
- if (allowAboveRoot) {
392
- res += res.length > 0 ? '/..' : '..';
393
- lastSegmentLength = 2;
394
- }
395
- }
396
- else {
397
- if (res.length > 0) {
398
- res += `/${path.slice(lastSlash + 1, index)}`;
399
- }
400
- else {
401
- res = path.slice(lastSlash + 1, index);
402
- }
403
- lastSegmentLength = index - lastSlash - 1;
39
+ let res = "", lastSegmentLength = 0, lastSlash = -1, dots = 0, char = null;
40
+ for (let index = 0; index <= path.length; ++index) {
41
+ if (index < path.length)
42
+ char = path[index];
43
+ else {
44
+ if (char === "/")
45
+ break;
46
+ char = "/";
47
+ }
48
+ if (char === "/") {
49
+ if (!(lastSlash === index - 1 || dots === 1))
50
+ if (dots === 2) {
51
+ if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
52
+ if (res.length > 2) {
53
+ const lastSlashIndex = res.lastIndexOf("/");
54
+ lastSlashIndex === -1 ? (res = "", lastSegmentLength = 0) : (res = res.slice(0, lastSlashIndex), lastSegmentLength = res.length - 1 - res.lastIndexOf("/")), lastSlash = index, dots = 0;
55
+ continue;
56
+ } else if (res.length > 0) {
57
+ res = "", lastSegmentLength = 0, lastSlash = index, dots = 0;
58
+ continue;
404
59
  }
405
- lastSlash = index;
406
- dots = 0;
407
- }
408
- else if (char === '.' && dots !== -1) {
409
- ++dots;
410
- }
411
- else {
412
- dots = -1;
413
- }
414
- }
415
- return res;
60
+ }
61
+ allowAboveRoot && (res += res.length > 0 ? "/.." : "..", lastSegmentLength = 2);
62
+ } else
63
+ res.length > 0 ? res += `/${path.slice(lastSlash + 1, index)}` : res = path.slice(lastSlash + 1, index), lastSegmentLength = index - lastSlash - 1;
64
+ lastSlash = index, dots = 0;
65
+ } else
66
+ char === "." && dots !== -1 ? ++dots : dots = -1;
67
+ }
68
+ return res;
69
+ }
70
+ const isAbsolute = function(p) {
71
+ return _IS_ABSOLUTE_RE.test(p);
72
+ }, dirname = function(p) {
73
+ const segments = normalizeWindowsPath(p).replace(/\/$/, "").split("/").slice(0, -1);
74
+ return segments.length === 1 && _DRIVE_LETTER_RE.test(segments[0]) && (segments[0] += "/"), segments.join("/") || (isAbsolute(p) ? "/" : ".");
75
+ }, decodeBase64 = typeof atob < "u" ? atob : (str) => Buffer.from(str, "base64").toString("utf-8"), CHAR_FORWARD_SLASH = 47, CHAR_BACKWARD_SLASH = 92, percentRegEx = /%/g, backslashRegEx = /\\/g, newlineRegEx = /\n/g, carriageReturnRegEx = /\r/g, tabRegEx = /\t/g, questionRegex = /\?/g, hashRegex = /#/g;
76
+ function encodePathChars(filepath) {
77
+ return filepath.indexOf("%") !== -1 && (filepath = filepath.replace(percentRegEx, "%25")), !isWindows && filepath.indexOf("\\") !== -1 && (filepath = filepath.replace(backslashRegEx, "%5C")), filepath.indexOf(`
78
+ `) !== -1 && (filepath = filepath.replace(newlineRegEx, "%0A")), filepath.indexOf("\r") !== -1 && (filepath = filepath.replace(carriageReturnRegEx, "%0D")), filepath.indexOf(" ") !== -1 && (filepath = filepath.replace(tabRegEx, "%09")), filepath;
416
79
  }
417
-
418
- class DecodedMap {
419
- map;
420
- _encoded;
421
- _decoded;
422
- _decodedMemo;
423
- url;
424
- version;
425
- names = [];
426
- resolvedSources;
427
- constructor(map, from) {
428
- this.map = map;
429
- const { mappings, names, sources } = map;
430
- this.version = map.version;
431
- this.names = names || [];
432
- this._encoded = mappings || '';
433
- this._decodedMemo = memoizedState();
434
- this.url = from;
435
- this.resolvedSources = (sources || []).map((s) => posixResolve(s || '', from));
436
- }
80
+ const posixDirname = dirname, posixResolve = resolve;
81
+ function posixPathToFileHref(posixPath) {
82
+ let resolved = posixResolve(posixPath);
83
+ const filePathLast = posixPath.charCodeAt(posixPath.length - 1);
84
+ return (filePathLast === CHAR_FORWARD_SLASH || isWindows && filePathLast === CHAR_BACKWARD_SLASH) && resolved[resolved.length - 1] !== "/" && (resolved += "/"), resolved = encodePathChars(resolved), resolved.indexOf("?") !== -1 && (resolved = resolved.replace(questionRegex, "%3F")), resolved.indexOf("#") !== -1 && (resolved = resolved.replace(hashRegex, "%23")), new URL(`file://${resolved}`).href;
437
85
  }
438
- // This is a copy of all methods that we need for decoding a source map from "@jridgewell/trace-mapping"
439
- function indexOf(mappings, index) {
440
- const idx = mappings.indexOf(';', index);
441
- return idx === -1 ? mappings.length : idx;
86
+ function toWindowsPath(path) {
87
+ return path.replace(/\//g, "\\");
442
88
  }
443
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
444
- const charToInt = new Uint8Array(128); // z is 122 in ASCII
89
+ const comma = ",".charCodeAt(0), chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", intToChar = new Uint8Array(64), charToInt = new Uint8Array(128);
445
90
  for (let i = 0; i < chars.length; i++) {
446
- const c = chars.charCodeAt(i);
447
- charToInt[c] = i;
91
+ const c = chars.charCodeAt(i);
92
+ intToChar[i] = c, charToInt[c] = i;
93
+ }
94
+ function decode(mappings) {
95
+ const state = new Int32Array(5), decoded = [];
96
+ let index = 0;
97
+ do {
98
+ const semi = indexOf(mappings, index), line = [];
99
+ let sorted = !0, lastCol = 0;
100
+ state[0] = 0;
101
+ for (let i = index; i < semi; i++) {
102
+ let seg;
103
+ i = decodeInteger(mappings, i, state, 0);
104
+ const col = state[0];
105
+ col < lastCol && (sorted = !1), lastCol = col, hasMoreVlq(mappings, i, semi) ? (i = decodeInteger(mappings, i, state, 1), i = decodeInteger(mappings, i, state, 2), i = decodeInteger(mappings, i, state, 3), hasMoreVlq(mappings, i, semi) ? (i = decodeInteger(mappings, i, state, 4), seg = [col, state[1], state[2], state[3], state[4]]) : seg = [col, state[1], state[2], state[3]]) : seg = [col], line.push(seg);
106
+ }
107
+ sorted || sort(line), decoded.push(line), index = semi + 1;
108
+ } while (index <= mappings.length);
109
+ return decoded;
110
+ }
111
+ function indexOf(mappings, index) {
112
+ const idx = mappings.indexOf(";", index);
113
+ return idx === -1 ? mappings.length : idx;
448
114
  }
449
115
  function decodeInteger(mappings, pos, state, j) {
450
- let value = 0;
451
- let shift = 0;
452
- let integer = 0;
453
- do {
454
- const c = mappings.charCodeAt(pos++);
455
- integer = charToInt[c];
456
- value |= (integer & 31) << shift;
457
- shift += 5;
458
- } while (integer & 32);
459
- const shouldNegate = value & 1;
460
- value >>>= 1;
461
- if (shouldNegate) {
462
- value = -0x80000000 | -value;
463
- }
464
- state[j] += value;
465
- return pos;
116
+ let value = 0, shift = 0, integer = 0;
117
+ do {
118
+ const c = mappings.charCodeAt(pos++);
119
+ integer = charToInt[c], value |= (integer & 31) << shift, shift += 5;
120
+ } while (integer & 32);
121
+ const shouldNegate = value & 1;
122
+ return value >>>= 1, shouldNegate && (value = -2147483648 | -value), state[j] += value, pos;
466
123
  }
467
- const comma = ','.charCodeAt(0);
468
124
  function hasMoreVlq(mappings, i, length) {
469
- if (i >= length)
470
- return false;
471
- return mappings.charCodeAt(i) !== comma;
472
- }
473
- function decode(mappings) {
474
- const state = new Int32Array(5);
475
- const decoded = [];
476
- let index = 0;
477
- do {
478
- const semi = indexOf(mappings, index);
479
- const line = [];
480
- let sorted = true;
481
- let lastCol = 0;
482
- state[0] = 0;
483
- for (let i = index; i < semi; i++) {
484
- let seg;
485
- i = decodeInteger(mappings, i, state, 0); // genColumn
486
- const col = state[0];
487
- if (col < lastCol)
488
- sorted = false;
489
- lastCol = col;
490
- if (hasMoreVlq(mappings, i, semi)) {
491
- i = decodeInteger(mappings, i, state, 1); // sourcesIndex
492
- i = decodeInteger(mappings, i, state, 2); // sourceLine
493
- i = decodeInteger(mappings, i, state, 3); // sourceColumn
494
- if (hasMoreVlq(mappings, i, semi)) {
495
- i = decodeInteger(mappings, i, state, 4); // namesIndex
496
- seg = [col, state[1], state[2], state[3], state[4]];
497
- }
498
- else {
499
- seg = [col, state[1], state[2], state[3]];
500
- }
501
- }
502
- else {
503
- seg = [col];
504
- }
505
- line.push(seg);
506
- }
507
- if (!sorted)
508
- line.sort((a, b) => a[0] - b[0]);
509
- decoded.push(line);
510
- index = semi + 1;
511
- } while (index <= mappings.length);
512
- return decoded;
125
+ return i >= length ? !1 : mappings.charCodeAt(i) !== comma;
513
126
  }
514
- const LINE_GTR_ZERO = '`line` must be greater than 0 (lines start at line 1)';
515
- const COL_GTR_EQ_ZERO = '`column` must be greater than or equal to 0 (columns start at column 0)';
516
- const COLUMN = 0;
517
- const SOURCES_INDEX = 1;
518
- const SOURCE_LINE = 2;
519
- const SOURCE_COLUMN = 3;
520
- const NAMES_INDEX = 4;
521
- function OMapping(source, line, column, name) {
522
- return { source, line, column, name };
127
+ function sort(line) {
128
+ line.sort(sortComparator);
523
129
  }
524
- function decodedMappings(map) {
525
- return map._decoded || (map._decoded = decode(map._encoded));
130
+ function sortComparator(a, b) {
131
+ return a[0] - b[0];
526
132
  }
527
- let found = false;
133
+ const COLUMN = 0, SOURCES_INDEX = 1, SOURCE_LINE = 2, SOURCE_COLUMN = 3, NAMES_INDEX = 4;
134
+ let found = !1;
528
135
  function binarySearch(haystack, needle, low, high) {
529
- while (low <= high) {
530
- const mid = low + ((high - low) >> 1);
531
- const cmp = haystack[mid][COLUMN] - needle;
532
- if (cmp === 0) {
533
- found = true;
534
- return mid;
535
- }
536
- if (cmp < 0) {
537
- low = mid + 1;
538
- }
539
- else {
540
- high = mid - 1;
541
- }
542
- }
543
- found = false;
544
- return low - 1;
136
+ for (; low <= high; ) {
137
+ const mid = low + (high - low >> 1), cmp = haystack[mid][COLUMN] - needle;
138
+ if (cmp === 0)
139
+ return found = !0, mid;
140
+ cmp < 0 ? low = mid + 1 : high = mid - 1;
141
+ }
142
+ return found = !1, low - 1;
143
+ }
144
+ function upperBound(haystack, needle, index) {
145
+ for (let i = index + 1; i < haystack.length && haystack[i][COLUMN] === needle; index = i++)
146
+ ;
147
+ return index;
545
148
  }
546
- function lowerBound(haystack, needle, index) {
547
- for (let i = index - 1; i >= 0; index = i--) {
548
- if (haystack[i][COLUMN] !== needle)
549
- break;
550
- }
551
- return index;
149
+ function memoizedBinarySearch(haystack, needle, state, key) {
150
+ const { lastKey, lastNeedle, lastIndex } = state;
151
+ let low = 0, high = haystack.length - 1;
152
+ if (key === lastKey) {
153
+ if (needle === lastNeedle)
154
+ return found = lastIndex !== -1 && haystack[lastIndex][COLUMN] === needle, lastIndex;
155
+ needle >= lastNeedle ? low = lastIndex === -1 ? 0 : lastIndex : high = lastIndex;
156
+ }
157
+ return state.lastKey = key, state.lastNeedle = needle, state.lastIndex = binarySearch(haystack, needle, low, high);
158
+ }
159
+ const LINE_GTR_ZERO = "`line` must be greater than 0 (lines start at line 1)", COL_GTR_EQ_ZERO = "`column` must be greater than or equal to 0 (columns start at column 0)";
160
+ function cast(map) {
161
+ return map;
552
162
  }
553
- function memoizedState() {
554
- return {
555
- lastKey: -1,
556
- lastNeedle: -1,
557
- lastIndex: -1,
558
- };
163
+ function decodedMappings(map) {
164
+ var _a;
165
+ return (_a = map)._decoded || (_a._decoded = decode(map._encoded));
166
+ }
167
+ function originalPositionFor(map, needle) {
168
+ let { line, column, bias } = needle;
169
+ if (line--, line < 0)
170
+ throw new Error(LINE_GTR_ZERO);
171
+ if (column < 0)
172
+ throw new Error(COL_GTR_EQ_ZERO);
173
+ const decoded = decodedMappings(map);
174
+ if (line >= decoded.length)
175
+ return OMapping(null, null, null, null);
176
+ const segments = decoded[line], index = traceSegmentInternal(segments, map._decodedMemo, line, column);
177
+ if (index === -1)
178
+ return OMapping(null, null, null, null);
179
+ const segment = segments[index];
180
+ if (segment.length === 1)
181
+ return OMapping(null, null, null, null);
182
+ const { names, resolvedSources } = map;
183
+ return OMapping(resolvedSources[segment[SOURCES_INDEX]], segment[SOURCE_LINE] + 1, segment[SOURCE_COLUMN], segment.length === 5 ? names[segment[NAMES_INDEX]] : null);
559
184
  }
560
- function memoizedBinarySearch(haystack, needle, state, key) {
561
- const { lastKey, lastNeedle, lastIndex } = state;
562
- let low = 0;
563
- let high = haystack.length - 1;
564
- if (key === lastKey) {
565
- if (needle === lastNeedle) {
566
- found = lastIndex !== -1 && haystack[lastIndex][COLUMN] === needle;
567
- return lastIndex;
568
- }
569
- if (needle >= lastNeedle) {
570
- // lastIndex may be -1 if the previous needle was not found.
571
- low = lastIndex === -1 ? 0 : lastIndex;
572
- }
573
- else {
574
- high = lastIndex;
575
- }
576
- }
577
- state.lastKey = key;
578
- state.lastNeedle = needle;
579
- return (state.lastIndex = binarySearch(haystack, needle, low, high));
185
+ function OMapping(source, line, column, name) {
186
+ return { source, line, column, name };
580
187
  }
581
- function traceSegmentInternal(segments, memo, line, column) {
582
- let index = memoizedBinarySearch(segments, column, memo, line);
583
- if (found) {
584
- index = lowerBound(segments, column, index);
585
- }
586
- if (index === -1 || index === segments.length)
587
- return -1;
588
- return index;
188
+ function traceSegmentInternal(segments, memo, line, column, bias) {
189
+ let index = memoizedBinarySearch(segments, column, memo, line);
190
+ return found ? index = upperBound(segments, column, index) : index++, index === -1 || index === segments.length ? -1 : index;
589
191
  }
590
- function getOriginalPosition(map, { line, column }) {
591
- line--;
592
- if (line < 0)
593
- throw new Error(LINE_GTR_ZERO);
594
- if (column < 0)
595
- throw new Error(COL_GTR_EQ_ZERO);
596
- map._decodedMemo ??= memoizedState();
597
- const decoded = decodedMappings(map);
598
- // It's common for parent source maps to have pointers to lines that have no
599
- // mapping (like a "//# sourceMappingURL=") at the end of the child file.
600
- if (line >= decoded.length)
601
- return null;
602
- const segments = decoded[line];
603
- const index = traceSegmentInternal(segments, map._decodedMemo, line, column);
604
- if (index === -1)
605
- return null;
606
- const segment = segments[index];
607
- if (segment.length === 1)
608
- return null;
609
- const { names, resolvedSources } = map;
610
- return OMapping(resolvedSources[segment[SOURCES_INDEX]], segment[SOURCE_LINE] + 1, segment[SOURCE_COLUMN], segment.length === 5 ? names[segment[NAMES_INDEX]] : null);
192
+ class DecodedMap {
193
+ map;
194
+ _encoded;
195
+ _decoded;
196
+ _decodedMemo;
197
+ url;
198
+ version;
199
+ names = [];
200
+ resolvedSources;
201
+ constructor(map, from) {
202
+ this.map = map;
203
+ const { mappings, names, sources } = map;
204
+ this.version = map.version, this.names = names || [], this._encoded = mappings || "", this._decodedMemo = memoizedState(), this.url = from, this.resolvedSources = (sources || []).map((s) => posixResolve(s || "", from));
205
+ }
206
+ }
207
+ function memoizedState() {
208
+ return {
209
+ lastKey: -1,
210
+ lastNeedle: -1,
211
+ lastIndex: -1
212
+ };
213
+ }
214
+ function getOriginalPosition(map, needle) {
215
+ const result = originalPositionFor(map, needle);
216
+ return result.column == null ? null : result;
611
217
  }
612
-
613
- let SOURCEMAPPING_URL = 'sourceMa';
614
- SOURCEMAPPING_URL += 'ppingURL';
615
- const VITE_RUNTIME_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`;
616
218
  const VITE_RUNTIME_SOURCEMAPPING_REGEXP = new RegExp(`//# ${VITE_RUNTIME_SOURCEMAPPING_URL};base64,(.+)`);
617
219
  class ModuleCacheMap extends Map {
618
- root;
619
- constructor(root, entries) {
620
- super(entries);
621
- this.root = withTrailingSlash(root);
622
- }
623
- normalize(fsPath) {
624
- return normalizeModuleId(fsPath, this.root);
625
- }
626
- /**
627
- * Assign partial data to the map
628
- */
629
- update(fsPath, mod) {
630
- fsPath = this.normalize(fsPath);
631
- if (!super.has(fsPath))
632
- this.setByModuleId(fsPath, mod);
633
- else
634
- Object.assign(super.get(fsPath), mod);
635
- return this;
636
- }
637
- setByModuleId(modulePath, mod) {
638
- return super.set(modulePath, mod);
639
- }
640
- set(fsPath, mod) {
641
- return this.setByModuleId(this.normalize(fsPath), mod);
642
- }
643
- getByModuleId(modulePath) {
644
- if (!super.has(modulePath))
645
- this.setByModuleId(modulePath, {});
646
- const mod = super.get(modulePath);
647
- if (!mod.imports) {
648
- Object.assign(mod, {
649
- imports: new Set(),
650
- importers: new Set(),
651
- });
652
- }
653
- return mod;
654
- }
655
- get(fsPath) {
656
- return this.getByModuleId(this.normalize(fsPath));
657
- }
658
- deleteByModuleId(modulePath) {
659
- return super.delete(modulePath);
660
- }
661
- delete(fsPath) {
662
- return this.deleteByModuleId(this.normalize(fsPath));
663
- }
664
- /**
665
- * Invalidate modules that dependent on the given modules, up to the main entry
666
- */
667
- invalidateDepTree(ids, invalidated = new Set()) {
668
- for (const _id of ids) {
669
- const id = this.normalize(_id);
670
- if (invalidated.has(id))
671
- continue;
672
- invalidated.add(id);
673
- const mod = super.get(id);
674
- if (mod?.importers)
675
- this.invalidateDepTree(mod.importers, invalidated);
676
- super.delete(id);
677
- }
678
- return invalidated;
679
- }
680
- /**
681
- * Invalidate dependency modules of the given modules, down to the bottom-level dependencies
682
- */
683
- invalidateSubDepTree(ids, invalidated = new Set()) {
684
- for (const _id of ids) {
685
- const id = this.normalize(_id);
686
- if (invalidated.has(id))
687
- continue;
688
- invalidated.add(id);
689
- const subIds = Array.from(super.entries())
690
- .filter(([, mod]) => mod.importers?.has(id))
691
- .map(([key]) => key);
692
- subIds.length && this.invalidateSubDepTree(subIds, invalidated);
693
- super.delete(id);
694
- }
695
- return invalidated;
696
- }
697
- getSourceMap(moduleId) {
698
- const mod = this.get(moduleId);
699
- if (mod.map)
700
- return mod.map;
701
- if (!mod.meta || !('code' in mod.meta))
702
- return null;
703
- const mapString = mod.meta.code.match(VITE_RUNTIME_SOURCEMAPPING_REGEXP)?.[1];
704
- if (!mapString)
705
- return null;
706
- const baseFile = mod.meta.file || moduleId.split('?')[0];
707
- mod.map = new DecodedMap(JSON.parse(decodeBase64(mapString)), baseFile);
708
- return mod.map;
709
- }
220
+ root;
221
+ constructor(root, entries) {
222
+ super(entries), this.root = withTrailingSlash(root);
223
+ }
224
+ normalize(fsPath) {
225
+ return normalizeModuleId(fsPath, this.root);
226
+ }
227
+ /**
228
+ * Assign partial data to the map
229
+ */
230
+ update(fsPath, mod) {
231
+ return fsPath = this.normalize(fsPath), super.has(fsPath) ? Object.assign(super.get(fsPath), mod) : this.setByModuleId(fsPath, mod), this;
232
+ }
233
+ setByModuleId(modulePath, mod) {
234
+ return super.set(modulePath, mod);
235
+ }
236
+ set(fsPath, mod) {
237
+ return this.setByModuleId(this.normalize(fsPath), mod);
238
+ }
239
+ getByModuleId(modulePath) {
240
+ super.has(modulePath) || this.setByModuleId(modulePath, {});
241
+ const mod = super.get(modulePath);
242
+ return mod.imports || Object.assign(mod, {
243
+ imports: /* @__PURE__ */ new Set(),
244
+ importers: /* @__PURE__ */ new Set()
245
+ }), mod;
246
+ }
247
+ get(fsPath) {
248
+ return this.getByModuleId(this.normalize(fsPath));
249
+ }
250
+ deleteByModuleId(modulePath) {
251
+ return super.delete(modulePath);
252
+ }
253
+ delete(fsPath) {
254
+ return this.deleteByModuleId(this.normalize(fsPath));
255
+ }
256
+ invalidate(id) {
257
+ const module = this.get(id);
258
+ module.evaluated = !1, module.meta = void 0, module.map = void 0, module.promise = void 0, module.exports = void 0, module.imports?.clear();
259
+ }
260
+ isImported({ importedId, importedBy }, seen = /* @__PURE__ */ new Set()) {
261
+ if (importedId = this.normalize(importedId), importedBy = this.normalize(importedBy), importedBy === importedId)
262
+ return !0;
263
+ if (seen.has(importedId))
264
+ return !1;
265
+ seen.add(importedId);
266
+ const importers = this.getByModuleId(importedId)?.importers;
267
+ if (!importers)
268
+ return !1;
269
+ if (importers.has(importedBy))
270
+ return !0;
271
+ for (const importer of importers)
272
+ if (this.isImported({
273
+ importedBy,
274
+ importedId: importer
275
+ }))
276
+ return !0;
277
+ return !1;
278
+ }
279
+ /**
280
+ * Invalidate modules that dependent on the given modules, up to the main entry
281
+ */
282
+ invalidateDepTree(ids, invalidated = /* @__PURE__ */ new Set()) {
283
+ for (const _id of ids) {
284
+ const id = this.normalize(_id);
285
+ if (invalidated.has(id))
286
+ continue;
287
+ invalidated.add(id);
288
+ const mod = super.get(id);
289
+ mod?.importers && this.invalidateDepTree(mod.importers, invalidated), super.delete(id);
290
+ }
291
+ return invalidated;
292
+ }
293
+ /**
294
+ * Invalidate dependency modules of the given modules, down to the bottom-level dependencies
295
+ */
296
+ invalidateSubDepTree(ids, invalidated = /* @__PURE__ */ new Set()) {
297
+ for (const _id of ids) {
298
+ const id = this.normalize(_id);
299
+ if (invalidated.has(id))
300
+ continue;
301
+ invalidated.add(id);
302
+ const subIds = Array.from(super.entries()).filter(([, mod]) => mod.importers?.has(id)).map(([key]) => key);
303
+ subIds.length && this.invalidateSubDepTree(subIds, invalidated), super.delete(id);
304
+ }
305
+ return invalidated;
306
+ }
307
+ getSourceMap(moduleId) {
308
+ const mod = this.get(moduleId);
309
+ if (mod.map)
310
+ return mod.map;
311
+ if (!mod.meta || !("code" in mod.meta))
312
+ return null;
313
+ const mapString = mod.meta.code.match(VITE_RUNTIME_SOURCEMAPPING_REGEXP)?.[1];
314
+ if (!mapString)
315
+ return null;
316
+ const baseFile = mod.meta.file || moduleId.split("?")[0];
317
+ return mod.map = new DecodedMap(JSON.parse(decodeBase64(mapString)), baseFile), mod.map;
318
+ }
319
+ }
320
+ const prefixedBuiltins = /* @__PURE__ */ new Set(["node:test"]);
321
+ function normalizeModuleId(file, root) {
322
+ if (prefixedBuiltins.has(file))
323
+ return file;
324
+ let unixFile = file.replace(/\\/g, "/").replace(/^\/@fs\//, isWindows ? "" : "/").replace(/^node:/, "").replace(/^\/+/, "/");
325
+ return unixFile.startsWith(root) && (unixFile = unixFile.slice(root.length - 1)), unixFile.replace(/^file:\//, "/");
710
326
  }
711
- function withTrailingSlash(path) {
712
- if (path[path.length - 1] !== '/') {
713
- return `${path}/`;
714
- }
715
- return path;
327
+ class HMRContext {
328
+ hmrClient;
329
+ ownerPath;
330
+ newListeners;
331
+ constructor(hmrClient, ownerPath) {
332
+ this.hmrClient = hmrClient, this.ownerPath = ownerPath, hmrClient.dataMap.has(ownerPath) || hmrClient.dataMap.set(ownerPath, {});
333
+ const mod = hmrClient.hotModulesMap.get(ownerPath);
334
+ mod && (mod.callbacks = []);
335
+ const staleListeners = hmrClient.ctxToListenersMap.get(ownerPath);
336
+ if (staleListeners)
337
+ for (const [event, staleFns] of staleListeners) {
338
+ const listeners = hmrClient.customListenersMap.get(event);
339
+ listeners && hmrClient.customListenersMap.set(event, listeners.filter((l) => !staleFns.includes(l)));
340
+ }
341
+ this.newListeners = /* @__PURE__ */ new Map(), hmrClient.ctxToListenersMap.set(ownerPath, this.newListeners);
342
+ }
343
+ get data() {
344
+ return this.hmrClient.dataMap.get(this.ownerPath);
345
+ }
346
+ accept(deps, callback) {
347
+ if (typeof deps == "function" || !deps)
348
+ this.acceptDeps([this.ownerPath], ([mod]) => deps?.(mod));
349
+ else if (typeof deps == "string")
350
+ this.acceptDeps([deps], ([mod]) => callback?.(mod));
351
+ else if (Array.isArray(deps))
352
+ this.acceptDeps(deps, callback);
353
+ else
354
+ throw new Error("invalid hot.accept() usage.");
355
+ }
356
+ // export names (first arg) are irrelevant on the client side, they're
357
+ // extracted in the server for propagation
358
+ acceptExports(_, callback) {
359
+ this.acceptDeps([this.ownerPath], ([mod]) => callback?.(mod));
360
+ }
361
+ dispose(cb) {
362
+ this.hmrClient.disposeMap.set(this.ownerPath, cb);
363
+ }
364
+ prune(cb) {
365
+ this.hmrClient.pruneMap.set(this.ownerPath, cb);
366
+ }
367
+ // Kept for backward compatibility (#11036)
368
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
369
+ decline() {
370
+ }
371
+ invalidate(message) {
372
+ this.hmrClient.notifyListeners("vite:invalidate", {
373
+ path: this.ownerPath,
374
+ message
375
+ }), this.send("vite:invalidate", { path: this.ownerPath, message }), this.hmrClient.logger.debug(`[vite] invalidate ${this.ownerPath}${message ? `: ${message}` : ""}`);
376
+ }
377
+ on(event, cb) {
378
+ const addToMap = (map) => {
379
+ const existing = map.get(event) || [];
380
+ existing.push(cb), map.set(event, existing);
381
+ };
382
+ addToMap(this.hmrClient.customListenersMap), addToMap(this.newListeners);
383
+ }
384
+ off(event, cb) {
385
+ const removeFromMap = (map) => {
386
+ const existing = map.get(event);
387
+ if (existing === void 0)
388
+ return;
389
+ const pruned = existing.filter((l) => l !== cb);
390
+ if (pruned.length === 0) {
391
+ map.delete(event);
392
+ return;
393
+ }
394
+ map.set(event, pruned);
395
+ };
396
+ removeFromMap(this.hmrClient.customListenersMap), removeFromMap(this.newListeners);
397
+ }
398
+ send(event, data) {
399
+ this.hmrClient.messenger.send(JSON.stringify({ type: "custom", event, data }));
400
+ }
401
+ acceptDeps(deps, callback = () => {
402
+ }) {
403
+ const mod = this.hmrClient.hotModulesMap.get(this.ownerPath) || {
404
+ id: this.ownerPath,
405
+ callbacks: []
406
+ };
407
+ mod.callbacks.push({
408
+ deps,
409
+ fn: callback
410
+ }), this.hmrClient.hotModulesMap.set(this.ownerPath, mod);
411
+ }
716
412
  }
717
- // unique id that is not available as "$bare_import" like "test"
718
- const prefixedBuiltins = new Set(['node:test']);
719
- // transform file url to id
720
- // virtual:custom -> virtual:custom
721
- // \0custom -> \0custom
722
- // /root/id -> /id
723
- // /root/id.js -> /id.js
724
- // C:/root/id.js -> /id.js
725
- // C:\root\id.js -> /id.js
726
- function normalizeModuleId(file, root) {
727
- if (prefixedBuiltins.has(file))
728
- return file;
729
- // unix style, but Windows path still starts with the drive letter to check the root
730
- let unixFile = file
731
- .replace(/\\/g, '/')
732
- .replace(/^\/@fs\//, isWindows ? '' : '/')
733
- .replace(/^node:/, '')
734
- .replace(/^\/+/, '/');
735
- if (unixFile.startsWith(root)) {
736
- // keep slash
737
- unixFile = unixFile.slice(root.length - 1);
738
- }
739
- // if it's not in the root, keep it as a path, not a URL
740
- return unixFile.replace(/^file:\//, '/');
413
+ class HMRMessenger {
414
+ connection;
415
+ constructor(connection) {
416
+ this.connection = connection;
417
+ }
418
+ queue = [];
419
+ send(message) {
420
+ this.queue.push(message), this.flush();
421
+ }
422
+ flush() {
423
+ this.connection.isReady() && (this.queue.forEach((msg) => this.connection.send(msg)), this.queue = []);
424
+ }
741
425
  }
742
-
743
- // they are exported from ssrTransform plugin, but we can't import from there for performance reasons
744
- const ssrModuleExportsKey = `__vite_ssr_exports__`;
745
- const ssrImportKey = `__vite_ssr_import__`;
746
- const ssrDynamicImportKey = `__vite_ssr_dynamic_import__`;
747
- const ssrExportAllKey = `__vite_ssr_exportAll__`;
748
- const ssrImportMetaKey = `__vite_ssr_import_meta__`;
749
-
750
- const noop = () => { };
751
- const silentConsole = {
752
- debug: noop,
753
- error: noop,
426
+ class HMRClient {
427
+ logger;
428
+ importUpdatedModule;
429
+ hotModulesMap = /* @__PURE__ */ new Map();
430
+ disposeMap = /* @__PURE__ */ new Map();
431
+ pruneMap = /* @__PURE__ */ new Map();
432
+ dataMap = /* @__PURE__ */ new Map();
433
+ customListenersMap = /* @__PURE__ */ new Map();
434
+ ctxToListenersMap = /* @__PURE__ */ new Map();
435
+ messenger;
436
+ constructor(logger, connection, importUpdatedModule) {
437
+ this.logger = logger, this.importUpdatedModule = importUpdatedModule, this.messenger = new HMRMessenger(connection);
438
+ }
439
+ async notifyListeners(event, data) {
440
+ const cbs = this.customListenersMap.get(event);
441
+ cbs && await Promise.allSettled(cbs.map((cb) => cb(data)));
442
+ }
443
+ clear() {
444
+ this.hotModulesMap.clear(), this.disposeMap.clear(), this.pruneMap.clear(), this.dataMap.clear(), this.customListenersMap.clear(), this.ctxToListenersMap.clear();
445
+ }
446
+ // After an HMR update, some modules are no longer imported on the page
447
+ // but they may have left behind side effects that need to be cleaned up
448
+ // (.e.g style injections)
449
+ // TODO Trigger their dispose callbacks.
450
+ prunePaths(paths) {
451
+ paths.forEach((path) => {
452
+ const fn = this.pruneMap.get(path);
453
+ fn && fn(this.dataMap.get(path));
454
+ });
455
+ }
456
+ warnFailedUpdate(err, path) {
457
+ err.message.includes("fetch") || this.logger.error(err), this.logger.error(`[hmr] Failed to reload ${path}. This could be due to syntax errors or importing non-existent modules. (see errors above)`);
458
+ }
459
+ updateQueue = [];
460
+ pendingUpdateQueue = !1;
461
+ /**
462
+ * buffer multiple hot updates triggered by the same src change
463
+ * so that they are invoked in the same order they were sent.
464
+ * (otherwise the order may be inconsistent because of the http request round trip)
465
+ */
466
+ async queueUpdate(payload) {
467
+ if (this.updateQueue.push(this.fetchUpdate(payload)), !this.pendingUpdateQueue) {
468
+ this.pendingUpdateQueue = !0, await Promise.resolve(), this.pendingUpdateQueue = !1;
469
+ const loading = [...this.updateQueue];
470
+ this.updateQueue = [], (await Promise.all(loading)).forEach((fn) => fn && fn());
471
+ }
472
+ }
473
+ async fetchUpdate(update) {
474
+ const { path, acceptedPath } = update, mod = this.hotModulesMap.get(path);
475
+ if (!mod)
476
+ return;
477
+ let fetchedModule;
478
+ const isSelfUpdate = path === acceptedPath, qualifiedCallbacks = mod.callbacks.filter(({ deps }) => deps.includes(acceptedPath));
479
+ if (isSelfUpdate || qualifiedCallbacks.length > 0) {
480
+ const disposer = this.disposeMap.get(acceptedPath);
481
+ disposer && await disposer(this.dataMap.get(acceptedPath));
482
+ try {
483
+ fetchedModule = await this.importUpdatedModule(update);
484
+ } catch (e) {
485
+ this.warnFailedUpdate(e, acceptedPath);
486
+ }
487
+ }
488
+ return () => {
489
+ for (const { deps, fn } of qualifiedCallbacks)
490
+ fn(deps.map((dep) => dep === acceptedPath ? fetchedModule : void 0));
491
+ const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`;
492
+ this.logger.debug(`[vite] hot updated: ${loggedPath}`);
493
+ };
494
+ }
495
+ }
496
+ const ssrModuleExportsKey = "__vite_ssr_exports__", ssrImportKey = "__vite_ssr_import__", ssrDynamicImportKey = "__vite_ssr_dynamic_import__", ssrExportAllKey = "__vite_ssr_exportAll__", ssrImportMetaKey = "__vite_ssr_import_meta__", noop = () => {
497
+ }, silentConsole = {
498
+ debug: noop,
499
+ error: noop
754
500
  };
755
-
756
- // updates to HMR should go one after another. It is possible to trigger another update during the invalidation for example.
757
501
  function createHMRHandler(runtime) {
758
- const queue = new Queue();
759
- return (payload) => queue.enqueue(() => handleHMRPayload(runtime, payload));
502
+ const queue = new Queue();
503
+ return (payload) => queue.enqueue(() => handleHMRPayload(runtime, payload));
760
504
  }
761
505
  async function handleHMRPayload(runtime, payload) {
762
- const hmrClient = runtime.hmrClient;
763
- if (!hmrClient || runtime.isDestroyed())
764
- return;
506
+ const hmrClient = runtime.hmrClient;
507
+ if (!(!hmrClient || runtime.isDestroyed()))
765
508
  switch (payload.type) {
766
- case 'connected':
767
- hmrClient.logger.debug(`[vite] connected.`);
768
- hmrClient.messenger.flush();
769
- break;
770
- case 'update':
771
- await hmrClient.notifyListeners('vite:beforeUpdate', payload);
772
- await Promise.all(payload.updates.map(async (update) => {
773
- if (update.type === 'js-update') {
774
- // runtime always caches modules by their full path without /@id/ prefix
775
- update.acceptedPath = unwrapId(update.acceptedPath);
776
- update.path = unwrapId(update.path);
777
- return hmrClient.queueUpdate(update);
778
- }
779
- hmrClient.logger.error('[vite] css hmr is not supported in runtime mode.');
780
- }));
781
- await hmrClient.notifyListeners('vite:afterUpdate', payload);
782
- break;
783
- case 'custom': {
784
- await hmrClient.notifyListeners(payload.event, payload.data);
785
- break;
786
- }
787
- case 'full-reload':
788
- hmrClient.logger.debug(`[vite] program reload`);
789
- await hmrClient.notifyListeners('vite:beforeFullReload', payload);
790
- Array.from(runtime.moduleCache.keys()).forEach((id) => {
791
- if (!id.includes('node_modules')) {
792
- runtime.moduleCache.deleteByModuleId(id);
793
- }
794
- });
795
- for (const id of runtime.entrypoints) {
796
- await runtime.executeUrl(id);
797
- }
798
- break;
799
- case 'prune':
800
- await hmrClient.notifyListeners('vite:beforePrune', payload);
801
- hmrClient.prunePaths(payload.paths);
802
- break;
803
- case 'error': {
804
- await hmrClient.notifyListeners('vite:error', payload);
805
- const err = payload.err;
806
- hmrClient.logger.error(`[vite] Internal Server Error\n${err.message}\n${err.stack}`);
807
- break;
808
- }
809
- default: {
810
- const check = payload;
811
- return check;
812
- }
509
+ case "connected":
510
+ hmrClient.logger.debug("[vite] connected."), hmrClient.messenger.flush();
511
+ break;
512
+ case "update":
513
+ await hmrClient.notifyListeners("vite:beforeUpdate", payload), await Promise.all(payload.updates.map(async (update) => {
514
+ if (update.type === "js-update")
515
+ return update.acceptedPath = unwrapId(update.acceptedPath), update.path = unwrapId(update.path), hmrClient.queueUpdate(update);
516
+ hmrClient.logger.error("[vite] css hmr is not supported in runtime mode.");
517
+ })), await hmrClient.notifyListeners("vite:afterUpdate", payload);
518
+ break;
519
+ case "custom": {
520
+ await hmrClient.notifyListeners(payload.event, payload.data);
521
+ break;
522
+ }
523
+ case "full-reload": {
524
+ const { triggeredBy } = payload, clearEntrypoints = triggeredBy ? [...runtime.entrypoints].filter((entrypoint) => runtime.moduleCache.isImported({
525
+ importedId: triggeredBy,
526
+ importedBy: entrypoint
527
+ })) : [...runtime.entrypoints];
528
+ if (!clearEntrypoints.length)
529
+ break;
530
+ hmrClient.logger.debug("[vite] program reload"), await hmrClient.notifyListeners("vite:beforeFullReload", payload), runtime.moduleCache.clear();
531
+ for (const id of clearEntrypoints)
532
+ await runtime.executeUrl(id);
533
+ break;
534
+ }
535
+ case "prune":
536
+ await hmrClient.notifyListeners("vite:beforePrune", payload), hmrClient.prunePaths(payload.paths);
537
+ break;
538
+ case "error": {
539
+ await hmrClient.notifyListeners("vite:error", payload);
540
+ const err = payload.err;
541
+ hmrClient.logger.error(`[vite] Internal Server Error
542
+ ${err.message}
543
+ ${err.stack}`);
544
+ break;
545
+ }
546
+ default:
547
+ return payload;
813
548
  }
814
549
  }
815
550
  class Queue {
816
- queue = [];
817
- pending = false;
818
- enqueue(promise) {
819
- return new Promise((resolve, reject) => {
820
- this.queue.push({
821
- promise,
822
- resolve,
823
- reject,
824
- });
825
- this.dequeue();
826
- });
827
- }
828
- dequeue() {
829
- if (this.pending) {
830
- return false;
831
- }
832
- const item = this.queue.shift();
833
- if (!item) {
834
- return false;
835
- }
836
- this.pending = true;
837
- item
838
- .promise()
839
- .then(item.resolve)
840
- .catch(item.reject)
841
- .finally(() => {
842
- this.pending = false;
843
- this.dequeue();
844
- });
845
- return true;
846
- }
847
- }
848
-
849
- const sourceMapCache = {};
850
- const fileContentsCache = {};
851
- const moduleGraphs = new Set();
852
- const retrieveFileHandlers = new Set();
853
- const retrieveSourceMapHandlers = new Set();
854
- const createExecHandlers = (handlers) => {
855
- return ((...args) => {
856
- for (const handler of handlers) {
857
- const result = handler(...args);
858
- if (result)
859
- return result;
860
- }
861
- return null;
551
+ queue = [];
552
+ pending = !1;
553
+ enqueue(promise) {
554
+ return new Promise((resolve2, reject) => {
555
+ this.queue.push({
556
+ promise,
557
+ resolve: resolve2,
558
+ reject
559
+ }), this.dequeue();
862
560
  });
863
- };
864
- const retrieveFileFromHandlers = createExecHandlers(retrieveFileHandlers);
865
- const retrievSourceMapFromHandlers = createExecHandlers(retrieveSourceMapHandlers);
866
- let overriden = false;
561
+ }
562
+ dequeue() {
563
+ if (this.pending)
564
+ return !1;
565
+ const item = this.queue.shift();
566
+ return item ? (this.pending = !0, item.promise().then(item.resolve).catch(item.reject).finally(() => {
567
+ this.pending = !1, this.dequeue();
568
+ }), !0) : !1;
569
+ }
570
+ }
571
+ const sourceMapCache = {}, fileContentsCache = {}, moduleGraphs = /* @__PURE__ */ new Set(), retrieveFileHandlers = /* @__PURE__ */ new Set(), retrieveSourceMapHandlers = /* @__PURE__ */ new Set(), createExecHandlers = (handlers) => (...args) => {
572
+ for (const handler of handlers) {
573
+ const result = handler(...args);
574
+ if (result)
575
+ return result;
576
+ }
577
+ return null;
578
+ }, retrieveFileFromHandlers = createExecHandlers(retrieveFileHandlers), retrievSourceMapFromHandlers = createExecHandlers(retrieveSourceMapHandlers);
579
+ let overriden = !1;
867
580
  const originalPrepare = Error.prepareStackTrace;
868
581
  function resetInterceptor(runtime, options) {
869
- moduleGraphs.delete(runtime.moduleCache);
870
- if (options.retrieveFile)
871
- retrieveFileHandlers.delete(options.retrieveFile);
872
- if (options.retrieveSourceMap)
873
- retrieveSourceMapHandlers.delete(options.retrieveSourceMap);
874
- if (moduleGraphs.size === 0) {
875
- Error.prepareStackTrace = originalPrepare;
876
- overriden = false;
877
- }
582
+ moduleGraphs.delete(runtime.moduleCache), options.retrieveFile && retrieveFileHandlers.delete(options.retrieveFile), options.retrieveSourceMap && retrieveSourceMapHandlers.delete(options.retrieveSourceMap), moduleGraphs.size === 0 && (Error.prepareStackTrace = originalPrepare, overriden = !1);
878
583
  }
879
584
  function interceptStackTrace(runtime, options = {}) {
880
- if (!overriden) {
881
- Error.prepareStackTrace = prepareStackTrace;
882
- overriden = true;
883
- }
884
- moduleGraphs.add(runtime.moduleCache);
885
- if (options.retrieveFile)
886
- retrieveFileHandlers.add(options.retrieveFile);
887
- if (options.retrieveSourceMap)
888
- retrieveSourceMapHandlers.add(options.retrieveSourceMap);
889
- return () => resetInterceptor(runtime, options);
585
+ return overriden || (Error.prepareStackTrace = prepareStackTrace, overriden = !0), moduleGraphs.add(runtime.moduleCache), options.retrieveFile && retrieveFileHandlers.add(options.retrieveFile), options.retrieveSourceMap && retrieveSourceMapHandlers.add(options.retrieveSourceMap), () => resetInterceptor(runtime, options);
890
586
  }
891
- // Support URLs relative to a directory, but be careful about a protocol prefix
892
587
  function supportRelativeURL(file, url) {
893
- if (!file)
894
- return url;
895
- const dir = posixDirname(file.replace(/\\/g, '/'));
896
- const match = /^\w+:\/\/[^/]*/.exec(dir);
897
- let protocol = match ? match[0] : '';
898
- const startPath = dir.slice(protocol.length);
899
- if (protocol && /^\/\w:/.test(startPath)) {
900
- // handle file:///C:/ paths
901
- protocol += '/';
902
- return (protocol +
903
- posixResolve(dir.slice(protocol.length), url).replace(/\\/g, '/'));
904
- }
905
- return protocol + posixResolve(dir.slice(protocol.length), url);
588
+ if (!file)
589
+ return url;
590
+ const dir = posixDirname(file.replace(/\\/g, "/")), match = /^\w+:\/\/[^/]*/.exec(dir);
591
+ let protocol = match ? match[0] : "";
592
+ const startPath = dir.slice(protocol.length);
593
+ return protocol && /^\/\w:/.test(startPath) ? (protocol += "/", protocol + posixResolve(dir.slice(protocol.length), url).replace(/\\/g, "/")) : protocol + posixResolve(dir.slice(protocol.length), url);
906
594
  }
907
595
  function getRuntimeSourceMap(position) {
908
- for (const moduleCache of moduleGraphs) {
909
- const sourceMap = moduleCache.getSourceMap(position.source);
910
- if (sourceMap) {
911
- return {
912
- url: position.source,
913
- map: sourceMap,
914
- vite: true,
915
- };
916
- }
917
- }
918
- return null;
596
+ for (const moduleCache of moduleGraphs) {
597
+ const sourceMap = moduleCache.getSourceMap(position.source);
598
+ if (sourceMap)
599
+ return {
600
+ url: position.source,
601
+ map: sourceMap,
602
+ vite: !0
603
+ };
604
+ }
605
+ return null;
919
606
  }
920
607
  function retrieveFile(path) {
921
- if (path in fileContentsCache)
922
- return fileContentsCache[path];
923
- const content = retrieveFileFromHandlers(path);
924
- if (typeof content === 'string') {
925
- fileContentsCache[path] = content;
926
- return content;
927
- }
928
- return null;
608
+ if (path in fileContentsCache)
609
+ return fileContentsCache[path];
610
+ const content = retrieveFileFromHandlers(path);
611
+ return typeof content == "string" ? (fileContentsCache[path] = content, content) : null;
929
612
  }
930
613
  function retrieveSourceMapURL(source) {
931
- // Get the URL of the source map
932
- const fileData = retrieveFile(source);
933
- if (!fileData)
934
- return null;
935
- const re = /\/\/[@#]\s*sourceMappingURL=([^\s'"]+)\s*$|\/\*[@#]\s*sourceMappingURL=[^\s*'"]+\s*\*\/\s*$/gm;
936
- // Keep executing the search to find the *last* sourceMappingURL to avoid
937
- // picking up sourceMappingURLs from comments, strings, etc.
938
- let lastMatch, match;
939
- while ((match = re.exec(fileData)))
940
- lastMatch = match;
941
- if (!lastMatch)
942
- return null;
943
- return lastMatch[1];
614
+ const fileData = retrieveFile(source);
615
+ if (!fileData)
616
+ return null;
617
+ const re = /\/\/[@#]\s*sourceMappingURL=([^\s'"]+)\s*$|\/\*[@#]\s*sourceMappingURL=[^\s*'"]+\s*\*\/\s*$/gm;
618
+ let lastMatch, match;
619
+ for (; match = re.exec(fileData); )
620
+ lastMatch = match;
621
+ return lastMatch ? lastMatch[1] : null;
944
622
  }
945
623
  const reSourceMap = /^data:application\/json[^,]+base64,/;
946
624
  function retrieveSourceMap(source) {
947
- const urlAndMap = retrievSourceMapFromHandlers(source);
948
- if (urlAndMap)
949
- return urlAndMap;
950
- let sourceMappingURL = retrieveSourceMapURL(source);
951
- if (!sourceMappingURL)
952
- return null;
953
- // Read the contents of the source map
954
- let sourceMapData;
955
- if (reSourceMap.test(sourceMappingURL)) {
956
- // Support source map URL as a data url
957
- const rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1);
958
- sourceMapData = Buffer.from(rawData, 'base64').toString();
959
- sourceMappingURL = source;
960
- }
961
- else {
962
- // Support source map URLs relative to the source URL
963
- sourceMappingURL = supportRelativeURL(source, sourceMappingURL);
964
- sourceMapData = retrieveFile(sourceMappingURL);
965
- }
966
- if (!sourceMapData)
967
- return null;
968
- return {
969
- url: sourceMappingURL,
970
- map: sourceMapData,
971
- };
625
+ const urlAndMap = retrievSourceMapFromHandlers(source);
626
+ if (urlAndMap)
627
+ return urlAndMap;
628
+ let sourceMappingURL = retrieveSourceMapURL(source);
629
+ if (!sourceMappingURL)
630
+ return null;
631
+ let sourceMapData;
632
+ if (reSourceMap.test(sourceMappingURL)) {
633
+ const rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(",") + 1);
634
+ sourceMapData = Buffer.from(rawData, "base64").toString(), sourceMappingURL = source;
635
+ } else
636
+ sourceMappingURL = supportRelativeURL(source, sourceMappingURL), sourceMapData = retrieveFile(sourceMappingURL);
637
+ return sourceMapData ? {
638
+ url: sourceMappingURL,
639
+ map: sourceMapData
640
+ } : null;
972
641
  }
973
642
  function mapSourcePosition(position) {
974
- if (!position.source)
975
- return position;
976
- let sourceMap = getRuntimeSourceMap(position);
977
- if (!sourceMap)
978
- sourceMap = sourceMapCache[position.source];
979
- if (!sourceMap) {
980
- // Call the (overrideable) retrieveSourceMap function to get the source map.
981
- const urlAndMap = retrieveSourceMap(position.source);
982
- if (urlAndMap && urlAndMap.map) {
983
- const url = urlAndMap.url;
984
- sourceMap = sourceMapCache[position.source] = {
985
- url,
986
- map: new DecodedMap(typeof urlAndMap.map === 'string'
987
- ? JSON.parse(urlAndMap.map)
988
- : urlAndMap.map, url),
989
- };
990
- const contents = sourceMap.map?.map.sourcesContent;
991
- // Load all sources stored inline with the source map into the file cache
992
- // to pretend like they are already loaded. They may not exist on disk.
993
- if (sourceMap.map && contents) {
994
- sourceMap.map.resolvedSources.forEach((source, i) => {
995
- const content = contents[i];
996
- if (content && source && url) {
997
- const contentUrl = supportRelativeURL(url, source);
998
- fileContentsCache[contentUrl] = content;
999
- }
1000
- });
1001
- }
1002
- }
1003
- else {
1004
- sourceMap = sourceMapCache[position.source] = {
1005
- url: null,
1006
- map: null,
1007
- };
1008
- }
1009
- }
1010
- // Resolve the source URL relative to the URL of the source map
1011
- if (sourceMap && sourceMap.map && sourceMap.url) {
1012
- const originalPosition = getOriginalPosition(sourceMap.map, position);
1013
- // Only return the original position if a matching line was found. If no
1014
- // matching line is found then we return position instead, which will cause
1015
- // the stack trace to print the path and line for the compiled file. It is
1016
- // better to give a precise location in the compiled file than a vague
1017
- // location in the original file.
1018
- if (originalPosition && originalPosition.source != null) {
1019
- originalPosition.source = supportRelativeURL(sourceMap.url, originalPosition.source);
1020
- if (sourceMap.vite) {
1021
- // @ts-expect-error vite is not defined
1022
- originalPosition._vite = true;
1023
- }
1024
- return originalPosition;
1025
- }
1026
- }
643
+ if (!position.source)
1027
644
  return position;
645
+ let sourceMap = getRuntimeSourceMap(position);
646
+ if (sourceMap || (sourceMap = sourceMapCache[position.source]), !sourceMap) {
647
+ const urlAndMap = retrieveSourceMap(position.source);
648
+ if (urlAndMap && urlAndMap.map) {
649
+ const url = urlAndMap.url;
650
+ sourceMap = sourceMapCache[position.source] = {
651
+ url,
652
+ map: new DecodedMap(typeof urlAndMap.map == "string" ? JSON.parse(urlAndMap.map) : urlAndMap.map, url)
653
+ };
654
+ const contents = sourceMap.map?.map.sourcesContent;
655
+ sourceMap.map && contents && sourceMap.map.resolvedSources.forEach((source, i) => {
656
+ const content = contents[i];
657
+ if (content && source && url) {
658
+ const contentUrl = supportRelativeURL(url, source);
659
+ fileContentsCache[contentUrl] = content;
660
+ }
661
+ });
662
+ } else
663
+ sourceMap = sourceMapCache[position.source] = {
664
+ url: null,
665
+ map: null
666
+ };
667
+ }
668
+ if (sourceMap && sourceMap.map && sourceMap.url) {
669
+ const originalPosition = getOriginalPosition(sourceMap.map, position);
670
+ if (originalPosition && originalPosition.source != null)
671
+ return originalPosition.source = supportRelativeURL(sourceMap.url, originalPosition.source), sourceMap.vite && (originalPosition._vite = !0), originalPosition;
672
+ }
673
+ return position;
1028
674
  }
1029
- // Parses code generated by FormatEvalOrigin(), a function inside V8:
1030
- // https://code.google.com/p/v8/source/browse/trunk/src/messages.js
1031
675
  function mapEvalOrigin(origin) {
1032
- // Most eval() calls are in this format
1033
- let match = /^eval at ([^(]+) \((.+):(\d+):(\d+)\)$/.exec(origin);
1034
- if (match) {
1035
- const position = mapSourcePosition({
1036
- name: null,
1037
- source: match[2],
1038
- line: +match[3],
1039
- column: +match[4] - 1,
1040
- });
1041
- return `eval at ${match[1]} (${position.source}:${position.line}:${position.column + 1})`;
1042
- }
1043
- // Parse nested eval() calls using recursion
1044
- match = /^eval at ([^(]+) \((.+)\)$/.exec(origin);
1045
- if (match)
1046
- return `eval at ${match[1]} (${mapEvalOrigin(match[2])})`;
1047
- // Make sure we still return useful information if we didn't find anything
1048
- return origin;
676
+ let match = /^eval at ([^(]+) \((.+):(\d+):(\d+)\)$/.exec(origin);
677
+ if (match) {
678
+ const position = mapSourcePosition({
679
+ name: null,
680
+ source: match[2],
681
+ line: +match[3],
682
+ column: +match[4] - 1
683
+ });
684
+ return `eval at ${match[1]} (${position.source}:${position.line}:${position.column + 1})`;
685
+ }
686
+ return match = /^eval at ([^(]+) \((.+)\)$/.exec(origin), match ? `eval at ${match[1]} (${mapEvalOrigin(match[2])})` : origin;
1049
687
  }
1050
- // This is copied almost verbatim from the V8 source code at
1051
- // https://code.google.com/p/v8/source/browse/trunk/src/messages.js. The
1052
- // implementation of wrapCallSite() used to just forward to the actual source
1053
- // code of CallSite.prototype.toString but unfortunately a new release of V8
1054
- // did something to the prototype chain and broke the shim. The only fix I
1055
- // could find was copy/paste.
1056
688
  function CallSiteToString() {
1057
- let fileName;
1058
- let fileLocation = '';
1059
- if (this.isNative()) {
1060
- fileLocation = 'native';
1061
- }
1062
- else {
1063
- fileName = this.getScriptNameOrSourceURL();
1064
- if (!fileName && this.isEval()) {
1065
- fileLocation = this.getEvalOrigin();
1066
- fileLocation += ', '; // Expecting source position to follow.
1067
- }
1068
- if (fileName) {
1069
- fileLocation += fileName;
1070
- }
1071
- else {
1072
- // Source code does not originate from a file and is not native, but we
1073
- // can still get the source position inside the source string, e.g. in
1074
- // an eval string.
1075
- fileLocation += '<anonymous>';
1076
- }
1077
- const lineNumber = this.getLineNumber();
1078
- if (lineNumber != null) {
1079
- fileLocation += `:${lineNumber}`;
1080
- const columnNumber = this.getColumnNumber();
1081
- if (columnNumber)
1082
- fileLocation += `:${columnNumber}`;
1083
- }
1084
- }
1085
- let line = '';
1086
- const functionName = this.getFunctionName();
1087
- let addSuffix = true;
1088
- const isConstructor = this.isConstructor();
1089
- const isMethodCall = !(this.isToplevel() || isConstructor);
1090
- if (isMethodCall) {
1091
- let typeName = this.getTypeName();
1092
- // Fixes shim to be backward compatable with Node v0 to v4
1093
- if (typeName === '[object Object]')
1094
- typeName = 'null';
1095
- const methodName = this.getMethodName();
1096
- if (functionName) {
1097
- if (typeName && functionName.indexOf(typeName) !== 0)
1098
- line += `${typeName}.`;
1099
- line += functionName;
1100
- if (methodName &&
1101
- functionName.indexOf(`.${methodName}`) !==
1102
- functionName.length - methodName.length - 1)
1103
- line += ` [as ${methodName}]`;
1104
- }
1105
- else {
1106
- line += `${typeName}.${methodName || '<anonymous>'}`;
1107
- }
1108
- }
1109
- else if (isConstructor) {
1110
- line += `new ${functionName || '<anonymous>'}`;
1111
- }
1112
- else if (functionName) {
1113
- line += functionName;
1114
- }
1115
- else {
1116
- line += fileLocation;
1117
- addSuffix = false;
1118
- }
1119
- if (addSuffix)
1120
- line += ` (${fileLocation})`;
1121
- return line;
689
+ let fileName, fileLocation = "";
690
+ if (this.isNative())
691
+ fileLocation = "native";
692
+ else {
693
+ fileName = this.getScriptNameOrSourceURL(), !fileName && this.isEval() && (fileLocation = this.getEvalOrigin(), fileLocation += ", "), fileName ? fileLocation += fileName : fileLocation += "<anonymous>";
694
+ const lineNumber = this.getLineNumber();
695
+ if (lineNumber != null) {
696
+ fileLocation += `:${lineNumber}`;
697
+ const columnNumber = this.getColumnNumber();
698
+ columnNumber && (fileLocation += `:${columnNumber}`);
699
+ }
700
+ }
701
+ let line = "";
702
+ const functionName = this.getFunctionName();
703
+ let addSuffix = !0;
704
+ const isConstructor = this.isConstructor();
705
+ if (!(this.isToplevel() || isConstructor)) {
706
+ let typeName = this.getTypeName();
707
+ typeName === "[object Object]" && (typeName = "null");
708
+ const methodName = this.getMethodName();
709
+ functionName ? (typeName && functionName.indexOf(typeName) !== 0 && (line += `${typeName}.`), line += functionName, methodName && functionName.indexOf(`.${methodName}`) !== functionName.length - methodName.length - 1 && (line += ` [as ${methodName}]`)) : line += `${typeName}.${methodName || "<anonymous>"}`;
710
+ } else
711
+ isConstructor ? line += `new ${functionName || "<anonymous>"}` : functionName ? line += functionName : (line += fileLocation, addSuffix = !1);
712
+ return addSuffix && (line += ` (${fileLocation})`), line;
1122
713
  }
1123
714
  function cloneCallSite(frame) {
1124
- const object = {};
1125
- Object.getOwnPropertyNames(Object.getPrototypeOf(frame)).forEach((name) => {
1126
- const key = name;
1127
- // @ts-expect-error difficult to type
1128
- object[key] = /^(?:is|get)/.test(name)
1129
- ? function () {
1130
- return frame[key].call(frame);
1131
- }
1132
- : frame[key];
1133
- });
1134
- object.toString = CallSiteToString;
1135
- return object;
715
+ const object = {};
716
+ return Object.getOwnPropertyNames(Object.getPrototypeOf(frame)).forEach((name) => {
717
+ const key = name;
718
+ object[key] = /^(?:is|get)/.test(name) ? function() {
719
+ return frame[key].call(frame);
720
+ } : frame[key];
721
+ }), object.toString = CallSiteToString, object;
1136
722
  }
1137
723
  function wrapCallSite(frame, state) {
1138
- // provides interface backward compatibility
1139
- if (state === undefined)
1140
- state = { nextPosition: null, curPosition: null };
1141
- if (frame.isNative()) {
1142
- state.curPosition = null;
1143
- return frame;
1144
- }
1145
- // Most call sites will return the source file from getFileName(), but code
1146
- // passed to eval() ending in "//# sourceURL=..." will return the source file
1147
- // from getScriptNameOrSourceURL() instead
1148
- const source = frame.getFileName() || frame.getScriptNameOrSourceURL();
1149
- if (source) {
1150
- const line = frame.getLineNumber();
1151
- let column = frame.getColumnNumber() - 1;
1152
- // Fix position in Node where some (internal) code is prepended.
1153
- // See https://github.com/evanw/node-source-map-support/issues/36
1154
- // Header removed in node at ^10.16 || >=11.11.0
1155
- // v11 is not an LTS candidate, we can just test the one version with it.
1156
- // Test node versions for: 10.16-19, 10.20+, 12-19, 20-99, 100+, or 11.11
1157
- const headerLength = 62;
1158
- if (line === 1 && column > headerLength && !frame.isEval())
1159
- column -= headerLength;
1160
- const position = mapSourcePosition({
1161
- name: null,
1162
- source,
1163
- line,
1164
- column,
1165
- });
1166
- state.curPosition = position;
1167
- frame = cloneCallSite(frame);
1168
- const originalFunctionName = frame.getFunctionName;
1169
- frame.getFunctionName = function () {
1170
- const name = (() => {
1171
- if (state.nextPosition == null)
1172
- return originalFunctionName();
1173
- return state.nextPosition.name || originalFunctionName();
1174
- })();
1175
- return name === 'eval' && '_vite' in position ? null : name;
1176
- };
1177
- frame.getFileName = function () {
1178
- return position.source ?? undefined;
1179
- };
1180
- frame.getLineNumber = function () {
1181
- return position.line;
1182
- };
1183
- frame.getColumnNumber = function () {
1184
- return position.column + 1;
1185
- };
1186
- frame.getScriptNameOrSourceURL = function () {
1187
- return position.source;
1188
- };
1189
- return frame;
1190
- }
1191
- // Code called using eval() needs special handling
1192
- let origin = frame.isEval() && frame.getEvalOrigin();
1193
- if (origin) {
1194
- origin = mapEvalOrigin(origin);
1195
- frame = cloneCallSite(frame);
1196
- frame.getEvalOrigin = function () {
1197
- return origin || undefined;
1198
- };
1199
- return frame;
1200
- }
1201
- // If we get here then we were unable to change the source position
1202
- return frame;
724
+ if (state === void 0 && (state = { nextPosition: null, curPosition: null }), frame.isNative())
725
+ return state.curPosition = null, frame;
726
+ const source = frame.getFileName() || frame.getScriptNameOrSourceURL();
727
+ if (source) {
728
+ const line = frame.getLineNumber();
729
+ let column = frame.getColumnNumber() - 1;
730
+ const headerLength = 62;
731
+ line === 1 && column > headerLength && !frame.isEval() && (column -= headerLength);
732
+ const position = mapSourcePosition({
733
+ name: null,
734
+ source,
735
+ line,
736
+ column
737
+ });
738
+ state.curPosition = position, frame = cloneCallSite(frame);
739
+ const originalFunctionName = frame.getFunctionName;
740
+ return frame.getFunctionName = function() {
741
+ const name = (() => state.nextPosition == null ? originalFunctionName() : state.nextPosition.name || originalFunctionName())();
742
+ return name === "eval" && "_vite" in position ? null : name;
743
+ }, frame.getFileName = function() {
744
+ return position.source ?? void 0;
745
+ }, frame.getLineNumber = function() {
746
+ return position.line;
747
+ }, frame.getColumnNumber = function() {
748
+ return position.column + 1;
749
+ }, frame.getScriptNameOrSourceURL = function() {
750
+ return position.source;
751
+ }, frame;
752
+ }
753
+ let origin = frame.isEval() && frame.getEvalOrigin();
754
+ return origin && (origin = mapEvalOrigin(origin), frame = cloneCallSite(frame), frame.getEvalOrigin = function() {
755
+ return origin || void 0;
756
+ }), frame;
1203
757
  }
1204
758
  function prepareStackTrace(error, stack) {
1205
- const name = error.name || 'Error';
1206
- const message = error.message || '';
1207
- const errorString = `${name}: ${message}`;
1208
- const state = { nextPosition: null, curPosition: null };
1209
- const processedStack = [];
1210
- for (let i = stack.length - 1; i >= 0; i--) {
1211
- processedStack.push(`\n at ${wrapCallSite(stack[i], state)}`);
1212
- state.nextPosition = state.curPosition;
1213
- }
1214
- state.curPosition = state.nextPosition = null;
1215
- return errorString + processedStack.reverse().join('');
759
+ const name = error.name || "Error", message = error.message || "", errorString = `${name}: ${message}`, state = { nextPosition: null, curPosition: null }, processedStack = [];
760
+ for (let i = stack.length - 1; i >= 0; i--)
761
+ processedStack.push(`
762
+ at ${wrapCallSite(stack[i], state)}`), state.nextPosition = state.curPosition;
763
+ return state.curPosition = state.nextPosition = null, errorString + processedStack.reverse().join("");
1216
764
  }
1217
-
1218
765
  function enableSourceMapSupport(runtime) {
1219
- if (runtime.options.sourcemapInterceptor === 'node') {
1220
- if (typeof process === 'undefined') {
1221
- throw new TypeError(`Cannot use "sourcemapInterceptor: 'node'" because global "process" variable is not available.`);
1222
- }
1223
- if (typeof process.setSourceMapsEnabled !== 'function') {
1224
- throw new TypeError(`Cannot use "sourcemapInterceptor: 'node'" because "process.setSourceMapsEnabled" function is not available. Please use Node >= 16.6.0.`);
1225
- }
1226
- const isEnabledAlready = process.sourceMapsEnabled ?? false;
1227
- process.setSourceMapsEnabled(true);
1228
- return () => !isEnabledAlready && process.setSourceMapsEnabled(false);
1229
- }
1230
- return interceptStackTrace(runtime, typeof runtime.options.sourcemapInterceptor === 'object'
1231
- ? runtime.options.sourcemapInterceptor
1232
- : undefined);
766
+ if (runtime.options.sourcemapInterceptor === "node") {
767
+ if (typeof process > "u")
768
+ throw new TypeError(`Cannot use "sourcemapInterceptor: 'node'" because global "process" variable is not available.`);
769
+ if (typeof process.setSourceMapsEnabled != "function")
770
+ throw new TypeError(`Cannot use "sourcemapInterceptor: 'node'" because "process.setSourceMapsEnabled" function is not available. Please use Node >= 16.6.0.`);
771
+ const isEnabledAlready = process.sourceMapsEnabled ?? !1;
772
+ return process.setSourceMapsEnabled(!0), () => !isEnabledAlready && process.setSourceMapsEnabled(!1);
773
+ }
774
+ return interceptStackTrace(runtime, typeof runtime.options.sourcemapInterceptor == "object" ? runtime.options.sourcemapInterceptor : void 0);
1233
775
  }
1234
-
1235
776
  class ViteRuntime {
1236
- options;
1237
- runner;
1238
- debug;
1239
- /**
1240
- * Holds the cache of modules
1241
- * Keys of the map are ids
1242
- */
1243
- moduleCache;
1244
- hmrClient;
1245
- entrypoints = new Set();
1246
- idToUrlMap = new Map();
1247
- fileToIdMap = new Map();
1248
- envProxy = new Proxy({}, {
1249
- get(_, p) {
1250
- throw new Error(`[vite-runtime] Dynamic access of "import.meta.env" is not supported. Please, use "import.meta.env.${String(p)}" instead.`);
1251
- },
777
+ options;
778
+ runner;
779
+ debug;
780
+ /**
781
+ * Holds the cache of modules
782
+ * Keys of the map are ids
783
+ */
784
+ moduleCache;
785
+ hmrClient;
786
+ entrypoints = /* @__PURE__ */ new Set();
787
+ idToUrlMap = /* @__PURE__ */ new Map();
788
+ fileToIdMap = /* @__PURE__ */ new Map();
789
+ envProxy = new Proxy({}, {
790
+ get(_, p) {
791
+ throw new Error(`[vite-runtime] Dynamic access of "import.meta.env" is not supported. Please, use "import.meta.env.${String(p)}" instead.`);
792
+ }
793
+ });
794
+ _destroyed = !1;
795
+ _resetSourceMapSupport;
796
+ constructor(options, runner, debug) {
797
+ this.options = options, this.runner = runner, this.debug = debug, this.moduleCache = options.moduleCache ?? new ModuleCacheMap(options.root), typeof options.hmr == "object" && (this.hmrClient = new HMRClient(options.hmr.logger === !1 ? silentConsole : options.hmr.logger || console, options.hmr.connection, ({ acceptedPath, ssrInvalidates }) => (this.moduleCache.invalidate(acceptedPath), ssrInvalidates && this.invalidateFiles(ssrInvalidates), this.executeUrl(acceptedPath))), options.hmr.connection.onUpdate(createHMRHandler(this))), options.sourcemapInterceptor !== !1 && (this._resetSourceMapSupport = enableSourceMapSupport(this));
798
+ }
799
+ /**
800
+ * URL to execute. Accepts file path, server path or id relative to the root.
801
+ */
802
+ async executeUrl(url) {
803
+ url = this.normalizeEntryUrl(url);
804
+ const fetchedModule = await this.cachedModule(url);
805
+ return await this.cachedRequest(url, fetchedModule);
806
+ }
807
+ /**
808
+ * Entrypoint URL to execute. Accepts file path, server path or id relative to the root.
809
+ * In the case of a full reload triggered by HMR, this is the module that will be reloaded.
810
+ * If this method is called multiple times, all entrypoints will be reloaded one at a time.
811
+ */
812
+ async executeEntrypoint(url) {
813
+ url = this.normalizeEntryUrl(url);
814
+ const fetchedModule = await this.cachedModule(url);
815
+ return await this.cachedRequest(url, fetchedModule, [], {
816
+ entrypoint: !0
1252
817
  });
1253
- _destroyed = false;
1254
- _resetSourceMapSupport;
1255
- constructor(options, runner, debug) {
1256
- this.options = options;
1257
- this.runner = runner;
1258
- this.debug = debug;
1259
- this.moduleCache = options.moduleCache ?? new ModuleCacheMap(options.root);
1260
- if (typeof options.hmr === 'object') {
1261
- this.hmrClient = new HMRClient(options.hmr.logger === false
1262
- ? silentConsole
1263
- : options.hmr.logger || console, options.hmr.connection, ({ acceptedPath, ssrInvalidates }) => {
1264
- this.moduleCache.delete(acceptedPath);
1265
- if (ssrInvalidates) {
1266
- this.invalidateFiles(ssrInvalidates);
1267
- }
1268
- return this.executeUrl(acceptedPath);
1269
- });
1270
- options.hmr.connection.onUpdate(createHMRHandler(this));
1271
- }
1272
- if (options.sourcemapInterceptor !== false) {
1273
- this._resetSourceMapSupport = enableSourceMapSupport(this);
1274
- }
1275
- }
1276
- /**
1277
- * URL to execute. Accepts file path, server path or id relative to the root.
1278
- */
1279
- async executeUrl(url) {
1280
- url = this.normalizeEntryUrl(url);
1281
- const fetchedModule = await this.cachedModule(url);
1282
- return await this.cachedRequest(url, fetchedModule);
1283
- }
1284
- /**
1285
- * Entrypoint URL to execute. Accepts file path, server path or id relative to the root.
1286
- * In the case of a full reload triggered by HMR, this is the module that will be reloaded.
1287
- * If this method is called multiple times, all entrypoints will be reloaded one at a time.
1288
- */
1289
- async executeEntrypoint(url) {
1290
- url = this.normalizeEntryUrl(url);
1291
- const fetchedModule = await this.cachedModule(url);
1292
- return await this.cachedRequest(url, fetchedModule, [], {
1293
- entrypoint: true,
1294
- });
1295
- }
1296
- /**
1297
- * Clear all caches including HMR listeners.
1298
- */
1299
- clearCache() {
1300
- this.moduleCache.clear();
1301
- this.idToUrlMap.clear();
1302
- this.entrypoints.clear();
1303
- this.hmrClient?.clear();
1304
- }
1305
- /**
1306
- * Clears all caches, removes all HMR listeners, and resets source map support.
1307
- * This method doesn't stop the HMR connection.
1308
- */
1309
- async destroy() {
1310
- this._resetSourceMapSupport?.();
1311
- this.clearCache();
1312
- this.hmrClient = undefined;
1313
- this._destroyed = true;
1314
- }
1315
- /**
1316
- * Returns `true` if the runtime has been destroyed by calling `destroy()` method.
1317
- */
1318
- isDestroyed() {
1319
- return this._destroyed;
1320
- }
1321
- invalidateFiles(files) {
1322
- files.forEach((file) => {
1323
- const ids = this.fileToIdMap.get(file);
1324
- if (ids) {
1325
- ids.forEach((id) => this.moduleCache.deleteByModuleId(id));
1326
- }
1327
- });
1328
- }
1329
- // we don't use moduleCache.normalize because this URL doesn't have to follow the same rules
1330
- // this URL is something that user passes down manually, and is later resolved by fetchModule
1331
- // moduleCache.normalize is used on resolved "file" property
1332
- normalizeEntryUrl(url) {
1333
- // expect fetchModule to resolve relative module correctly
1334
- if (url[0] === '.') {
1335
- return url;
1336
- }
1337
- // file:///C:/root/id.js -> C:/root/id.js
1338
- if (url.startsWith('file://')) {
1339
- // 8 is the length of "file:///"
1340
- url = url.slice(isWindows ? 8 : 7);
1341
- }
1342
- url = url.replace(/\\/g, '/');
1343
- const _root = this.options.root;
1344
- const root = _root[_root.length - 1] === '/' ? _root : `${_root}/`;
1345
- // strip root from the URL because fetchModule prefers a public served url path
1346
- // packages/vite/src/node/server/moduleGraph.ts:17
1347
- if (url.startsWith(root)) {
1348
- // /root/id.js -> /id.js
1349
- // C:/root/id.js -> /id.js
1350
- // 1 is to keep the leading slash
1351
- return url.slice(root.length - 1);
1352
- }
1353
- // if it's a server url (starts with a slash), keep it, otherwise assume a virtual module
1354
- // /id.js -> /id.js
1355
- // virtual:custom -> /@id/virtual:custom
1356
- return url[0] === '/' ? url : wrapId(url);
1357
- }
1358
- processImport(exports, fetchResult, metadata) {
1359
- if (!('externalize' in fetchResult)) {
1360
- return exports;
1361
- }
1362
- const { id, type } = fetchResult;
1363
- if (type !== 'module' && type !== 'commonjs')
1364
- return exports;
1365
- analyzeImportedModDifference(exports, id, type, metadata);
1366
- return proxyGuardOnlyEsm(exports, id, metadata);
1367
- }
1368
- async cachedRequest(id, fetchedModule, callstack = [], metadata) {
1369
- const moduleId = fetchedModule.id;
1370
- if (metadata?.entrypoint) {
1371
- this.entrypoints.add(moduleId);
1372
- }
1373
- const mod = this.moduleCache.getByModuleId(moduleId);
1374
- const { imports, importers } = mod;
1375
- const importee = callstack[callstack.length - 1];
1376
- if (importee)
1377
- importers.add(importee);
1378
- // check circular dependency
1379
- if (callstack.includes(moduleId) ||
1380
- Array.from(imports.values()).some((i) => importers.has(i))) {
1381
- if (mod.exports)
1382
- return this.processImport(mod.exports, fetchedModule, metadata);
1383
- }
1384
- let debugTimer;
1385
- if (this.debug) {
1386
- debugTimer = setTimeout(() => {
1387
- const getStack = () => `stack:\n${[...callstack, moduleId]
1388
- .reverse()
1389
- .map((p) => ` - ${p}`)
1390
- .join('\n')}`;
1391
- this.debug(`[vite-runtime] module ${moduleId} takes over 2s to load.\n${getStack()}`);
1392
- }, 2000);
1393
- }
1394
- try {
1395
- // cached module
1396
- if (mod.promise)
1397
- return this.processImport(await mod.promise, fetchedModule, metadata);
1398
- const promise = this.directRequest(id, fetchedModule, callstack);
1399
- mod.promise = promise;
1400
- mod.evaluated = false;
1401
- return this.processImport(await promise, fetchedModule, metadata);
1402
- }
1403
- finally {
1404
- mod.evaluated = true;
1405
- if (debugTimer)
1406
- clearTimeout(debugTimer);
1407
- }
1408
- }
1409
- async cachedModule(id, importer) {
1410
- if (this._destroyed) {
1411
- throw new Error(`[vite] Vite runtime has been destroyed.`);
1412
- }
1413
- const normalized = this.idToUrlMap.get(id);
1414
- if (normalized) {
1415
- const mod = this.moduleCache.getByModuleId(normalized);
1416
- if (mod.meta) {
1417
- return mod.meta;
1418
- }
1419
- }
1420
- this.debug?.('[vite-runtime] fetching', id);
1421
- // fast return for established externalized patterns
1422
- const fetchedModule = id.startsWith('data:')
1423
- ? ({ externalize: id, type: 'builtin' })
1424
- : await this.options.fetchModule(id, importer);
1425
- // base moduleId on "file" and not on id
1426
- // if `import(variable)` is called it's possible that it doesn't have an extension for example
1427
- // if we used id for that, it's possible to have a duplicated module
1428
- const idQuery = id.split('?')[1];
1429
- const query = idQuery ? `?${idQuery}` : '';
1430
- const file = 'file' in fetchedModule ? fetchedModule.file : undefined;
1431
- const fullFile = file ? `${file}${query}` : id;
1432
- const moduleId = this.moduleCache.normalize(fullFile);
1433
- const mod = this.moduleCache.getByModuleId(moduleId);
1434
- fetchedModule.id = moduleId;
1435
- mod.meta = fetchedModule;
1436
- if (file) {
1437
- const fileModules = this.fileToIdMap.get(file) || [];
1438
- fileModules.push(moduleId);
1439
- this.fileToIdMap.set(file, fileModules);
1440
- }
1441
- this.idToUrlMap.set(id, moduleId);
1442
- this.idToUrlMap.set(unwrapId(id), moduleId);
1443
- return fetchedModule;
1444
- }
1445
- // override is allowed, consider this a public API
1446
- async directRequest(id, fetchResult, _callstack) {
1447
- const moduleId = fetchResult.id;
1448
- const callstack = [..._callstack, moduleId];
1449
- const mod = this.moduleCache.getByModuleId(moduleId);
1450
- const request = async (dep, metadata) => {
1451
- const fetchedModule = await this.cachedModule(dep, moduleId);
1452
- const depMod = this.moduleCache.getByModuleId(fetchedModule.id);
1453
- depMod.importers.add(moduleId);
1454
- mod.imports.add(fetchedModule.id);
1455
- return this.cachedRequest(dep, fetchedModule, callstack, metadata);
1456
- };
1457
- const dynamicRequest = async (dep) => {
1458
- // it's possible to provide an object with toString() method inside import()
1459
- dep = String(dep);
1460
- if (dep[0] === '.') {
1461
- dep = posixResolve(posixDirname(id), dep);
1462
- }
1463
- return request(dep, { isDynamicImport: true });
1464
- };
1465
- if ('externalize' in fetchResult) {
1466
- const { externalize } = fetchResult;
1467
- this.debug?.('[vite-runtime] externalizing', externalize);
1468
- const exports = await this.runner.runExternalModule(externalize);
1469
- mod.exports = exports;
1470
- return exports;
1471
- }
1472
- const { code, file } = fetchResult;
1473
- if (code == null) {
1474
- const importer = callstack[callstack.length - 2];
1475
- throw new Error(`[vite-runtime] Failed to load "${id}"${importer ? ` imported from ${importer}` : ''}`);
1476
- }
1477
- const modulePath = cleanUrl(file || moduleId);
1478
- // disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
1479
- const href = posixPathToFileHref(modulePath);
1480
- const filename = modulePath;
1481
- const dirname = posixDirname(modulePath);
1482
- const meta = {
1483
- filename: isWindows ? toWindowsPath(filename) : filename,
1484
- dirname: isWindows ? toWindowsPath(dirname) : dirname,
1485
- url: href,
1486
- env: this.envProxy,
1487
- resolve(id, parent) {
1488
- throw new Error('[vite-runtime] "import.meta.resolve" is not supported.');
1489
- },
1490
- // should be replaced during transformation
1491
- glob() {
1492
- throw new Error('[vite-runtime] "import.meta.glob" is not supported.');
1493
- },
1494
- };
1495
- const exports = Object.create(null);
1496
- Object.defineProperty(exports, Symbol.toStringTag, {
1497
- value: 'Module',
1498
- enumerable: false,
1499
- configurable: false,
1500
- });
1501
- mod.exports = exports;
1502
- let hotContext;
1503
- if (this.hmrClient) {
1504
- Object.defineProperty(meta, 'hot', {
1505
- enumerable: true,
1506
- get: () => {
1507
- if (!this.hmrClient) {
1508
- throw new Error(`[vite-runtime] HMR client was destroyed.`);
1509
- }
1510
- this.debug?.('[vite-runtime] creating hmr context for', moduleId);
1511
- hotContext ||= new HMRContext(this.hmrClient, moduleId);
1512
- return hotContext;
1513
- },
1514
- set: (value) => {
1515
- hotContext = value;
1516
- },
1517
- });
1518
- }
1519
- const context = {
1520
- [ssrImportKey]: request,
1521
- [ssrDynamicImportKey]: dynamicRequest,
1522
- [ssrModuleExportsKey]: exports,
1523
- [ssrExportAllKey]: (obj) => exportAll(exports, obj),
1524
- [ssrImportMetaKey]: meta,
1525
- };
1526
- this.debug?.('[vite-runtime] executing', href);
1527
- await this.runner.runViteModule(context, code, id);
1528
- return exports;
1529
- }
818
+ }
819
+ /**
820
+ * Clear all caches including HMR listeners.
821
+ */
822
+ clearCache() {
823
+ this.moduleCache.clear(), this.idToUrlMap.clear(), this.entrypoints.clear(), this.hmrClient?.clear();
824
+ }
825
+ /**
826
+ * Clears all caches, removes all HMR listeners, and resets source map support.
827
+ * This method doesn't stop the HMR connection.
828
+ */
829
+ async destroy() {
830
+ this._resetSourceMapSupport?.(), this.clearCache(), this.hmrClient = void 0, this._destroyed = !0;
831
+ }
832
+ /**
833
+ * Returns `true` if the runtime has been destroyed by calling `destroy()` method.
834
+ */
835
+ isDestroyed() {
836
+ return this._destroyed;
837
+ }
838
+ invalidateFiles(files) {
839
+ files.forEach((file) => {
840
+ const ids = this.fileToIdMap.get(file);
841
+ ids && ids.forEach((id) => this.moduleCache.invalidate(id));
842
+ });
843
+ }
844
+ // we don't use moduleCache.normalize because this URL doesn't have to follow the same rules
845
+ // this URL is something that user passes down manually, and is later resolved by fetchModule
846
+ // moduleCache.normalize is used on resolved "file" property
847
+ normalizeEntryUrl(url) {
848
+ if (url[0] === ".")
849
+ return url;
850
+ url.startsWith("file://") && (url = url.slice(isWindows ? 8 : 7)), url = url.replace(/\\/g, "/");
851
+ const _root = this.options.root, root = _root[_root.length - 1] === "/" ? _root : `${_root}/`;
852
+ return url.startsWith(root) ? url.slice(root.length - 1) : url[0] === "/" ? url : wrapId(url);
853
+ }
854
+ processImport(exports, fetchResult, metadata) {
855
+ if (!("externalize" in fetchResult))
856
+ return exports;
857
+ const { id, type } = fetchResult;
858
+ return type !== "module" && type !== "commonjs" ? exports : (analyzeImportedModDifference(exports, id, type, metadata), proxyGuardOnlyEsm(exports, id, metadata));
859
+ }
860
+ async cachedRequest(id, fetchedModule, callstack = [], metadata) {
861
+ const moduleId = fetchedModule.id;
862
+ metadata?.entrypoint && this.entrypoints.add(moduleId);
863
+ const mod = this.moduleCache.getByModuleId(moduleId), { imports, importers } = mod, importee = callstack[callstack.length - 1];
864
+ if (importee && importers.add(importee), (callstack.includes(moduleId) || Array.from(imports.values()).some((i) => importers.has(i))) && mod.exports)
865
+ return this.processImport(mod.exports, fetchedModule, metadata);
866
+ let debugTimer;
867
+ this.debug && (debugTimer = setTimeout(() => {
868
+ const getStack = () => `stack:
869
+ ${[...callstack, moduleId].reverse().map((p) => ` - ${p}`).join(`
870
+ `)}`;
871
+ this.debug(`[vite-runtime] module ${moduleId} takes over 2s to load.
872
+ ${getStack()}`);
873
+ }, 2e3));
874
+ try {
875
+ if (mod.promise)
876
+ return this.processImport(await mod.promise, fetchedModule, metadata);
877
+ const promise = this.directRequest(id, fetchedModule, callstack);
878
+ return mod.promise = promise, mod.evaluated = !1, this.processImport(await promise, fetchedModule, metadata);
879
+ } finally {
880
+ mod.evaluated = !0, debugTimer && clearTimeout(debugTimer);
881
+ }
882
+ }
883
+ async cachedModule(id, importer) {
884
+ if (this._destroyed)
885
+ throw new Error("[vite] Vite runtime has been destroyed.");
886
+ const normalized = this.idToUrlMap.get(id);
887
+ if (normalized) {
888
+ const mod2 = this.moduleCache.getByModuleId(normalized);
889
+ if (mod2.meta)
890
+ return mod2.meta;
891
+ }
892
+ this.debug?.("[vite-runtime] fetching", id);
893
+ const fetchedModule = id.startsWith("data:") ? { externalize: id, type: "builtin" } : await this.options.fetchModule(id, importer), idQuery = id.split("?")[1], query = idQuery ? `?${idQuery}` : "", file = "file" in fetchedModule ? fetchedModule.file : void 0, fullFile = file ? `${file}${query}` : id, moduleId = this.moduleCache.normalize(fullFile), mod = this.moduleCache.getByModuleId(moduleId);
894
+ if (fetchedModule.id = moduleId, mod.meta = fetchedModule, file) {
895
+ const fileModules = this.fileToIdMap.get(file) || [];
896
+ fileModules.push(moduleId), this.fileToIdMap.set(file, fileModules);
897
+ }
898
+ return this.idToUrlMap.set(id, moduleId), this.idToUrlMap.set(unwrapId(id), moduleId), fetchedModule;
899
+ }
900
+ // override is allowed, consider this a public API
901
+ async directRequest(id, fetchResult, _callstack) {
902
+ const moduleId = fetchResult.id, callstack = [..._callstack, moduleId], mod = this.moduleCache.getByModuleId(moduleId), request = async (dep, metadata) => {
903
+ const fetchedModule = await this.cachedModule(dep, moduleId);
904
+ return this.moduleCache.getByModuleId(fetchedModule.id).importers.add(moduleId), mod.imports.add(fetchedModule.id), this.cachedRequest(dep, fetchedModule, callstack, metadata);
905
+ }, dynamicRequest = async (dep) => (dep = String(dep), dep[0] === "." && (dep = posixResolve(posixDirname(id), dep)), request(dep, { isDynamicImport: !0 }));
906
+ if ("externalize" in fetchResult) {
907
+ const { externalize } = fetchResult;
908
+ this.debug?.("[vite-runtime] externalizing", externalize);
909
+ const exports2 = await this.runner.runExternalModule(externalize);
910
+ return mod.exports = exports2, exports2;
911
+ }
912
+ const { code, file } = fetchResult;
913
+ if (code == null) {
914
+ const importer = callstack[callstack.length - 2];
915
+ throw new Error(`[vite-runtime] Failed to load "${id}"${importer ? ` imported from ${importer}` : ""}`);
916
+ }
917
+ const modulePath = cleanUrl(file || moduleId), href = posixPathToFileHref(modulePath), filename = modulePath, dirname2 = posixDirname(modulePath), meta = {
918
+ filename: isWindows ? toWindowsPath(filename) : filename,
919
+ dirname: isWindows ? toWindowsPath(dirname2) : dirname2,
920
+ url: href,
921
+ env: this.envProxy,
922
+ resolve(id2, parent) {
923
+ throw new Error('[vite-runtime] "import.meta.resolve" is not supported.');
924
+ },
925
+ // should be replaced during transformation
926
+ glob() {
927
+ throw new Error('[vite-runtime] "import.meta.glob" is not supported.');
928
+ }
929
+ }, exports = /* @__PURE__ */ Object.create(null);
930
+ Object.defineProperty(exports, Symbol.toStringTag, {
931
+ value: "Module",
932
+ enumerable: !1,
933
+ configurable: !1
934
+ }), mod.exports = exports;
935
+ let hotContext;
936
+ this.hmrClient && Object.defineProperty(meta, "hot", {
937
+ enumerable: !0,
938
+ get: () => {
939
+ if (!this.hmrClient)
940
+ throw new Error("[vite-runtime] HMR client was destroyed.");
941
+ return this.debug?.("[vite-runtime] creating hmr context for", moduleId), hotContext ||= new HMRContext(this.hmrClient, moduleId), hotContext;
942
+ },
943
+ set: (value) => {
944
+ hotContext = value;
945
+ }
946
+ });
947
+ const context = {
948
+ [ssrImportKey]: request,
949
+ [ssrDynamicImportKey]: dynamicRequest,
950
+ [ssrModuleExportsKey]: exports,
951
+ [ssrExportAllKey]: (obj) => exportAll(exports, obj),
952
+ [ssrImportMetaKey]: meta
953
+ };
954
+ return this.debug?.("[vite-runtime] executing", href), await this.runner.runViteModule(context, code, id), exports;
955
+ }
1530
956
  }
1531
957
  function exportAll(exports, sourceModule) {
1532
- // when a module exports itself it causes
1533
- // call stack error
1534
- if (exports === sourceModule)
1535
- return;
1536
- if (isPrimitive(sourceModule) ||
1537
- Array.isArray(sourceModule) ||
1538
- sourceModule instanceof Promise)
1539
- return;
1540
- for (const key in sourceModule) {
1541
- if (key !== 'default' && key !== '__esModule') {
1542
- try {
1543
- Object.defineProperty(exports, key, {
1544
- enumerable: true,
1545
- configurable: true,
1546
- get: () => sourceModule[key],
1547
- });
1548
- }
1549
- catch (_err) { }
958
+ if (exports !== sourceModule && !(isPrimitive(sourceModule) || Array.isArray(sourceModule) || sourceModule instanceof Promise)) {
959
+ for (const key in sourceModule)
960
+ if (key !== "default" && key !== "__esModule")
961
+ try {
962
+ Object.defineProperty(exports, key, {
963
+ enumerable: !0,
964
+ configurable: !0,
965
+ get: () => sourceModule[key]
966
+ });
967
+ } catch {
1550
968
  }
1551
- }
969
+ }
1552
970
  }
1553
- /**
1554
- * Vite converts `import { } from 'foo'` to `const _ = __vite_ssr_import__('foo')`.
1555
- * Top-level imports and dynamic imports work slightly differently in Node.js.
1556
- * This function normalizes the differences so it matches prod behaviour.
1557
- */
1558
971
  function analyzeImportedModDifference(mod, rawId, moduleType, metadata) {
1559
- // No normalization needed if the user already dynamic imports this module
1560
- if (metadata?.isDynamicImport)
1561
- return;
1562
- // If file path is ESM, everything should be fine
1563
- if (moduleType === 'module')
1564
- return;
1565
- // For non-ESM, named imports is done via static analysis with cjs-module-lexer in Node.js.
1566
- // If the user named imports a specifier that can't be analyzed, error.
1567
- if (metadata?.importedNames?.length) {
1568
- const missingBindings = metadata.importedNames.filter((s) => !(s in mod));
1569
- if (missingBindings.length) {
1570
- const lastBinding = missingBindings[missingBindings.length - 1];
1571
- // Copied from Node.js
1572
- throw new SyntaxError(`\
1573
- [vite] Named export '${lastBinding}' not found. The requested module '${rawId}' is a CommonJS module, which may not support all module.exports as named exports.
972
+ if (!metadata?.isDynamicImport && moduleType !== "module" && metadata?.importedNames?.length) {
973
+ const missingBindings = metadata.importedNames.filter((s) => !(s in mod));
974
+ if (missingBindings.length) {
975
+ const lastBinding = missingBindings[missingBindings.length - 1];
976
+ throw new SyntaxError(`[vite] Named export '${lastBinding}' not found. The requested module '${rawId}' is a CommonJS module, which may not support all module.exports as named exports.
1574
977
  CommonJS modules can always be imported via the default export, for example using:
1575
978
 
1576
979
  import pkg from '${rawId}';
1577
- const {${missingBindings.join(', ')}} = pkg;
980
+ const {${missingBindings.join(", ")}} = pkg;
1578
981
  `);
1579
- }
1580
982
  }
983
+ }
1581
984
  }
1582
- /**
1583
- * Guard invalid named exports only, similar to how Node.js errors for top-level imports.
1584
- * But since we transform as dynamic imports, we need to emulate the error manually.
1585
- */
1586
985
  function proxyGuardOnlyEsm(mod, rawId, metadata) {
1587
- // If the module doesn't import anything explicitly, e.g. `import 'foo'` or
1588
- // `import * as foo from 'foo'`, we can skip the proxy guard.
1589
- if (!metadata?.importedNames?.length)
1590
- return mod;
1591
- return new Proxy(mod, {
1592
- get(mod, prop) {
1593
- if (prop !== 'then' && !(prop in mod)) {
1594
- throw new SyntaxError(`[vite] The requested module '${rawId}' does not provide an export named '${prop.toString()}'`);
1595
- }
1596
- return mod[prop];
1597
- },
1598
- });
1599
- }
1600
-
1601
- // eslint-disable-next-line @typescript-eslint/no-empty-function
1602
- const AsyncFunction = async function () { }.constructor;
1603
- class ESModulesRunner {
1604
- async runViteModule(context, code) {
1605
- // use AsyncFunction instead of vm module to support broader array of environments out of the box
1606
- const initModule = new AsyncFunction(ssrModuleExportsKey, ssrImportMetaKey, ssrImportKey, ssrDynamicImportKey, ssrExportAllKey,
1607
- // source map should already be inlined by Vite
1608
- '"use strict";' + code);
1609
- await initModule(context[ssrModuleExportsKey], context[ssrImportMetaKey], context[ssrImportKey], context[ssrDynamicImportKey], context[ssrExportAllKey]);
1610
- Object.seal(context[ssrModuleExportsKey]);
1611
- }
1612
- runExternalModule(filepath) {
1613
- return import(filepath);
986
+ return metadata?.importedNames?.length ? new Proxy(mod, {
987
+ get(mod2, prop) {
988
+ if (prop !== "then" && !(prop in mod2))
989
+ throw new SyntaxError(`[vite] The requested module '${rawId}' does not provide an export named '${prop.toString()}'`);
990
+ return mod2[prop];
1614
991
  }
992
+ }) : mod;
1615
993
  }
1616
-
1617
- export { ESModulesRunner, ModuleCacheMap, ViteRuntime, ssrDynamicImportKey, ssrExportAllKey, ssrImportKey, ssrImportMetaKey, ssrModuleExportsKey };
994
+ const AsyncFunction = async function() {
995
+ }.constructor;
996
+ class ESModulesRunner {
997
+ async runViteModule(context, code) {
998
+ await new AsyncFunction(
999
+ ssrModuleExportsKey,
1000
+ ssrImportMetaKey,
1001
+ ssrImportKey,
1002
+ ssrDynamicImportKey,
1003
+ ssrExportAllKey,
1004
+ // source map should already be inlined by Vite
1005
+ '"use strict";' + code
1006
+ )(context[ssrModuleExportsKey], context[ssrImportMetaKey], context[ssrImportKey], context[ssrDynamicImportKey], context[ssrExportAllKey]), Object.seal(context[ssrModuleExportsKey]);
1007
+ }
1008
+ runExternalModule(filepath) {
1009
+ return import(filepath);
1010
+ }
1011
+ }
1012
+ export {
1013
+ ESModulesRunner,
1014
+ ModuleCacheMap,
1015
+ ViteRuntime,
1016
+ ssrDynamicImportKey,
1017
+ ssrExportAllKey,
1018
+ ssrImportKey,
1019
+ ssrImportMetaKey,
1020
+ ssrModuleExportsKey
1021
+ };