shakapacker 6.2.0 → 6.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6e31a49e348719f7663178aee3ec877d3a53f6bd9e264fd8e82278ac17070e7
4
- data.tar.gz: c2d9294063be7bab761baf8f10eeb82c350dfcc0bfc322cac52fa06fdb4cbef5
3
+ metadata.gz: ceac7ca1c43918c99f5c5f50eec4f34fc591ef9de90bac0f2a35c7d0e7c4cc69
4
+ data.tar.gz: 4f571c49df398b3ff2dfd3faf476908c4492b361a9ebaf64d4c91e4c6074df5e
5
5
  SHA512:
6
- metadata.gz: 6cb3b71113fd1c7aad90098a4b0927830347feafe9cb9ef245aa6551950d67b9488be5d9aa411fcb6f775260856189d9cbfc5899732113fffbd7549aee84b7ef
7
- data.tar.gz: 1729dccb33de9f34a2c3ca26268c79255e813a71fae548acd5066d7faaef30e4a58123955a32e7b48c19bb988cd078f407cbdcebae6bcf817522451914fbac7e
6
+ metadata.gz: 4bcccce7cc22a42f20ac8738904a97d8288abb862a5d0c98e487393450d60a6a703559aa1b3add2f70f5211643fb02769ca16ef118cee5870f7de580a07cfcbb
7
+ data.tar.gz: 6f27881ee63c1cc94c416c9d59e8354a3425707e055b035bb89d1da86905c2ae876e3d329da59f9015f29f3219f716f042dccabaf563a01cf327a0d8be89b41e
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,43 @@ 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] - May 19, 2022
12
+
13
+ ### Improved
14
+ - Add ability to configure usage of either last modified timestamp and digest strategies when checking asset freshness. [PR 112](https://github.com/shakacode/shakapacker/pull/112) by [tomdracz](https://github.com/tomdracz).
15
+
16
+ ### Fixed
17
+ - On Windows CSS urls no longer contain backslashes resulting in 404 errors. [PR 115](https://github.com/shakacode/shakapacker/pull/115) by [daniel-rikowski](https://github.com/daniel-rikowski).
18
+
19
+ ## [v6.3.0-rc.1] - April 24, 2022
20
+
21
+ Note: [Rubygem is 6.3.0.pre.rc.1](https://rubygems.org/gems/shakapacker/versions/6.3.0.pre.rc.1) and [NPM is 6.3.0-rc.1](https://www.npmjs.com/package/shakapacker/v/6.3.0-rc.1).
22
+
23
+ ### Changed
24
+ - 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).
25
+
26
+ 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.
27
+
28
+ 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.
29
+
30
+ ### Added
31
+ - 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).
32
+ - 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).
33
+
34
+ ### Improved
35
+ - 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).
36
+
37
+ 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).
38
+
39
+ 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.
40
+
41
+ - 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).
42
+ - 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).
43
+
44
+ ## [v6.2.1] - April 15, 2022
45
+ ### Fixed
46
+ - 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).
47
+
10
48
  ## [v6.2.0] - March 22, 2022
11
49
 
12
50
  ### Added
@@ -87,7 +125,10 @@ Changes since last non-beta release.
87
125
  ## v5.4.3 and prior changes from rails/webpacker
88
126
  See [CHANGELOG.md in rails/webpacker (up to v5.4.3)](https://github.com/rails/webpacker/blob/master/CHANGELOG.md)
89
127
 
90
- [Unreleased]: https://github.com/shakacode/shakapacker/compare/v6.2.0...master
128
+ [Unreleased]: https://github.com/shakacode/shakapacker/compare/v6.3.0...master
129
+ [v6.3.0]: https://github.com/shakacode/shakapacker/compare/v6.3.0-rc.1...v6.3.0
130
+ [v6.3.0-rc.1]: https://github.com/shakacode/shakapacker/compare/v6.2.1...v6.3.0-rc.1
131
+ [v6.2.1]: https://github.com/shakacode/shakapacker/compare/v6.2.0...v6.2.1
91
132
  [v6.2.0]: https://github.com/shakacode/shakapacker/compare/v6.1.1...v6.2.0
92
133
  [v6.1.1]: https://github.com/shakacode/shakapacker/compare/v6.1.0...v6.1.1
93
134
  [v6.1.0]: https://github.com/shakacode/shakapacker/compare/v6.0.2...v6.1.0
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.1)
4
+ shakapacker (6.3.0.pre.rc.1)
5
5
  activesupport (>= 5.2)
