Skip to content
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

Closed
wants to merge 8 commits into from

Conversation

justin808
Copy link
Contributor

@justin808 justin808 commented Nov 28, 2021

What?

Let's have the webpack config for rails/webpacker match jsbundling-rails and use /webpack.config.js.

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.

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:

image

@justin808 justin808 self-assigned this Nov 28, 2021
@justin808 justin808 added enhancement javascript Pull requests that update Javascript code labels Nov 28, 2021
@justin808 justin808 added this to the 6.0 milestone Nov 28, 2021
justin808 added a commit to shakacode/react_on_rails_demo_ssr_hmr that referenced this pull request Nov 28, 2021
See rails/webpacker#3240

This commit shows the modest change needed for the upgrade to one config
file.
justin808 added a commit to shakacode/react_on_rails_demo_ssr_hmr that referenced this pull request Nov 28, 2021
See rails/webpacker#3240

This commit shows the modest change needed for the upgrade to one config
file.
@justin808 justin808 force-pushed the justin808-use-one-config branch from d3b99f7 to 792c161 Compare December 5, 2021 22:47
```

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:
Copy link

@frullah frullah Jan 1, 2022

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

Suggested change
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:

Copy link
Contributor Author

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. Upgrade the Webpacker Ruby gem and the NPM package
2. Upgrade the Webpacker Ruby gem and the NPM package

Copy link
Contributor Author

@justin808 justin808 Jan 3, 2022

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`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`.


17. Make sure that you can run `bin/webpack` without errors.
1. Make sure that you can run `bin/webpack` without errors.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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+.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@justin808
Copy link
Contributor Author

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 /webpack.config.js.

Agree? If so, I'll make the change.

CC: @guillaumebriday

@justin808 justin808 changed the title Use single webpack config for rails/webpacker Use single webpack config, /webpack.config.js for rails/webpacker, matching jsbundling-rails Jan 10, 2022
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.
@justin808
Copy link
Contributor Author

Merged: shakacode/shakapacker#5.

@justin808 justin808 deleted the justin808-use-one-config branch April 6, 2022 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement javascript Pull requests that update Javascript code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants