Skip to content

Commit 7912e63

Browse files
authored
Disable format conversion when detecting format (#2766)
* Disable format conversion when detecting format, closes #1668 Wrangler automatically detects whether your code is a `modules` or `service-worker` format Worker based on the presence of a `default` `export`. This check currently works by building your entrypoint with `esbuild` and looking at the output metafile. Previously, we were passing `format: "esm"` to `esbuild` when performing this check, which enables *format conversion*. This may introduce `export default` into the built code, even if it wasn't there to start with, resulting in incorrect format detections. This change removes `format: "esm"` which disables format conversion when bundling is disabled: https://esbuild.github.io/api/#format. We may want to use a package like `es-module-lexer` in the future, but this issue is affecting users, and this is probably the simplest fix. Closes #1668 Closes #2396 Ref #2737 * fixup! Disable format conversion when detecting format, closes #1668
1 parent 9f82f5d commit 7912e63

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: correctly detect `service-worker` format when using `typeof module`
6+
7+
Wrangler automatically detects whether your code is a `modules` or `service-worker` format Worker based on the presence of a `default` `export`. This check currently works by building your entrypoint with `esbuild` and looking at the output metafile.
8+
9+
Previously, we were passing `format: "esm"` to `esbuild` when performing this check, which enables _format conversion_. This may introduce `export default` into the built code, even if it wasn't there to start with, resulting in incorrect format detections.
10+
11+
This change removes `format: "esm"` which disables format conversion when bundling is disabled. See https://esbuild.github.io/api/#format for more details.

packages/wrangler/src/__tests__/guess-worker-format.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,41 @@ describe("guess worker format", () => {
3232
expect(guess).toBe("service-worker");
3333
});
3434

35+
it('should detect a "service-worker" worker using `typeof module`', async () => {
36+
await writeFile("./index.ts", "typeof module");
37+
const guess = await guessWorkerFormat(
38+
path.join(process.cwd(), "./index.ts"),
39+
process.cwd(),
40+
undefined
41+
);
42+
expect(guess).toBe("service-worker");
43+
});
44+
45+
it('should detect a "service-worker" worker using imports', async () => {
46+
await writeFile(
47+
"./dep.ts",
48+
`
49+
const value = 'thing';
50+
export default value;
51+
`
52+
);
53+
await writeFile(
54+
"./index.ts",
55+
`
56+
import value from './dep.ts';
57+
addEventListener('fetch', (event) => {
58+
event.respondWith(new Response(value));
59+
});
60+
`
61+
);
62+
const guess = await guessWorkerFormat(
63+
path.join(process.cwd(), "./index.ts"),
64+
process.cwd(),
65+
undefined
66+
);
67+
expect(guess).toBe("service-worker");
68+
});
69+
3570
it("should throw an error when the hint doesn't match the guess (modules - service-worker)", async () => {
3671
await writeFile("./index.ts", "export default {};");
3772
await expect(

packages/wrangler/src/entry.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ export default async function guessWorkerFormat(
151151
absWorkingDir: entryWorkingDirectory,
152152
metafile: true,
153153
bundle: false,
154-
format: "esm",
155154
target: "es2022",
156155
write: false,
157156
loader: {

0 commit comments

Comments
 (0)