Skip to content

Commit

Permalink
fix: assume a worker is a module worker only if it has a default ex…
Browse files Browse the repository at this point in the history
…port (#726)

This tweaks the logic that guesses worker formats to check whether a `default` export is defined on an entry point before assuming it's a module worker.
  • Loading branch information
threepointone authored Mar 29, 2022
1 parent e2b7352 commit c4e5dc3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/sharp-owls-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: assume a worker is a module worker only if it has a `default` export

This tweaks the logic that guesses worker formats to check whether a `default` export is defined on an entry point before assuming it's a module worker.
17 changes: 16 additions & 1 deletion packages/wrangler/src/__tests__/guess-worker-format.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { writeFile } from "fs/promises";
import path from "path";
import guessWorkerFormat from "../entry";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";

describe("guess worker format", () => {
runInTempDir();
const std = mockConsoleMethods();
it('should detect a "modules" worker', async () => {
await writeFile("./index.ts", "export default {};");
// Note that this isn't actually a valid worker, because it's missing
Expand Down Expand Up @@ -39,7 +41,7 @@ describe("guess worker format", () => {
"service-worker"
)
).rejects.toThrow(
"You configured this worker to be a 'service-worker', but the file you are trying to build appears to have ES module exports. Please pass `--format modules`, or simply remove the configuration."
"You configured this worker to be a 'service-worker', but the file you are trying to build appears to have a `default` export like a module worker. Please pass `--format modules`, or simply remove the configuration."
);
});

Expand All @@ -65,4 +67,17 @@ describe("guess worker format", () => {
);
expect(guess).toBe("service-worker");
});

it("logs a warning when a worker has exports, but not a default one", async () => {
await writeFile("./index.ts", "export const foo = 1;");
const guess = await guessWorkerFormat(
path.join(process.cwd(), "./index.ts"),
process.cwd(),
undefined
);
expect(guess).toBe("service-worker");
expect(std.warn).toMatchInlineSnapshot(
`"The entrypoint index.ts 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..."`
);
});
});
22 changes: 19 additions & 3 deletions packages/wrangler/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,30 @@ export default async function guessWorkerFormat(
"More than one entry-point found for generated bundle." +
listEntryPoints(entryPoints)
);
const guessedWorkerFormat =
entryPoints[0][1].exports.length > 0 ? "modules" : "service-worker";

let guessedWorkerFormat: CfScriptFormat;
const scriptExports = entryPoints[0][1].exports;
if (scriptExports.length > 0) {
if (scriptExports.includes("default")) {
guessedWorkerFormat = "modules";
} else {
console.warn(
`The entrypoint ${path.relative(
process.cwd(),
entryFile
)} 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...`
);
guessedWorkerFormat = "service-worker";
}
} else {
guessedWorkerFormat = "service-worker";
}

if (hint) {
if (hint !== guessedWorkerFormat) {
if (hint === "service-worker") {
throw new Error(
"You configured this worker to be a 'service-worker', but the file you are trying to build appears to have ES module exports. Please pass `--format modules`, or simply remove the configuration."
"You configured this worker to be a 'service-worker', but the file you are trying to build appears to have a `default` export like a module worker. Please pass `--format modules`, or simply remove the configuration."
);
} else {
throw new Error(
Expand Down

0 comments on commit c4e5dc3

Please sign in to comment.