From e42d4524f706ee546802d8459a91d156491ad743 Mon Sep 17 00:00:00 2001 From: Zyphrax Date: Sun, 3 Mar 2019 18:28:08 -0500 Subject: [PATCH] Clean up v4 upgrade guide, add section about namespaces --- README.md | 2 +- docs/v4-upgrade.md | 63 +++++++++++++++++++++++++++++++--------------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 49b87d033..42e35b3dc 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ even JavaScript Sprinkles (that all continues to live in app/assets). However, it is possible to use Webpacker for CSS, images and fonts assets as well, in which case you may not even need the asset pipeline. This is mostly relevant when exclusively using component-based JavaScript frameworks. -**NOTE:** The master branch now hosts the code for v4.x.x. Please refer to [3-x-stable](https://github.com/rails/webpacker/tree/3-x-stable) branch for 3.x documentation. +**NOTE:** The master branch now hosts the code for v4.x.x. Please refer to [3-x-stable](https://github.com/rails/webpacker/tree/3-x-stable) branch for 3.x documentation. See the [v4-upgrade guide](docs/v4-upgrade.md) for an overview of the changes. diff --git a/docs/v4-upgrade.md b/docs/v4-upgrade.md index 52c1a6c9a..a316869f5 100644 --- a/docs/v4-upgrade.md +++ b/docs/v4-upgrade.md @@ -1,19 +1,27 @@ +# Webpacker Upgrade Guide + + To update a Webpacker v3.5 app to v4, follow these steps: -1. Update the `webpacker` gem and the `@rails/webpacker` package to v4. This will upgrade webpack itself from 3.x to 4.x, make sure you're aware of [any deprecations which might effect you](https://webpack.js.org/migrate/4/). Also make sure any other packages you depend on support webpack 4 and don't require any changes, e.g. if you explicitly include `webpack` you need to upgrade it to 4.x, and if you use `webpack-dev-server` you need to upgrade it to 3.x. -1. Browser support definitions have been moved to [`.browserslistrc`](../lib/install/config/.browserslistrc) to `/`. -1. Merge any differences between [`config/webpacker.yml`](../lib/install/config/webpacker.yml) and your `config/webpacker.yml`. -1. Webpacker v4 upgrades Babel to [v7](https://babeljs.io/docs/en/v7-migration), see also [the release blog post](https://babeljs.io/blog/2018/08/27/7.0.0). Many packages were moved to the `@babel/` namespace, any babel plugins you have will need to be updated. It may be worth checking out [babel-upgrade](https://github.com/babel/babel-upgrade) if you have problems. ([#1564](https://github.com/rails/webpacker/pull/1564)) -1. `.babelrc` should be replaced with `babel.config.js` and `.postcssrc.yml` should be replaced with `postcss.config.js` ([#1822](https://github.com/rails/webpacker/pull/1822)). If you never changed these files from their defaults, the versions of [babel.config.js](../lib/install/config/babel.config.js) and [postcss.config.js](../lib/install/config/postcss.config.js) in the webpacker repository should be usable. -1. Due to the change in [#1625](https://github.com/rails/webpacker/pull/1625), you'll want to make sure that `extract_css` is set to true for the `default` environment in `webpacker.yml` if you want to have Webpacker supply your CSS. +1. Update the `webpacker` gem and the `@rails/webpacker` package to v4. This will upgrade Webpack itself from 3.x to 4.x, make sure you're aware of [any deprecations which might effect you](https://webpack.js.org/migrate/4/). Also make sure any other packages you depend on support Webpack 4 and don't require any changes, e.g. if you explicitly include `webpack` you need to upgrade it to 4.x, and if you use `webpack-dev-server` you need to upgrade it to 3.x. + +2. Browser support definitions have been moved to [`.browserslistrc`](../lib/install/config/.browserslistrc) to `/`. + +3. Merge any differences between [`config/webpacker.yml`](../lib/install/config/webpacker.yml) and your `config/webpacker.yml`. + +4. Webpacker v4 upgrades Babel to [v7](https://babeljs.io/docs/en/v7-migration), see also [the release blog post](https://babeljs.io/blog/2018/08/27/7.0.0). Many packages were moved to the `@babel/` namespace, any babel plugins you have will need to be updated. It may be worth checking out [babel-upgrade](https://github.com/babel/babel-upgrade) if you have problems. ([#1564](https://github.com/rails/webpacker/pull/1564)) + +5. `.babelrc` should be replaced with `babel.config.js` and `.postcssrc.yml` should be replaced with `postcss.config.js` ([#1822](https://github.com/rails/webpacker/pull/1822)). If you never changed these files from their defaults, the versions of [babel.config.js](../lib/install/config/babel.config.js) and [postcss.config.js](../lib/install/config/postcss.config.js) in the webpacker repository should be usable. + +6. Due to the change in [#1625](https://github.com/rails/webpacker/pull/1625), you'll want to make sure that `extract_css` is set to true for the `default` environment in `webpacker.yml` if you want to have Webpacker supply your CSS. -### Add SplitChunks +## SplitChunks Configuration If you used the `CommonsChunkPlugin` you'll need to upgrade to using the new `splitChunks`. -Originally, chunks (and modules imported inside them) were connected by a parent-child relationship in the internal webpack graph. The `CommonsChunkPlugin` was used to avoid duplicated dependencies across them, but further optimizations were not possible. +Originally, chunks (and modules imported inside them) were connected by a parent-child relationship in the internal Webpack graph. The `CommonsChunkPlugin` was used to avoid duplicated dependencies across them, but further optimizations were not possible. -In webpack v4, `CommonsChunkPlugin` was removed in favor of `optimization.splitChunks`. +In Webpack v4, the `CommonsChunkPlugin` was removed in favor of `optimization.splitChunks`. For the full configuration options of `splitChunks`, see the [Webpack documentation](https://webpack.js.org/plugins/split-chunks-plugin/). @@ -28,13 +36,13 @@ environment.splitChunks() environment.splitChunks((config) => Object.assign({}, config, { optimization: { splitChunks: false }})) ``` -Then use, `javascript_packs_with_chunks_tag` helper to include all the transpiled +Then use the `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` helpers to include all the transpiled packs with the chunks in your view, which creates html tags for all the chunks. ```erb <%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %> - + @@ -42,8 +50,7 @@ packs with the chunks in your view, which creates html tags for all the chunks. ``` -**Important:** Pass all your pack names when using this helper otherwise you will -get duplicated chunks on the page. +**Important:** Pass all your pack names to the helper otherwise you will get duplicated chunks on the page. ```erb <%# DO %> @@ -54,17 +61,17 @@ get duplicated chunks on the page. <%= javascript_packs_with_chunks_tag 'map' %> ``` -### Package-specific notes: +## Package-Specific Notes - If you're using React, you need to add `"@babel/preset-react"`, to the list of `presets` in your babel config. -- If you're using Vue Loader, you'll need to upgrade to [v15](https://vue-loader.vuejs.org/migrating.html) for webpack 4. -- To see what webpacker generates for a given framework with v4, you may want to re-run `bundle exec rake webpacker:install:FRAMEWORK` and let it override the files for your given JavaScript framework, and then compare them to see what changes you'll need to make. +- If you're using Vue Loader, you'll need to upgrade to [v15](https://vue-loader.vuejs.org/migrating.html) for Webpack 4. +- To see what Webpacker generates for a given framework with v4, you may want to re-run `bundle exec rake webpacker:install:FRAMEWORK` and let it override the files for your given JavaScript framework, and then compare them to see what changes you'll need to make. -### Excluding node_modules from being transpiled by babel-loader +## Excluding node_modules From Being Transpiled By Babel-Loader One change to take into consideration, is that Webpacker 4 transpiles the `node_modules` folder with the `babel-loader`. This folder used to be ignored by -webpacker 3. The new behavior helps in case some library contains ES6 code, but in +Webpacker 3. The new behavior helps in case some library contains ES6 code, but in some cases it can lead to issues. To avoid running `babel-loader` in the `node_modules` folder, replicating the same behavior as Webpacker 3, the following code can be added to `config/webpack/environment.js`: @@ -87,7 +94,7 @@ nodeModulesLoader.exclude.push(/some-library/) // replace `some-library` with // the actual path to exclude ``` -### Source Maps are enabled by default +## Source Maps Enabled By Default Source maps are now enabled in production to make debugging in production easier. Enabling source maps doesn't have drawbacks for most of the applications since maps are compressed by default and aren't loaded by browsers unless Dev Tools are opened. @@ -102,7 +109,23 @@ environment.config.merge({ devtool: 'none' }) module.exports = environment.toWebpackConfig() ``` -### Example upgrades +## Namespaces + +The compiled packs in the public directory are now stored under namespaces: + +- JavaScripts are stored under `js` +- Stylesheets are stored under `css` +- Other resources are stored under `media` + +```rb +# Before +"runtime~hello_react" => "/packs/runtime~hello_react-da2baf7fd07b0e8b6d17.js" + +# After +"runtime~hello_react" => "/packs/js/runtime~hello_react-da2baf7fd07b0e8b6d17.js" +``` + +## Example Upgrades This is what an upgrade to Webpacker 4 looked like for existing Rails apps (please contribute yours!):