vite-node 0.15.2 → 0.17.1
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/chunk-client.cjs +25 -10
- package/dist/chunk-client.mjs +25 -10
- package/dist/chunk-hmr.cjs +233 -0
- package/dist/chunk-hmr.mjs +221 -0
- package/dist/chunk-server.cjs +1 -20
- package/dist/chunk-server.mjs +1 -20
- package/dist/chunk-utils.cjs +3 -3
- package/dist/chunk-utils.mjs +4 -4
- package/dist/cli.cjs +21 -35
- package/dist/cli.d.ts +1 -1
- package/dist/cli.mjs +22 -36
- package/dist/client.d.ts +1 -1
- package/dist/hmr.cjs +18 -0
- package/dist/hmr.d.ts +57 -0
- package/dist/hmr.mjs +4 -0
- package/dist/index.d.ts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/{types-93bdaf32.d.ts → types-b59fb161.d.ts} +104 -1
- package/dist/types.d.ts +1 -1
- package/dist/utils.d.ts +1 -1
- package/package.json +7 -2
- package/cli.d.ts +0 -8
- package/client.d.ts +0 -37
- package/index.d.ts +0 -1
- package/server.d.ts +0 -26
- package/types.d.ts +0 -68
- package/utils.d.ts +0 -10
package/dist/chunk-client.cjs
CHANGED
|
@@ -91,31 +91,34 @@ class ViteNodeRunner {
|
|
|
91
91
|
return await this.cachedRequest(id, []);
|
|
92
92
|
}
|
|
93
93
|
async cachedRequest(rawId, callstack) {
|
|
94
|
-
var _a, _b;
|
|
94
|
+
var _a, _b, _c, _d;
|
|
95
95
|
const id = utils.normalizeRequestId(rawId, this.options.base);
|
|
96
96
|
const fsPath = utils.toFilePath(id, this.root);
|
|
97
|
-
if ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.
|
|
98
|
-
return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.
|
|
97
|
+
if (callstack.includes(fsPath) && ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.exports))
|
|
98
|
+
return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.exports;
|
|
99
|
+
if ((_c = this.moduleCache.get(fsPath)) == null ? void 0 : _c.promise)
|
|
100
|
+
return (_d = this.moduleCache.get(fsPath)) == null ? void 0 : _d.promise;
|
|
99
101
|
const promise = this.directRequest(id, fsPath, callstack);
|
|
100
102
|
this.moduleCache.set(fsPath, { promise });
|
|
101
103
|
return await promise;
|
|
102
104
|
}
|
|
103
105
|
async directRequest(id, fsPath, _callstack) {
|
|
104
|
-
const callstack = [..._callstack,
|
|
106
|
+
const callstack = [..._callstack, fsPath];
|
|
105
107
|
const request = async (dep) => {
|
|
106
108
|
var _a;
|
|
109
|
+
const fsPath2 = utils.toFilePath(utils.normalizeRequestId(dep, this.options.base), this.root);
|
|
107
110
|
const getStack = () => {
|
|
108
111
|
return `stack:
|
|
109
|
-
${[...callstack,
|
|
112
|
+
${[...callstack, fsPath2].reverse().map((p) => `- ${p}`).join("\n")}`;
|
|
110
113
|
};
|
|
111
114
|
let debugTimer;
|
|
112
115
|
if (this.debug)
|
|
113
|
-
debugTimer = setTimeout(() => this.debugLog(() => `module ${
|
|
116
|
+
debugTimer = setTimeout(() => this.debugLog(() => `module ${fsPath2} takes over 2s to load.
|
|
114
117
|
${getStack()}`), 2e3);
|
|
115
118
|
try {
|
|
116
|
-
if (callstack.includes(
|
|
119
|
+
if (callstack.includes(fsPath2)) {
|
|
117
120
|
this.debugLog(() => `circular dependency, ${getStack()}`);
|
|
118
|
-
const depExports = (_a = this.moduleCache.get(
|
|
121
|
+
const depExports = (_a = this.moduleCache.get(fsPath2)) == null ? void 0 : _a.exports;
|
|
119
122
|
if (depExports)
|
|
120
123
|
return depExports;
|
|
121
124
|
throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`);
|
|
@@ -152,9 +155,10 @@ ${getStack()}`), 2e3);
|
|
|
152
155
|
if (transformed == null)
|
|
153
156
|
throw new Error(`[vite-node] Failed to load ${id}`);
|
|
154
157
|
const url$1 = url.pathToFileURL(fsPath).href;
|
|
158
|
+
const meta = { url: url$1 };
|
|
155
159
|
const exports = /* @__PURE__ */ Object.create(null);
|
|
156
160
|
exports[Symbol.toStringTag] = "Module";
|
|
157
|
-
this.moduleCache.set(
|
|
161
|
+
this.moduleCache.set(fsPath, { code: transformed, exports });
|
|
158
162
|
const __filename = url.fileURLToPath(url$1);
|
|
159
163
|
const moduleProxy = {
|
|
160
164
|
set exports(value) {
|
|
@@ -165,12 +169,23 @@ ${getStack()}`), 2e3);
|
|
|
165
169
|
return exports;
|
|
166
170
|
}
|
|
167
171
|
};
|
|
172
|
+
let hotContext;
|
|
173
|
+
if (this.options.createHotContext) {
|
|
174
|
+
Object.defineProperty(meta, "hot", {
|
|
175
|
+
enumerable: true,
|
|
176
|
+
get: () => {
|
|
177
|
+
var _a, _b;
|
|
178
|
+
hotContext || (hotContext = (_b = (_a = this.options).createHotContext) == null ? void 0 : _b.call(_a, this, `/@fs/${fsPath}`));
|
|
179
|
+
return hotContext;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
168
183
|
const context = this.prepareContext({
|
|
169
184
|
__vite_ssr_import__: request,
|
|
170
185
|
__vite_ssr_dynamic_import__: request,
|
|
171
186
|
__vite_ssr_exports__: exports,
|
|
172
187
|
__vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
|
|
173
|
-
__vite_ssr_import_meta__:
|
|
188
|
+
__vite_ssr_import_meta__: meta,
|
|
174
189
|
__vitest_resolve_id__: resolveId,
|
|
175
190
|
require: module$1.createRequire(url$1),
|
|
176
191
|
exports,
|
package/dist/chunk-client.mjs
CHANGED
|
@@ -66,31 +66,34 @@ class ViteNodeRunner {
|
|
|
66
66
|
return await this.cachedRequest(id, []);
|
|
67
67
|
}
|
|
68
68
|
async cachedRequest(rawId, callstack) {
|
|
69
|
-
var _a, _b;
|
|
69
|
+
var _a, _b, _c, _d;
|
|
70
70
|
const id = normalizeRequestId(rawId, this.options.base);
|
|
71
71
|
const fsPath = toFilePath(id, this.root);
|
|
72
|
-
if ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.
|
|
73
|
-
return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.
|
|
72
|
+
if (callstack.includes(fsPath) && ((_a = this.moduleCache.get(fsPath)) == null ? void 0 : _a.exports))
|
|
73
|
+
return (_b = this.moduleCache.get(fsPath)) == null ? void 0 : _b.exports;
|
|
74
|
+
if ((_c = this.moduleCache.get(fsPath)) == null ? void 0 : _c.promise)
|
|
75
|
+
return (_d = this.moduleCache.get(fsPath)) == null ? void 0 : _d.promise;
|
|
74
76
|
const promise = this.directRequest(id, fsPath, callstack);
|
|
75
77
|
this.moduleCache.set(fsPath, { promise });
|
|
76
78
|
return await promise;
|
|
77
79
|
}
|
|
78
80
|
async directRequest(id, fsPath, _callstack) {
|
|
79
|
-
const callstack = [..._callstack,
|
|
81
|
+
const callstack = [..._callstack, fsPath];
|
|
80
82
|
const request = async (dep) => {
|
|
81
83
|
var _a;
|
|
84
|
+
const fsPath2 = toFilePath(normalizeRequestId(dep, this.options.base), this.root);
|
|
82
85
|
const getStack = () => {
|
|
83
86
|
return `stack:
|
|
84
|
-
${[...callstack,
|
|
87
|
+
${[...callstack, fsPath2].reverse().map((p) => `- ${p}`).join("\n")}`;
|
|
85
88
|
};
|
|
86
89
|
let debugTimer;
|
|
87
90
|
if (this.debug)
|
|
88
|
-
debugTimer = setTimeout(() => this.debugLog(() => `module ${
|
|
91
|
+
debugTimer = setTimeout(() => this.debugLog(() => `module ${fsPath2} takes over 2s to load.
|
|
89
92
|
${getStack()}`), 2e3);
|
|
90
93
|
try {
|
|
91
|
-
if (callstack.includes(
|
|
94
|
+
if (callstack.includes(fsPath2)) {
|
|
92
95
|
this.debugLog(() => `circular dependency, ${getStack()}`);
|
|
93
|
-
const depExports = (_a = this.moduleCache.get(
|
|
96
|
+
const depExports = (_a = this.moduleCache.get(fsPath2)) == null ? void 0 : _a.exports;
|
|
94
97
|
if (depExports)
|
|
95
98
|
return depExports;
|
|
96
99
|
throw new Error(`[vite-node] Failed to resolve circular dependency, ${getStack()}`);
|
|
@@ -127,9 +130,10 @@ ${getStack()}`), 2e3);
|
|
|
127
130
|
if (transformed == null)
|
|
128
131
|
throw new Error(`[vite-node] Failed to load ${id}`);
|
|
129
132
|
const url = pathToFileURL(fsPath).href;
|
|
133
|
+
const meta = { url };
|
|
130
134
|
const exports = /* @__PURE__ */ Object.create(null);
|
|
131
135
|
exports[Symbol.toStringTag] = "Module";
|
|
132
|
-
this.moduleCache.set(
|
|
136
|
+
this.moduleCache.set(fsPath, { code: transformed, exports });
|
|
133
137
|
const __filename = fileURLToPath(url);
|
|
134
138
|
const moduleProxy = {
|
|
135
139
|
set exports(value) {
|
|
@@ -140,12 +144,23 @@ ${getStack()}`), 2e3);
|
|
|
140
144
|
return exports;
|
|
141
145
|
}
|
|
142
146
|
};
|
|
147
|
+
let hotContext;
|
|
148
|
+
if (this.options.createHotContext) {
|
|
149
|
+
Object.defineProperty(meta, "hot", {
|
|
150
|
+
enumerable: true,
|
|
151
|
+
get: () => {
|
|
152
|
+
var _a, _b;
|
|
153
|
+
hotContext || (hotContext = (_b = (_a = this.options).createHotContext) == null ? void 0 : _b.call(_a, this, `/@fs/${fsPath}`));
|
|
154
|
+
return hotContext;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
143
158
|
const context = this.prepareContext({
|
|
144
159
|
__vite_ssr_import__: request,
|
|
145
160
|
__vite_ssr_dynamic_import__: request,
|
|
146
161
|
__vite_ssr_exports__: exports,
|
|
147
162
|
__vite_ssr_exportAll__: (obj) => exportAll(exports, obj),
|
|
148
|
-
__vite_ssr_import_meta__:
|
|
163
|
+
__vite_ssr_import_meta__: meta,
|
|
149
164
|
__vitest_resolve_id__: resolveId,
|
|
150
165
|
require: createRequire(url),
|
|
151
166
|
exports,
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var events = require('events');
|
|
4
|
+
var kolorist = require('kolorist');
|
|
5
|
+
var createDebug = require('debug');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
|
|
10
|
+
|
|
11
|
+
function createHmrEmitter() {
|
|
12
|
+
const emitter = new events.EventEmitter();
|
|
13
|
+
return emitter;
|
|
14
|
+
}
|
|
15
|
+
function viteNodeHmrPlugin() {
|
|
16
|
+
const emitter = createHmrEmitter();
|
|
17
|
+
return {
|
|
18
|
+
name: "vite-node:hmr",
|
|
19
|
+
configureServer(server) {
|
|
20
|
+
const _send = server.ws.send;
|
|
21
|
+
server.emitter = emitter;
|
|
22
|
+
server.ws.send = function(payload) {
|
|
23
|
+
_send(payload);
|
|
24
|
+
emitter.emit("message", payload);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const debugHmr = createDebug__default["default"]("vite-node:hmr");
|
|
31
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
32
|
+
function getCache(runner) {
|
|
33
|
+
if (!cache.has(runner)) {
|
|
34
|
+
cache.set(runner, {
|
|
35
|
+
hotModulesMap: /* @__PURE__ */ new Map(),
|
|
36
|
+
dataMap: /* @__PURE__ */ new Map(),
|
|
37
|
+
disposeMap: /* @__PURE__ */ new Map(),
|
|
38
|
+
pruneMap: /* @__PURE__ */ new Map(),
|
|
39
|
+
customListenersMap: /* @__PURE__ */ new Map(),
|
|
40
|
+
ctxToListenersMap: /* @__PURE__ */ new Map(),
|
|
41
|
+
messageBuffer: [],
|
|
42
|
+
isFirstUpdate: false,
|
|
43
|
+
pending: false,
|
|
44
|
+
queued: []
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return cache.get(runner);
|
|
48
|
+
}
|
|
49
|
+
function sendMessageBuffer(runner, emitter) {
|
|
50
|
+
const maps = getCache(runner);
|
|
51
|
+
maps.messageBuffer.forEach((msg) => emitter.emit("custom", msg));
|
|
52
|
+
maps.messageBuffer.length = 0;
|
|
53
|
+
}
|
|
54
|
+
async function reload(runner, files) {
|
|
55
|
+
Array.from(runner.moduleCache.keys()).forEach((fsPath) => {
|
|
56
|
+
if (!fsPath.includes("node_modules"))
|
|
57
|
+
runner.moduleCache.delete(fsPath);
|
|
58
|
+
});
|
|
59
|
+
return Promise.all(files.map((file) => runner.executeId(file)));
|
|
60
|
+
}
|
|
61
|
+
function notifyListeners(runner, event, data) {
|
|
62
|
+
const maps = getCache(runner);
|
|
63
|
+
const cbs = maps.customListenersMap.get(event);
|
|
64
|
+
if (cbs)
|
|
65
|
+
cbs.forEach((cb) => cb(data));
|
|
66
|
+
}
|
|
67
|
+
async function queueUpdate(runner, p) {
|
|
68
|
+
const maps = getCache(runner);
|
|
69
|
+
maps.queued.push(p);
|
|
70
|
+
if (!maps.pending) {
|
|
71
|
+
maps.pending = true;
|
|
72
|
+
await Promise.resolve();
|
|
73
|
+
maps.pending = false;
|
|
74
|
+
const loading = [...maps.queued];
|
|
75
|
+
maps.queued = [];
|
|
76
|
+
(await Promise.all(loading)).forEach((fn) => fn && fn());
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function fetchUpdate(runner, { path, acceptedPath }) {
|
|
80
|
+
const maps = getCache(runner);
|
|
81
|
+
const mod = maps.hotModulesMap.get(path);
|
|
82
|
+
if (!mod) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const moduleMap = /* @__PURE__ */ new Map();
|
|
86
|
+
const isSelfUpdate = path === acceptedPath;
|
|
87
|
+
const modulesToUpdate = /* @__PURE__ */ new Set();
|
|
88
|
+
if (isSelfUpdate) {
|
|
89
|
+
modulesToUpdate.add(path);
|
|
90
|
+
} else {
|
|
91
|
+
for (const { deps } of mod.callbacks) {
|
|
92
|
+
deps.forEach((dep) => {
|
|
93
|
+
if (acceptedPath === dep)
|
|
94
|
+
modulesToUpdate.add(dep);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const qualifiedCallbacks = mod.callbacks.filter(({ deps }) => {
|
|
99
|
+
return deps.some((dep) => modulesToUpdate.has(dep));
|
|
100
|
+
});
|
|
101
|
+
await Promise.all(Array.from(modulesToUpdate).map(async (dep) => {
|
|
102
|
+
const disposer = maps.disposeMap.get(dep);
|
|
103
|
+
if (disposer)
|
|
104
|
+
await disposer(maps.dataMap.get(dep));
|
|
105
|
+
try {
|
|
106
|
+
const newMod = await reload(runner, [dep]);
|
|
107
|
+
moduleMap.set(dep, newMod);
|
|
108
|
+
} catch (e) {
|
|
109
|
+
warnFailedFetch(e, dep);
|
|
110
|
+
}
|
|
111
|
+
}));
|
|
112
|
+
return () => {
|
|
113
|
+
for (const { deps, fn } of qualifiedCallbacks)
|
|
114
|
+
fn(deps.map((dep) => moduleMap.get(dep)));
|
|
115
|
+
const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`;
|
|
116
|
+
console.log(`${kolorist.cyan("[vite-node]")} hot updated: ${loggedPath}`);
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function warnFailedFetch(err, path) {
|
|
120
|
+
if (!err.message.match("fetch"))
|
|
121
|
+
console.error(err);
|
|
122
|
+
console.error(`[hmr] Failed to reload ${path}. This could be due to syntax errors or importing non-existent modules. (see errors above)`);
|
|
123
|
+
}
|
|
124
|
+
async function handleMessage(runner, emitter, files, payload) {
|
|
125
|
+
const maps = getCache(runner);
|
|
126
|
+
switch (payload.type) {
|
|
127
|
+
case "connected":
|
|
128
|
+
sendMessageBuffer(runner, emitter);
|
|
129
|
+
break;
|
|
130
|
+
case "update":
|
|
131
|
+
notifyListeners(runner, "vite:beforeUpdate", payload);
|
|
132
|
+
if (maps.isFirstUpdate) {
|
|
133
|
+
reload(runner, files);
|
|
134
|
+
maps.isFirstUpdate = true;
|
|
135
|
+
}
|
|
136
|
+
payload.updates.forEach((update) => {
|
|
137
|
+
if (update.type === "js-update") {
|
|
138
|
+
queueUpdate(runner, fetchUpdate(runner, update));
|
|
139
|
+
} else {
|
|
140
|
+
console.error(`${kolorist.cyan("[vite-node]")} no support css hmr.}`);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
break;
|
|
144
|
+
case "full-reload":
|
|
145
|
+
reload(runner, files);
|
|
146
|
+
break;
|
|
147
|
+
case "prune":
|
|
148
|
+
payload.paths.forEach((path) => {
|
|
149
|
+
const fn = maps.pruneMap.get(path);
|
|
150
|
+
if (fn)
|
|
151
|
+
fn(maps.dataMap.get(path));
|
|
152
|
+
});
|
|
153
|
+
break;
|
|
154
|
+
case "error": {
|
|
155
|
+
notifyListeners(runner, "vite:error", payload);
|
|
156
|
+
const err = payload.err;
|
|
157
|
+
console.error(`${kolorist.cyan("[vite-node]")} Internal Server Error
|
|
158
|
+
${err.message}
|
|
159
|
+
${err.stack}`);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function createHotContext(runner, emitter, files, ownerPath) {
|
|
165
|
+
debugHmr("createHotContext", ownerPath);
|
|
166
|
+
const maps = getCache(runner);
|
|
167
|
+
if (!maps.dataMap.has(ownerPath))
|
|
168
|
+
maps.dataMap.set(ownerPath, {});
|
|
169
|
+
const mod = maps.hotModulesMap.get(ownerPath);
|
|
170
|
+
if (mod)
|
|
171
|
+
mod.callbacks = [];
|
|
172
|
+
const newListeners = /* @__PURE__ */ new Map();
|
|
173
|
+
maps.ctxToListenersMap.set(ownerPath, newListeners);
|
|
174
|
+
function acceptDeps(deps, callback = () => {
|
|
175
|
+
}) {
|
|
176
|
+
const mod2 = maps.hotModulesMap.get(ownerPath) || {
|
|
177
|
+
id: ownerPath,
|
|
178
|
+
callbacks: []
|
|
179
|
+
};
|
|
180
|
+
mod2.callbacks.push({
|
|
181
|
+
deps,
|
|
182
|
+
fn: callback
|
|
183
|
+
});
|
|
184
|
+
maps.hotModulesMap.set(ownerPath, mod2);
|
|
185
|
+
}
|
|
186
|
+
const hot = {
|
|
187
|
+
get data() {
|
|
188
|
+
return maps.dataMap.get(ownerPath);
|
|
189
|
+
},
|
|
190
|
+
accept(deps, callback) {
|
|
191
|
+
if (typeof deps === "function" || !deps) {
|
|
192
|
+
acceptDeps([ownerPath], ([mod2]) => deps && deps(mod2));
|
|
193
|
+
} else if (typeof deps === "string") {
|
|
194
|
+
acceptDeps([deps], ([mod2]) => callback && callback(mod2));
|
|
195
|
+
} else if (Array.isArray(deps)) {
|
|
196
|
+
acceptDeps(deps, callback);
|
|
197
|
+
} else {
|
|
198
|
+
throw new TypeError("invalid hot.accept() usage.");
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
dispose(cb) {
|
|
202
|
+
maps.disposeMap.set(ownerPath, cb);
|
|
203
|
+
},
|
|
204
|
+
prune(cb) {
|
|
205
|
+
maps.pruneMap.set(ownerPath, cb);
|
|
206
|
+
},
|
|
207
|
+
invalidate() {
|
|
208
|
+
return reload(runner, files);
|
|
209
|
+
},
|
|
210
|
+
on(event, cb) {
|
|
211
|
+
const addToMap = (map) => {
|
|
212
|
+
const existing = map.get(event) || [];
|
|
213
|
+
existing.push(cb);
|
|
214
|
+
map.set(event, existing);
|
|
215
|
+
};
|
|
216
|
+
addToMap(maps.customListenersMap);
|
|
217
|
+
addToMap(newListeners);
|
|
218
|
+
},
|
|
219
|
+
send(event, data) {
|
|
220
|
+
maps.messageBuffer.push(JSON.stringify({ type: "custom", event, data }));
|
|
221
|
+
sendMessageBuffer(runner, emitter);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
return hot;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
exports.createHmrEmitter = createHmrEmitter;
|
|
228
|
+
exports.createHotContext = createHotContext;
|
|
229
|
+
exports.getCache = getCache;
|
|
230
|
+
exports.handleMessage = handleMessage;
|
|
231
|
+
exports.reload = reload;
|
|
232
|
+
exports.sendMessageBuffer = sendMessageBuffer;
|
|
233
|
+
exports.viteNodeHmrPlugin = viteNodeHmrPlugin;
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { cyan } from 'kolorist';
|
|
3
|
+
import createDebug from 'debug';
|
|
4
|
+
|
|
5
|
+
function createHmrEmitter() {
|
|
6
|
+
const emitter = new EventEmitter();
|
|
7
|
+
return emitter;
|
|
8
|
+
}
|
|
9
|
+
function viteNodeHmrPlugin() {
|
|
10
|
+
const emitter = createHmrEmitter();
|
|
11
|
+
return {
|
|
12
|
+
name: "vite-node:hmr",
|
|
13
|
+
configureServer(server) {
|
|
14
|
+
const _send = server.ws.send;
|
|
15
|
+
server.emitter = emitter;
|
|
16
|
+
server.ws.send = function(payload) {
|
|
17
|
+
_send(payload);
|
|
18
|
+
emitter.emit("message", payload);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const debugHmr = createDebug("vite-node:hmr");
|
|
25
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
26
|
+
function getCache(runner) {
|
|
27
|
+
if (!cache.has(runner)) {
|
|
28
|
+
cache.set(runner, {
|
|
29
|
+
hotModulesMap: /* @__PURE__ */ new Map(),
|
|
30
|
+
dataMap: /* @__PURE__ */ new Map(),
|
|
31
|
+
disposeMap: /* @__PURE__ */ new Map(),
|
|
32
|
+
pruneMap: /* @__PURE__ */ new Map(),
|
|
33
|
+
customListenersMap: /* @__PURE__ */ new Map(),
|
|
34
|
+
ctxToListenersMap: /* @__PURE__ */ new Map(),
|
|
35
|
+
messageBuffer: [],
|
|
36
|
+
isFirstUpdate: false,
|
|
37
|
+
pending: false,
|
|
38
|
+
queued: []
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return cache.get(runner);
|
|
42
|
+
}
|
|
43
|
+
function sendMessageBuffer(runner, emitter) {
|
|
44
|
+
const maps = getCache(runner);
|
|
45
|
+
maps.messageBuffer.forEach((msg) => emitter.emit("custom", msg));
|
|
46
|
+
maps.messageBuffer.length = 0;
|
|
47
|
+
}
|
|
48
|
+
async function reload(runner, files) {
|
|
49
|
+
Array.from(runner.moduleCache.keys()).forEach((fsPath) => {
|
|
50
|
+
if (!fsPath.includes("node_modules"))
|
|
51
|
+
runner.moduleCache.delete(fsPath);
|
|
52
|
+
});
|
|
53
|
+
return Promise.all(files.map((file) => runner.executeId(file)));
|
|
54
|
+
}
|
|
55
|
+
function notifyListeners(runner, event, data) {
|
|
56
|
+
const maps = getCache(runner);
|
|
57
|
+
const cbs = maps.customListenersMap.get(event);
|
|
58
|
+
if (cbs)
|
|
59
|
+
cbs.forEach((cb) => cb(data));
|
|
60
|
+
}
|
|
61
|
+
async function queueUpdate(runner, p) {
|
|
62
|
+
const maps = getCache(runner);
|
|
63
|
+
maps.queued.push(p);
|
|
64
|
+
if (!maps.pending) {
|
|
65
|
+
maps.pending = true;
|
|
66
|
+
await Promise.resolve();
|
|
67
|
+
maps.pending = false;
|
|
68
|
+
const loading = [...maps.queued];
|
|
69
|
+
maps.queued = [];
|
|
70
|
+
(await Promise.all(loading)).forEach((fn) => fn && fn());
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function fetchUpdate(runner, { path, acceptedPath }) {
|
|
74
|
+
const maps = getCache(runner);
|
|
75
|
+
const mod = maps.hotModulesMap.get(path);
|
|
76
|
+
if (!mod) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const moduleMap = /* @__PURE__ */ new Map();
|
|
80
|
+
const isSelfUpdate = path === acceptedPath;
|
|
81
|
+
const modulesToUpdate = /* @__PURE__ */ new Set();
|
|
82
|
+
if (isSelfUpdate) {
|
|
83
|
+
modulesToUpdate.add(path);
|
|
84
|
+
} else {
|
|
85
|
+
for (const { deps } of mod.callbacks) {
|
|
86
|
+
deps.forEach((dep) => {
|
|
87
|
+
if (acceptedPath === dep)
|
|
88
|
+
modulesToUpdate.add(dep);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const qualifiedCallbacks = mod.callbacks.filter(({ deps }) => {
|
|
93
|
+
return deps.some((dep) => modulesToUpdate.has(dep));
|
|
94
|
+
});
|
|
95
|
+
await Promise.all(Array.from(modulesToUpdate).map(async (dep) => {
|
|
96
|
+
const disposer = maps.disposeMap.get(dep);
|
|
97
|
+
if (disposer)
|
|
98
|
+
await disposer(maps.dataMap.get(dep));
|
|
99
|
+
try {
|
|
100
|
+
const newMod = await reload(runner, [dep]);
|
|
101
|
+
moduleMap.set(dep, newMod);
|
|
102
|
+
} catch (e) {
|
|
103
|
+
warnFailedFetch(e, dep);
|
|
104
|
+
}
|
|
105
|
+
}));
|
|
106
|
+
return () => {
|
|
107
|
+
for (const { deps, fn } of qualifiedCallbacks)
|
|
108
|
+
fn(deps.map((dep) => moduleMap.get(dep)));
|
|
109
|
+
const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`;
|
|
110
|
+
console.log(`${cyan("[vite-node]")} hot updated: ${loggedPath}`);
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function warnFailedFetch(err, path) {
|
|
114
|
+
if (!err.message.match("fetch"))
|
|
115
|
+
console.error(err);
|
|
116
|
+
console.error(`[hmr] Failed to reload ${path}. This could be due to syntax errors or importing non-existent modules. (see errors above)`);
|
|
117
|
+
}
|
|
118
|
+
async function handleMessage(runner, emitter, files, payload) {
|
|
119
|
+
const maps = getCache(runner);
|
|
120
|
+
switch (payload.type) {
|
|
121
|
+
case "connected":
|
|
122
|
+
sendMessageBuffer(runner, emitter);
|
|
123
|
+
break;
|
|
124
|
+
case "update":
|
|
125
|
+
notifyListeners(runner, "vite:beforeUpdate", payload);
|
|
126
|
+
if (maps.isFirstUpdate) {
|
|
127
|
+
reload(runner, files);
|
|
128
|
+
maps.isFirstUpdate = true;
|
|
129
|
+
}
|
|
130
|
+
payload.updates.forEach((update) => {
|
|
131
|
+
if (update.type === "js-update") {
|
|
132
|
+
queueUpdate(runner, fetchUpdate(runner, update));
|
|
133
|
+
} else {
|
|
134
|
+
console.error(`${cyan("[vite-node]")} no support css hmr.}`);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
break;
|
|
138
|
+
case "full-reload":
|
|
139
|
+
reload(runner, files);
|
|
140
|
+
break;
|
|
141
|
+
case "prune":
|
|
142
|
+
payload.paths.forEach((path) => {
|
|
143
|
+
const fn = maps.pruneMap.get(path);
|
|
144
|
+
if (fn)
|
|
145
|
+
fn(maps.dataMap.get(path));
|
|
146
|
+
});
|
|
147
|
+
break;
|
|
148
|
+
case "error": {
|
|
149
|
+
notifyListeners(runner, "vite:error", payload);
|
|
150
|
+
const err = payload.err;
|
|
151
|
+
console.error(`${cyan("[vite-node]")} Internal Server Error
|
|
152
|
+
${err.message}
|
|
153
|
+
${err.stack}`);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function createHotContext(runner, emitter, files, ownerPath) {
|
|
159
|
+
debugHmr("createHotContext", ownerPath);
|
|
160
|
+
const maps = getCache(runner);
|
|
161
|
+
if (!maps.dataMap.has(ownerPath))
|
|
162
|
+
maps.dataMap.set(ownerPath, {});
|
|
163
|
+
const mod = maps.hotModulesMap.get(ownerPath);
|
|
164
|
+
if (mod)
|
|
165
|
+
mod.callbacks = [];
|
|
166
|
+
const newListeners = /* @__PURE__ */ new Map();
|
|
167
|
+
maps.ctxToListenersMap.set(ownerPath, newListeners);
|
|
168
|
+
function acceptDeps(deps, callback = () => {
|
|
169
|
+
}) {
|
|
170
|
+
const mod2 = maps.hotModulesMap.get(ownerPath) || {
|
|
171
|
+
id: ownerPath,
|
|
172
|
+
callbacks: []
|
|
173
|
+
};
|
|
174
|
+
mod2.callbacks.push({
|
|
175
|
+
deps,
|
|
176
|
+
fn: callback
|
|
177
|
+
});
|
|
178
|
+
maps.hotModulesMap.set(ownerPath, mod2);
|
|
179
|
+
}
|
|
180
|
+
const hot = {
|
|
181
|
+
get data() {
|
|
182
|
+
return maps.dataMap.get(ownerPath);
|
|
183
|
+
},
|
|
184
|
+
accept(deps, callback) {
|
|
185
|
+
if (typeof deps === "function" || !deps) {
|
|
186
|
+
acceptDeps([ownerPath], ([mod2]) => deps && deps(mod2));
|
|
187
|
+
} else if (typeof deps === "string") {
|
|
188
|
+
acceptDeps([deps], ([mod2]) => callback && callback(mod2));
|
|
189
|
+
} else if (Array.isArray(deps)) {
|
|
190
|
+
acceptDeps(deps, callback);
|
|
191
|
+
} else {
|
|
192
|
+
throw new TypeError("invalid hot.accept() usage.");
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
dispose(cb) {
|
|
196
|
+
maps.disposeMap.set(ownerPath, cb);
|
|
197
|
+
},
|
|
198
|
+
prune(cb) {
|
|
199
|
+
maps.pruneMap.set(ownerPath, cb);
|
|
200
|
+
},
|
|
201
|
+
invalidate() {
|
|
202
|
+
return reload(runner, files);
|
|
203
|
+
},
|
|
204
|
+
on(event, cb) {
|
|
205
|
+
const addToMap = (map) => {
|
|
206
|
+
const existing = map.get(event) || [];
|
|
207
|
+
existing.push(cb);
|
|
208
|
+
map.set(event, existing);
|
|
209
|
+
};
|
|
210
|
+
addToMap(maps.customListenersMap);
|
|
211
|
+
addToMap(newListeners);
|
|
212
|
+
},
|
|
213
|
+
send(event, data) {
|
|
214
|
+
maps.messageBuffer.push(JSON.stringify({ type: "custom", event, data }));
|
|
215
|
+
sendMessageBuffer(runner, emitter);
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
return hot;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export { createHmrEmitter as a, createHotContext as c, getCache as g, handleMessage as h, reload as r, sendMessageBuffer as s, viteNodeHmrPlugin as v };
|
package/dist/chunk-server.cjs
CHANGED
|
@@ -95,25 +95,6 @@ function patchWindowsImportPath(path) {
|
|
|
95
95
|
return path;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
var __defProp = Object.defineProperty;
|
|
99
|
-
var __defProps = Object.defineProperties;
|
|
100
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
101
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
102
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
103
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
104
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
105
|
-
var __spreadValues = (a, b) => {
|
|
106
|
-
for (var prop in b || (b = {}))
|
|
107
|
-
if (__hasOwnProp.call(b, prop))
|
|
108
|
-
__defNormalProp(a, prop, b[prop]);
|
|
109
|
-
if (__getOwnPropSymbols)
|
|
110
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
111
|
-
if (__propIsEnum.call(b, prop))
|
|
112
|
-
__defNormalProp(a, prop, b[prop]);
|
|
113
|
-
}
|
|
114
|
-
return a;
|
|
115
|
-
};
|
|
116
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
117
98
|
const debugRequest = createDebug__default["default"]("vite-node:server:request");
|
|
118
99
|
const RealDate = Date;
|
|
119
100
|
class ViteNodeServer {
|
|
@@ -147,7 +128,7 @@ class ViteNodeServer {
|
|
|
147
128
|
async fetchModule(id) {
|
|
148
129
|
if (!this.fetchPromiseMap.has(id)) {
|
|
149
130
|
this.fetchPromiseMap.set(id, this._fetchModule(id).then((r) => {
|
|
150
|
-
return this.options.sourcemap !== true ?
|
|
131
|
+
return this.options.sourcemap !== true ? { ...r, map: void 0 } : r;
|
|
151
132
|
}).finally(() => {
|
|
152
133
|
this.fetchPromiseMap.delete(id);
|
|
153
134
|
}));
|
package/dist/chunk-server.mjs
CHANGED
|
@@ -89,25 +89,6 @@ function patchWindowsImportPath(path) {
|
|
|
89
89
|
return path;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
var __defProp = Object.defineProperty;
|
|
93
|
-
var __defProps = Object.defineProperties;
|
|
94
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
95
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
96
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
97
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
98
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
99
|
-
var __spreadValues = (a, b) => {
|
|
100
|
-
for (var prop in b || (b = {}))
|
|
101
|
-
if (__hasOwnProp.call(b, prop))
|
|
102
|
-
__defNormalProp(a, prop, b[prop]);
|
|
103
|
-
if (__getOwnPropSymbols)
|
|
104
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
105
|
-
if (__propIsEnum.call(b, prop))
|
|
106
|
-
__defNormalProp(a, prop, b[prop]);
|
|
107
|
-
}
|
|
108
|
-
return a;
|
|
109
|
-
};
|
|
110
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
111
92
|
const debugRequest = createDebug("vite-node:server:request");
|
|
112
93
|
const RealDate = Date;
|
|
113
94
|
class ViteNodeServer {
|
|
@@ -141,7 +122,7 @@ class ViteNodeServer {
|
|
|
141
122
|
async fetchModule(id) {
|
|
142
123
|
if (!this.fetchPromiseMap.has(id)) {
|
|
143
124
|
this.fetchPromiseMap.set(id, this._fetchModule(id).then((r) => {
|
|
144
|
-
return this.options.sourcemap !== true ?
|
|
125
|
+
return this.options.sourcemap !== true ? { ...r, map: void 0 } : r;
|
|
145
126
|
}).finally(() => {
|
|
146
127
|
this.fetchPromiseMap.delete(id);
|
|
147
128
|
}));
|