This commit is contained in:
2025-04-17 19:16:42 +02:00
parent 94a90edabd
commit ab2db755ef
88 changed files with 8427 additions and 10994 deletions

View File

@@ -383,12 +383,15 @@ class HMRContext {
decline() {
}
invalidate(message) {
const firstInvalidatedBy = this.hmrClient.currentFirstInvalidatedBy ?? this.ownerPath;
this.hmrClient.notifyListeners("vite:invalidate", {
path: this.ownerPath,
message
message,
firstInvalidatedBy
}), this.send("vite:invalidate", {
path: this.ownerPath,
message
message,
firstInvalidatedBy
}), this.hmrClient.logger.debug(
`invalidate ${this.ownerPath}${message ? `: ${message}` : ""}`
);
@@ -439,6 +442,7 @@ class HMRClient {
dataMap = /* @__PURE__ */ new Map();
customListenersMap = /* @__PURE__ */ new Map();
ctxToListenersMap = /* @__PURE__ */ new Map();
currentFirstInvalidatedBy;
async notifyListeners(event, data) {
const cbs = this.customListenersMap.get(event);
cbs && await Promise.allSettled(cbs.map((cb) => cb(data)));
@@ -466,7 +470,7 @@ class HMRClient {
});
}
warnFailedUpdate(err, path) {
err.message.includes("fetch") || this.logger.error(err), this.logger.error(
(!(err instanceof Error) || !err.message.includes("fetch")) && this.logger.error(err), this.logger.error(
`Failed to reload ${path}. This could be due to syntax errors or importing non-existent modules. (see errors above)`
);
}
@@ -485,7 +489,7 @@ class HMRClient {
}
}
async fetchUpdate(update) {
const { path, acceptedPath } = update, mod = this.hotModulesMap.get(path);
const { path, acceptedPath, firstInvalidatedBy } = update, mod = this.hotModulesMap.get(path);
if (!mod)
return;
let fetchedModule;
@@ -502,12 +506,19 @@ class HMRClient {
}
}
return () => {
for (const { deps, fn } of qualifiedCallbacks)
fn(
deps.map((dep) => dep === acceptedPath ? fetchedModule : void 0)
);
const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`;
this.logger.debug(`hot updated: ${loggedPath}`);
try {
this.currentFirstInvalidatedBy = firstInvalidatedBy;
for (const { deps, fn } of qualifiedCallbacks)
fn(
deps.map(
(dep) => dep === acceptedPath ? fetchedModule : void 0
)
);
const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`;
this.logger.debug(`hot updated: ${loggedPath}`);
} finally {
this.currentFirstInvalidatedBy = void 0;
}
};
}
}
@@ -722,67 +733,9 @@ const createInvokeableTransport = (transport) => {
debug: (...msg) => console.log("[vite]", ...msg),
error: (error) => console.log("[vite]", error)
};
function createHMRHandler(runner) {
function createHMRHandler(handler) {
const queue = new Queue();
return (payload) => queue.enqueue(() => handleHotPayload(runner, payload));
}
async function handleHotPayload(runner, payload) {
const hmrClient = runner.hmrClient;
if (!(!hmrClient || runner.isClosed()))
switch (payload.type) {
case "connected":
hmrClient.logger.debug("connected.");
break;
case "update":
await hmrClient.notifyListeners("vite:beforeUpdate", payload), await Promise.all(
payload.updates.map(async (update) => {
if (update.type === "js-update")
return update.acceptedPath = unwrapId(update.acceptedPath), update.path = unwrapId(update.path), hmrClient.queueUpdate(update);
hmrClient.logger.error("css hmr is not supported in runner mode.");
})
), await hmrClient.notifyListeners("vite:afterUpdate", payload);
break;
case "custom": {
await hmrClient.notifyListeners(payload.event, payload.data);
break;
}
case "full-reload": {
const { triggeredBy } = payload, clearEntrypointUrls = triggeredBy ? getModulesEntrypoints(
runner,
getModulesByFile(runner, slash(triggeredBy))
) : findAllEntrypoints(runner);
if (!clearEntrypointUrls.size) break;
hmrClient.logger.debug("program reload"), await hmrClient.notifyListeners("vite:beforeFullReload", payload), runner.evaluatedModules.clear();
for (const url of clearEntrypointUrls)
try {
await runner.import(url);
} catch (err) {
err.code !== ERR_OUTDATED_OPTIMIZED_DEP && hmrClient.logger.error(
`An error happened during full reload
${err.message}
${err.stack}`
);
}
break;
}
case "prune":
await hmrClient.notifyListeners("vite:beforePrune", payload), await hmrClient.prunePaths(payload.paths);
break;
case "error": {
await hmrClient.notifyListeners("vite:error", payload);
const err = payload.err;
hmrClient.logger.error(
`Internal Server Error
${err.message}
${err.stack}`
);
break;
}
case "ping":
break;
default:
return payload;
}
return (payload) => queue.enqueue(() => handler(payload));
}
class Queue {
queue = [];
@@ -805,6 +758,66 @@ class Queue {
}), !0) : !1;
}
}
function createHMRHandlerForRunner(runner) {
return createHMRHandler(async (payload) => {
const hmrClient = runner.hmrClient;
if (!(!hmrClient || runner.isClosed()))
switch (payload.type) {
case "connected":
hmrClient.logger.debug("connected.");
break;
case "update":
await hmrClient.notifyListeners("vite:beforeUpdate", payload), await Promise.all(
payload.updates.map(async (update) => {
if (update.type === "js-update")
return update.acceptedPath = unwrapId(update.acceptedPath), update.path = unwrapId(update.path), hmrClient.queueUpdate(update);
hmrClient.logger.error("css hmr is not supported in runner mode.");
})
), await hmrClient.notifyListeners("vite:afterUpdate", payload);
break;
case "custom": {
await hmrClient.notifyListeners(payload.event, payload.data);
break;
}
case "full-reload": {
const { triggeredBy } = payload, clearEntrypointUrls = triggeredBy ? getModulesEntrypoints(
runner,
getModulesByFile(runner, slash(triggeredBy))
) : findAllEntrypoints(runner);
if (!clearEntrypointUrls.size) break;
hmrClient.logger.debug("program reload"), await hmrClient.notifyListeners("vite:beforeFullReload", payload), runner.evaluatedModules.clear();
for (const url of clearEntrypointUrls)
try {
await runner.import(url);
} catch (err) {
err.code !== ERR_OUTDATED_OPTIMIZED_DEP && hmrClient.logger.error(
`An error happened during full reload
${err.message}
${err.stack}`
);
}
break;
}
case "prune":
await hmrClient.notifyListeners("vite:beforePrune", payload), await hmrClient.prunePaths(payload.paths);
break;
case "error": {
await hmrClient.notifyListeners("vite:error", payload);
const err = payload.err;
hmrClient.logger.error(
`Internal Server Error
${err.message}
${err.stack}`
);
break;
}
case "ping":
break;
default:
return payload;
}
});
}
function getModulesByFile(runner, file) {
const nodes = runner.evaluatedModules.getModulesByFile(file);
return nodes ? [...nodes].map((node) => node.id) : [];
@@ -1078,7 +1091,7 @@ class ModuleRunner {
throw new Error(
"HMR is not supported by this runner transport, but `hmr` option was set to true"
);
this.transport.connect(createHMRHandler(this));
this.transport.connect(createHMRHandlerForRunner(this));
} else
this.transport.connect?.();
options.sourcemapInterceptor !== !1 && (this.resetSourceMapSupport = enableSourceMapSupport(this));