shakapacker 6.1.1 → 6.3.0.pre.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.node-version +1 -1
  3. data/CHANGELOG.md +48 -4
  4. data/CONTRIBUTING.md +2 -2
  5. data/Gemfile.lock +1 -1
  6. data/README.md +60 -26
  7. data/docs/customizing_babel_config.md +2 -0
  8. data/docs/deployment.md +2 -2
  9. data/docs/style_loader_vs_mini_css.md +48 -0
  10. data/docs/troubleshooting.md +18 -0
  11. data/docs/using_esbuild_loader.md +1 -1
  12. data/docs/using_swc_loader.md +2 -2
  13. data/docs/v6_upgrade.md +72 -74
  14. data/lib/install/config/webpacker.yml +16 -0
  15. data/lib/install/template.rb +2 -2
  16. data/lib/tasks/webpacker/clean.rake +1 -3
  17. data/lib/tasks/webpacker/clobber.rake +1 -3
  18. data/lib/tasks/webpacker/compile.rake +1 -4
  19. data/lib/webpacker/commands.rb +2 -2
  20. data/lib/webpacker/compiler.rb +12 -30
  21. data/lib/webpacker/configuration.rb +22 -4
  22. data/lib/webpacker/dev_server.rb +11 -2
  23. data/lib/webpacker/helper.rb +26 -8
  24. data/lib/webpacker/instance.rb +1 -1
  25. data/lib/webpacker/manifest.rb +4 -4
  26. data/lib/webpacker/railtie.rb +7 -0
  27. data/lib/webpacker/version.rb +1 -1
  28. data/lib/webpacker/version_checker.rb +152 -0
  29. data/package/__tests__/config.js +11 -0
  30. data/package/babel/preset.js +0 -1
  31. data/package/config.js +6 -0
  32. data/package/environments/base.js +1 -1
  33. data/package/inliningCss.js +1 -1
  34. data/package/rules/__tests__/file.js +35 -0
  35. data/package/rules/__tests__/index.js +11 -0
  36. data/package/rules/__tests__/raw.js +18 -0
  37. data/package/rules/file.js +2 -17
  38. data/package/rules/raw.js +2 -2
  39. data/package/swc/index.js +3 -3
  40. data/package.json +12 -11
  41. data/test/compiler_test.rb +27 -32
  42. data/test/configuration_test.rb +48 -2
  43. data/test/fixtures/beta_package.json +13 -0
  44. data/test/fixtures/git_url_package.json +13 -0
  45. data/test/fixtures/github_url_package.json +13 -0
  46. data/test/fixtures/normal_package.json +13 -0
  47. data/test/fixtures/relative_path_package.json +13 -0
  48. data/test/fixtures/semver_caret_package.json +13 -0
  49. data/test/fixtures/semver_tilde_package.json +13 -0
  50. data/test/fixtures/without_package.json +13 -0
  51. data/test/helper_test.rb +34 -12
  52. data/test/manifest_test.rb +3 -3
  53. data/test/test_app/config/webpacker.yml +4 -0
  54. data/test/test_app/config/webpacker_manifest_path.yml +80 -0
  55. data/test/test_app/config/webpacker_no_precompile.yml +7 -0
  56. data/test/version_checker_test.rb +271 -0
  57. data/test/webpacker_test.rb +15 -0
  58. data/yarn.lock +917 -884
  59. metadata +32 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e8eb4a9da593290c85f4a4225565a8ebdde75e1a994be209a319da4e85951de
4
- data.tar.gz: a662b202fa970d2f87713e7f1cb4fbf39b584d6638eb0d05c57a923a024e41d8
3
+ metadata.gz: 862dd1243830eb271661d0a7be8ad35b4837ab8aaabfa2a39cc9a65e5c6d01f3
4
+ data.tar.gz: a9b58deb1b8038aecbac2df1ca3516d59c03dfd2cc5a6a71e06edf480ba3443c
5
5
  SHA512:
