Skip to content

Commit

Permalink
fix: bundle worker as iife if detected as a service worker (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
threepointone committed May 9, 2022
1 parent 1436c50 commit d84b568
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .changeset/good-geese-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wrangler": patch
---

fix: bundle worker as iife if detected as a service worker

We detect whether a worker is a "modules" format worker by the presence of a `default` export. This is a pretty good heuristic overall, but sometimes folks can make mistakes. One situation that's popped up a few times, is people writing exports, but still writing it in "service worker" format. We detect this fine, and log a warning about the exports, but send it up with the exports included. Unfortunately, our runtime throws when we mark a worker as a service worker, but still has exports. This patch fixes it so that the exports are not included in a service-worker worker.

Note that if you're missing an event listener, it'll still error with "No event handlers were registered. This script does nothing." but that's a better error than the SyntaxError _even when the listener was there_.

Fixes https://github.com/cloudflare/wrangler2/issues/937
72 changes: 72 additions & 0 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "node:fs";
import * as path from "node:path";
import * as TOML from "@iarna/toml";
import * as esbuild from "esbuild";
import { writeAuthConfigFile } from "../user";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import {
Expand Down Expand Up @@ -878,6 +879,77 @@ export default{
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should preserve exports on a module format worker", async () => {
writeWranglerToml();
fs.writeFileSync(
"index.js",
`
export const abc = 123;
export const def = "show me the money";
export default {};`
);

await runWrangler("publish index.js --dry-run --outdir out");

expect(
(
await esbuild.build({
entryPoints: [path.resolve("./out/index.js")],
metafile: true,
write: false,
})
).metafile?.outputs["index.js"].exports
).toMatchInlineSnapshot(`
Array [
"abc",
"def",
"default",
]
`);
expect(std).toMatchInlineSnapshot(`
Object {
"debug": "",
"err": "",
"out": "--dry-run: exiting now.",
"warn": "",
}
`);
});

it("should not preserve exports on a service-worker format worker", async () => {
writeWranglerToml();
fs.writeFileSync(
"index.js",
`
export const abc = 123;
export const def = "show me the money";
addEventListener('fetch', event => {});`
);

await runWrangler("publish index.js --dry-run --outdir out --minify");

expect(
(
await esbuild.build({
entryPoints: [path.resolve("./out/index.js")],
metafile: true,
write: false,
})
).metafile?.outputs["index.js"].exports
).toMatchInlineSnapshot(`Array []`);

expect(std).toMatchInlineSnapshot(`
Object {
"debug": "",
"err": "",
"out": "--dry-run: exiting now.",
"warn": "▲ [WARNING] The entrypoint index.js has exports like an ES Module, but hasn't defined a default export like a module worker normally would. Building the worker using \\"service-worker\\" format...
",
}
`);
});

it("should be able to transpile entry-points in sub-directories (sw)", async () => {
writeWranglerToml();
writeWorkerSource({ basePath: "./src", type: "sw" });
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function bundleWorker(
absWorkingDir: entry.directory,
outdir: destination,
external: ["__STATIC_CONTENT_MANIFEST"],
format: "esm",
format: entry.format === "modules" ? "esm" : "iife",
target: "es2020",
sourcemap: true,
minify,
Expand Down

0 comments on commit d84b568

Please sign in to comment.