Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 7394bc7

Browse files
committed
Force re-build of all plugins on Miniflare#reload()
...regardless of whether options have changed or not, ensuring files (e.g. scripts, .env files, WASM modules) are re-read from disk
1 parent a353f62 commit 7394bc7

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

packages/core/src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export class MiniflareCore<
316316
}
317317

318318
readonly #initPromise: Promise<void>;
319-
async #init(): Promise<void> {
319+
async #init(reloadAll = false): Promise<void> {
320320
// The caller must eventually call #reload() at some point after #init()
321321
this.#ctx.log.debug("Initialising worker...");
322322

@@ -365,13 +365,14 @@ export class MiniflareCore<
365365
// through execution. (NOTE: ??= will only assign on undefined, not false)
366366
this.#watching ??= options.CorePlugin.watch ?? false;
367367

368-
// Build compatibility manager, rebuild all plugins if compatibility data,
369-
// root path or any limits have changed
368+
// Build compatibility manager, rebuild all plugins if reloadAll is set,
369+
// compatibility data, root path or any limits have changed
370370
const { compatibilityDate, compatibilityFlags, globalAsyncIO } =
371371
options.CorePlugin;
372372
let ctxUpdate =
373373
(this.#previousRootPath && this.#previousRootPath !== rootPath) ||
374-
this.#previousGlobalAsyncIO !== globalAsyncIO;
374+
this.#previousGlobalAsyncIO !== globalAsyncIO ||
375+
reloadAll;
375376
this.#previousRootPath = rootPath;
376377

377378
if (this.#compat) {
@@ -930,7 +931,10 @@ export class MiniflareCore<
930931

931932
async reload(): Promise<void> {
932933
await this.#initPromise;
933-
await this.#init();
934+
// Force re-build of all plugins, regardless of whether options have changed
935+
// or not. This ensures files (scripts, .env files, WASM modules, etc.) are
936+
// re-read from disk.
937+
await this.#init(/* reloadAll */ true);
934938
await this.#reload();
935939
}
936940

packages/core/test/index.spec.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -873,22 +873,35 @@ test("MiniflareCore: #watcherCallback: re-runs setup for script-providing plugin
873873

874874
test("MiniflareCore: reload: reloads worker", async (t) => {
875875
const tmp = await useTmp(t);
876+
const scriptPath = path.join(tmp, "worker.js");
876877
const wranglerConfigPath = path.join(tmp, "wrangler.toml");
878+
await fs.writeFile(
879+
scriptPath,
880+
"export default { fetch: (req, env) => new Response(`1:${env.KEY}`) };"
881+
);
877882
await fs.writeFile(wranglerConfigPath, '[vars]\nKEY = "value1"');
878-
const mf = useMiniflare({ BindingsPlugin }, { wranglerConfigPath });
879-
let globalScope = await mf.getGlobalScope();
880-
t.is(globalScope.KEY, "value1");
883+
const mf = useMiniflare(
884+
{ BindingsPlugin },
885+
{ modules: true, scriptPath, wranglerConfigPath }
886+
);
887+
let res = await mf.dispatchFetch("http://localhost/");
888+
t.is(await res.text(), "1:value1");
881889

882-
// Change wrangler config, check not automatically reloaded (watch disabled)
890+
// Change wrangler config and script, check not automatically reloaded
891+
// (note watch is disabled)
892+
await fs.writeFile(
893+
scriptPath,
894+
"export default { fetch: (req, env) => new Response(`2:${env.KEY}`) };"
895+
);
883896
await fs.writeFile(wranglerConfigPath, '[vars]\nKEY = "value2"');
884897
await setTimeout(100);
885-
globalScope = await mf.getGlobalScope();
886-
t.is(globalScope.KEY, "value1");
898+
res = await mf.dispatchFetch("http://localhost/");
899+
t.is(await res.text(), "1:value1");
887900

888-
// Manually reload(), check config reloaded
901+
// Manually reload(), check config and script reloaded
889902
await mf.reload();
890-
globalScope = await mf.getGlobalScope();
891-
t.is(globalScope.KEY, "value2");
903+
res = await mf.dispatchFetch("http://localhost/");
904+
t.is(await res.text(), "2:value2");
892905
});
893906

894907
test("MiniflareCore: dispatches reload events", async (t) => {

0 commit comments

Comments
 (0)