6
- metadata.gz: 64c7b38f28f82090984e322e276a15c093acf086262f5dd043e712613aedaf6196bbefa0413679e4629ec8ef547861cb5a9e032b30685c2dae6a6756cb2aa0f8
7
- data.tar.gz: e0849b58c3025876469b6db0166df9c0496c7cac0c6b3c8a64c91959c61153b2e2e6acc596bf41a9cf6a02fd60c0e4be64e51513059e0b0893195ead4778cf3c
6
+ metadata.gz: c03da71f446adc4f470acdca8de67487ff7934650abca9278a4e26c854329432d6a94b02368f5825cca0214f1fb878399711114b67c5f8e1dbb9d27620cf22b9
7
+ data.tar.gz: 900fdb88440208f4860c7255b5ca71ba316d6ba69223f720a67b4564663f633eb11e31403b41247c94e5755488804dd9f6745b9ddbfb6c82c5f928ec360abdd5
data/.node-version CHANGED
@@ -1 +1 @@
1
- 16
1
+ 16.14.0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,6 @@
1
- * For versions prior to v6, see the [5.x stable branch of rails/webpacker](https://github.com/rails/webpacker/tree/5-x-stable).
2
- * Please see [v6 Upgrade Guide](./docs/v6_upgrade.md) to go from version prior to v6.
1
+ * For the changelog of versions prior to v6, see the [5.x stable branch of rails/webpacker](https://github.com/rails/webpacker/tree/5-x-stable).
2
+ * Please see the [v6 Upgrade Guide](./docs/v6_upgrade.md) to go from versions prior to v6.
3
+ * [ShakaCode](https://www.shakacode.com) offers support for upgrading from webpacker or using Shakapacker. If interested, contact [justin@shakacode.com](mailto:justin@shakacode.com).
3
4
 
4
5
  ## Versions
5
6
  ## [Unreleased]
@@ -7,6 +8,46 @@ Changes since last non-beta release.
7
8
 
8
9
  *Please add entries here for your pull requests that are not yet released.*
9
10
 
11
+ ## [v6.3.0-rc.1] - April 24, 2024
12
+
13
+ ### Changed
14
+ - Remove Loose mode from the default @babel-preset/env configuration. [PR 107](https://github.com/shakacode/shakapacker/pull/107) by [Jeremy Liberman](https://github.com/MrLeebo).
15
+
16
+ Loose mode compiles the bundle down to be compatible with ES5, but saves space by skipping over behaviors that are considered edge cases. Loose mode can affect how your code runs in a variety of ways, but in newer versions of Babel it's better to use [Compiler Assumptions](https://babeljs.io/docs/en/assumptions) to have finer-grained control over which edge cases you're choosing to ignore.
17
+
18
+ This change may increase the file size of your bundles, and may change some behavior in your app if your code touches upon one of the edge cases where Loose mode differs from native JavaScript. There are notes in the linked PR about how to turn Loose mode back on if you need to, but consider migrating to Compiler Assumptions when you can. If you have already customized your babel config, this change probably won't affect you.
19
+
20
+ ### Added
21
+ - Adds `webpacker_precompile` setting to `webpacker.yml` to allow controlling precompile behaviour, similar to existing `ENV["WEBPACKER_PRECOMPILE"]` variable. [PR 102](https://github.com/shakacode/shakapacker/pull/102) by [Judahmeek](https://github.com/Judahmeek).
22
+ - Adds `append_javascript_pack_tag` helper. Allows for easier usage and coordination of multiple javascript packs. [PR 94](https://github.com/shakacode/shakapacker/pull/94) by [tomdracz](https://github.com/tomdracz).
23
+
24
+ ### Improved
25
+ - Use last modified timestamps rather than file digest to determine compiler freshness. [PR 97](https://github.com/shakacode/shakapacker/pull/97) by [tomdracz](https://github.com/tomdracz).
26
+
27
+ Rather than calculating SHA digest of all the files in the paths watched by the compiler, we are now comparing the modified time of the `manifest.json` file versues the latest modified timestamp of files and directories in watched paths. Unlike calculating digest, which only looked at the files, the new calculation also considers directory timestamps, including the parent ones (i.e. `config.source_path` folder timestamp will be checked together will timestamps of all files and directories inside of it).
28
+
29
+ This change should result in improved compiler checks performance but might be breaking for certain setups and edge cases. If you encounter any issues, please report them at https://github.com/shakacode/shakapacker/issues.
30
+
31
+ - Bump dependency versions in package.json to address security vulnerabilities. [PR 109](https://github.com/shakacode/shakapacker/pull/109) by [tomdracz](https://github.com/tomdracz).
32
+ - Add `webpack-dev-server` as `peerDependency` to make its usage clear. [PR 109](https://github.com/shakacode/shakapacker/pull/109) by [tomdracz](https://github.com/tomdracz).
33
+
34
+ ## [v6.2.1] - April 15, 2022
35
+ ### Fixed
36
+ - Put back config.public_manifest_path, removed in 6.2.0 in PR 78. [PR 104](https://github.com/shakacode/shakapacker/pull/104) by [justin808](https://github.com/justin808).
37
+
38
+ ## [v6.2.0] - March 22, 2022
39
+
40
+ ### Added
41
+ - Make manifest_path configurable, to keep manifest.json private if desired. [PR 78](https://github.com/shakacode/shakapacker/pull/78) by [jdelStrother](https://github.com/jdelStrother).
42
+ - Rewrite webpack module rules as regular expressions. Allows for easy iteration during config customization. [PR 60](https://github.com/shakacode/shakapacker/pull/60) by [blnoonan](https://github.com/blnoonan).
43
+ - Initialization check to ensure shakapacker gem and NPM package version are consistent. Opt-in behaviour enabled by setting `ensure_consistent_versioning` configuration variable. [PR 51](https://github.com/shakacode/shakapacker/pull/51) by [tomdracz](https://github.com/tomdracz).
44
+ - Add `dev_server.inline_css: bool` config option to allow for opting out of style-loader and into mini-extract-css-plugin for CSS HMR in development. [PR 69](https://github.com/shakacode/shakapacker/pull/69) by [cheald](https://github.com/cheald).
45
+
46
+ ### Improved
47
+ - Increase default connect timeout for dev server connections, establishing connections more reliably for busy machines. [PR 74](https://github.com/shakacode/shakapacker/pull/74) by [stevecrozz](https://github.com/stevecrozz).
48
+ - Allow multiple invocations of stylesheet_pack_tag (eg for a regular stylesheet & a print stylesheet). [PR 82](https://github.com/shakacode/shakapacker/pull/82) by [jdelStrother](https://github.com/jdelStrother).
49
+ - Tweak swc config for parity with Babel. [PR 79](https://github.com/shakacode/shakapacker/pull/79) by [dleavitt](https://github.com/dleavitt).
50
+
10
51
  ## [v6.1.1] - February 6, 2022
11
52
 
12
53
  ### Added
@@ -74,8 +115,11 @@ Changes since last non-beta release.
74
115
  ## v5.4.3 and prior changes from rails/webpacker
75
116
  See [CHANGELOG.md in rails/webpacker (up to v5.4.3)](https://github.com/rails/webpacker/blob/master/CHANGELOG.md)
76
117
 
77
- [Unreleased]: https://github.com/shakacode/shakapacker/compare/v6.1.1...master
78
- [v6.1.1]: https://github.com/shakacode/shakapacker/compare/v6.1.1...v6.1.1
118
+ [Unreleased]: https://github.com/shakacode/shakapacker/compare/v6.3.0-rc.1...master
119
+ [v6.3.0-rc.0]: https://github.com/shakacode/shakapacker/compare/v6.2.1...v6.3.0-rc.1
120
+ [v6.2.1]: https://github.com/shakacode/shakapacker/compare/v6.2.0...v6.2.1
121
+ [v6.2.0]: https://github.com/shakacode/shakapacker/compare/v6.1.1...v6.2.0
122
+ [v6.1.1]: https://github.com/shakacode/shakapacker/compare/v6.1.0...v6.1.1
79
123
  [v6.1.0]: https://github.com/shakacode/shakapacker/compare/v6.0.2...v6.1.0
80
124
  [v6.0.2]: https://github.com/shakacode/shakapacker/compare/v6.0.1...v6.0.2
81
125
  [v6.0.1]: https://github.com/shakacode/shakapacker/compare/v6.0.0...v6.0.1
data/CONTRIBUTING.md CHANGED
@@ -56,7 +56,7 @@ If you change the generator, check that install instructions work.
56
56
 
57
57
  1. Update the gemfile so that gem "webpacker" has a line like this, pointing to your install of webpacker
58
58
  ```ruby
59
- gem 'webpacker', path: "~/shakacode/forks/webpacker"
60
- ```
59
+ gem 'webpacker', path: "~/shakacode/forks/shakapacker"
60
+ ```
61
61
  2. `bundle`
62
62
  3. Run the generator confirm that you got the right changes.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shakapacker (6.1.0)
4
+ shakapacker (6.2.1)
5
5
  activesupport (>= 5.2)
6
6
  rack-proxy (>= 0.6.1)
7
7
  railties (>= 5.2)
data/README.md CHANGED
@@ -1,9 +1,8 @@
1
1
  # Shakapacker
2
2
 
3
- _Official, actively maintained successor to [rails/webpacker](https://github.com/rails/webpacker). Internal naming for `shakapacker` will continue to use `webpacker` where possible for v6. ShakaCode stands behind long-term maintainence and development of this project for the Rails community._
3
+ _Official, actively maintained successor to [rails/webpacker](https://github.com/rails/webpacker). Internal naming for `shakapacker` will continue to use `webpacker` where possible for v6. ShakaCode stands behind the long-term maintainence and development of this project for the Rails community._
4
4
 
5
-
6
- * See [V6 Upgrade](./docs/v6_upgrade.md) for upgrading from v5 or prior v6 releases.
5
+ * See [V6 Upgrade](./docs/v6_upgrade.md) for upgrading from v5 or prior v6 releases.
7
6
 
8
7
  [![Ruby specs](https://github.com/shakacode/shakapacker/workflows/Ruby%20specs/badge.svg)](https://github.com/shakacode/shakapacker/actions)
9
8
  [![Jest specs](https://github.com/shakacode/shakapacker/workflows/Jest%20specs/badge.svg)](https://github.com/shakacode/shakapacker/actions)
@@ -18,13 +17,16 @@ Webpacker makes it easy to use the JavaScript pre-processor and bundler [Webpack
18
17
  to manage application-like JavaScript in Rails. It can coexist with the asset pipeline,
19
18
  leaving Webpack responsible solely for app-like JavaScript, or it can be used exclusively, making it also responsible for images, fonts, and CSS.
20
19
 
21
- Check out 6.1.0.beta.0 for [SWC](https://swc.rs/) support! It's way faster than babel.
20
+ Check out 6.1.1+ for [SWC](https://swc.rs/) and [esbuild-loader](https://github.com/privatenumber/esbuild-loader) support! They are faster than Babel!
22
21
 
23
22
  See a comparison of [webpacker with jsbundling-rails](https://github.com/rails/jsbundling-rails/blob/main/docs/comparison_with_webpacker.md).
24
23
 
25
- Discussion forums to discuss debugging and troubleshooting tips. Please open issues for bugs and feature requests:
26
- 1. [rails/webpacker discussion forum](https://discuss.rubyonrails.org/c/webpacker/10)
27
- 2. [Discussions tab](https://github.com/shakacode/shakapacker/discussions)
24
+ Discussion forum and Slack to discuss debugging and troubleshooting tips. Please open issues for bugs and feature requests:
25
+ 1. [Discussions tab](https://github.com/shakacode/shakapacker/discussions)
26
+ 2. [Slack discussion channel](https://reactrails.slack.com/join/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE)
27
+ 3. [Tweets with tag `#shakapacker`](https://twitter.com/hashtag/shakapacker?src=hashtag_click)
28
+
29
+ [ShakaCode](https://www.shakacode.com) offers support for upgrading from webpacker or using Shakapacker. If interested, contact [justin@shakacode.com](mailto:justin@shakacode.com). ShakaCode is [hiring passionate engineers](https://jobs.lever.co/shakacode/3bdbfdb3-4495-4611-a279-01dddb351abe) that love open source.
28
30
 
29
31
  ---
30
32
 
@@ -78,12 +80,12 @@ Discussion forums to discuss debugging and troubleshooting tips. Please open iss
78
80
  - Yarn
79
81
 
80
82
  ## Features
81
- - Rails view helpers that fully support webpack output
83
+ - Rails view helpers that fully support Webpack output, including HMR and code splitting.
82
84
  - Convenient but not required webpack configuration. The only requirement is that your webpack configuration create a manifest.
83
85
  - HMR with the webpack-dev-server, such as for hot-reloading for React!
84
86
  - Automatic code splitting using multiple entry points to optimize JavaScript downloads
85
- - [Webpack v5](https://webpack.js.org/)
86
- - ES6 with [babel](https://babeljs.io/)
87
+ - [Webpack v5+](https://webpack.js.org/)
88
+ - ES6 with [babel](https://babeljs.io/), [SWC](https://swc.rs/), or [Esbuild](https://github.com/privatenumber/esbuild-loader)
87
89
  - Asset compression, source-maps, and minification
88
90
  - CDN support
89
91
  - Extensible and configurable. For example, all major dependencies are specified as peers, so you can upgrade easily.
@@ -107,11 +109,10 @@ rails new myapp --skip-javascript
107
109
 
108
110
  _Note, Rails 6 installs the older v5 version of webpacker unless you specify `--skip-javascript`._
109
111
 
110
- Update your `Gemfile`:
112
+ Add `shakapacker` gem to your `Gemfile`:
111
113
 
112
- ```ruby
113
- # Gemfile
114
- gem 'shakapacker', '~> 6.0'
114
+ ```bash
115
+ bundle add shakapacker --strict
115
116
  ```
116
117
 
117
118
  Then running the following to install Webpacker:
@@ -208,9 +209,29 @@ The result looks like this:
208
209
  <%= stylesheet_pack_tag 'map' %>
209
210
  ```
210
211
 
212
+ However, you may use multiple calls to stylesheet_pack_tag if, say, you require multiple <style> tags for different output media:
213
+
214
+ ``` erb
215
+ <%= stylesheet_pack_tag 'application', media: 'screen' %>
216
+ <%= stylesheet_pack_tag 'print', media: 'print' %>
217
+ ```
218
+
219
+ You can also use `append_javascript_pack_tag` helper to bundle additional tags together when calling `javascript_pack_tag`:
220
+
221
+ ```erb
222
+ <% append_javascript_pack_tag 'calendar', 'map' %>
223
+ <%= javascript_pack_tag 'application' %>
224
+ ```
225
+
226
+ In the example above, `calendar` and `map` tags will be bundled together with `application` and deduplicated once `javascript_pack_tag` is used.
227
+
228
+ **Important:** `append_javascript_pack_tag` can be used anywhere in your application as long as it is executed BEFORE the `javascript_pack_tag`. If you attempt to call `append_javascript_pack_tag` helper after `javascript_pack_tag`, an error will be raised. You should aim to have only single `javascript_pack_tag` invocation in your page load.
229
+
230
+ For alternative options of setting the additional packs, [see this discussion](https://github.com/shakacode/shakapacker/issues/39).
231
+
211
232
  If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
212
233
  ```erb
213
- <img src="<%= asset_pack_path 'images/logo.svg' %>" />
234
+ <img src="<%= asset_pack_path 'static/logo.svg' %>" />
214
235
  ```
215
236
 
216
237
  Or use the dedicated helper:
@@ -237,7 +258,7 @@ If you want to use images in your stylesheets:
237
258
  }
238
259
  ```
239
260
  ### Defer for `javascript_pack_tag`
240
- Note, the default of "defer" for the `javascript_pack_tag`. You can override that to `false`. If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/packs/entrypoints/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
261
+ Note, the default of "defer" for the `javascript_pack_tag`. You can override that to `false`. 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`.
241
262
 
242
263
  ### Server-Side Rendering (SSR)
243
264
 
@@ -251,6 +272,8 @@ Webpacker ships with two binstubs: `./bin/webpacker` and `./bin/webpacker-dev-se
251
272
 
252
273
  In development, Webpacker compiles on demand rather than upfront by default. This happens when you refer to any of the pack assets using the Webpacker helper methods. This means that you don't have to run any separate processes. Compilation errors are logged to the standard Rails log. However, this auto-compilation happens when a web request is made that requires an updated webpack build, not when files change. Thus, that can be painfully slow for front-end development in this default way. Instead, you should either run the `bin/webpacker --watch` or run `./bin/webpacker-dev-server`
253
274
 
275
+ **Note:** If you are not using webpack dev server, your packs will be served by Rails public file server. If you've enabled caching (Rails application `config.action_controller.perform_caching` setting), your changes will likely not be picked up due to `Cache-Control` header being set and assets being cached in browser memory. For more details see the [issue #88](https://github.com/shakacode/shakapacker/issues/88).
276
+
254
277
  If you want to use live code reloading, or you have enough JavaScript that on-demand compilation is too slow, you'll need to run `./bin/webpacker-dev-server` or `ruby ./bin/webpacker-dev-server`. Windows users will need to run these commands in a terminal separate from `bundle exec rails s`. This process will watch for changes in the relevant files, defined by `webpacker.yml` configuration settings for `source_path`, `source_entry_path`, and `additional_paths`, and it will then automatically reload the browser to match. This feature is also known as [Hot Module Replacement](https://webpack.js.org/concepts/hot-module-replacement/).
255
278
 
256
279
  ```bash
@@ -278,13 +301,13 @@ Once you start this webpack development server, Webpacker will automatically sta
278
301
  You can use environment variables as options supported by [webpack-dev-server](https://webpack.js.org/configuration/dev-server/) in the form `WEBPACKER_DEV_SERVER_<OPTION>`. Please note that these environmental variables will always take precedence over the ones already set in the configuration file, and that the _same_ environmental variables must be available to the `rails server` process.
279
302
 
280
303
  ```bash
281
- WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpacker-dev-server
304
+ WEBPACKER_DEV_SERVER_PORT=4305 WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpacker-dev-server
282
305
  ```
283
306
 
284
- By default, the webpack dev server listens on `localhost` in development for security purposes. However, if you want your app to be available over local LAN IP or a VM instance like vagrant, you can set the `host` when running `./bin/webpacker-dev-server` binstub:
307
+ By default, the webpack dev server listens on `localhost:3035` in development for security purposes. However, if you want your app to be available on port 4035 over local LAN IP or a VM instance like vagrant, you can set the `port` and `host` when running `./bin/webpacker-dev-server` binstub:
285
308
 
286
309
  ```bash
287
- WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpacker-dev-server
310
+ WEBPACKER_DEV_SERVER_PORT=4305 WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpacker-dev-server
288
311
  ```
289
312
 
290
313
  **Note:** You need to allow webpack-dev-server host as an allowed origin for `connect-src` if you are running your application in a restrict CSP environment (like Rails 5.2+). This can be done in Rails 5.2+ in the CSP initializer `config/initializers/content_security_policy.rb` with a snippet like this:
@@ -300,7 +323,7 @@ end
300
323
 
301
324
  ### Webpack Configuration
302
325
 
303
- First, you don't _need_ to use Shakapacker's webpack configuration. However, the `shakapacker` NPM package provides convenient access to configuration code that reads the `config/webpacker.yml` file which the view helpers also use. If you have your own customized webpack configuration, at the mininmum, you must ensure:
326
+ First, you don't _need_ to use Shakapacker's webpack configuration. However, the `shakapacker` NPM package provides convenient access to configuration code that reads the `config/webpacker.yml` file which the view helpers also use. If you have your own customized webpack configuration, at the minimum, you must ensure:
304
327
 
305
328
  1. Your output files go the right directory
306
329
  2. You provide a manifest, via package [`webpack-assets-manifest`](https://github.com/webdeveric/webpack-assets-manifest) that maps output names (your 'packs') to the fingerprinted versions, including bundle-splitting dependencies. That's the main secret sauce of webpacker!
@@ -369,6 +392,13 @@ console.log(webpackConfig.source_path)
369
392
  console.log(JSON.stringify(webpackConfig, undefined, 2))
370
393
  ```
371
394
 
395
+ You may want to modify rules in the default configuration. For instance, if you are using a custom svg loader, you may want to remove `.svg` from the default file loader rules. You can search and filter the default rules like so:
396
+
397
+ ```js
398
+ const svgRule = config.module.rules.find(rule => rule.test.test('.svg'));
399
+ svgRule.test = svgRule.test.filter(t => !t.test('.svg'))
400
+ ```
401
+
372
402
  ### Babel configuration
373
403
 
374
404
  By default, you will find the Webpacker preset in your `package.json`. Note, you need to use the new NPM package name, `shakapacker`.
@@ -456,7 +486,7 @@ Add tsconfig.json
456
486
  "moduleResolution": "node",
457
487
  "baseUrl": ".",
458
488
  "paths": {
459
- "*": ["node_modules/*", "app/packs/*"]
489
+ "*": ["node_modules/*", "app/javascript/*"]
460
490
  },
461
491
  "sourceMap": true,
462
492
  "target": "es5",
@@ -645,13 +675,13 @@ Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
645
675
 
646
676
  By default, Webpacker ships with simple conventions for where the JavaScript app files and compiled webpack bundles will go in your Rails app. All these options are configurable from `config/webpacker.yml` file.
647
677
 
648
- The configuration for what webpack is supposed to compile by default rests on the convention that every file in `app/packs/entrypoints/*`**(default)** or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration is turned into their own output files (or entry points, as webpack calls it). Therefore you don't want to put anything inside `packs` directory that you do not want to be an entry file. As a rule of thumb, put all files you want to link in your views inside "packs" directory and keep everything else under `app/packs`.
678
+ The configuration for what webpack is supposed to compile by default rests on the convention that every file in `app/javascript/`**(default)** or whatever path you set for `source_entry_path` in the `webpacker.yml` configuration is turned into their own output files (or entry points, as webpack calls it). Therefore you don't want to put any file inside `app/javascript` directory that you do not want to be an entry file. As a rule of thumb, put all files you want to link in your views inside "app/javascript/" directory and keep everything else under subdirectories like `app/javascript/controllers`.
649
679
 
650
- Suppose you want to change the source directory from `app/packs` to `frontend` and output to `assets/packs`. This is how you would do it:
680
+ Suppose you want to change the source directory from `app/javascript` to `frontend` and output to `assets/packs`. This is how you would do it:
651
681
 
652
682
  ```yml
653
683
  # config/webpacker.yml
654
- source_path: frontend # packs are in frontend/packs
684
+ source_path: frontend # packs are the files in frontend/
655
685
  public_output_path: assets/packs # outputs to => public/assets/packs
656
686
  ```
657
687
 
@@ -665,7 +695,9 @@ development:
665
695
  port: 3035
666
696
  ```
667
697
 
668
- If you have `hmr` turned to true, then the `stylesheet_pack_tag` generates no output, as you will want to configure your styles to be inlined in your JavaScript for hot reloading. During production and testing, the `stylesheet_pack_tag` will create the appropriate HTML tags.
698
+ If you have `hmr` turned to true and `inline_css` is not false, then the `stylesheet_pack_tag` generates no output, as you will want to configure your styles to be inlined in your JavaScript for hot reloading. During production and testing, the `stylesheet_pack_tag` will create the appropriate HTML tags.
699
+
700
+ If you want to have HMR and separate link tags, set `hmr: true` and `inline_css: false`. This will cause styles to be extracted and reloaded with the `mini-css-extract-plugin` loader. Note that in this scenario, you do not need to include style-loader in your project dependencies.
669
701
 
670
702
  ### Additional paths
671
703
 
@@ -686,13 +718,15 @@ import 'images/rails.png'
686
718
 
687
719
  **Note:** Please be careful when adding paths here otherwise it will make the compilation slow, consider adding specific paths instead of whole parent directory if you just need to reference one or two modules
688
720
 
689
- **Also note:** While importing assets living outside your `source_path` defined in webpacker.yml (like, for instance, assets under `app/assets`) from within your packs using _relative_ paths like `import '../../assets/javascripts/file.js'` will work in development, Webpacker won't recompile the bundle in production unless a file that lives in one of it's watched paths has changed (check out `Webpacker::Compiler#watched_files_digest`). That's why you'd need to add `app/assets` to the additional_paths as stated above and use `import 'javascripts/file.js'` instead.
721
+ **Also note:** While importing assets living outside your `source_path` defined in webpacker.yml (like, for instance, assets under `app/assets`) from within your packs using _relative_ paths like `import '../../assets/javascripts/file.js'` will work in development, Webpacker won't recompile the bundle in production unless a file that lives in one of it's watched paths has changed (check out `Webpacker::Compiler#latest_modified_timestamp`). That's why you'd need to add `app/assets` to the additional_paths as stated above and use `import 'javascripts/file.js'` instead.
690
722
 
691
723
 
692
724
  ## Deployment
693
725
 
694
726
  Webpacker hooks up a new `webpacker:compile` task to `assets:precompile`, which gets run whenever you run `assets:precompile`. If you are not using Sprockets, `webpacker:compile` is automatically aliased to `assets:precompile`. Similar to sprockets both rake tasks will compile packs in production mode but will use `RAILS_ENV` to load configuration from `config/webpacker.yml` (if available).
695
727
 
728
+ This behavior is optional & can be disabled by either setting an `WEBPACKER_PRECOMPILE` environment variable to `false`, `no`, `n`, or `f`, or by setting a `webpacker_precompile` key in your `webpacker.yml` to `false`. ([source code](./lib/webpacker/configuration.rb#L30))
729
+
696
730
  When compiling assets for production on a remote server, such as a continuous integration environment, it's recommended to use `yarn install --frozen-lockfile` to install NPM packages on the remote host to ensure that the installed packages match the `yarn.lock` file.
697
731
 
698
732
  If you are using a CDN setup, webpacker will use the configured [asset host](https://guides.rubyonrails.org/configuring.html#rails-general-configuration) value to prefix URLs for images or font icons which are included inside JS code or CSS. It is possible to override this value during asset compilation by setting the `WEBPACKER_ASSET_HOST` environment variable.
@@ -29,7 +29,9 @@ yarn add --dev @pmmmwh/react-refresh-webpack-plugin react-refresh
29
29
  module.exports = function (api) {
30
30
  const defaultConfigFunc = require('shakapacker/package/babel/preset.js')
31
31
  const resultConfig = defaultConfigFunc(api)
32
+ const isDevelopmentEnv = api.env('development')
32
33
  const isProductionEnv = api.env('production')
34
+ const isTestEnv = api.env('test')
33
35
 
34
36
  const changesOnDefault = {
35
37
  presets: [
data/docs/deployment.md CHANGED
@@ -93,10 +93,10 @@ you don't need to do anything extra for Webpacker, it just works.
93
93
 
94
94
  ### Assets compiling on every deployment even if JavaScript and CSS files are not changed
95
95
 
96
- Make sure you have `public/packs` and `node_modules` in `:linked_dirs`
96
+ Make sure you have your public outputh path (default `public/packs`), the webpacker cache path (default `tmp/webpacker`) and `node_modules` in `:linked_dirs`
97
97
 
98
98
  ```ruby
99
- append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/packs", ".bundle", "node_modules"
99
+ append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "tmp/webpacker", "public/packs", ".bundle", "node_modules"
100
100
  ```
101
101
 
102
102
  If you have `node_modules` added to `:linked_dirs` you'll need to run yarn install before `deploy:assets:precompile`, so you can add this code snippet at the bottom deploy.rb
@@ -0,0 +1,48 @@
1
+ # CSS Delivery In Development
2
+
3
+ You have two options for serving CSS in development:
4
+
5
+ 1. You can opt to serve CSS via style-loader (as was traditionally done in a Webpack setup), where CSS is written by Javascript served by Webpacker into a `<style>` tag, or
6
+ 2. You can opt to serve CSS as a full CSS file via mini-css-extract-plugin, which uses a standard `<link>` tag to load a fully separate CSS file.
7
+
8
+ Both options support HMR. The default is style-loader. If you want to use mini-css-extract-plugin in development, set `inline_css: false` in the development dev_server section of your webpacker.yml:
9
+
10
+ ```yml
11
+ development:
12
+ <<: *default
13
+ dev_server:
14
+ hmr: true
15
+ inline_css: false # Use mini-css-extract-plugin for CSS delivery
16
+ ```
17
+
18
+ ## Why would I pick style-loader?
19
+
20
+ style-loader is how you are probably are used to serving CSS in development with HMR in Webpack.
21
+
22
+ ### benefits
23
+
24
+ * No [Flash Of Unstyled Content (FOUC)](https://en.wikipedia.org/wiki/Flash_of_unstyled_content) on HMR refreshes
25
+ * Smaller/faster incremental updates.
26
+
27
+ ### drawbacks
28
+
29
+ * Inflated JS deliverable size; requires JS execution before CSS is available
30
+ * FOUC on initial page load
31
+ * Adds an extra dependency
32
+ * Divergence in delivery mechanism from production
33
+
34
+ ## Why would I pick mini-extract-css-plugin?
35
+
36
+ mini-css-extract-plugin's behavior is much more true to a production deployment's behavior, in that CSS is loaded via `link rel=stylsheet` tags, rather than injected by Javascript into `style` tags.
37
+
38
+ ### benefits
39
+
40
+ * Required for production, so it's going to be in play anyhow. Using only it simplifies the config and eliminates the style-loader dependency.
41
+ * No FOUC on initial page loads
42
+ * CSS delivered via `<link>` tags matches the mechanism used in production (I have been guilty of omitting my `stylesheet_pack_tag` for my first deploy because CSS worked fine with just the `javascript_pack_tag` in development.)
43
+
44
+ ### drawbacks
45
+
46
+ * Invokes a separate HTTP request, compared to style-loader
47
+ * Potential for FOUC on HMR refreshes
48
+ * More data transferred per refresh (full stylesheet reload, rather than just an incremental patch). Not likely to be noticed for local development, but still a technical difference. This may only be the case [when you're using local CSS modules](https://github.com/webpack-contrib/mini-css-extract-plugin/blob/master/src/hmr/hotModuleReplacement.js#L267-L273).
@@ -17,6 +17,24 @@
17
17
 
18
18
  4. You can also pass additional options to the command to run the webpack-dev-server and start the webpack-dev-server with the option `--debug-webpacker`
19
19
 
20
+ ## Incorrect peer dependencies
21
+ The latest version of Shakapacker uses peer dependencies to make upgrading easier. However, there's a catch.
22
+
23
+ If you fail to update peer dependencies, you'll see an easy-to-overlook warning from `yarn install`.
24
+
25
+ ```
26
+ warning " > shakapacker@6.1.1" has incorrect peer dependency "compression-webpack-plugin@^9.0.0".
27
+ ```
28
+
29
+ This omission resulted in an error in the browser:
30
+ ```
31
+ Failed to load resource: net::ERR_CONTENT_DECODING_FAILED
32
+ ```
33
+
34
+ The error was caused by an old version of the peer dependency webpack-compression-plugin.
35
+
36
+ So, be sure to investigate warnings from `yarn install`!
37
+
20
38
  ## ENOENT: no such file or directory - node-sass
21
39
 
22
40
  If you get the error `ENOENT: no such file or directory - node-sass` on deploy with
@@ -24,7 +24,7 @@ To use esbuild as your transpiler today. You need to do two things:
24
24
  1. Make sure you've installed `esbuild` and `esbuild-loader` packages.
25
25
 
26
26
  ```
27
- yarn add -D esbuild esbuild-loader
27
+ yarn add esbuild esbuild-loader
28
28
  ```
29
29
 
30
30
  2. Add or change `webpacker_loader` value in your default `webpacker.yml` config to `esbuild`
@@ -19,10 +19,10 @@ In order to use SWC as your compiler today. You need to do two things:
19
19
  1. Make sure you've installed `@swc/core` and `swc-loader` packages.
20
20
 
21
21
  ```
22
- yarn add -D @swc/core swc-loader
22
+ yarn add @swc/core swc-loader
23
23
  ```
24
24
 
25
- 2. Add or change `webpacker_loader` value in your default `webpacker.yml` config to `swc`
25
+ 2. Add or change `webpack_loader` value in your default `webpacker.yml` config to `swc`
26
26
  The default configuration of babel is done by using `package.json` to use the file within the `shakapacker` package.
27
27
 
28
28
  ```yml