6
6
  rack-proxy (>= 0.6.1)
7
7
  railties (>= 5.2)
data/README.md CHANGED
@@ -1,6 +1,6 @@
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
5
  * See [V6 Upgrade](./docs/v6_upgrade.md) for upgrading from v5 or prior v6 releases.
6
6
 
@@ -17,13 +17,16 @@ Webpacker makes it easy to use the JavaScript pre-processor and bundler [Webpack
17
17
  to manage application-like JavaScript in Rails. It can coexist with the asset pipeline,
18
18
  leaving Webpack responsible solely for app-like JavaScript, or it can be used exclusively, making it also responsible for images, fonts, and CSS.
19
19
 
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!
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!
21
21
 
22
22
  See a comparison of [webpacker with jsbundling-rails](https://github.com/rails/jsbundling-rails/blob/main/docs/comparison_with_webpacker.md).
23
23
 
24
24
  Discussion forum and Slack to discuss debugging and troubleshooting tips. Please open issues for bugs and feature requests:
25
25
  1. [Discussions tab](https://github.com/shakacode/shakapacker/discussions)
26
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.
27
30
 
28
31
  ---
29
32
 
@@ -59,6 +62,7 @@ Discussion forum and Slack to discuss debugging and troubleshooting tips. Please
59
62
  - [Other frameworks](#other-frameworks)
60
63
  - [Custom Rails environments](#custom-rails-environments)
61
64
  - [Upgrading](#upgrading)
65
+ - [Compiler strategies](#compiler-strategies)
62
66
  - [Paths](#paths)
63
67
  - [Additional paths](#additional-paths)
64
68
  - [Deployment](#deployment)
@@ -213,7 +217,18 @@ However, you may use multiple calls to stylesheet_pack_tag if, say, you require
213
217
  <%= stylesheet_pack_tag 'print', media: 'print' %>
214
218
  ```
215
219
 
216
- If you need to setup the pack name args in partials, [see this discussion](https://github.com/shakacode/shakapacker/issues/39).
220
+ You can also use `append_javascript_pack_tag` helper to bundle additional tags together when calling `javascript_pack_tag`:
221
+
222
+ ```erb
223
+ <% append_javascript_pack_tag 'calendar', 'map' %>
224
+ <%= javascript_pack_tag 'application' %>
225
+ ```
226
+
227
+ In the example above, `calendar` and `map` tags will be bundled together with `application` and deduplicated once `javascript_pack_tag` is used.
228
+
229
+ **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.
230
+
231
+ For alternative options of setting the additional packs, [see this discussion](https://github.com/shakacode/shakapacker/issues/39).
217
232
 
218
233
  If you want to link a static asset for `<img />` tag, you can use the `asset_pack_path` helper:
219
234
  ```erb
@@ -244,7 +259,7 @@ If you want to use images in your stylesheets:
244
259
  }
245
260
  ```
246
261
  ### Defer for `javascript_pack_tag`
247
- 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`.
262
+ 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`.
248
263
 
249
264
  ### Server-Side Rendering (SSR)
250
265
 
@@ -258,6 +273,8 @@ Webpacker ships with two binstubs: `./bin/webpacker` and `./bin/webpacker-dev-se
258
273
 
259
274
  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`
260
275
 
276
+ **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).
277
+
261
278
  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/).
262
279
 
263
280
  ```bash
@@ -285,13 +302,13 @@ Once you start this webpack development server, Webpacker will automatically sta
285
302
  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.
286
303
 
287
304
  ```bash
288
- WEBPACKER_DEV_SERVER_HOST=example.com WEBPACKER_DEV_SERVER_INLINE=true WEBPACKER_DEV_SERVER_HOT=false ./bin/webpacker-dev-server
305
+ 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
289
306
  ```
290
307
 
291
- 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:
308
+ 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:
292
309
 
293
310
  ```bash
294
- WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpacker-dev-server
311
+ WEBPACKER_DEV_SERVER_PORT=4305 WEBPACKER_DEV_SERVER_HOST=0.0.0.0 ./bin/webpacker-dev-server
295
312
  ```
296
313
 
297
314
  **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:
@@ -307,7 +324,7 @@ end
307
324
 
308
325
  ### Webpack Configuration
309
326
 
310
- 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:
327
+ 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:
311
328
 
312
329
  1. Your output files go the right directory
313
330
  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!
@@ -415,7 +432,9 @@ Webpacker out of the box supports JS and static assets (fonts, images etc.) comp
415
432
 
416
433
  #### React
417
434
 
418
- See customization example the [Customizing Babel Config](./docs/customizing_babel_config.md) for React configuration.
435
+ See here for detailed instructions on how to [configure Shakapacker to bundle a React app](./docs/react.md) (with optional HMR).
436
+
437
+ See also [Customizing Babel Config](./docs/customizing_babel_config.md) for an example React configuration.
419
438
 
420
439
  #### Typescript
421
440
  ...if you are using typescript, update your `tsconfig.json`
@@ -470,7 +489,7 @@ Add tsconfig.json
470
489
  "moduleResolution": "node",
471
490
  "baseUrl": ".",
472
491
  "paths": {
473
- "*": ["node_modules/*", "app/packs/*"]
492
+ "*": ["node_modules/*", "app/javascript/*"]
474
493
  },
475
494
  "sourceMap": true,
476
495
  "target": "es5",
@@ -655,17 +674,28 @@ yarn add shakapacker@next
655
674
 
656
675
  Also, consult the [CHANGELOG](./CHANGELOG.md) for additional upgrade links.
657
676
 
677
+ ### Compiler strategies
678
+
679
+ Shakapacker ships with two different strategies that are used to determine whether assets need recompilation.
680
+
681
+ - `digest` - This strategy calculates SHA1 digest of files in your watched paths (see below). The calculated digest is then stored in a temp file. To check whether the assets need to be recompiled, Shakapacker calculates the SHA1 of the watched files and compares it with the one stored. If the digests are equal, no recompilation occurs. If the digests are different or the temp file is missing, files are recompiled.
682
+ - `mtime` - This strategy looks at last modified at timestamps of both files AND directories in your watched paths. The timestamp of the most recent file or directory is then compared with the timestamp of `manifest.json` file generated. If the manifest file timestamp is newer than one of the most recently modified file or directory in the watched paths, no recompilation occurs. If the manifest file is order, files are recompiled.
683
+
684
+ `mtime` strategy is generally faster than the `digest` one, but it requires stable timestamps, this makes it perfect for development environment. In production or CI environments, the `digest` strategy is more suitable, unless you are using incremental builds or caching and can guarantee that the timestamps will not change after e.g. cache restore.
685
+
686
+ You can control what strategy is used by `compiler_strategy` option in `webpacker.yml` config file. By default `mtime` strategy is used in development environment, `digest` is used elsewhere.
687
+
658
688
  ### Paths
659
689
 
660
690
  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.
661
691
 
662
- 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`.
692
+ 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`.
663
693
 
664
- 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:
694
+ 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:
665
695
 
666
696
  ```yml
667
697
  # config/webpacker.yml
668
- source_path: frontend # packs are in frontend/packs
698
+ source_path: frontend # packs are the files in frontend/
669
699
  public_output_path: assets/packs # outputs to => public/assets/packs
670
700
  ```
671
701
 
@@ -702,13 +732,15 @@ import 'images/rails.png'
702
732
 
703
733
  **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
704
734
 
705
- **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.
735
+ **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::MtimeStrategy#latest_modified_timestamp` or `Webpacker::DigestStrategy#watched_files_digest` depending on strategy configured by `compiler_strategy` option in `webpacker.yml`). That's why you'd need to add `app/assets` to the additional_paths as stated above and use `import 'javascripts/file.js'` instead.
706
736
 
707
737
 
708
738
  ## Deployment
709
739
 
710
740
  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).
711
741
 
742
+ 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))
743
+
712
744
  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.
713
745
 
714
746
  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.
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
data/docs/react.md ADDED
@@ -0,0 +1,262 @@
1
+ # React Integration
2
+
3
+ These steps describe how to create a Rails/React app, using Shakapacker as the bundler.
4
+
5
+ Before starting, ensure that you have Yarn installed, for example:
6
+
7
+ ```shell
8
+ npm i -g yarn
9
+ ```
10
+
11
+ ## Basic Setup
12
+
13
+ Create a new Rails app as per the [installation instructions in the README](https://github.com/shakacode/shakapacker#installation).
14
+
15
+ Add React, as well as the necessary libraries to enable CSS support in your application:
16
+
17
+ ```shell
18
+ yarn add react react-dom @babel/preset-react
19
+ yarn add css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
20
+ ```
21
+
22
+ Update the Babel configuration in the `package.json` file:
23
+
24
+ ```diff
25
+ "babel": {
26
+ "presets": [
27
+ "./node_modules/shakapacker/package/babel/preset.js",
28
+ + "@babel/preset-react"
29
+ ]
30
+ },
31
+ ```
32
+
33
+ And that's it. You can now create a React app using `app/javascript/application.js` as your entry point.
34
+
35
+ ## Enabling Hot Module Replacement (HMR)
36
+
37
+ With HMR enabled, Shakapacker will automatically update only that part of the page that changed when it detects changes in your project files. This has the nice advantage of preserving your app’s state.
38
+
39
+ To enable HMR in a React app, proceed as follows:.
40
+
41
+ In `config/webpacker.yml` set `hmr` is set to `true`.
42
+
43
+ Install the [react-refresh](https://www.npmjs.com/package/react-refresh) package, as well as [@pmmmwh/react-refresh-webpack-plugin](https://www.npmjs.com/package/@pmmmwh/react-refresh-webpack-plugin):
44
+
45
+ ```shell
46
+ yarn add --dev react-refresh @pmmmwh/react-refresh-webpack-plugin
47
+ ```
48
+
49
+ Alter `config/webpack/webpack.config.js` like so:
50
+
51
+ ```js
52
+ const { webpackConfig, inliningCss } = require('shakapacker');
53
+ const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
54
+ const isDevelopment = process.env.NODE_ENV !== 'production';
55
+
56
+ if (isDevelopment && inliningCss) {
57
+ webpackConfig.plugins.push(
58
+ new ReactRefreshWebpackPlugin({
59
+ overlay: {
60
+ sockPort: webpackConfig.devServer.port,
61
+ },
62
+ })
63
+ );
64
+ }
65
+
66
+ module.exports = webpackConfig;
67
+ ```
68
+
69
+ This applies the plugin to the webpack configuration.
70
+
71
+ Delete the Babel configuration from `package.json`:
72
+
73
+ ```diff
74
+ - "babel": {
75
+ - "presets": [
76
+ - "./node_modules/shakapacker/package/babel/preset.js",
77
+ - "@babel/preset-react"
78
+ - ]
79
+ - },
80
+ ```
81
+
82
+ Then create a `babel.config.js` file in the root of project and add the following:
83
+
84
+ ```js
85
+ module.exports = function (api) {
86
+ const defaultConfigFunc = require('shakapacker/package/babel/preset.js')
87
+ const resultConfig = defaultConfigFunc(api)
88
+ const isDevelopmentEnv = api.env('development')
89
+ const isProductionEnv = api.env('production')
90
+ const isTestEnv = api.env('test')
91
+
92
+ const changesOnDefault = {
93
+ presets: [
94
+ [
95
+ '@babel/preset-react',
96
+ {
97
+ development: isDevelopmentEnv || isTestEnv,
98
+ useBuiltIns: true
99
+ }
100
+ ]
101
+ ].filter(Boolean),
102
+ plugins: [
103
+ isProductionEnv && ['babel-plugin-transform-react-remove-prop-types',
104
+ {
105
+ removeImport: true
106
+ }
107
+ ],
108
+ process.env.WEBPACK_SERVE && 'react-refresh/babel'
109
+ ].filter(Boolean),
110
+ }
111
+
112
+ resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets]
113
+ resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins ]
114
+
115
+ return resultConfig
116
+ }
117
+ ```
118
+
119
+ This is taken from the [sample React Babel config](https://github.com/jameshibbard/shakapacker/blob/master/docs/customizing_babel_config.md#react-configuration).
120
+
121
+ HMR for your React app is now enabled. 🚀
122
+
123
+ ## A Basic Demo App
124
+
125
+ To test that all of the above is working, you can follow these instructions to create a basic React app using Shakapacker.
126
+
127
+ 1. Create a new Rails app:
128
+ ```shell
129
+ rails new myapp --skip-javascript
130
+ cd myapp
131
+ bundle add shakapacker --strict
132
+ ./bin/bundle install
133
+ ./bin/rails webpacker:install
134
+ yarn add react react-dom @babel/preset-react
135
+ yarn add css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
136
+ ```
137
+
138
+ 2. Generate a controller
139
+ ```shell
140
+ rails g controller site index
141
+ echo '<div id="root"></div>' > app/views/site/index.html.erb
142
+ ```
143
+
144
+ 3. Create a CSS file and a React component:
145
+ ```shell
146
+ touch app/javascript/App.css app/javascript/App.js
147
+ ```
148
+
149
+ 4. Edit `app/javascript/application.js` like so:
150
+ ```jsx
151
+ import React from 'react';
152
+ import { createRoot } from 'react-dom/client';
153
+ import HelloMessage from './App';
154
+
155
+ const container = document.getElementById('root');
156
+ const root = createRoot(container);
157
+
158
+ document.addEventListener('DOMContentLoaded', () => {
159
+ root.render(<HelloMessage name="World" />);
160
+ });
161
+ ```
162
+
163
+ 5. Add the following to `app/javascript/App.js`:
164
+ ```jsx
165
+ import React from 'react';
166
+ import 'App.css';
167
+ const HelloMessage = ({ name }) => <h1>Hello, {name}!</h1>;
168
+ export default HelloMessage;
169
+ ```
170
+
171
+ 6. Add the following to `app/javascript/App.css`:
172
+ ```css
173
+ h1 { color: blue; }
174
+ ```
175
+
176
+ 7. Enable HMR in config/webpacker.yml:
177
+ ```shell
178
+ hmr: true
179
+ ```
180
+
181
+ 8. Install the [react-refresh](https://www.npmjs.com/package/react-refresh) package, as well as [@pmmmwh/react-refresh-webpack-plugin](https://www.npmjs.com/package/@pmmmwh/react-refresh-webpack-plugin):
182
+
183
+ ```shell
184
+ yarn add --dev react-refresh @pmmmwh/react-refresh-webpack-plugin
185
+ ```
186
+
187
+ 9. Alter `config/webpack/webpack.config.js` like so:
188
+
189
+ ```js
190
+ const { webpackConfig, inliningCss } = require('shakapacker');
191
+ const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
192
+ const isDevelopment = process.env.NODE_ENV !== 'production';
193
+
194
+ if (isDevelopment && inliningCss) {
195
+ webpackConfig.plugins.push(
196
+ new ReactRefreshWebpackPlugin({
197
+ overlay: {
198
+ sockPort: webpackConfig.devServer.port,
199
+ },
200
+ })
201
+ );
202
+ }
203
+
204
+ module.exports = webpackConfig;
205
+ ```
206
+
207
+ 10. Remove the Babel configuration from `package.json`
208
+ ```diff
209
+ - "babel": {
210
+ - "presets": [
211
+ - "./node_modules/shakapacker/package/babel/preset.js"
212
+ - ]
213
+ - },
214
+ ```
215
+
216
+ 11. Create a `babel.config.js` file in the project root and add the following [sample code](https://github.com/shakacode/shakapacker/blob/master/docs/customizing_babel_config.md#react-configuration):
217
+ ```js
218
+ module.exports = function (api) {
219
+ const defaultConfigFunc = require('shakapacker/package/babel/preset.js')
220
+ const resultConfig = defaultConfigFunc(api)
221
+ const isDevelopmentEnv = api.env('development')
222
+ const isProductionEnv = api.env('production')
223
+ const isTestEnv = api.env('test')
224
+
225
+ const changesOnDefault = {
226
+ presets: [
227
+ [
228
+ '@babel/preset-react',
229
+ {
230
+ development: isDevelopmentEnv || isTestEnv,
231
+ useBuiltIns: true
232
+ }
233
+ ]
234
+ ].filter(Boolean),
235
+ plugins: [
236
+ isProductionEnv && ['babel-plugin-transform-react-remove-prop-types',
237
+ {
238
+ removeImport: true
239
+ }
240
+ ],
241
+ process.env.WEBPACK_SERVE && 'react-refresh/babel'
242
+ ].filter(Boolean),
243
+ }
244
+
245
+ resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets]
246
+ resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins ]
247
+
248
+ return resultConfig
249
+ }
250
+ ```
251
+
252
+ 9. Start the Rails server and the webpack-dev-server in separate console windows:
253
+ ```shell
254
+ rails s
255
+ ./bin/webpacker-dev-server
256
+ ```
257
+
258
+ 10. Hit: <http://localhost:3000/site/index>
259
+
260
+ 11. Edit either the React component at `app/javascript/App.js` or the CSS file at `app/javascript/App.css` and observe the HMR goodness.
261
+
262
+ Note that HMR will not work if you edit `app/javascript/application.js` and you experience a full refresh with a warning in the console. For more info on this, see here: https://github.com/pmmmwh/react-refresh-webpack-plugin/issues/177
@@ -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
data/docs/v6_upgrade.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  There are several substantial changes in Shakapacker v6 that you need to manually account for when coming from Webpacker 5. This guide will help you through it.
4
4
 
5
+ [ShakaCode](https://www.shakacode.com) offers support for upgrading from webpacker or using Shakapacker. If interested, contact [justin@shakacode.com](mailto:justin@shakacode.com).
6
+
5
7
  ## Webpacker/Shakapacker has become a slimmer wrapper around Webpack
6
8
 
7
9
  By default, Webpacker 6 is focused on compiling and bundling JavaScript. This pairs with the existing asset pipeline in Rails that's setup to transpile CSS and static images using [Sprockets](https://github.com/rails/sprockets). For most developers, that's the recommended combination. But if you'd like to use Webpacker for CSS and static assets as well, please see [integrations](https://github.com/shakacode/shakapacker#integrations) for more information.
@@ -17,7 +19,7 @@ See an example migration here: [PR 27](https://github.com/shakacode/react_on_rai
17
19
  _If you're on webpacker v5, follow below steps to get to v6.0.0.rc.6 first._
18
20
 
19
21
  1. Change the gem name from `webpacker` to `shakapacker` and the NPM package from `@rails/webpacker` to `shakapacker`.
20
- 1. Install the peer dependencies. Run `yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/runtime babel-loader compression-webpack-plugin terser-webpack-plugin webpack webpack-assets-manifest webpack-cli webpack-merge webpack-sources webpack-dev-server`
22
+ 1. Install the peer dependencies. Run `yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/runtime babel-loader compression-webpack-plugin terser-webpack-plugin webpack webpack-assets-manifest webpack-cli webpack-merge webpack-sources webpack-dev-server`. You may have old versions of libraries. Run `yarn install` and check for warnings like `warning " > shakapacker@6.1.1" has incorrect peer dependency "compression-webpack-plugin@^9.0.0"` and `file-loader@1.1.11" has incorrect peer dependency "webpack@^2.0.0 || ^3.0.0 || ^4.0.0"`. In other words, warnings like these are **serious** and will cause considerable confusion if not respected.
21
23
  1. Update any scripts that called `bin/webpack` or `bin/webpack-dev-server` to `bin/webpacker` or `bin/webpacker-dev-server`
22
24
  1. Update your webpack config for a single config file, `config/webpack/webpack.config.js`. If you want to use the prior style of having a separate file for each NODE_ENV, you can use this shim for `config/webpack/webpack.config.js`. WARNING, previously, if you did not set `NODE_ENV`, `NODE_ENV` defaulted to `development`. Thus, you might expect `config/webpack/development.js` to run, but you'll instead be using the `config/webpack/RAILS_ENV.js`
23
25
  ```js
@@ -7,6 +7,8 @@ default: &default
7
7
  public_output_path: packs
8
8
  cache_path: tmp/webpacker
9
9
  webpack_compile_output: true
10
+ # See https://github.com/shakacode/shakapacker#deployment
11
+ webpacker_precompile: true
10
12
 
11
13
  # Location for manifest.json, defaults to {public_output_path}/manifest.json if unset
12
14
  # manifest_path: public/packs/manifest.json
@@ -24,9 +26,13 @@ default: &default
24
26
  # Set to true to enable check for matching versions of shakapacker gem and NPM package - will raise an error if there is a mismatch or wildcard versioning is used
25
27
  ensure_consistent_versioning: false
26
28
 
29
+ # Select whether the compiler will use SHA digest ('digest' option) or most most recent modified timestamp ('mtime') to determine freshness
30
+ compiler_strategy: digest
31
+
27
32
  development:
28
33
  <<: *default
29
34
  compile: true
35
+ compiler_strategy: mtime
30
36
 
31
37
  # Reference: https://webpack.js.org/configuration/dev-server/
32
38
  dev_server:
@@ -11,9 +11,7 @@ namespace :webpacker do
11
11
  end
12
12
  end
13
13
 
14
- skip_webpacker_clean = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
15
-
16
- unless skip_webpacker_clean
14
+ if Webpacker.config.webpacker_precompile?
17
15
  # Run clean if the assets:clean is run
18
16
  if Rake::Task.task_defined?("assets:clean")
19
17
  Rake::Task["assets:clean"].enhance do
@@ -8,9 +8,7 @@ namespace :webpacker do
8
8
  end
9
9
  end
10
10
 
11
- skip_webpacker_clobber = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
12
-
13
- unless skip_webpacker_clobber
11
+ if Webpacker.config.webpacker_precompile?
14
12
  # Run clobber if the assets:clobber is run
15
13
  if Rake::Task.task_defined?("assets:clobber")
16
14
  Rake::Task["assets:clobber"].enhance do