Skip to content

Commit

Permalink
Disable format conversion when detecting format (#2766)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mrbbot committed Feb 20, 2023
1 parent 9f82f5d commit 7912e63
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .changeset/nervous-scissors-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wrangler": patch
---

fix: correctly detect `service-worker` format when using `typeof module`

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. See https://esbuild.github.io/api/#format for more details.
35 changes: 35 additions & 0 deletions packages/wrangler/src/__tests__/guess-worker-format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,41 @@ describe("guess worker format", () => {
expect(guess).toBe("service-worker");
});

it('should detect a "service-worker" worker using `typeof module`', async () => {
await writeFile("./index.ts", "typeof module");
const guess = await guessWorkerFormat(
path.join(process.cwd(), "./index.ts"),
process.cwd(),
undefined
);
expect(guess).toBe("service-worker");
});

it('should detect a "service-worker" worker using imports', async () => {
await writeFile(
"./dep.ts",
`
const value = 'thing';
export default value;
`
);
await writeFile(
"./index.ts",
`
import value from './dep.ts';
addEventListener('fetch', (event) => {
event.respondWith(new Response(value));
});
`
);
const guess = await guessWorkerFormat(
path.join(process.cwd(), "./index.ts"),
process.cwd(),
undefined
);
expect(guess).toBe("service-worker");
});

it("should throw an error when the hint doesn't match the guess (modules - service-worker)", async () => {
await writeFile("./index.ts", "export default {};");
await expect(
Expand Down
1 change: 0 additions & 1 deletion packages/wrangler/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ export default async function guessWorkerFormat(
absWorkingDir: entryWorkingDirectory,
metafile: true,
bundle: false,
format: "esm",
target: "es2022",
write: false,
loader: {
Expand Down

0 comments on commit 7912e63

Please sign in to comment.