This commit is contained in:
parent
2e7c0b4893
commit
20cbb45743
2 changed files with 45 additions and 13 deletions
|
|
@ -405,7 +405,7 @@ export class MTPClient {
|
||||||
},
|
},
|
||||||
(error) => emit(normalizedOptions.logger, {
|
(error) => emit(normalizedOptions.logger, {
|
||||||
hint: "error",
|
hint: "error",
|
||||||
type: "error",
|
type: "Error",
|
||||||
error: String(error),
|
error: String(error),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
@ -610,7 +610,7 @@ export class MTPClient {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
emit(this.#options.logger, {
|
emit(this.#options.logger, {
|
||||||
hint: "error",
|
hint: "error",
|
||||||
type: "error",
|
type: "Error",
|
||||||
error: String(error),
|
error: String(error),
|
||||||
direction: "send",
|
direction: "send",
|
||||||
});
|
});
|
||||||
|
|
@ -631,7 +631,7 @@ export class MTPClient {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
emit(this.#options.logger, {
|
emit(this.#options.logger, {
|
||||||
hint: "error",
|
hint: "error",
|
||||||
type: "error",
|
type: "Error",
|
||||||
error: String(error),
|
error: String(error),
|
||||||
direction: "send",
|
direction: "send",
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -369,7 +369,7 @@ export function mtp(options: MTPVitePluginOptions): VitePlugin {
|
||||||
buildStart() {
|
buildStart() {
|
||||||
this.addWatchFile(state.typeMapsPath);
|
this.addWatchFile(state.typeMapsPath);
|
||||||
},
|
},
|
||||||
configureServer(server) {
|
async configureServer(server) {
|
||||||
const wasmPath = path.join(state.outDir, wasmEntryName);
|
const wasmPath = path.join(state.outDir, wasmEntryName);
|
||||||
const wasmUrl = devServerPath(server.config.root, wasmPath);
|
const wasmUrl = devServerPath(server.config.root, wasmPath);
|
||||||
if (wasmUrl) {
|
if (wasmUrl) {
|
||||||
|
|
@ -381,6 +381,7 @@ export function mtp(options: MTPVitePluginOptions): VitePlugin {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
res.setHeader("Content-Type", "application/wasm");
|
res.setHeader("Content-Type", "application/wasm");
|
||||||
|
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||||
res.end(await fs.readFile(wasmPath));
|
res.end(await fs.readFile(wasmPath));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
|
|
@ -388,11 +389,37 @@ export function mtp(options: MTPVitePluginOptions): VitePlugin {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sourceWatchDirs = [
|
||||||
|
"wasm/src",
|
||||||
|
"common/src",
|
||||||
|
"codec/src",
|
||||||
|
"crypto/src",
|
||||||
|
"type-map/src",
|
||||||
|
].map((rel) => path.join(packageRoot, rel));
|
||||||
|
|
||||||
server.watcher.add(state.typeMapsPath);
|
server.watcher.add(state.typeMapsPath);
|
||||||
server.watcher.on("change", async (changedPath) => {
|
for (const dir of sourceWatchDirs) {
|
||||||
if (path.resolve(changedPath) !== state.typeMapsPath) {
|
if (await pathExists(dir)) {
|
||||||
|
server.watcher.add(dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let rebuildTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
const scheduleRebuild = (changedPath: string) => {
|
||||||
|
const resolved = path.resolve(changedPath);
|
||||||
|
const isTypeMap = resolved === state.typeMapsPath;
|
||||||
|
const isSource = sourceWatchDirs.some((dir) => resolved.startsWith(`${dir}${path.sep}`));
|
||||||
|
if (!isTypeMap && !isSource) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (rebuildTimer) {
|
||||||
|
clearTimeout(rebuildTimer);
|
||||||
|
}
|
||||||
|
|
||||||
|
rebuildTimer = setTimeout(() => {
|
||||||
|
rebuildTimer = null;
|
||||||
|
void (async () => {
|
||||||
try {
|
try {
|
||||||
await buildIfNeeded(state, true);
|
await buildIfNeeded(state, true);
|
||||||
server.moduleGraph.invalidateAll();
|
server.moduleGraph.invalidateAll();
|
||||||
|
|
@ -400,7 +427,12 @@ export function mtp(options: MTPVitePluginOptions): VitePlugin {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
server.config.logger.error(error instanceof Error ? error.message : String(error));
|
server.config.logger.error(error instanceof Error ? error.message : String(error));
|
||||||
}
|
}
|
||||||
});
|
})();
|
||||||
|
}, 200);
|
||||||
|
};
|
||||||
|
|
||||||
|
server.watcher.on("change", scheduleRebuild);
|
||||||
|
server.watcher.on("add", scheduleRebuild);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue