-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use single webpack config, /webpack.config.js for rails/webpacker, matching jsbundling-rails #3240
Conversation
See rails/webpacker#3240 This commit shows the modest change needed for the upgrade to one config file.
See rails/webpacker#3240 This commit shows the modest change needed for the upgrade to one config file.
d3b99f7
to
792c161
Compare
for peer deps change
Co-authored-by: Guillaume Briday <[email protected]>
This dries up the list of peers to package.json
docs/v6_upgrade.md
Outdated
``` | ||
|
||
1. There is now a single default configuration file of `config/webpack/webpack.config.js`. Previously, the config file was set | ||
to `config/webpack/#{NODE_ENV}.js`. In the `config/webpack/` directory, you can either refactor your code in `test.js`, `development.js`, and `production.js` to a single file, `config.webpack.js` or you can replace the contents of `config/webpack/config.webpack.js` to conditionally load the old file based on the NODE_ENV with this snippet: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think config.webpack.js
should be webpack.config.js
to `config/webpack/#{NODE_ENV}.js`. In the `config/webpack/` directory, you can either refactor your code in `test.js`, `development.js`, and `production.js` to a single file, `config.webpack.js` or you can replace the contents of `config/webpack/config.webpack.js` to conditionally load the old file based on the NODE_ENV with this snippet: | |
to `config/webpack/#{NODE_ENV}.js`. In the `config/webpack/` directory, you can either refactor your code in `test.js`, `development.js`, and `production.js` to a single file, `webpack.config.js` or you can replace the contents of `config/webpack/webpack.config.js` to conditionally load the old file based on the NODE_ENV with this snippet: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree! Per the docs: https://webpack.js.org/configuration/, webpack.config.js
is what's used.
Then consider moving your `app/javascript/packs/*` (including `application.js`) to `app/javascript/` and updating the configuration file. | ||
1. Ensure you have a clean working git branch. You will be overwriting all your files and reverting the changes that you don't want. | ||
|
||
1. Upgrade the Webpacker Ruby gem and the NPM package |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Upgrade the Webpacker Ruby gem and the NPM package | |
2. Upgrade the Webpacker Ruby gem and the NPM package |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using 1.
everywhere results in less merge conflicts!
@guillaumebriday, @dhh Do you agree that only 1
is better for a numbered markdown list?
|
||
module.exports = envSpecificConfig() | ||
``` | ||
1. Review the new default's changes to `webpacker.yml`. Consider each suggested change carefully, especially the change to have your `source_entry_path` be at the top level of your `source_path`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Review the new default's changes to `webpacker.yml`. Consider each suggested change carefully, especially the change to have your `source_entry_path` be at the top level of your `source_path`. | |
3. Review the new default's changes to `webpacker.yml`. Consider each suggested change carefully, especially the change to have your `source_entry_path` be at the top level of your `source_path`. |
|
||
2. **Ensure no nested directories in your `source_entry_path`.** Check if you had any entry point files in child directories of your `source_entry_path`. Files for entry points in child directories are not supported by rails/webpacker v6. Move those files to the top level, adjusting any imports in those files. | ||
1. **Ensure no nested directories in your `source_entry_path`.** Check if you had any entry point files in child directories of your `source_entry_path`. Files for entry points in child directories are not supported by rails/webpacker v6. Move those files to the top level, adjusting any imports in those files. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. **Ensure no nested directories in your `source_entry_path`.** Check if you had any entry point files in child directories of your `source_entry_path`. Files for entry points in child directories are not supported by rails/webpacker v6. Move those files to the top level, adjusting any imports in those files. | |
4. **Ensure no nested directories in your `source_entry_path`.** Check if you had any entry point files in child directories of your `source_entry_path`. Files for entry points in child directories are not supported by rails/webpacker v6. Move those files to the top level, adjusting any imports in those files. |
# Gemfile | ||
gem 'webpacker', '6.0.0.rc.5' | ||
``` | ||
1. Update `webpack-dev-server` to the current version, greater than 4.2, updating `package.json`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Update `webpack-dev-server` to the current version, greater than 4.2, updating `package.json`. | |
5. Update `webpack-dev-server` to the current version, greater than 4.2, updating `package.json`. |
```bash | ||
bundle install | ||
``` | ||
1. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` and **at most one call** to `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932). If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/javascript/application.js`, pass the option `defer: false` to your `javascript_pack_tag`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` and **at most one call** to `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932). If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/javascript/application.js`, pass the option `defer: false` to your `javascript_pack_tag`. | |
6. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` and **at most one call** to `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932). If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/javascript/application.js`, pass the option `defer: false` to your `javascript_pack_tag`. |
docs/v6_upgrade.md
Outdated
|
||
17. Make sure that you can run `bin/webpack` without errors. | ||
1. Make sure that you can run `bin/webpack` without errors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Make sure that you can run `bin/webpack` without errors. | |
16. Make sure that you can run `bin/webpack` without errors. |
|
||
18. Try running `RAILS_ENV=production bin/rails assets:precompile`. If all goes well, don't forget to clean the generated assets with `bin/rails assets:clobber`. | ||
1. Try running `RAILS_ENV=production bin/rails assets:precompile`. If all goes well, don't forget to clean the generated assets with `bin/rails assets:clobber`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Try running `RAILS_ENV=production bin/rails assets:precompile`. If all goes well, don't forget to clean the generated assets with `bin/rails assets:clobber`. | |
17. Test your production build locally by running `RAILS_ENV=production bin/rails assets:precompile`. If all goes well, don't forget to clean the generated assets with `bin/rails assets:clobber`. |
|
||
19. Run `yarn add webpack-dev-server` if those are not already in your dev dependencies. Make sure you're using v4+. | ||
1. Run `yarn add webpack-dev-server` if those are not already in your dev dependencies. Make sure you're using v4+. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Run `yarn add webpack-dev-server` if those are not already in your dev dependencies. Make sure you're using v4+. | |
18. Run `yarn add -D webpack-dev-server` if those are not already in your dev dependencies. Make sure you're using v4+. |
|
||
20. Try your app! | ||
1. Try your app! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Try your app! | |
19. Try your app! |
source_entry_path: / | ||
``` | ||
Then consider moving your `app/javascript/packs/*` (including `application.js`) to `app/javascript/` and updating the configuration file. | ||
1. Ensure you have a clean working git branch. You will be overwriting all your files and reverting the changes that you don't want. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Ensure you have a clean working git branch. You will be overwriting all your files and reverting the changes that you don't want. | |
1. Ensure you have a clean working git branch. You will be overwriting all your files and reverting the changes that you don't want. It's recommended removing `node_modules` directory and running `rails assets:clobber` before upgrading. |
I think that it's worth considering having the having the default webpack config match https://github.com/rails/jsbundling-rails (and webpack's default): So we can have the default be Agree? If so, I'll make the change. CC: @guillaumebriday |
Why? This allows easy updating of the dependencies. Let's lock at the major versions, and loosen as requested. See a working version of this: shakacode/react_on_rails_demo_ssr_hmr#23 See original PR: rails/webpacker#3234
Why? KISS, DRY 1. Simplicity and DRY. The default install was already cleaned up so there are no differences between installing test.js, development.js, and prododuction.js. This is just more clutter for people to dig through. 2. Avoid confusion. Most rails developers think about the RAILS_ENV. It's not at all obvious that old configuration file used was based on the NODE_ENV. And what if you wanted to adjust the configuration based on the RAILS_ENV. Surely, you could have gotten confused. Additionally, the default of using NODE_ENV for building a Node file was also non-obvious. 3. Easy upgrade. The upgrade from using the old files to the new configuration only requires copy/pasting a few lines of code into the new configuration file.
792c161
to
84c31d0
Compare
Merged: shakacode/shakapacker#5. |
What?
Let's have the webpack config for rails/webpacker match jsbundling-rails and use
/webpack.config.js
.Why?
KISS, DRY
there are no differences between installing test.js, development.js,
and prododuction.js. This is just more clutter for people to dig
through.
It's not at all obvious that old configuration file used was based on
the NODE_ENV. And what if you wanted to adjust the configuration based
on the RAILS_ENV. Surely, you could have gotten confused. Additionally,
the default of using NODE_ENV for building a Node file was also
non-obvious.
requires copy/pasting a few lines of code into the new configuration
file.
Demo of the migration
Single change in shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh
Visual of the current, more complicated flow
Here is a clip from my slides from my past RailsConf talk on the topic: