Bundling
By default, Wrangler bundles your Worker code using esbuild
. This means that Wrangler has built-in support for importing modules from npm defined in your package.json
. To review the exact code that Wrangler will upload to Cloudflare, run wrangler publish --dry-run --outdir dist
, which will show your Worker code after Wrangler’s bundling.
Files which will not be bundled
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:
.txt
.html
.bin
.wasm
and.wasm?module
Refer to Bundling configuration to customize these file types.
For example, with the following import, the variable data
will be a string containing the contents of example.html
:
import data from "./example.html"; // Where `example.html` is a file in your local directory
This is also the basis of Wasm support with Wrangler. To use a Wasm module in a Worker developed with Wrangler, add the following to your Worker:
import wasm from "./example.wasm"; // Where `example.wasm` is a file in your local directory
export default { fetch(request) { const module = WebAssembly.instantiate(wasm); },
};
Conditional exports
Wrangler respects the conditional exports
field 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. Refer to the Wrangler repository for an example isomorphic package.
Disable bundling
Opt out of bundling by using the --no-bundle
command line flag: wrangler publish --no-bundle
. If you opt out of bundling, Wrangler will not process your code and a number of features will not be available. You can use Custom Builds to customize what Wrangler will bundle and upload to the Cloudflare global network when you use wrangler dev
and wrangler publish
.
D1 bindings
During the beta period for D1, D1 bindings will not be available to a Worker published with --no-bundle
.
Editing via the Cloudflare dashboard
It is not possible to edit your Worker in the Cloudflare dashboard when you use the --no-bundle
flag. The Cloudflare dashboard does not support Workers with multiple modules. If you use a Custom Build script, which bundles your Worker into a single module, this limitation can be bypassed.