Skip to content

Commit d3a758c

Browse files
implement module finding in normal bundling if the find_additional_modules flag is on
1 parent b8b61c6 commit d3a758c

30 files changed

+746
-257
lines changed

.changeset/clever-turkeys-leave.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
feat: support partial bundling with configurable external modules
6+
7+
Setting `find_additional_modules` to `true` in your configuration file will now instruct Wrangler to look for files in
8+
your `base_dir` that match your configured `rules`, and deploy them as unbundled, external modules with your Worker.
9+
`base_dir` defaults to the directory containing your `main` entrypoint.
10+
11+
Wrangler can operate in two modes: the default bundling mode and `--no-bundle` mode. In bundling mode, dynamic imports
12+
(e.g. `await import("./large-dep.mjs")`) would be bundled into your entrypoint, making lazy loading less effective.
13+
Additionally, variable dynamic imports (e.g. `` await import(`./lang/${language}.mjs`) ``) would always fail at runtime,
14+
as Wrangler would have no way of knowing which modules to upload. The `--no-bundle` mode sought to address these issues
15+
by disabling Wrangler's bundling entirely, and just deploying code as is. Unfortunately, this also disabled Wrangler's
16+
code transformations (e.g. TypeScript compilation, `--assets`, `--test-scheduled`, etc).
17+
18+
With this change, we now additionally support _partial bundling_. Files are bundled unless `find_additional_modules` is
19+
`true`, and the file matches one of the configured `rules`. See
20+
https://developers.cloudflare.com/workers/wrangler/bundling/ for more details and examples.

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ packages/wrangler/CHANGELOG.md
77
packages/jest-environment-wrangler/CHANGELOG.md
88
packages/wranglerjs-compat-webpack-plugin/lib
99
packages/wrangler-devtools/built-devtools
10+
packages/wrangler-devtools/.cipd
11+
packages/wrangler-devtools/depot
12+
packages/wrangler-devtools/devtools-frontend
1013
packages/edge-preview-authenticated-proxy/package.json
1114
packages/format-errors/package.json
1215
packages/**/dist/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "additional-modules",
3+
"version": "0.0.1",
4+
"private": true,
5+
"scripts": {
6+
"build": "wrangler deploy --dry-run --outdir=dist",
7+
"check:type": "tsc",
8+
"deploy": "wrangler deploy",
9+
"start": "wrangler dev",
10+
"test": "vitest run",
11+
"test:ci": "vitest run",
12+
"test:watch": "vitest",
13+
"type:tests": "tsc -p ./test/tsconfig.json"
14+
},
15+
"devDependencies": {
16+
"@cloudflare/workers-types": "^4.20230724.0",
17+
"wrangler": "*"
18+
}
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "bundled";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "dynamic";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import dep from "./dep";
2+
import text from "./text.txt";
3+
4+
export default <ExportedHandler>{
5+
async fetch(request) {
6+
const url = new URL(request.url);
7+
if (url.pathname === "/dep") {
8+
return new Response(dep);
9+
}
10+
if (url.pathname === "/text") {
11+
return new Response(text);
12+
}
13+
if (url.pathname === "/dynamic") {
14+
return new Response((await import("./dynamic.js")).default);
15+
}
16+
if (url.pathname.startsWith("/lang/")) {
17+
const language = url.pathname.substring("/lang/".length);
18+
return new Response(
19+
(await import(`./lang/${language}.js`)).default.hello
20+
);
21+
}
22+
return new Response("Not Found", { status: 404 });
23+
},
24+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default { hello: "hello" };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default { hello: "bonjour" };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "*.txt" {
2+
const value: string;
3+
export default value;
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

0 commit comments

Comments
 (0)