|
| 1 | +--- |
| 2 | +title: Migrating from 0.x |
| 3 | +parent: Guides |
| 4 | +nav_order: 1 |
| 5 | +--- |
| 6 | + |
| 7 | +# Migrating from 0.x to 1.0 |
| 8 | + |
| 9 | +Version 1.0 is a major release that migrates the package to Webpack 5, drops several legacy loaders and plugins, and raises the minimum Node.js version. This guide covers every breaking change with before/after examples. |
| 10 | + |
| 11 | +## 1. Upgrade Node.js to 22+ |
| 12 | + |
| 13 | +Node.js 22 is now required. Check your current version with `node --version`. If you use nvm: |
| 14 | + |
| 15 | +```bash |
| 16 | +nvm install 22 |
| 17 | +nvm use 22 |
| 18 | +``` |
| 19 | + |
| 20 | +An `.nvmrc` file is included in the project; `nvm use` with no arguments will pick up the correct version automatically. |
| 21 | + |
| 22 | +## 2. Upgrade peer dependencies |
| 23 | + |
| 24 | +Update `webpack`, `webpack-cli`, and `webpack-dev-server` in your project: |
| 25 | + |
| 26 | +```bash |
| 27 | +npm install --save-dev webpack@5 webpack-cli@5 webpack-dev-server@5 |
| 28 | +``` |
| 29 | + |
| 30 | +Webpack 5 contains breaking changes of its own. If you maintain any custom webpack configuration beyond what the presets provide, consult the [Webpack 5 migration guide](https://webpack.js.org/migrate/5/) alongside this one. |
| 31 | + |
| 32 | +## 3. Replace `loaders.url()` and `loaders.file()` |
| 33 | + |
| 34 | +`loaders.url()` and `loaders.file()` have been removed. Webpack 5 handles binary assets natively via [asset modules](https://webpack.js.org/guides/asset-modules/), so these third-party loaders are no longer needed. |
| 35 | + |
| 36 | +The presets already use the new loaders by default. If you referenced the old loaders directly in custom configuration, replace them as follows: |
| 37 | + |
| 38 | +**`loaders.url()` → `loaders.asset()`** |
| 39 | + |
| 40 | +`url-loader` inlined small files as data URLs and emitted larger files to disk. `loaders.asset()` provides the same behaviour using Webpack's native asset module: |
| 41 | + |
| 42 | +```js |
| 43 | +// Before |
| 44 | +module.exports = { |
| 45 | + module: { |
| 46 | + rules: [ |
| 47 | + loaders.url( { options: { limit: 10000 } } ), |
| 48 | + ], |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +// After |
| 53 | +module.exports = { |
| 54 | + module: { |
| 55 | + rules: [ |
| 56 | + loaders.asset(), // 10 KB threshold by default |
| 57 | + ], |
| 58 | + }, |
| 59 | +}; |
| 60 | +``` |
| 61 | + |
| 62 | +To adjust the inline size threshold, mutate the defaults or use `filterLoaders`: |
| 63 | + |
| 64 | +```js |
| 65 | +// Mutate defaults directly |
| 66 | +loaders.asset.defaults.parser.dataUrlCondition.maxSize = 5000; |
| 67 | + |
| 68 | +// Or via filterLoaders in a preset |
| 69 | +presets.production( options, { |
| 70 | + filterLoaders: ( loader, loaderType ) => { |
| 71 | + if ( loaderType === 'asset' ) { |
| 72 | + loader.parser.dataUrlCondition.maxSize = 5000; |
| 73 | + } |
| 74 | + return loader; |
| 75 | + }, |
| 76 | +} ); |
| 77 | +``` |
| 78 | + |
| 79 | +**`loaders.file()` → `loaders.assetResource()`** |
| 80 | + |
| 81 | +`file-loader` always emitted files to disk. `loaders.assetResource()` is the equivalent: |
| 82 | + |
| 83 | +```js |
| 84 | +// Before |
| 85 | +loaders.file() |
| 86 | + |
| 87 | +// After |
| 88 | +loaders.assetResource() |
| 89 | +``` |
| 90 | + |
| 91 | +**`loaders.assetInline()`** is also available if you want to always inline a file as a data URL, regardless of size. |
| 92 | + |
| 93 | +Note that asset modules do not support `options.publicPath` on the rule itself. Set [`output.publicPath`](https://webpack.js.org/configuration/output/#outputpublicpath) at the top level of your webpack config instead. |
| 94 | + |
| 95 | +## 4. Replace `loaders.eslint()` with `plugins.eslint()` |
| 96 | + |
| 97 | +ESLint linting has moved from a loader rule to a webpack plugin. Remove `loaders.eslint()` from your `module.rules` array and add `plugins.eslint()` to your `plugins` array. |
| 98 | + |
| 99 | +```js |
| 100 | +// Before |
| 101 | +const { loaders, presets } = require( '@humanmade/webpack-helpers' ); |
| 102 | + |
| 103 | +module.exports = { |
| 104 | + module: { |
| 105 | + rules: [ |
| 106 | + loaders.eslint(), |
| 107 | + loaders.js(), |
| 108 | + // ... |
| 109 | + ], |
| 110 | + }, |
| 111 | +}; |
| 112 | + |
| 113 | +// After |
| 114 | +const { plugins, presets } = require( '@humanmade/webpack-helpers' ); |
| 115 | + |
| 116 | +module.exports = presets.production( { |
| 117 | + plugins: [ |
| 118 | + plugins.eslint(), |
| 119 | + ], |
| 120 | + // ... |
| 121 | +} ); |
| 122 | +``` |
| 123 | + |
| 124 | +ESLint is not added automatically by the presets — you must add `plugins.eslint()` explicitly if you want linting as part of your build. |
| 125 | + |
| 126 | +## 5. Replace `plugins.optimizeCssAssets()` with `plugins.cssMinimizer()` |
| 127 | + |
| 128 | +`plugins.optimizeCssAssets()` has been removed along with the `optimize-css-assets-webpack-plugin` dependency. The replacement is `plugins.cssMinimizer()`, which wraps `css-minimizer-webpack-plugin` and is already included in `presets.production()`. |
| 129 | + |
| 130 | +If you referenced `plugins.optimizeCssAssets()` directly in a custom `optimization.minimizer` array: |
| 131 | + |
| 132 | +```js |
| 133 | +// Before |
| 134 | +const config = presets.production( { entry, output } ); |
| 135 | +module.exports = { |
| 136 | + ...config, |
| 137 | + optimization: { |
| 138 | + ...config.optimization, |
| 139 | + minimizer: [ |
| 140 | + plugins.terser(), |
| 141 | + plugins.optimizeCssAssets(), |
| 142 | + ], |
| 143 | + }, |
| 144 | +}; |
| 145 | + |
| 146 | +// After |
| 147 | +module.exports = { |
| 148 | + ...config, |
| 149 | + optimization: { |
| 150 | + ...config.optimization, |
| 151 | + minimizer: [ |
| 152 | + plugins.terser(), |
| 153 | + plugins.cssMinimizer(), |
| 154 | + ], |
| 155 | + }, |
| 156 | +}; |
| 157 | +``` |
| 158 | + |
| 159 | +If you used `plugins.constructors.OptimizeCssAssetsPlugin` directly, switch to `plugins.constructors.CssMinimizerPlugin`. |
| 160 | + |
| 161 | +## 6. Migrate your ESLint configuration to flat config format |
| 162 | + |
| 163 | +ESLint 9+ requires the new flat configuration format. The legacy `.eslintrc`, `.eslintrc.js`, `.eslintrc.json`, and `.eslintrc.yml` files are not supported. |
| 164 | + |
| 165 | +Create an `eslint.config.js` (or `eslint.config.mjs`) file in your project root. A minimal example: |
| 166 | + |
| 167 | +```js |
| 168 | +// eslint.config.js |
| 169 | +import js from '@eslint/js'; |
| 170 | + |
| 171 | +export default [ |
| 172 | + js.configs.recommended, |
| 173 | + { |
| 174 | + files: [ '**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx' ], |
| 175 | + languageOptions: { |
| 176 | + ecmaVersion: 2024, |
| 177 | + sourceType: 'module', |
| 178 | + }, |
| 179 | + rules: { |
| 180 | + // your project rules |
| 181 | + }, |
| 182 | + }, |
| 183 | +]; |
| 184 | +``` |
| 185 | + |
| 186 | +Refer to the [ESLint flat config migration guide](https://eslint.org/docs/latest/use/configure/migration-guide) for a complete reference, including how to translate common `.eslintrc` options. |
| 187 | + |
| 188 | +## 7. Update custom `optimization` config (if applicable) |
| 189 | + |
| 190 | +Webpack 5 renamed the `noEmitOnErrors` optimization option to `emitOnErrors`, with the boolean sense inverted. If you have a custom `optimization` block: |
| 191 | + |
| 192 | +```js |
| 193 | +// Before (Webpack 4) |
| 194 | +module.exports = { |
| 195 | + optimization: { |
| 196 | + noEmitOnErrors: true, |
| 197 | + }, |
| 198 | +}; |
| 199 | + |
| 200 | +// After (Webpack 5) |
| 201 | +module.exports = { |
| 202 | + optimization: { |
| 203 | + emitOnErrors: false, |
| 204 | + }, |
| 205 | +}; |
| 206 | +``` |
| 207 | + |
| 208 | +If you use the presets without customising `optimization`, no change is needed. |
0 commit comments