You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/workers/wrangler/bundling.mdx
+30-13Lines changed: 30 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,24 +3,28 @@ pcx_content_type: configuration
3
3
title: Bundling
4
4
head: []
5
5
description: Review Wrangler's default bundling.
6
-
7
6
---
8
7
9
8
By default, Wrangler bundles your Worker code using [`esbuild`](https://esbuild.github.io/). This means that Wrangler has built-in support for importing modules from [npm](https://www.npmjs.com/) defined in your `package.json`. To review the exact code that Wrangler will upload to Cloudflare, run `npx wrangler deploy --dry-run --outdir dist`, which will show your Worker code after Wrangler's bundling.
10
9
11
10
:::note
12
11
13
-
We recommend using Wrangler's inbuilt bundling, but we understand there are cases where you will need more flexibility. We have built an escape hatch in the form of [Custom Builds](/workers/wrangler/custom-builds/), which lets you run your own build before Wrangler's built-in one.
12
+
Wrangler's inbuilt bundling usually provides the best experience, but we understand there are cases where you will need more flexibility.
13
+
You can provide `rules` and set `find_additional_modules` in your configuration to control which files are included in the deployed Worker but not bundled into the entry-point file.
14
+
Furthermore, we have an escape hatch in the form of [Custom Builds](/workers/wrangler/custom-builds/), which lets you run your own build before Wrangler's built-in one.
14
15
:::
15
16
16
-
## Files which will not be bundled
17
+
## Including non-JavaScript modules
17
18
18
-
Bundling your Worker code takes multiple modules and bundles them into one. Sometimes, you might have modules that should not be inlined directly into the bundle. For example, instead of bundling a Wasm file into your JavaScript Worker, you would want to upload the Wasm file as a separate module that can be imported at runtime. Wrangler supports this for the following file types:
19
+
Bundling your Worker code takes multiple modules and bundles them into one file.
20
+
Sometimes, you might have modules that cannot be inlined directly into the bundle.
21
+
For example, instead of bundling a Wasm file into your JavaScript Worker, you would want to upload the Wasm file as a separate module that can be imported at runtime.
22
+
Wrangler supports this for the following file types:
19
23
20
-
*`.txt`
21
-
*`.html`
22
-
*`.bin`
23
-
*`.wasm` and `.wasm?module`
24
+
-`.txt`
25
+
-`.html`
26
+
-`.bin`
27
+
-`.wasm` and `.wasm?module`
24
28
25
29
Refer to [Bundling configuration](/workers/wrangler/configuration/#bundling) to customize these file types.
26
30
@@ -37,17 +41,30 @@ import wasm from "./example.wasm"; // Where `example.wasm` is a file in your loc
37
41
constinstance=awaitWebAssembly.instantiate(wasm); // Instantiate Wasm modules in global scope, not within the fetch() handler
38
42
39
43
exportdefault {
40
-
fetch(request) {
41
-
constresult=instance.exports.exported_func();
42
-
},
44
+
fetch(request) {
45
+
constresult=instance.exports.exported_func();
46
+
},
43
47
};
44
48
```
45
49
46
50
:::caution
47
51
48
-
Cloudflare Workers does not support `WebAssembly.instantiateStreaming()`.
52
+
Cloudflare Workers does not support `WebAssembly.instantiateStreaming()`.
49
53
:::
50
54
55
+
## Find additional modules
56
+
57
+
By setting `find_additional_modules` to `true` in your configuration file, Wrangler will traverse the file tree below `base_dir`.
58
+
Any files that match the `rules` you define will also be included as unbundled, external modules in the deployed Worker.
59
+
60
+
This approach is useful for supporting lazy loading of large or dynamically imported JavaScript files:
61
+
62
+
- Normally a large lazy imported file (e.g. `await import("./large-dep.mjs")`) would be bundled directly into your entrypoint, making lazy loading less effective.
63
+
If matching rule is added to `rules`, then this file would only be loaded and executed at runtime when it is actually imported.
64
+
- Previously, variable based dynamic imports (e.g. ``await import(`./lang/${language}.mjs`)``) would always fail at runtime, as Wrangler would have no way of knowing which modules to include in the upload.
65
+
Providing a rule that matches all these files (e.g. ` {type="EsModule", globs= ["./land/**/*.mjs"], fallthrough=true}), will ensure this module is available at runtime.
66
+
- "Partial bundling" is supported when `find_additional_modules` is `true`, and a source file matches one of the configured `rules`, since Wrangler will then treat it as "external" and not try to bundle it into the entry-point file.
67
+
51
68
## Conditional exports
52
69
53
70
Wrangler respects the [conditional `exports` field](https://nodejs.org/api/packages.html#conditional-exports) in `package.json`. This allows developers to implement isomorphic libraries that have different implementations depending on the JavaScript runtime they are running in. When bundling, Wrangler will try to load the [`workerd` key](https://runtime-keys.proposal.wintercg.org/#workerd). Refer to the Wrangler repository for [an example isomorphic package](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/isomorphic-random-example).
@@ -56,7 +73,7 @@ Wrangler respects the [conditional `exports` field](https://nodejs.org/api/packa
56
73
57
74
:::caution
58
75
59
-
Disabling bundling is not recommended in most scenarios. Use this option only when deploying code pre-processed by other tooling.
76
+
Disabling bundling is not recommended in most scenarios. Use this option only when deploying code pre-processed by other tooling.
60
77
:::
61
78
62
79
If your build tooling already produces build artifacts suitable for direct deployment to Cloudflare, you can opt out of bundling by using the `--no-bundle` command line flag: `npx wrangler deploy --no-bundle`. If you opt out of bundling, Wrangler will not process your code and some features introduced by Wrangler bundling (for example minification, and polyfills injection) will not be available.
- Deprecated — Instead, [enable the `nodejs_compat_v2` compatibility flag](/workers/runtime-apis/nodejs/#enable-nodejs-with-workers), which enables both built-in Node.js APIs, and adds polyfills as necessary.
142
-
Setting `node_compat = true` will add polyfills for Node.js built-in modules and globals to your Worker's code, when bundled with Wrangler.
143
-
This is powered by `@esbuild-plugins/node-globals-polyfill` which in itself is powered by [rollup-plugin-node-polyfills](https://github.com/ionic-team/rollup-plugin-node-polyfills/).
144
+
- The directory in which module "rules" should be evaluated when including additional files (via `find_additional_modules`) into a Worker deployment. Defaults to the directory containing the `main` entry point of the Worker if not specified.
- Deprecated — Instead, [enable the `nodejs_compat_v2` compatibility flag](/workers/runtime-apis/nodejs/#enable-nodejs-with-workers), which enables both built-in Node.js APIs, and adds polyfills as necessary.
159
+
Setting `node_compat = true` will add polyfills for Node.js built-in modules and globals to your Worker's code, when bundled with Wrangler.
160
+
This is powered by `@esbuild-plugins/node-globals-polyfill` which in itself is powered by [rollup-plugin-node-polyfills](https://github.com/ionic-team/rollup-plugin-node-polyfills/).
- Enables Workers Trace Events Logpush for a Worker. Any scripts with this property will automatically get picked up by the Workers Logpush job configured for your account. Defaults to `false`.
You can bundle modules into your Worker using the `rules` key, making these modules available to be imported when your Worker is invoked. The `rules` key will be an array of the below object.
956
+
Wrangler can operate in two modes: the default bundling mode and `--no-bundle` mode.
957
+
In bundling mode, Wrangler will traverse all the imports of your code and generate a single JavaScript "entry-point" file.
958
+
Imported source code is "inlined/bundled" into this entry-point file.
959
+
960
+
It is also possible to include additional modules into your Worker, which are uploaded alongside the entry-point.
961
+
You specify which additional modules should be included into your Worker using the `rules` key, making these modules available to be imported when your Worker is invoked.
962
+
The `rules` key will be an array of the below object.
0 commit comments