vite-node 0.34.4 → 0.34.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.
- package/dist/cli.cjs +6 -11
- package/dist/cli.d.ts +2 -2
- package/dist/cli.mjs +3 -3
- package/dist/client.cjs +4 -29
- package/dist/client.d.ts +2 -2
- package/dist/constants.cjs +0 -2
- package/dist/hmr.cjs +240 -14
- package/dist/hmr.d.ts +2 -2
- package/dist/hmr.mjs +237 -5
- package/dist/{types-0f31b933.d.ts → index-6fb787b2.d.ts} +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/server.cjs +3 -10
- package/dist/server.d.ts +2 -2
- package/dist/source-map.cjs +5 -12
- package/dist/source-map.d.ts +1 -1
- package/dist/trace-mapping.d-e677e8f4.d.ts +54 -0
- package/dist/types.d.ts +2 -2
- package/dist/utils.cjs +0 -2
- package/dist/utils.d.ts +2 -2
- package/package.json +4 -4
- package/dist/chunk-hmr.cjs +0 -250
- package/dist/chunk-hmr.mjs +0 -237
- package/dist/types.d-7442d07f.d.ts +0 -23
package/dist/cli.cjs
CHANGED
|
@@ -6,28 +6,23 @@ var vite = require('vite');
|
|
|
6
6
|
var server = require('./server.cjs');
|
|
7
7
|
var client = require('./client.cjs');
|
|
8
8
|
var utils = require('./utils.cjs');
|
|
9
|
-
var hmr = require('./chunk-hmr.cjs');
|
|
10
9
|
var sourceMap = require('./source-map.cjs');
|
|
10
|
+
var hmr = require('./hmr.cjs');
|
|
11
11
|
require('perf_hooks');
|
|
12
12
|
require('fs');
|
|
13
13
|
require('pathe');
|
|
14
14
|
require('debug');
|
|
15
15
|
require('mlly');
|
|
16
16
|
require('./constants.cjs');
|
|
17
|
-
require('node:url');
|
|
18
17
|
require('module');
|
|
19
18
|
require('path');
|
|
19
|
+
require('node:url');
|
|
20
20
|
require('node:vm');
|
|
21
21
|
require('node:events');
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
var cac__default = /*#__PURE__*/_interopDefaultLegacy(cac);
|
|
26
|
-
var c__default = /*#__PURE__*/_interopDefaultLegacy(c);
|
|
27
|
-
|
|
28
|
-
var version = "0.34.4";
|
|
23
|
+
var version = "0.34.5";
|
|
29
24
|
|
|
30
|
-
const cli =
|
|
25
|
+
const cli = cac("vite-node");
|
|
31
26
|
cli.option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-m, --mode <mode>", "Set env mode").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--script", "Use vite-node as a script runner").option("--options <options>", "Use specified Vite server options").option("-v, --version", "Output the version number").option("-h, --help", "Display help for command");
|
|
32
27
|
cli.command("[...files]").allowUnknownOptions().action(run);
|
|
33
28
|
cli.parse(process.argv, { run: false });
|
|
@@ -58,7 +53,7 @@ async function run(files, options = {}) {
|
|
|
58
53
|
process.exit(0);
|
|
59
54
|
}
|
|
60
55
|
if (!files.length) {
|
|
61
|
-
console.error(
|
|
56
|
+
console.error(c.red("No files specified."));
|
|
62
57
|
cli.version(version).outputHelp();
|
|
63
58
|
process.exit(1);
|
|
64
59
|
}
|
|
@@ -103,7 +98,7 @@ async function run(files, options = {}) {
|
|
|
103
98
|
});
|
|
104
99
|
if (options.watch) {
|
|
105
100
|
process.on("uncaughtException", (err) => {
|
|
106
|
-
console.error(
|
|
101
|
+
console.error(c.red("[vite-node] Failed to execute file: \n"), err);
|
|
107
102
|
});
|
|
108
103
|
}
|
|
109
104
|
}
|
package/dist/cli.d.ts
CHANGED
package/dist/cli.mjs
CHANGED
|
@@ -5,20 +5,20 @@ import { ViteNodeServer } from './server.mjs';
|
|
|
5
5
|
import { ViteNodeRunner } from './client.mjs';
|
|
6
6
|
import { toArray } from './utils.mjs';
|
|
7
7
|
import { installSourcemapsSupport } from './source-map.mjs';
|
|
8
|
-
import {
|
|
8
|
+
import { viteNodeHmrPlugin, createHotContext, handleMessage } from './hmr.mjs';
|
|
9
9
|
import 'node:perf_hooks';
|
|
10
10
|
import 'node:fs';
|
|
11
11
|
import 'pathe';
|
|
12
12
|
import 'debug';
|
|
13
13
|
import 'mlly';
|
|
14
14
|
import './constants.mjs';
|
|
15
|
-
import 'node:url';
|
|
16
15
|
import 'node:module';
|
|
17
16
|
import 'node:path';
|
|
17
|
+
import 'node:url';
|
|
18
18
|
import 'node:vm';
|
|
19
19
|
import 'node:events';
|
|
20
20
|
|
|
21
|
-
var version = "0.34.
|
|
21
|
+
var version = "0.34.5";
|
|
22
22
|
|
|
23
23
|
const cli = cac("vite-node");
|
|
24
24
|
cli.option("-r, --root <path>", "Use specified root directory").option("-c, --config <path>", "Use specified config file").option("-m, --mode <mode>", "Set env mode").option("-w, --watch", 'Restart on file changes, similar to "nodemon"').option("--script", "Use vite-node as a script runner").option("--options <options>", "Use specified Vite server options").option("-v, --version", "Output the version number").option("-h, --help", "Display help for command");
|
package/dist/client.cjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var module$1 = require('module');
|
|
6
4
|
var path = require('path');
|
|
7
5
|
var node_url = require('node:url');
|
|
@@ -12,32 +10,9 @@ var utils = require('./utils.cjs');
|
|
|
12
10
|
var sourceMap = require('./source-map.cjs');
|
|
13
11
|
require('fs');
|
|
14
12
|
|
|
15
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
16
|
-
|
|
17
|
-
function _interopNamespace(e) {
|
|
18
|
-
if (e && e.__esModule) return e;
|
|
19
|
-
var n = Object.create(null);
|
|
20
|
-
if (e) {
|
|
21
|
-
Object.keys(e).forEach(function (k) {
|
|
22
|
-
if (k !== 'default') {
|
|
23
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
24
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () { return e[k]; }
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
n["default"] = e;
|
|
32
|
-
return Object.freeze(n);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
var vm__default = /*#__PURE__*/_interopDefaultLegacy(vm);
|
|
36
|
-
var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
|
|
37
|
-
|
|
38
13
|
const { setTimeout, clearTimeout } = globalThis;
|
|
39
|
-
const debugExecute =
|
|
40
|
-
const debugNative =
|
|
14
|
+
const debugExecute = createDebug("vite-node:client:execute");
|
|
15
|
+
const debugNative = createDebug("vite-node:client:native");
|
|
41
16
|
const clientStub = {
|
|
42
17
|
injectQuery: (id) => id,
|
|
43
18
|
createHotContext: () => {
|
|
@@ -365,7 +340,7 @@ ${getStack()}`), 2e3);
|
|
|
365
340
|
lineOffset: 0,
|
|
366
341
|
columnOffset: -codeDefinition.length
|
|
367
342
|
};
|
|
368
|
-
const fn =
|
|
343
|
+
const fn = vm.runInThisContext(code, options);
|
|
369
344
|
await fn(...Object.values(context));
|
|
370
345
|
}
|
|
371
346
|
prepareContext(context) {
|
|
@@ -381,7 +356,7 @@ ${getStack()}`), 2e3);
|
|
|
381
356
|
return !path.endsWith(".mjs") && "default" in mod;
|
|
382
357
|
}
|
|
383
358
|
importExternalModule(path) {
|
|
384
|
-
return (
|
|
359
|
+
return import(path);
|
|
385
360
|
}
|
|
386
361
|
/**
|
|
387
362
|
* Import a module and interop it
|
package/dist/client.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import './
|
|
2
|
-
export {
|
|
1
|
+
import './trace-mapping.d-e677e8f4.js';
|
|
2
|
+
export { e as DEFAULT_REQUEST_STUBS, M as ModuleCacheMap, a as ViteNodeRunner } from './index-6fb787b2.js';
|
package/dist/constants.cjs
CHANGED
package/dist/hmr.cjs
CHANGED
|
@@ -1,23 +1,249 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
require('
|
|
7
|
-
require('picocolors');
|
|
8
|
-
require('debug');
|
|
9
|
-
require('./utils.cjs');
|
|
3
|
+
var node_events = require('node:events');
|
|
4
|
+
var c = require('picocolors');
|
|
5
|
+
var createDebug = require('debug');
|
|
6
|
+
var utils = require('./utils.cjs');
|
|
10
7
|
require('node:url');
|
|
11
8
|
require('module');
|
|
12
9
|
require('fs');
|
|
13
10
|
require('pathe');
|
|
14
11
|
|
|
12
|
+
function createHmrEmitter() {
|
|
13
|
+
const emitter = new node_events.EventEmitter();
|
|
14
|
+
return emitter;
|
|
15
|
+
}
|
|
16
|
+
function viteNodeHmrPlugin() {
|
|
17
|
+
const emitter = createHmrEmitter();
|
|
18
|
+
return {
|
|
19
|
+
name: "vite-node:hmr",
|
|
20
|
+
config() {
|
|
21
|
+
if (process.platform === "darwin" && process.env.VITE_TEST_WATCHER_DEBUG) {
|
|
22
|
+
return {
|
|
23
|
+
server: {
|
|
24
|
+
watch: {
|
|
25
|
+
useFsEvents: false,
|
|
26
|
+
usePolling: false
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
configureServer(server) {
|
|
33
|
+
const _send = server.ws.send;
|
|
34
|
+
server.emitter = emitter;
|
|
35
|
+
server.ws.send = function(payload) {
|
|
36
|
+
_send(payload);
|
|
37
|
+
emitter.emit("message", payload);
|
|
38
|
+
};
|
|
39
|
+
if (process.env.VITE_TEST_WATCHER_DEBUG) {
|
|
40
|
+
server.watcher.on("ready", () => {
|
|
41
|
+
console.log("[debug] watcher is ready");
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
15
47
|
|
|
48
|
+
const debugHmr = createDebug("vite-node:hmr");
|
|
49
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
50
|
+
function getCache(runner) {
|
|
51
|
+
if (!cache.has(runner)) {
|
|
52
|
+
cache.set(runner, {
|
|
53
|
+
hotModulesMap: /* @__PURE__ */ new Map(),
|
|
54
|
+
dataMap: /* @__PURE__ */ new Map(),
|
|
55
|
+
disposeMap: /* @__PURE__ */ new Map(),
|
|
56
|
+
pruneMap: /* @__PURE__ */ new Map(),
|
|
57
|
+
customListenersMap: /* @__PURE__ */ new Map(),
|
|
58
|
+
ctxToListenersMap: /* @__PURE__ */ new Map(),
|
|
59
|
+
messageBuffer: [],
|
|
60
|
+
isFirstUpdate: false,
|
|
61
|
+
pending: false,
|
|
62
|
+
queued: []
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return cache.get(runner);
|
|
66
|
+
}
|
|
67
|
+
function sendMessageBuffer(runner, emitter) {
|
|
68
|
+
const maps = getCache(runner);
|
|
69
|
+
maps.messageBuffer.forEach((msg) => emitter.emit("custom", msg));
|
|
70
|
+
maps.messageBuffer.length = 0;
|
|
71
|
+
}
|
|
72
|
+
async function reload(runner, files) {
|
|
73
|
+
Array.from(runner.moduleCache.keys()).forEach((fsPath) => {
|
|
74
|
+
if (!fsPath.includes("node_modules"))
|
|
75
|
+
runner.moduleCache.delete(fsPath);
|
|
76
|
+
});
|
|
77
|
+
return Promise.all(files.map((file) => runner.executeId(file)));
|
|
78
|
+
}
|
|
79
|
+
async function notifyListeners(runner, event, data) {
|
|
80
|
+
const maps = getCache(runner);
|
|
81
|
+
const cbs = maps.customListenersMap.get(event);
|
|
82
|
+
if (cbs)
|
|
83
|
+
await Promise.all(cbs.map((cb) => cb(data)));
|
|
84
|
+
}
|
|
85
|
+
async function queueUpdate(runner, p) {
|
|
86
|
+
const maps = getCache(runner);
|
|
87
|
+
maps.queued.push(p);
|
|
88
|
+
if (!maps.pending) {
|
|
89
|
+
maps.pending = true;
|
|
90
|
+
await Promise.resolve();
|
|
91
|
+
maps.pending = false;
|
|
92
|
+
const loading = [...maps.queued];
|
|
93
|
+
maps.queued = [];
|
|
94
|
+
(await Promise.all(loading)).forEach((fn) => fn && fn());
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async function fetchUpdate(runner, { path, acceptedPath }) {
|
|
98
|
+
path = utils.normalizeRequestId(path);
|
|
99
|
+
acceptedPath = utils.normalizeRequestId(acceptedPath);
|
|
100
|
+
const maps = getCache(runner);
|
|
101
|
+
const mod = maps.hotModulesMap.get(path);
|
|
102
|
+
if (!mod) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const isSelfUpdate = path === acceptedPath;
|
|
106
|
+
let fetchedModule;
|
|
107
|
+
const qualifiedCallbacks = mod.callbacks.filter(
|
|
108
|
+
({ deps }) => deps.includes(acceptedPath)
|
|
109
|
+
);
|
|
110
|
+
if (isSelfUpdate || qualifiedCallbacks.length > 0) {
|
|
111
|
+
const disposer = maps.disposeMap.get(acceptedPath);
|
|
112
|
+
if (disposer)
|
|
113
|
+
await disposer(maps.dataMap.get(acceptedPath));
|
|
114
|
+
try {
|
|
115
|
+
[fetchedModule] = await reload(runner, [acceptedPath]);
|
|
116
|
+
} catch (e) {
|
|
117
|
+
warnFailedFetch(e, acceptedPath);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return () => {
|
|
121
|
+
for (const { deps, fn } of qualifiedCallbacks)
|
|
122
|
+
fn(deps.map((dep) => dep === acceptedPath ? fetchedModule : void 0));
|
|
123
|
+
const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`;
|
|
124
|
+
console.log(`${c.cyan("[vite-node]")} hot updated: ${loggedPath}`);
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function warnFailedFetch(err, path) {
|
|
128
|
+
if (!err.message.match("fetch"))
|
|
129
|
+
console.error(err);
|
|
130
|
+
console.error(
|
|
131
|
+
`[hmr] Failed to reload ${path}. This could be due to syntax errors or importing non-existent modules. (see errors above)`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
async function handleMessage(runner, emitter, files, payload) {
|
|
135
|
+
const maps = getCache(runner);
|
|
136
|
+
switch (payload.type) {
|
|
137
|
+
case "connected":
|
|
138
|
+
sendMessageBuffer(runner, emitter);
|
|
139
|
+
break;
|
|
140
|
+
case "update":
|
|
141
|
+
await notifyListeners(runner, "vite:beforeUpdate", payload);
|
|
142
|
+
await Promise.all(payload.updates.map((update) => {
|
|
143
|
+
if (update.type === "js-update")
|
|
144
|
+
return queueUpdate(runner, fetchUpdate(runner, update));
|
|
145
|
+
console.error(`${c.cyan("[vite-node]")} no support css hmr.}`);
|
|
146
|
+
return null;
|
|
147
|
+
}));
|
|
148
|
+
await notifyListeners(runner, "vite:afterUpdate", payload);
|
|
149
|
+
break;
|
|
150
|
+
case "full-reload":
|
|
151
|
+
await notifyListeners(runner, "vite:beforeFullReload", payload);
|
|
152
|
+
maps.customListenersMap.delete("vite:beforeFullReload");
|
|
153
|
+
await reload(runner, files);
|
|
154
|
+
break;
|
|
155
|
+
case "custom":
|
|
156
|
+
await notifyListeners(runner, payload.event, payload.data);
|
|
157
|
+
break;
|
|
158
|
+
case "prune":
|
|
159
|
+
await notifyListeners(runner, "vite:beforePrune", payload);
|
|
160
|
+
payload.paths.forEach((path) => {
|
|
161
|
+
const fn = maps.pruneMap.get(path);
|
|
162
|
+
if (fn)
|
|
163
|
+
fn(maps.dataMap.get(path));
|
|
164
|
+
});
|
|
165
|
+
break;
|
|
166
|
+
case "error": {
|
|
167
|
+
await notifyListeners(runner, "vite:error", payload);
|
|
168
|
+
const err = payload.err;
|
|
169
|
+
console.error(`${c.cyan("[vite-node]")} Internal Server Error
|
|
170
|
+
${err.message}
|
|
171
|
+
${err.stack}`);
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function createHotContext(runner, emitter, files, ownerPath) {
|
|
177
|
+
debugHmr("createHotContext", ownerPath);
|
|
178
|
+
const maps = getCache(runner);
|
|
179
|
+
if (!maps.dataMap.has(ownerPath))
|
|
180
|
+
maps.dataMap.set(ownerPath, {});
|
|
181
|
+
const mod = maps.hotModulesMap.get(ownerPath);
|
|
182
|
+
if (mod)
|
|
183
|
+
mod.callbacks = [];
|
|
184
|
+
const newListeners = /* @__PURE__ */ new Map();
|
|
185
|
+
maps.ctxToListenersMap.set(ownerPath, newListeners);
|
|
186
|
+
function acceptDeps(deps, callback = () => {
|
|
187
|
+
}) {
|
|
188
|
+
const mod2 = maps.hotModulesMap.get(ownerPath) || {
|
|
189
|
+
id: ownerPath,
|
|
190
|
+
callbacks: []
|
|
191
|
+
};
|
|
192
|
+
mod2.callbacks.push({
|
|
193
|
+
deps,
|
|
194
|
+
fn: callback
|
|
195
|
+
});
|
|
196
|
+
maps.hotModulesMap.set(ownerPath, mod2);
|
|
197
|
+
}
|
|
198
|
+
const hot = {
|
|
199
|
+
get data() {
|
|
200
|
+
return maps.dataMap.get(ownerPath);
|
|
201
|
+
},
|
|
202
|
+
acceptExports(_, callback) {
|
|
203
|
+
acceptDeps([ownerPath], callback && (([mod2]) => callback(mod2)));
|
|
204
|
+
},
|
|
205
|
+
accept(deps, callback) {
|
|
206
|
+
if (typeof deps === "function" || !deps) {
|
|
207
|
+
acceptDeps([ownerPath], ([mod2]) => deps && deps(mod2));
|
|
208
|
+
} else if (typeof deps === "string") {
|
|
209
|
+
acceptDeps([deps], ([mod2]) => callback && callback(mod2));
|
|
210
|
+
} else if (Array.isArray(deps)) {
|
|
211
|
+
acceptDeps(deps, callback);
|
|
212
|
+
} else {
|
|
213
|
+
throw new TypeError("invalid hot.accept() usage.");
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
dispose(cb) {
|
|
217
|
+
maps.disposeMap.set(ownerPath, cb);
|
|
218
|
+
},
|
|
219
|
+
prune(cb) {
|
|
220
|
+
maps.pruneMap.set(ownerPath, cb);
|
|
221
|
+
},
|
|
222
|
+
invalidate() {
|
|
223
|
+
notifyListeners(runner, "vite:invalidate", { path: ownerPath, message: void 0 });
|
|
224
|
+
return reload(runner, files);
|
|
225
|
+
},
|
|
226
|
+
on(event, cb) {
|
|
227
|
+
const addToMap = (map) => {
|
|
228
|
+
const existing = map.get(event) || [];
|
|
229
|
+
existing.push(cb);
|
|
230
|
+
map.set(event, existing);
|
|
231
|
+
};
|
|
232
|
+
addToMap(maps.customListenersMap);
|
|
233
|
+
addToMap(newListeners);
|
|
234
|
+
},
|
|
235
|
+
send(event, data) {
|
|
236
|
+
maps.messageBuffer.push(JSON.stringify({ type: "custom", event, data }));
|
|
237
|
+
sendMessageBuffer(runner, emitter);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
return hot;
|
|
241
|
+
}
|
|
16
242
|
|
|
17
|
-
exports.createHmrEmitter =
|
|
18
|
-
exports.createHotContext =
|
|
19
|
-
exports.getCache =
|
|
20
|
-
exports.handleMessage =
|
|
21
|
-
exports.reload =
|
|
22
|
-
exports.sendMessageBuffer =
|
|
23
|
-
exports.viteNodeHmrPlugin =
|
|
243
|
+
exports.createHmrEmitter = createHmrEmitter;
|
|
244
|
+
exports.createHotContext = createHotContext;
|
|
245
|
+
exports.getCache = getCache;
|
|
246
|
+
exports.handleMessage = handleMessage;
|
|
247
|
+
exports.reload = reload;
|
|
248
|
+
exports.sendMessageBuffer = sendMessageBuffer;
|
|
249
|
+
exports.viteNodeHmrPlugin = viteNodeHmrPlugin;
|
package/dist/hmr.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EventEmitter } from 'node:events';
|
|
2
2
|
import { HMRPayload, Plugin } from 'vite';
|
|
3
|
-
import {
|
|
4
|
-
import './
|
|
3
|
+
import { C as CustomEventMap, a as ViteNodeRunner, H as HMRPayload$1, b as HotContext } from './index-6fb787b2.js';
|
|
4
|
+
import './trace-mapping.d-e677e8f4.js';
|
|
5
5
|
|
|
6
6
|
type EventType = string | symbol;
|
|
7
7
|
type Handler<T = unknown> = (event: T) => void;
|
package/dist/hmr.mjs
CHANGED
|
@@ -1,9 +1,241 @@
|
|
|
1
|
-
|
|
2
|
-
import '
|
|
3
|
-
import '
|
|
4
|
-
import '
|
|
5
|
-
import './utils.mjs';
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import c from 'picocolors';
|
|
3
|
+
import createDebug from 'debug';
|
|
4
|
+
import { normalizeRequestId } from './utils.mjs';
|
|
6
5
|
import 'node:url';
|
|
7
6
|
import 'node:module';
|
|
8
7
|
import 'node:fs';
|
|
9
8
|
import 'pathe';
|
|
9
|
+
|
|
10
|
+
function createHmrEmitter() {
|
|
11
|
+
const emitter = new EventEmitter();
|
|
12
|
+
return emitter;
|
|
13
|
+
}
|
|
14
|
+
function viteNodeHmrPlugin() {
|
|
15
|
+
const emitter = createHmrEmitter();
|
|
16
|
+
return {
|
|
17
|
+
name: "vite-node:hmr",
|
|
18
|
+
config() {
|
|
19
|
+
if (process.platform === "darwin" && process.env.VITE_TEST_WATCHER_DEBUG) {
|
|
20
|
+
return {
|
|
21
|
+
server: {
|
|
22
|
+
watch: {
|
|
23
|
+
useFsEvents: false,
|
|
24
|
+
usePolling: false
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
configureServer(server) {
|
|
31
|
+
const _send = server.ws.send;
|
|
32
|
+
server.emitter = emitter;
|
|
33
|
+
server.ws.send = function(payload) {
|
|
34
|
+
_send(payload);
|
|
35
|
+
emitter.emit("message", payload);
|
|
36
|
+
};
|
|
37
|
+
if (process.env.VITE_TEST_WATCHER_DEBUG) {
|
|
38
|
+
server.watcher.on("ready", () => {
|
|
39
|
+
console.log("[debug] watcher is ready");
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const debugHmr = createDebug("vite-node:hmr");
|
|
47
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
48
|
+
function getCache(runner) {
|
|
49
|
+
if (!cache.has(runner)) {
|
|
50
|
+
cache.set(runner, {
|
|
51
|
+
hotModulesMap: /* @__PURE__ */ new Map(),
|
|
52
|
+
dataMap: /* @__PURE__ */ new Map(),
|
|
53
|
+
disposeMap: /* @__PURE__ */ new Map(),
|
|
54
|
+
pruneMap: /* @__PURE__ */ new Map(),
|
|
55
|
+
customListenersMap: /* @__PURE__ */ new Map(),
|
|
56
|
+
ctxToListenersMap: /* @__PURE__ */ new Map(),
|
|
57
|
+
messageBuffer: [],
|
|
58
|
+
isFirstUpdate: false,
|
|
59
|
+
pending: false,
|
|
60
|
+
queued: []
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return cache.get(runner);
|
|
64
|
+
}
|
|
65
|
+
function sendMessageBuffer(runner, emitter) {
|
|
66
|
+
const maps = getCache(runner);
|
|
67
|
+
maps.messageBuffer.forEach((msg) => emitter.emit("custom", msg));
|
|
68
|
+
maps.messageBuffer.length = 0;
|
|
69
|
+
}
|
|
70
|
+
async function reload(runner, files) {
|
|
71
|
+
Array.from(runner.moduleCache.keys()).forEach((fsPath) => {
|
|
72
|
+
if (!fsPath.includes("node_modules"))
|
|
73
|
+
runner.moduleCache.delete(fsPath);
|
|
74
|
+
});
|
|
75
|
+
return Promise.all(files.map((file) => runner.executeId(file)));
|
|
76
|
+
}
|
|
77
|
+
async function notifyListeners(runner, event, data) {
|
|
78
|
+
const maps = getCache(runner);
|
|
79
|
+
const cbs = maps.customListenersMap.get(event);
|
|
80
|
+
if (cbs)
|
|
81
|
+
await Promise.all(cbs.map((cb) => cb(data)));
|
|
82
|
+
}
|
|
83
|
+
async function queueUpdate(runner, p) {
|
|
84
|
+
const maps = getCache(runner);
|
|
85
|
+
maps.queued.push(p);
|
|
86
|
+
if (!maps.pending) {
|
|
87
|
+
maps.pending = true;
|
|
88
|
+
await Promise.resolve();
|
|
89
|
+
maps.pending = false;
|
|
90
|
+
const loading = [...maps.queued];
|
|
91
|
+
maps.queued = [];
|
|
92
|
+
(await Promise.all(loading)).forEach((fn) => fn && fn());
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function fetchUpdate(runner, { path, acceptedPath }) {
|
|
96
|
+
path = normalizeRequestId(path);
|
|
97
|
+
acceptedPath = normalizeRequestId(acceptedPath);
|
|
98
|
+
const maps = getCache(runner);
|
|
99
|
+
const mod = maps.hotModulesMap.get(path);
|
|
100
|
+
if (!mod) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const isSelfUpdate = path === acceptedPath;
|
|
104
|
+
let fetchedModule;
|
|
105
|
+
const qualifiedCallbacks = mod.callbacks.filter(
|
|
106
|
+
({ deps }) => deps.includes(acceptedPath)
|
|
107
|
+
);
|
|
108
|
+
if (isSelfUpdate || qualifiedCallbacks.length > 0) {
|
|
109
|
+
const disposer = maps.disposeMap.get(acceptedPath);
|
|
110
|
+
if (disposer)
|
|
111
|
+
await disposer(maps.dataMap.get(acceptedPath));
|
|
112
|
+
try {
|
|
113
|
+
[fetchedModule] = await reload(runner, [acceptedPath]);
|
|
114
|
+
} catch (e) {
|
|
115
|
+
warnFailedFetch(e, acceptedPath);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return () => {
|
|
119
|
+
for (const { deps, fn } of qualifiedCallbacks)
|
|
120
|
+
fn(deps.map((dep) => dep === acceptedPath ? fetchedModule : void 0));
|
|
121
|
+
const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`;
|
|
122
|
+
console.log(`${c.cyan("[vite-node]")} hot updated: ${loggedPath}`);
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function warnFailedFetch(err, path) {
|
|
126
|
+
if (!err.message.match("fetch"))
|
|
127
|
+
console.error(err);
|
|
128
|
+
console.error(
|
|
129
|
+
`[hmr] Failed to reload ${path}. This could be due to syntax errors or importing non-existent modules. (see errors above)`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
async function handleMessage(runner, emitter, files, payload) {
|
|
133
|
+
const maps = getCache(runner);
|
|
134
|
+
switch (payload.type) {
|
|
135
|
+
case "connected":
|
|
136
|
+
sendMessageBuffer(runner, emitter);
|
|
137
|
+
break;
|
|
138
|
+
case "update":
|
|
139
|
+
await notifyListeners(runner, "vite:beforeUpdate", payload);
|
|
140
|
+
await Promise.all(payload.updates.map((update) => {
|
|
141
|
+
if (update.type === "js-update")
|
|
142
|
+
return queueUpdate(runner, fetchUpdate(runner, update));
|
|
143
|
+
console.error(`${c.cyan("[vite-node]")} no support css hmr.}`);
|
|
144
|
+
return null;
|
|
145
|
+
}));
|
|
146
|
+
await notifyListeners(runner, "vite:afterUpdate", payload);
|
|
147
|
+
break;
|
|
148
|
+
case "full-reload":
|
|
149
|
+
await notifyListeners(runner, "vite:beforeFullReload", payload);
|
|
150
|
+
maps.customListenersMap.delete("vite:beforeFullReload");
|
|
151
|
+
await reload(runner, files);
|
|
152
|
+
break;
|
|
153
|
+
case "custom":
|
|
154
|
+
await notifyListeners(runner, payload.event, payload.data);
|
|
155
|
+
break;
|
|
156
|
+
case "prune":
|
|
157
|
+
await notifyListeners(runner, "vite:beforePrune", payload);
|
|
158
|
+
payload.paths.forEach((path) => {
|
|
159
|
+
const fn = maps.pruneMap.get(path);
|
|
160
|
+
if (fn)
|
|
161
|
+
fn(maps.dataMap.get(path));
|
|
162
|
+
});
|
|
163
|
+
break;
|
|
164
|
+
case "error": {
|
|
165
|
+
await notifyListeners(runner, "vite:error", payload);
|
|
166
|
+
const err = payload.err;
|
|
167
|
+
console.error(`${c.cyan("[vite-node]")} Internal Server Error
|
|
168
|
+
${err.message}
|
|
169
|
+
${err.stack}`);
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function createHotContext(runner, emitter, files, ownerPath) {
|
|
175
|
+
debugHmr("createHotContext", ownerPath);
|
|
176
|
+
const maps = getCache(runner);
|
|
177
|
+
if (!maps.dataMap.has(ownerPath))
|
|
178
|
+
maps.dataMap.set(ownerPath, {});
|
|
179
|
+
const mod = maps.hotModulesMap.get(ownerPath);
|
|
180
|
+
if (mod)
|
|
181
|
+
mod.callbacks = [];
|
|
182
|
+
const newListeners = /* @__PURE__ */ new Map();
|
|
183
|
+
maps.ctxToListenersMap.set(ownerPath, newListeners);
|
|
184
|
+
function acceptDeps(deps, callback = () => {
|
|
185
|
+
}) {
|
|
186
|
+
const mod2 = maps.hotModulesMap.get(ownerPath) || {
|
|
187
|
+
id: ownerPath,
|
|
188
|
+
callbacks: []
|
|
189
|
+
};
|
|
190
|
+
mod2.callbacks.push({
|
|
191
|
+
deps,
|
|
192
|
+
fn: callback
|
|
193
|
+
});
|
|
194
|
+
maps.hotModulesMap.set(ownerPath, mod2);
|
|
195
|
+
}
|
|
196
|
+
const hot = {
|
|
197
|
+
get data() {
|
|
198
|
+
return maps.dataMap.get(ownerPath);
|
|
199
|
+
},
|
|
200
|
+
acceptExports(_, callback) {
|
|
201
|
+
acceptDeps([ownerPath], callback && (([mod2]) => callback(mod2)));
|
|
202
|
+
},
|
|
203
|
+
accept(deps, callback) {
|
|
204
|
+
if (typeof deps === "function" || !deps) {
|
|
205
|
+
acceptDeps([ownerPath], ([mod2]) => deps && deps(mod2));
|
|
206
|
+
} else if (typeof deps === "string") {
|
|
207
|
+
acceptDeps([deps], ([mod2]) => callback && callback(mod2));
|
|
208
|
+
} else if (Array.isArray(deps)) {
|
|
209
|
+
acceptDeps(deps, callback);
|
|
210
|
+
} else {
|
|
211
|
+
throw new TypeError("invalid hot.accept() usage.");
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
dispose(cb) {
|
|
215
|
+
maps.disposeMap.set(ownerPath, cb);
|
|
216
|
+
},
|
|
217
|
+
prune(cb) {
|
|
218
|
+
maps.pruneMap.set(ownerPath, cb);
|
|
219
|
+
},
|
|
220
|
+
invalidate() {
|
|
221
|
+
notifyListeners(runner, "vite:invalidate", { path: ownerPath, message: void 0 });
|
|
222
|
+
return reload(runner, files);
|
|
223
|
+
},
|
|
224
|
+
on(event, cb) {
|
|
225
|
+
const addToMap = (map) => {
|
|
226
|
+
const existing = map.get(event) || [];
|
|
227
|
+
existing.push(cb);
|
|
228
|
+
map.set(event, existing);
|
|
229
|
+
};
|
|
230
|
+
addToMap(maps.customListenersMap);
|
|
231
|
+
addToMap(newListeners);
|
|
232
|
+
},
|
|
233
|
+
send(event, data) {
|
|
234
|
+
maps.messageBuffer.push(JSON.stringify({ type: "custom", event, data }));
|
|
235
|
+
sendMessageBuffer(runner, emitter);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
return hot;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export { createHmrEmitter, createHotContext, getCache, handleMessage, reload, sendMessageBuffer, viteNodeHmrPlugin };
|