Skip to content

Commit 3882d41

Browse files
cod1kpetebacondarwin
authored andcommitted
Add support for run_worker_first flag in Miniflare options
This update introduces support for the `run_worker_first` flag when configuring Miniflare options. A new test suite ensures proper handling of this flag, including scenarios where it is undefined. Additionally, the `miniflare-options` logic was updated to pass the flag to the worker configuration when specified.
1 parent 2c50115 commit 3882d41

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {
2+
afterAll,
3+
afterEach,
4+
beforeAll,
5+
describe,
6+
expect,
7+
test,
8+
vi,
9+
} from "vitest";
10+
import { ROUTER_WORKER_NAME } from "../constants";
11+
import { getDevMiniflareOptions } from "../miniflare-options";
12+
import type { WorkerOptions } from "miniflare";
13+
14+
describe("getDevMiniflareOptions", () => {
15+
beforeAll(() => {
16+
vi.mock("fs");
17+
vi.mock("path");
18+
vi.mock("wrangler", () => ({
19+
unstable_getMiniflareWorkerOptions: vi.fn(() => ({
20+
externalWorkers: {},
21+
workerOptions: {},
22+
})),
23+
}));
24+
});
25+
afterEach(() => {
26+
vi.resetAllMocks();
27+
});
28+
afterAll(() => {
29+
vi.restoreAllMocks();
30+
});
31+
test.each([
32+
{ run_worker_first: true },
33+
{ run_worker_first: false },
34+
{ run_worker_first: undefined },
35+
])(
36+
"Should consider run_worker_first=$run_worker_first flag",
37+
({ run_worker_first }) => {
38+
// @ts-expect-error
39+
const mf_options: { workers: WorkerOptions[] } = getDevMiniflareOptions(
40+
{
41+
type: "workers",
42+
entryWorkerEnvironmentName: "test",
43+
workers: {
44+
// @ts-expect-error
45+
test: {
46+
assets: {
47+
run_worker_first,
48+
},
49+
},
50+
},
51+
},
52+
{ config: { logLevel: "info" } },
53+
false
54+
);
55+
expect(mf_options).toMatchObject({ workers: expect.any(Array) });
56+
const worker = mf_options.workers.find(
57+
(worker) => worker.name === ROUTER_WORKER_NAME
58+
);
59+
expect(worker).toBeDefined();
60+
if (run_worker_first === undefined) {
61+
expect(worker).toMatchObject({
62+
bindings: {
63+
CONFIG: expect.not.objectContaining({
64+
invoke_user_worker_ahead_of_assets: expect.anything(),
65+
}),
66+
},
67+
});
68+
} else {
69+
expect(worker).toMatchObject({
70+
bindings: {
71+
CONFIG: {
72+
invoke_user_worker_ahead_of_assets: run_worker_first,
73+
},
74+
},
75+
});
76+
}
77+
}
78+
);
79+
});

packages/vite-plugin-cloudflare/src/miniflare-options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ export function getDevMiniflareOptions(
219219
bindings: {
220220
CONFIG: {
221221
has_user_worker: resolvedPluginConfig.type === "workers",
222+
...(typeof assetsConfig.run_worker_first === "boolean" && {
223+
invoke_user_worker_ahead_of_assets: assetsConfig.run_worker_first,
224+
}),
222225
},
223226
},
224227
serviceBindings: {

0 commit comments

Comments
 (0)