3.5 KiB
no-bundle-import
0.0.1
Patch Changes
-
#2769
0a779904Thanks @penalosa! - feature: Support modules with--no-bundleWhen the
--no-bundleflag is set, Wrangler now has support for uploading additional modules alongside the entrypoint. This will allow modules to be imported at runtime on Cloudflare's Edge. This respects Wrangler's module rules configuration, which means that only imports of non-JS modules will trigger an upload by default. For instance, the following code will now work with--no-bundle(assuming theexample.wasmfile exists at the correct path):// index.js import wasm from './example.wasm' export default { async fetch() { await WebAssembly.instantiate(wasm, ...) ... } }For JS modules, it's necessary to specify an additional module rule (or rules) in your
wrangler.tomlto configure your modules as ES modules or Common JS modules. For instance, to upload additional JavaScript files as ES modules, add the following module rule to yourwrangler.toml, which tells Wrangler that all**/*.jsfiles are ES modules.rules = [ { type = "ESModule", globs = ["**/*.js"]}, ]If you have Common JS modules, you'd configure Wrangler with a CommonJS rule (the following rule tells Wrangler that all
.cjsfiles are Common JS modules):rules = [ { type = "CommonJS", globs = ["**/*.cjs"]}, ]In most projects, adding a single rule will be sufficient. However, for advanced usecases where you're mixing ES modules and Common JS modules, you'll need to use multiple rule definitions. For instance, the following set of rules will match all
.mjsfiles as ES modules, all.cjsfiles as Common JS modules, and thenested/say-hello.jsfile as Common JS.rules = [ { type = "CommonJS", globs = ["nested/say-hello.js", "**/*.cjs"]}, { type = "ESModule", globs = ["**/*.mjs"]} ]If multiple rules overlap, Wrangler will log a warning about the duplicate rules, and will discard additional rules that matches a module. For example, the following rule configuration classifies
dep.jsas both a Common JS module and an ES module:rules = [ { type = "CommonJS", globs = ["dep.js"]}, { type = "ESModule", globs = ["dep.js"]} ]Wrangler will treat
dep.jsas a Common JS module, since that was the first rule that matched, and will log the following warning:▲ [WARNING] Ignoring duplicate module: dep.js (esm)This also adds a new configuration option to
wrangler.toml:base_dir. Defaulting to the directory of your Worker's main entrypoint, this tells Wrangler where your additional modules are located, and determines the module paths against which your module rule globs are matched.For instance, given the following directory structure:
- wrangler.toml - src/ - index.html - vendor/ - dependency.js - js/ - index.jsIf your
wrangler.tomlhadmain = "src/js/index.js", you would need to setbase_dir = "src"in order to be able to importsrc/vendor/dependency.jsandsrc/index.htmlfromsrc/js/index.js.