vite-node 3.0.9 → 3.1.0-beta.2

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