Migrate webpack projects from wrangler
version 1
Previous versions of wrangler
offered rudimentary support for webpack with the type
and webpack_config
keys in wrangler.toml
. Starting with Wrangler 2, wrangler
no longer supports the type
and webpack_config
keys, but you can still use webpack with your Workers.
As a developer using webpack with Workers, you may be in one of four categories:
I use
[build]
to run webpack (or another bundler) external towrangler
.I use
type = webpack
, but do not provide my own configuration and letwrangler
take care of it.
If you do not see yourself represented, file an issue and we can assist you with your specific situation and improve this guide for future readers.
I use [build]
to run webpack (or another bundler) external to Wrangler.
wrangler
2 supports the [build]
key, so your Workers will continue to build using your own setup.
I use type = webpack
, but do not provide my own configuration and let wrangler
take care of it.
wrangler
will continue to take care of it. Remove type = webpack
from your wrangler.toml
file.
I use type = webpack
and webpack_config = <path/to/webpack.config.js>
to handle JSX, Typescript, WebAssembly, HTML files, and other non-standard filetypes.
As of Wrangler 2, wrangler
has built-in support for this use case due to the module system
.
The Workers runtime handles JSX and Typescript. You can import
any modules you need into your code and the Workers runtime includes them in the built Worker automatically.
You should remove the type
and webpack_config
keys from your wrangler.toml
file.
I use type = webpack
and webpack_config = <path/to/webpack.config.js>
to perform code-transforms and/or other code-modifying functionality.
wrangler
2 drops support for project types, including type = webpack
and configuration via the webpack_config
key. If your webpack configuration performs operations beyond adding loaders (for example, for Typescript) you will need to maintain your custom webpack configuration. In the long term, you should migrate to an external [build]
process
. In the short term, it is still possible to reproduce wrangler
1’s build steps in newer versions of wrangler
by following the instructions below.
- Add wranglerjs-compat-webpack-plugin as a
devDependency
wrangler-js, shipped as a separate library from wrangler 1, is a Node script that configures and executes webpack 4 for you. When you set type = webpack
, wrangler 1 would execute this script for you. We have ported the functionality over to a new package, wranglerjs-compat-webpack-plugin, which you can use as a webpack plugin.
To do that, you will need to add it as a dependency:
npm install --save-dev webpack@^4.46.0 webpack-cli wranglerjs-compat-webpack-plugin# oryarn add --dev webpack@4.46.0 webpack-cli wranglerjs-compat-webpack-plugin
You should see this reflected in your package.json
file:
{ "name": "my-worker", "version": "x.y.z", // ... "devDependencies": { // ... "wranglerjs-compat-webpack-plugin": "^x.y.z", "webpack": "^4.46.0", "webpack-cli": "^x.y.z" }
}
- Add [wranglerjs-compat-webpack-plugin] to
webpack.config.js
Modify your webpack.config.js
file to include the plugin you just installed.
const { WranglerJsCompatWebpackPlugin,
} = require("wranglerjs-compat-webpack-plugin");
module.exports = { // ... plugins: [new WranglerJsCompatWebpackPlugin()],
};
Add a build script your
package.json
{"name": "my-worker","verion": "2.0.0",// ..."scripts": {"build": "webpack" // <-- Add this line!// ...}}Remove unsupported entries from your
wrangler.toml
Remove the type
and webpack_config
keys from your wrangler.toml
file, as they are not supported anymore.
# Remove these!
type = "webpack"
webpack_config = "webpack.config.js"
- Tell
wrangler
how to bundle your Worker
wrangler
no longer has any knowledge of how to build your Worker. You will need to tell it how to call webpack and where to look for webpack’s output. This translates into two fields:
main = "./worker/script.js" # by default, or whatever file webpack outputs
[build]
command = "npm run build" # or "yarn build"
# ...
- Test
Try running wrangler publish
to test that your configuration works as expected.