Skip to content

Commit

Permalink
allow cloudflare-specific js module (#2905)
Browse files Browse the repository at this point in the history
Mark cloudflare-specific js module as external so that scripts can be
published that reference it.
  • Loading branch information
edevil committed Mar 16, 2023
1 parent 9996ac4 commit 6fd0624
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/afraid-readers-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": minor
---

feat: support external imports from `cloudflare:...` prefixed modules

Going forward Workers will be providing built-in modules (similar to `node:...`) that can be imported using the `cloudflare:...` prefix. This change adds support to the Wrangler bundler to mark these imports as external.
26 changes: 26 additions & 0 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,32 @@ export default{
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should allow cloudflare module import", async () => {
writeWranglerToml();
fs.writeFileSync(
"./index.js",
`
import { EmailMessage } from "cloudflare:email";
export default{
fetch(){
return new Response("all done");
}
}
`
);
mockUploadWorkerRequest();
mockSubDomainRequest();
await runWrangler("publish index.js");
expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Uploaded test-name (TIMINGS)
Published test-name (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Deployment ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should be able to transpile entry-points in sub-directories (esm)", async () => {
writeWranglerToml();
writeWorkerSource({ basePath: "./src" });
Expand Down
10 changes: 10 additions & 0 deletions packages/wrangler/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ const nodejsCompatPlugin: esbuild.Plugin = {
},
};

const cloudflareJsPlugin: esbuild.Plugin = {
name: "cloudflare javascript Plugin",
setup(pluginBuild) {
pluginBuild.onResolve({ filter: /^cloudflare:.*/ }, () => {
return { external: true };
});
},
};

/**
* Generate a bundle for the worker identified by the arguments passed in.
*/
Expand Down Expand Up @@ -379,6 +388,7 @@ export async function bundleWorker(
? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
: []),
...(nodejsCompat ? [nodejsCompatPlugin] : []),
...[cloudflareJsPlugin],
...(plugins || []),
],
...(jsxFactory && { jsxFactory }),
Expand Down

0 comments on commit 6fd0624

Please sign in to comment.