Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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