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.
@@ -1,237 +0,0 @@
1
- import { EventEmitter } from 'node:events';
2
- import c from 'picocolors';
3
- import createDebug from 'debug';
4
- import { normalizeRequestId } from './utils.mjs';
5
-
6
- function createHmrEmitter() {
7
- const emitter = new EventEmitter();
8
- return emitter;
9
- }
10
- function viteNodeHmrPlugin() {
11
- const emitter = createHmrEmitter();
12
- return {
13
- name: "vite-node:hmr",
14
- config() {
15
- if (process.platform === "darwin" && process.env.VITE_TEST_WATCHER_DEBUG) {
16
- return {
17
- server: {
18
- watch: {
19
- useFsEvents: false,
20
- usePolling: false
21
- }
22
- }
23
- };
24
- }
25
- },
26
- configureServer(server) {
27
- const _send = server.ws.send;
28
- server.emitter = emitter;
29
- server.ws.send = function(payload) {
30
- _send(payload);
31
- emitter.emit("message", payload);
32
- };
33
- if (process.env.VITE_TEST_WATCHER_DEBUG) {
34
- server.watcher.on("ready", () => {
35
- console.log("[debug] watcher is ready");
36
- });
37
- }
38
- }
39
- };
40
- }
41
-
42
- const debugHmr = createDebug("vite-node:hmr");
43
- const cache = /* @__PURE__ */ new WeakMap();
44
- function getCache(runner) {
45
- if (!cache.has(runner)) {
46
- cache.set(runner, {
47
- hotModulesMap: /* @__PURE__ */ new Map(),
48
- dataMap: /* @__PURE__ */ new Map(),
49
- disposeMap: /* @__PURE__ */ new Map(),
50
- pruneMap: /* @__PURE__ */ new Map(),
51
- customListenersMap: /* @__PURE__ */ new Map(),
52
- ctxToListenersMap: /* @__PURE__ */ new Map(),
53
- messageBuffer: [],
54
- isFirstUpdate: false,
55
- pending: false,
56
- queued: []
57
- });
58
- }
59
- return cache.get(runner);
60
- }
61
- function sendMessageBuffer(runner, emitter) {
62
- const maps = getCache(runner);
63
- maps.messageBuffer.forEach((msg) => emitter.emit("custom", msg));
64
- maps.messageBuffer.length = 0;
65
- }
66
- async function reload(runner, files) {
67
- Array.from(runner.moduleCache.keys()).forEach((fsPath) => {
68
- if (!fsPath.includes("node_modules"))
69
- runner.moduleCache.delete(fsPath);
70
- });
71
- return Promise.all(files.map((file) => runner.executeId(file)));
72
- }
73
- async function notifyListeners(runner, event, data) {
74
- const maps = getCache(runner);
75
- const cbs = maps.customListenersMap.get(event);
76
- if (cbs)
77
- await Promise.all(cbs.map((cb) => cb(data)));
78
- }
79
- async function queueUpdate(runner, p) {
80
- const maps = getCache(runner);
81
- maps.queued.push(p);
82
- if (!maps.pending) {
83
- maps.pending = true;
84
- await Promise.resolve();
85
- maps.pending = false;
86
- const loading = [...maps.queued];
87
- maps.queued = [];
88
- (await Promise.all(loading)).forEach((fn) => fn && fn());
89
- }
90
- }
91
- async function fetchUpdate(runner, { path, acceptedPath }) {
92
- path = normalizeRequestId(path);
93
- acceptedPath = normalizeRequestId(acceptedPath);
94
- const maps = getCache(runner);
95
- const mod = maps.hotModulesMap.get(path);
96
- if (!mod) {
97
- return;
98
- }
99
- const isSelfUpdate = path === acceptedPath;
100
- let fetchedModule;
101
- const qualifiedCallbacks = mod.callbacks.filter(
102
- ({ deps }) => deps.includes(acceptedPath)
103
- );
104
- if (isSelfUpdate || qualifiedCallbacks.length > 0) {
105
- const disposer = maps.disposeMap.get(acceptedPath);
106
- if (disposer)
107
- await disposer(maps.dataMap.get(acceptedPath));
108
- try {
109
- [fetchedModule] = await reload(runner, [acceptedPath]);
110
- } catch (e) {
111
- warnFailedFetch(e, acceptedPath);
112
- }
113
- }
114
- return () => {
115
- for (const { deps, fn } of qualifiedCallbacks)
116
- fn(deps.map((dep) => dep === acceptedPath ? fetchedModule : void 0));
117
- const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`;
118
- console.log(`${c.cyan("[vite-node]")} hot updated: ${loggedPath}`);
119
- };
120
- }
121
- function warnFailedFetch(err, path) {
122
- if (!err.message.match("fetch"))
123
- console.error(err);
124
- console.error(
125
- `[hmr] Failed to reload ${path}. This could be due to syntax errors or importing non-existent modules. (see errors above)`
126
- );
127
- }
128
- async function handleMessage(runner, emitter, files, payload) {
129
- const maps = getCache(runner);
130
- switch (payload.type) {
131
- case "connected":
132
- sendMessageBuffer(runner, emitter);
133
- break;
134
- case "update":
135
- await notifyListeners(runner, "vite:beforeUpdate", payload);
136
- await Promise.all(payload.updates.map((update) => {
137
- if (update.type === "js-update")
138
- return queueUpdate(runner, fetchUpdate(runner, update));
139
- console.error(`${c.cyan("[vite-node]")} no support css hmr.}`);
140
- return null;
141
- }));
142
- await notifyListeners(runner, "vite:afterUpdate", payload);
143
- break;
144
- case "full-reload":
145
- await notifyListeners(runner, "vite:beforeFullReload", payload);
146
- maps.customListenersMap.delete("vite:beforeFullReload");
147
- await reload(runner, files);
148
- break;
149
- case "custom":
150
- await notifyListeners(runner, payload.event, payload.data);
151
- break;
152
- case "prune":
153
- await notifyListeners(runner, "vite:beforePrune", payload);
154
- payload.paths.forEach((path) => {
155
- const fn = maps.pruneMap.get(path);
156
- if (fn)
157
- fn(maps.dataMap.get(path));
158
- });
159
- break;
160
- case "error": {
161
- await notifyListeners(runner, "vite:error", payload);
162
- const err = payload.err;
163
- console.error(`${c.cyan("[vite-node]")} Internal Server Error
164
- ${err.message}
165
- ${err.stack}`);
166
- break;
167
- }
168
- }
169
- }
170
- function createHotContext(runner, emitter, files, ownerPath) {
171
- debugHmr("createHotContext", ownerPath);
172
- const maps = getCache(runner);
173
- if (!maps.dataMap.has(ownerPath))
174
- maps.dataMap.set(ownerPath, {});
175
- const mod = maps.hotModulesMap.get(ownerPath);
176
- if (mod)
177
- mod.callbacks = [];
178
- const newListeners = /* @__PURE__ */ new Map();
179
- maps.ctxToListenersMap.set(ownerPath, newListeners);
180
- function acceptDeps(deps, callback = () => {
181
- }) {
182
- const mod2 = maps.hotModulesMap.get(ownerPath) || {
183
- id: ownerPath,
184
- callbacks: []
185
- };
186
- mod2.callbacks.push({
187
- deps,
188
- fn: callback
189
- });
190
- maps.hotModulesMap.set(ownerPath, mod2);
191
- }
192
- const hot = {
193
- get data() {
194
- return maps.dataMap.get(ownerPath);
195
- },
196
- acceptExports(_, callback) {
197
- acceptDeps([ownerPath], callback && (([mod2]) => callback(mod2)));
198
- },
199
- accept(deps, callback) {
200
- if (typeof deps === "function" || !deps) {
201
- acceptDeps([ownerPath], ([mod2]) => deps && deps(mod2));
202
- } else if (typeof deps === "string") {
203
- acceptDeps([deps], ([mod2]) => callback && callback(mod2));
204
- } else if (Array.isArray(deps)) {
205
- acceptDeps(deps, callback);
206
- } else {
207
- throw new TypeError("invalid hot.accept() usage.");
208
- }
209
- },
210
- dispose(cb) {
211
- maps.disposeMap.set(ownerPath, cb);
212
- },
213
- prune(cb) {
214
- maps.pruneMap.set(ownerPath, cb);
215
- },
216
- invalidate() {
217
- notifyListeners(runner, "vite:invalidate", { path: ownerPath, message: void 0 });
218
- return reload(runner, files);
219
- },
220
- on(event, cb) {
221
- const addToMap = (map) => {
222
- const existing = map.get(event) || [];
223
- existing.push(cb);
224
- map.set(event, existing);
225
- };
226
- addToMap(maps.customListenersMap);
227
- addToMap(newListeners);
228
- },
229
- send(event, data) {
230
- maps.messageBuffer.push(JSON.stringify({ type: "custom", event, data }));
231
- sendMessageBuffer(runner, emitter);
232
- }
233
- };
234
- return hot;
235
- }
236
-
237
- export { createHmrEmitter as a, createHotContext as c, getCache as g, handleMessage as h, reload as r, sendMessageBuffer as s, viteNodeHmrPlugin as v };
@@ -1,23 +0,0 @@
1
- declare type GeneratedColumn = number;
2
- declare type SourcesIndex = number;
3
- declare type SourceLine = number;
4
- declare type SourceColumn = number;
5
- declare type NamesIndex = number;
6
- declare type SourceMapSegment = [GeneratedColumn] | [GeneratedColumn, SourcesIndex, SourceLine, SourceColumn] | [GeneratedColumn, SourcesIndex, SourceLine, SourceColumn, NamesIndex];
7
-
8
- interface SourceMapV3 {
9
- file?: string | null;
10
- names: string[];
11
- sourceRoot?: string;
12
- sources: (string | null)[];
13
- sourcesContent?: (string | null)[];
14
- version: 3;
15
- }
16
- interface EncodedSourceMap extends SourceMapV3 {
17
- mappings: string;
18
- }
19
- interface DecodedSourceMap extends SourceMapV3 {
20
- mappings: SourceMapSegment[][];
21
- }
22
-
23
- export { DecodedSourceMap as D, EncodedSourceMap as E };