shakapacker 7.2.3 → 8.0.0.pre.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintignore +1 -1
  3. data/.eslintrc.js +29 -8
  4. data/.github/PULL_REQUEST_TEMPLATE.md +10 -3
  5. data/.github/workflows/dummy.yml +5 -1
  6. data/.github/workflows/generator.yml +13 -18
  7. data/.github/workflows/node.yml +56 -0
  8. data/.github/workflows/ruby.yml +28 -17
  9. data/.rubocop.yml +1 -0
  10. data/CHANGELOG.md +52 -1
  11. data/README.md +88 -50
  12. data/Rakefile +1 -7
  13. data/docs/customizing_babel_config.md +2 -2
  14. data/docs/deployment.md +13 -6
  15. data/docs/react.md +6 -12
  16. data/docs/troubleshooting.md +4 -4
  17. data/docs/using_esbuild_loader.md +1 -1
  18. data/docs/using_swc_loader.md +1 -1
  19. data/docs/v8_upgrade.md +148 -0
  20. data/gemfiles/Gemfile-rails.7.1.x +12 -0
  21. data/jest.config.js +4 -0
  22. data/lib/install/config/shakapacker.yml +2 -2
  23. data/lib/install/template.rb +36 -30
  24. data/lib/shakapacker/base_strategy.rb +2 -1
  25. data/lib/shakapacker/compiler.rb +5 -20
  26. data/lib/shakapacker/configuration.rb +2 -46
  27. data/lib/shakapacker/deprecation_helper.rb +0 -78
  28. data/lib/shakapacker/dev_server.rb +1 -16
  29. data/lib/shakapacker/dev_server_runner.rb +2 -21
  30. data/lib/shakapacker/env.rb +1 -1
  31. data/lib/shakapacker/instance.rb +1 -3
  32. data/lib/shakapacker/manifest.rb +1 -1
  33. data/lib/shakapacker/railtie.rb +7 -0
  34. data/lib/shakapacker/runner.rb +4 -18
  35. data/lib/shakapacker/utils/manager.rb +58 -0
  36. data/lib/shakapacker/utils/misc.rb +0 -12
  37. data/lib/shakapacker/version.rb +1 -1
  38. data/lib/shakapacker/version_checker.rb +9 -30
  39. data/lib/shakapacker/webpack_runner.rb +2 -21
  40. data/lib/shakapacker.rb +0 -2
  41. data/lib/tasks/shakapacker/check_binstubs.rake +12 -26
  42. data/lib/tasks/shakapacker/check_manager.rake +3 -8
  43. data/lib/tasks/shakapacker/info.rake +6 -15
  44. data/package/babel/preset.js +15 -15
  45. data/package/config.js +15 -17
  46. data/package/dev_server.js +4 -4
  47. data/package/env.js +19 -17
  48. data/package/environments/base.js +41 -24
  49. data/package/environments/development.js +6 -6
  50. data/package/environments/production.js +16 -16
  51. data/package/environments/test.js +1 -1
  52. data/package/esbuild/index.js +6 -6
  53. data/package/index.d.ts +0 -1
  54. data/package/index.js +17 -46
  55. data/package/rules/babel.js +6 -8
  56. data/package/rules/coffee.js +2 -2
  57. data/package/rules/css.js +1 -1
  58. data/package/rules/erb.js +4 -4
  59. data/package/rules/esbuild.js +5 -7
  60. data/package/rules/file.js +17 -7
  61. data/package/rules/index.js +11 -11
  62. data/package/rules/jscommon.js +5 -5
  63. data/package/rules/less.js +6 -6
  64. data/package/rules/raw.js +1 -1
  65. data/package/rules/sass.js +4 -4
  66. data/package/rules/stylus.js +6 -6
  67. data/package/rules/swc.js +5 -7
  68. data/package/swc/index.js +10 -10
  69. data/package/utils/configPath.js +3 -18
  70. data/package/utils/defaultConfigPath.js +1 -1
  71. data/package/utils/{get_style_rule.js → getStyleRule.js} +6 -6
  72. data/package/utils/helpers.js +2 -43
  73. data/package/utils/inliningCss.js +4 -3
  74. data/package/utils/snakeToCamelCase.js +5 -0
  75. data/package/webpackDevServerConfig.js +28 -25
  76. data/package.json +11 -17
  77. data/prettier.config.js +4 -0
  78. data/shakapacker.gemspec +1 -1
  79. data/spec/dummy/app/javascript/packs/application.js +1 -1
  80. data/spec/dummy/config/application.rb +3 -0
  81. data/spec/dummy/config/initializers/react_on_rails.rb +2 -2
  82. data/spec/dummy/config/shakapacker.yml +3 -4
  83. data/spec/dummy/config/webpack/commonWebpackConfig.js +0 -1
  84. data/spec/dummy/config/webpack/webpack.config.js +1 -1
  85. data/spec/dummy/package.json +2 -1
  86. data/spec/dummy/yarn.lock +2 -3
  87. data/spec/generator_specs/e2e_template/template.rb +15 -28
  88. data/spec/generator_specs/generator_spec.rb +6 -136
  89. data/spec/mounted_app/test/dummy/config/webpacker.yml +0 -1
  90. data/spec/shakapacker/compiler_spec.rb +1 -3
  91. data/spec/shakapacker/configuration_spec.rb +4 -56
  92. data/spec/shakapacker/dev_server_runner_spec.rb +19 -102
  93. data/spec/shakapacker/dev_server_spec.rb +1 -16
  94. data/spec/shakapacker/engine_rake_tasks_spec.rb +1 -16
  95. data/spec/shakapacker/instance_spec.rb +1 -3
  96. data/spec/shakapacker/rake_tasks_spec.rb +2 -14
  97. data/spec/shakapacker/shakapacker_spec.rb +0 -1
  98. data/spec/shakapacker/test_app/config/shakapacker_css_extract_ignore_order_warnings.yml +0 -1
  99. data/spec/shakapacker/test_app/config/shakapacker_manifest_path.yml +0 -1
  100. data/spec/shakapacker/test_app/config/shakapacker_nested_entries.yml +0 -1
  101. data/spec/shakapacker/test_app/config/shakapacker_other_location.yml +0 -1
  102. data/spec/shakapacker/test_app/package.json +2 -1
  103. data/spec/shakapacker/utils_manager_spec.rb +125 -0
  104. data/spec/shakapacker/version_checker_spec.rb +4 -4
  105. data/spec/shakapacker/webpack_runner_spec.rb +19 -28
  106. data/spec/support/package_json_helpers.rb +7 -4
  107. data/test/helpers.js +67 -0
  108. data/test/package/config.test.js +50 -0
  109. data/test/package/dev_server.test.js +44 -0
  110. data/test/package/development.test.js +43 -0
  111. data/test/package/env.test.js +57 -0
  112. data/test/package/environments/base.test.js +105 -0
  113. data/test/package/environments/development.test.js +50 -0
  114. data/test/package/environments/production.test.js +106 -0
  115. data/test/package/index.test.js +45 -0
  116. data/test/package/production.test.js +32 -0
  117. data/test/package/rules/babel.test.js +58 -0
  118. data/test/package/rules/esbuild.test.js +59 -0
  119. data/test/package/rules/file.test.js +81 -0
  120. data/test/package/rules/index.test.js +7 -0
  121. data/test/package/rules/raw.test.js +12 -0
  122. data/test/package/rules/swc.test.js +59 -0
  123. data/test/package/staging.test.js +31 -0
  124. data/test/package/test.test.js +28 -0
  125. data/yarn.lock +1278 -421
  126. metadata +52 -102
  127. data/.github/workflows/jest.yml +0 -34
  128. data/.github/workflows/js-lint.yml +0 -34
  129. data/.github/workflows/rubocop.yml +0 -29
  130. data/.github/workflows/ruby-backward-compatibility.yml +0 -52
  131. data/lib/install/package.json +0 -15
  132. data/lib/tasks/shakapacker/check_yarn.rake +0 -34
  133. data/lib/tasks/shakapacker/yarn_install.rake +0 -24
  134. data/lib/tasks/webpacker/binstubs.rake +0 -9
  135. data/lib/tasks/webpacker/check_binstubs.rake +0 -9
  136. data/lib/tasks/webpacker/check_node.rake +0 -9
  137. data/lib/tasks/webpacker/check_yarn.rake +0 -9
  138. data/lib/tasks/webpacker/clean.rake +0 -9
  139. data/lib/tasks/webpacker/clobber.rake +0 -11
  140. data/lib/tasks/webpacker/compile.rake +0 -9
  141. data/lib/tasks/webpacker/info.rake +0 -9
  142. data/lib/tasks/webpacker/install.rake +0 -9
  143. data/lib/tasks/webpacker/verify_config.rake +0 -9
  144. data/lib/tasks/webpacker/verify_install.rake +0 -9
  145. data/lib/tasks/webpacker/yarn_install.rake +0 -9
  146. data/lib/tasks/webpacker.rake +0 -20
  147. data/lib/webpacker/dev_server_runner.rb +0 -9
  148. data/lib/webpacker/webpack_runner.rb +0 -9
  149. data/lib/webpacker.rb +0 -4
  150. data/package/__tests__/config-bc.js +0 -27
  151. data/package/__tests__/config.js +0 -46
  152. data/package/__tests__/dev_server-bc.js +0 -46
  153. data/package/__tests__/dev_server.js +0 -46
  154. data/package/__tests__/development-bc.js +0 -66
  155. data/package/__tests__/development.js +0 -66
  156. data/package/__tests__/env-bc.js +0 -59
  157. data/package/__tests__/env.js +0 -59
  158. data/package/__tests__/index-bc.js +0 -22
  159. data/package/__tests__/index.js +0 -46
  160. data/package/__tests__/production-bc.js +0 -51
  161. data/package/__tests__/production.js +0 -51
  162. data/package/__tests__/staging-bc.js +0 -53
  163. data/package/__tests__/staging.js +0 -53
  164. data/package/__tests__/test-bc.js +0 -43
  165. data/package/__tests__/test.js +0 -43
  166. data/package/environments/__tests__/base-bc.js +0 -107
  167. data/package/environments/__tests__/base.js +0 -107
  168. data/package/environments/__tests__/development.js +0 -53
  169. data/package/environments/__tests__/production.js +0 -92
  170. data/package/rules/__tests__/__utils__/webpack.js +0 -50
  171. data/package/rules/__tests__/babel.js +0 -63
  172. data/package/rules/__tests__/esbuild.js +0 -64
  173. data/package/rules/__tests__/file.js +0 -62
  174. data/package/rules/__tests__/index.js +0 -7
  175. data/package/rules/__tests__/raw.js +0 -18
  176. data/package/rules/__tests__/swc.js +0 -63
  177. data/package/utils/snakeToCamelCase +0 -7
  178. data/spec/backward_compatibility_specs/command_spec.rb +0 -116
  179. data/spec/backward_compatibility_specs/compiler_spec.rb +0 -59
  180. data/spec/backward_compatibility_specs/compiler_strategy_spec.rb +0 -24
  181. data/spec/backward_compatibility_specs/configuration_spec.rb +0 -294
  182. data/spec/backward_compatibility_specs/dev_server_runner_spec.rb +0 -206
  183. data/spec/backward_compatibility_specs/dev_server_spec.rb +0 -47
  184. data/spec/backward_compatibility_specs/digest_strategy_spec.rb +0 -38
  185. data/spec/backward_compatibility_specs/engine_rake_tasks_spec.rb +0 -67
  186. data/spec/backward_compatibility_specs/env_spec.rb +0 -23
  187. data/spec/backward_compatibility_specs/helper_spec.rb +0 -243
  188. data/spec/backward_compatibility_specs/instance_spec.rb +0 -31
  189. data/spec/backward_compatibility_specs/manifest_spec.rb +0 -100
  190. data/spec/backward_compatibility_specs/mtime_strategy_spec.rb +0 -55
  191. data/spec/backward_compatibility_specs/rake_tasks_spec.rb +0 -41
  192. data/spec/backward_compatibility_specs/spec_helper_initializer.rb +0 -24
  193. data/spec/backward_compatibility_specs/webpack_runner_spec.rb +0 -86
  194. data/spec/backward_compatibility_specs/webpacker_spec.rb +0 -41
  195. data/spec/backward_compatibility_specs/webpacker_test_app/.gitignore +0 -2
  196. data/spec/backward_compatibility_specs/webpacker_test_app/Rakefile +0 -3
  197. data/spec/backward_compatibility_specs/webpacker_test_app/app/packs/entrypoints/application.js +0 -10
  198. data/spec/backward_compatibility_specs/webpacker_test_app/app/packs/entrypoints/generated/something.js +0 -2
  199. data/spec/backward_compatibility_specs/webpacker_test_app/app/packs/entrypoints/multi_entry.css +0 -4
  200. data/spec/backward_compatibility_specs/webpacker_test_app/app/packs/entrypoints/multi_entry.js +0 -4
  201. data/spec/backward_compatibility_specs/webpacker_test_app/bin/webpacker +0 -13
  202. data/spec/backward_compatibility_specs/webpacker_test_app/bin/webpacker-dev-server +0 -13
  203. data/spec/backward_compatibility_specs/webpacker_test_app/config/application.rb +0 -11
  204. data/spec/backward_compatibility_specs/webpacker_test_app/config/environment.rb +0 -4
  205. data/spec/backward_compatibility_specs/webpacker_test_app/config/initializers/inspect_autoload_paths.rb +0 -1
  206. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpack/webpack.config.js +0 -0
  207. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker.yml +0 -83
  208. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker_css_extract_ignore_order_warnings.yml +0 -84
  209. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker_defaults_fallback.yml +0 -11
  210. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker_manifest_path.yml +0 -80
  211. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker_nested_entries.yml +0 -83
  212. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker_no_precompile.yml +0 -7
  213. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker_other_location.yml +0 -85
  214. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker_public_root.yml +0 -18
  215. data/spec/backward_compatibility_specs/webpacker_test_app/config.ru +0 -5
  216. data/spec/backward_compatibility_specs/webpacker_test_app/package.json +0 -13
  217. data/spec/backward_compatibility_specs/webpacker_test_app/public/packs/manifest.json +0 -58
  218. data/spec/backward_compatibility_specs/webpacker_test_app/some.config.js +0 -0
  219. data/spec/backward_compatibility_specs/webpacker_test_app/tmp/shakapacker/last-compilation-digest-production +0 -1
  220. data/spec/backward_compatibility_specs/webpacker_test_app/yarn.lock +0 -11
data/Rakefile CHANGED
@@ -14,12 +14,6 @@ namespace :run_spec do
14
14
  sh("bundle exec rspec spec/shakapacker/*_spec.rb")
15
15
  end
16
16
 
17
- desc "Run backward compatibility specs"
18
- task :gem_bc do
19
- puts "Running Shakapacker gem specs for backward compatibility"
20
- sh("bundle exec rspec spec/backward_compatibility_specs/*_spec.rb")
21
- end
22
-
23
17
  desc "Run specs in the dummy app"
24
18
  task :dummy do
25
19
  puts "Running dummy app specs"
@@ -41,7 +35,7 @@ namespace :run_spec do
41
35
  end
42
36
 
43
37
  desc "Run all specs"
44
- task all_specs: %i[gem gem_bc dummy generator] do
38
+ task all_specs: %i[gem dummy generator] do
45
39
  puts "Completed all RSpec tests"
46
40
  end
47
41
  end
@@ -20,8 +20,8 @@ This example shows how you can create an object and apply _additional_ presets a
20
20
  To use this example file,
21
21
 
22
22
  ```
23
- yarn add react react-dom @babel/preset-react
24
- yarn add --dev @pmmmwh/react-refresh-webpack-plugin react-refresh
23
+ npm install react react-dom @babel/preset-react
24
+ npm install --dev @pmmmwh/react-refresh-webpack-plugin react-refresh
25
25
  ```
26
26
 
27
27
  ```js
data/docs/deployment.md CHANGED
@@ -24,7 +24,7 @@ We're essentially doing the following here:
24
24
  * Adding the Heroku NodeJS and Ruby buildpacks for your app. This allows the `npm` or `yarn` executables to properly function when compiling your app - as well as Ruby.
25
25
  * Pushing your code to Heroku and kicking off the deployment
26
26
 
27
- Your production build process is responsible for running `yarn install` before `rake assets:precompile`. For example, if you are on Heroku, the `heroku/nodejs` buildpack must run **prior** to the `heroku/ruby` buildpack for precompilation to run successfully.
27
+ Your production build process is responsible for installing your JavaScript dependencies before `rake assets:precompile`. For example, if you are on Heroku, the `heroku/nodejs` buildpack must run **prior** to the `heroku/ruby` buildpack for precompilation to run successfully.
28
28
 
29
29
  ## Nginx
30
30
 
@@ -100,18 +100,25 @@ Make sure you have your public output path (default `public/packs`), the shakapa
100
100
  append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "tmp/shakapacker", "public/packs", ".bundle", "node_modules"
101
101
  ```
102
102
 
103
- 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
103
+ If you have `node_modules` added to `:linked_dirs` you'll need to install your JavaScript dependencies before `deploy:assets:precompile`; you can use `package_json` to do this generically:
104
104
 
105
105
  ```ruby
106
- before "deploy:assets:precompile", "deploy:yarn_install"
106
+ before "deploy:assets:precompile", "deploy:js_install"
107
107
  namespace :deploy do
108
- desc "Run rake yarn install"
109
- task :yarn_install do
108
+ desc "Run rake js install"
109
+ task :js_install do
110
+ require "package_json"
111
+
112
+ # this will use the package manager specified via `packageManager`, or otherwise fallback to `npm`
113
+ native_js_install_command = PackageJson.read.manager.native_install_command(frozen: true).join(" ")
114
+
110
115
  on roles(:web) do
111
116
  within release_path do
112
- execute("cd #{release_path} && yarn install --silent --no-progress --no-audit --no-optional")
117
+ execute("cd #{release_path} && #{native_js_install_command}")
113
118
  end
114
119
  end
115
120
  end
116
121
  end
117
122
  ```
123
+
124
+ You can also replace the use of `package_json` with the underlying native install command for your preferred package manager.
data/docs/react.md CHANGED
@@ -2,12 +2,6 @@
2
2
 
3
3
  These steps describe creating a Rails/React app, using Shakapacker as the bundler.
4
4
 
5
- Before starting, ensure that you have Yarn installed, for example:
6
-
7
- ```shell
8
- npm i -g yarn
9
- ```
10
-
11
5
  ## Easy Setup
12
6
  If you'd like easy integration of React with Ruby on Rails, see [React on Rails](https://github.com/shakacode/react_on_rails).
13
7
 
@@ -24,8 +18,8 @@ Create a new Rails app as per the [installation instructions in the README](http
24
18
  Add React, as well as the necessary libraries to enable CSS support in your application:
25
19
 
26
20
  ```shell
27
- yarn add react react-dom @babel/preset-react
28
- yarn add css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
21
+ npm install react react-dom @babel/preset-react
22
+ npm install css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
29
23
  ```
30
24
 
31
25
  Update the Babel configuration in the `package.json` file:
@@ -52,7 +46,7 @@ In `config/shakapacker.yml` set `hmr` is set to `true`.
52
46
  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):
53
47
 
54
48
  ```shell
55
- yarn add --dev react-refresh @pmmmwh/react-refresh-webpack-plugin
49
+ npm install --dev react-refresh @pmmmwh/react-refresh-webpack-plugin
56
50
  ```
57
51
 
58
52
  Alter `config/webpack/webpack.config.js` like so:
@@ -142,8 +136,8 @@ cd myapp
142
136
  bundle add shakapacker --strict
143
137
  ./bin/bundle install
144
138
  ./bin/rails shakapacker:install
145
- yarn add react react-dom @babel/preset-react
146
- yarn add css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
139
+ npm install react react-dom @babel/preset-react
140
+ npm install css-loader style-loader mini-css-extract-plugin css-minimizer-webpack-plugin
147
141
  ```
148
142
 
149
143
  2. Generate a controller
@@ -192,7 +186,7 @@ hmr: true
192
186
  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):
193
187
 
194
188
  ```shell
195
- yarn add --dev react-refresh @pmmmwh/react-refresh-webpack-plugin
189
+ npm install --dev react-refresh @pmmmwh/react-refresh-webpack-plugin
196
190
  ```
197
191
 
198
192
  9. Alter `config/webpack/webpack.config.js` like so:
@@ -18,9 +18,9 @@
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-shakapacker`
19
19
 
20
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`.
21
+ Shakapacker uses peer dependencies to make it easier to manage what versions are being used for your app, which is especially
22
+ useful for patching security vulnerabilities. However, not all package managers will actually enforce these versions - notably,
23
+ Yarn will omit a warning rather than erroring if you forget to update a peer dependency:
24
24
 
25
25
  ```
26
26
  warning " > shakapacker@6.1.1" has incorrect peer dependency "compression-webpack-plugin@^9.0.0".
@@ -31,7 +31,7 @@ This omission resulted in an error in the browser:
31
31
  Failed to load resource: net::ERR_CONTENT_DECODING_FAILED
32
32
  ```
33
33
 
34
- The error was caused by an old version of the peer dependency webpack-compression-plugin.
34
+ The error was caused by an old version of the peer dependency `webpack-compression-plugin`.
35
35
 
36
36
  So, be sure to investigate warnings from `yarn install`!
37
37
 
@@ -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 esbuild esbuild-loader
27
+ npm install esbuild esbuild-loader
28
28
  ```
29
29
 
30
30
  2. Add or change `shakapacker_loader` value in your default `shakapacker.yml` config to `esbuild`
@@ -19,7 +19,7 @@ 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 @swc/core swc-loader
22
+ npm install @swc/core swc-loader
23
23
  ```
24
24
 
25
25
  2. Add or change `webpack_loader` value in your default `shakapacker.yml` config to `swc`
@@ -0,0 +1,148 @@
1
+ # Upgrading from Shakapacker v7 to v8
2
+
3
+ The majority of the breaking changes in v8 were about dropping deprecated
4
+ functions and features, along with switching to be agnostic about what package
5
+ manager is used to manage JavaScript dependencies.
6
+
7
+ Support for Ruby 2.6 and Node v12 has also been dropped since they're very old
8
+ at this point.
9
+
10
+ ## The `packageManager` property in `package.json` is used to determine the package manager
11
+
12
+ The biggest functional change in v8, `shakapacker` is now able to work with any
13
+ of the major JavaScript package managers thanks to the
14
+ [`package_json`](https://github.com/shakacode/package_json) gem which uses the
15
+ [`packageManager`](https://nodejs.org/api/packages.html#packagemanager) property
16
+ in the `package.json`.
17
+
18
+ In alignment with the behaviour of Node and `corepack`, in the absence of the
19
+ `packageManager` property `npm` will be used as the package manager so as part
20
+ of upgrading you will want to ensure that is set to `yarn@<version>` if you want
21
+ to continue using Yarn.
22
+
23
+ An error will be raised in the presences of a lockfile other than
24
+ `package-lock.json` if this property is not set with the recommended value to
25
+ use, but it important the property is set to ensure all tooling uses the right
26
+ package manager.
27
+
28
+ The `check_yarn` rake task has also been renamed to `check_manager` to reflect
29
+ this change.
30
+
31
+ Check out the [installation section](../README.md#installation) of the readme
32
+ for more details.
33
+
34
+ ## Usages of `webpacker` must now be `shakapacker`
35
+
36
+ The `webpacker` spelling was deprecated in v7 and has now been completely
37
+ removed in v8 - this includes constants, environment variables, and rake tasks.
38
+
39
+ If you are still using references to `webpacker`, see the
40
+ [v7 Upgrade Guide](../docs/v7_upgrade.md) for how to migrate.
41
+
42
+ ## JavaScript dependencies are no longer installed automatically as part of `assets:precompile`
43
+
44
+ You will now need to ensure your dependencies are installed before compiling
45
+ assets.
46
+
47
+ Some platforms like Heroku will install dependencies automatically but if you're
48
+ using a tool like `capistrano` to deploy to servers you can enhance the
49
+ `assets:precompile` command like so:
50
+
51
+ ```ruby
52
+ namespace :assets do
53
+ desc "Ensures that dependencies required to compile assets are installed"
54
+ task install_dependencies: :environment do
55
+ # npm v6+
56
+ raise if File.exist?("package.json") && !(system "npm ci")
57
+
58
+ # yarn v1.x (classic)
59
+ raise if File.exist?("package.json") && !(system "yarn install --immutable")
60
+
61
+ # yarn v2+ (berry)
62
+ raise if File.exist?("package.json") && !(system "yarn install --frozen-lockfile")
63
+
64
+ # bun v1+
65
+ raise if File.exist?("package.json") && !(system "bun install --frozen-lockfile")
66
+
67
+ # pnpm v6+
68
+ raise if File.exist?("package.json") && !(system "pnpm install --frozen-lockfile")
69
+ end
70
+ end
71
+
72
+ Rake::Task["assets:precompile"].enhance ["assets:install_dependencies"]
73
+ ```
74
+
75
+ This allows more flexibility than what `shakapacker` could provide - for
76
+ example, you might only want to do an immutable install if you're in CI.
77
+
78
+ ## `ensure_consistent_versioning` is now enabled by default
79
+
80
+ This has `shakapacker` check that the versions of the installed Ruby gem and
81
+ JavaScript package are compatible; this should only be impactful for codebases
82
+ that are not using lockfiles.
83
+
84
+ ## Usages of `globalMutableWebpackConfig` must be replaced with `generateWebpackConfig()`
85
+
86
+ The function will return the same object with less risk:
87
+
88
+ ```js
89
+ // before
90
+ const { globalMutableWebpackConfig, merge } = require('shakapacker');
91
+
92
+ const customConfig = {
93
+ module: {
94
+ rules: [
95
+ {
96
+ test: require.resolve('jquery'),
97
+ loader: 'expose-loader',
98
+ options: { exposes: ['$', 'jQuery'] }
99
+ }
100
+ ]
101
+ }
102
+ };
103
+
104
+ module.exports = merge(globalMutableWebpackConfig, customConfig);
105
+ ```
106
+
107
+ ```js
108
+ // after
109
+ const { generateWebpackConfig } = require('shakapacker');
110
+
111
+ const customConfig = {
112
+ module: {
113
+ rules: [
114
+ {
115
+ test: require.resolve('jquery'),
116
+ loader: 'expose-loader',
117
+ options: { exposes: ['$', 'jQuery'] }
118
+ }
119
+ ]
120
+ }
121
+ };
122
+
123
+ // you can also pass your config directly to the generator function to have it merged in!
124
+ module.exports = merge(generateWebpackConfig(), customConfig);
125
+ ```
126
+
127
+ ## `additional_paths` are now stripped just like with `source_path`
128
+
129
+ This means going forward asset paths should be same regardless of their source:
130
+
131
+ ```erb
132
+ <%# before %>
133
+ <%= image_pack_tag('marketing/images/people_looking_happy.png') %>
134
+
135
+ <%# after %>
136
+ <%= image_pack_tag('image/people_looking_happy.png') %>
137
+ ```
138
+
139
+ ## Misc. removals
140
+
141
+ In addition to the above, v8 has also removed a number of miscellaneous
142
+ functions that no one is probably using anyway but technically could have been
143
+ including:
144
+
145
+ - `isArray` js utility function (just use `Array.isArray` directly)
146
+ - `relative_url_root` config getter (it was never used)
147
+ - `verify_file_existance` method (use `verify_file_existence` instead)
148
+ - `https` option for `webpack-dev-server` (use `server: 'https'` instead)
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
4
+
5
+ gemspec path: "../"
6
+
7
+ gem "rails", '~>7.1.0'
8
+ gem "arel", github: "rails/arel"
9
+ gem "rake", ">= 11.1"
10
+ gem "rack-proxy", require: false
11
+ gem "rspec-rails", "~> 6.0.0"
12
+ gem "byebug"
data/jest.config.js ADDED
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ roots: ["<rootDir>/test"],
3
+ testPathIgnorePatterns: ["/__fixtures__/", "/__utils__/"]
4
+ }
@@ -39,8 +39,8 @@ default: &default
39
39
  # Select loader to use, available options are 'babel' (default), 'swc' or 'esbuild'
40
40
  webpack_loader: 'babel'
41
41
 
42
- # 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
43
- ensure_consistent_versioning: false
42
+ # Raises an error if there is a mismatch in the shakapacker gem and npm package being used
43
+ ensure_consistent_versioning: true
44
44
 
45
45
  # Select whether the compiler will use SHA digest ('digest' option) or most most recent modified timestamp ('mtime') to determine freshness
46
46
  compiler_strategy: digest
@@ -1,12 +1,14 @@
1
1
  require "shakapacker/utils/misc"
2
+ require "shakapacker/utils/manager"
2
3
  require "shakapacker/utils/version_syntax_converter"
4
+ require "package_json"
3
5
 
4
6
  # Install Shakapacker
5
7
 
6
8
  force_option = ENV["FORCE"] ? { force: true } : {}
7
9
 
8
10
  copy_file "#{__dir__}/config/shakapacker.yml", "config/shakapacker.yml", force_option
9
- copy_file "#{__dir__}/package.json", "package.json", force_option
11
+ remove_file "#{__dir__}/package.json" if force_option[:force]
10
12
 
11
13
  say "Copying webpack core config"
12
14
  directory "#{__dir__}/config/webpack", "config/webpack", force_option
@@ -43,22 +45,37 @@ else
43
45
  end
44
46
 
45
47
  def package_json
46
- if @package_json.nil?
47
- Shakapacker::Utils::Misc.require_package_json_gem
48
+ @package_json ||= PackageJson.new
49
+ end
50
+
51
+ # setup the package manager with default values
52
+ package_json.merge! do |pj|
53
+ babel = pj.fetch("babel", {})
54
+
55
+ babel["presets"] ||= []
56
+ babel["presets"].push("./node_modules/shakapacker/package/babel/preset.js")
48
57
 
49
- @package_json = PackageJson.read
58
+ package_manager = pj.fetch("packageManager") do
59
+ "#{Shakapacker::Utils::Manager.guess_binary}@#{Shakapacker::Utils::Manager.guess_version}"
50
60
  end
51
61
 
52
- @package_json
62
+ {
63
+ "name" => "app",
64
+ "private" => true,
65
+ "version" => "0.1.0",
66
+ "babel" => babel,
67
+ "browserslist" => [
68
+ "defaults"
69
+ ],
70
+ "packageManager" => package_manager
71
+ }.merge(pj)
53
72
  end
54
73
 
74
+ Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious!
75
+
55
76
  # Ensure there is `system!("bin/yarn")` command in `./bin/setup` file
56
77
  if (setup_path = Rails.root.join("bin/setup")).exist?
57
- def native_install_command
58
- return "bin/yarn" unless Shakapacker::Utils::Misc.use_package_json_gem
59
-
60
- package_json.manager.native_install_command.join(" ")
61
- end
78
+ native_install_command = package_json.manager.native_install_command.join(" ")
62
79
 
63
80
  say "Run #{native_install_command} during bin/setup"
64
81
 
@@ -88,23 +105,14 @@ if (setup_path = Rails.root.join("bin/setup")).exist?
88
105
  end
89
106
 
90
107
  def add_dependencies(dependencies, type)
91
- return package_json.manager.add!(dependencies, type: type) if Shakapacker::Utils::Misc.use_package_json_gem
92
-
93
- # TODO: check that run actually errors
94
- run("yarn add #{dependencies.join(" ")}") if type == :production
95
- run("yarn add --dev #{dependencies.join(" ")}") if type == :dev
108
+ package_json.manager.add!(dependencies, type: type)
96
109
  rescue PackageJson::Error
97
110
  say "Shakapacker installation failed 😭 See above for details.", :red
98
111
  exit 1
99
112
  end
100
113
 
101
114
  def fetch_peer_dependencies
102
- if Shakapacker::Utils::Misc.use_package_json_gem
103
- return PackageJson.read("#{__dir__}/../../").fetch("peerDependencies")
104
- end
105
-
106
- package_json = File.read("#{__dir__}/../../package.json")
107
- JSON.parse(package_json)["peerDependencies"]
115
+ PackageJson.read("#{__dir__}/../../").fetch("peerDependencies")
108
116
  end
109
117
 
110
118
  Dir.chdir(Rails.root) do
@@ -112,15 +120,13 @@ Dir.chdir(Rails.root) do
112
120
  say "Installing shakapacker@#{npm_version}"
113
121
  add_dependencies(["shakapacker@#{npm_version}"], :production)
114
122
 
115
- if Shakapacker::Utils::Misc.use_package_json_gem
116
- package_json.merge! do |pj|
117
- {
118
- "dependencies" => pj["dependencies"].merge({
119
- # TODO: workaround for test suite - long-run need to actually account for diff pkg manager behaviour
120
- "shakapacker" => pj["dependencies"]["shakapacker"].delete_prefix("^")
121
- })
122
- }
123
- end
123
+ package_json.merge! do |pj|
124
+ {
125
+ "dependencies" => pj["dependencies"].merge({
126
+ # TODO: workaround for test suite - long-run need to actually account for diff pkg manager behaviour
127
+ "shakapacker" => pj["dependencies"]["shakapacker"].delete_prefix("^")
128
+ })
129
+ }
124
130
  end
125
131
 
126
132
  peers = fetch_peer_dependencies
@@ -16,7 +16,8 @@ module Shakapacker
16
16
  [
17
17
  *config.additional_paths.map { |path| "#{path}{,/**/*}" },
18
18
  "#{config.source_path}{,/**/*}",
19
- "yarn.lock", "package.json",
19
+ "package.json", "package-lock.json", "yarn.lock",
20
+ "pnpm-lock.yaml", "bun.lockb",
20
21
  "config/webpack{,/**/*}"
21
22
  ].freeze
22
23
  end
@@ -72,7 +72,7 @@ class Shakapacker::Compiler
72
72
  config.root_path.join("tmp/shakapacker.lock")
73
73
  end
74
74
 
75
- def optionalRubyRunner
75
+ def optional_ruby_runner
76
76
  first_line = File.readlines(bin_shakapacker_path).first.chomp
77
77
  /ruby/.match?(first_line) ? RbConfig.ruby : ""
78
78
  end
@@ -82,7 +82,7 @@ class Shakapacker::Compiler
82
82
 
83
83
  stdout, stderr, status = Open3.capture3(
84
84
  webpack_env,
85
- "#{optionalRubyRunner} '#{bin_shakapacker_path}'",
85
+ "#{optional_ruby_runner} '#{bin_shakapacker_path}'",
86
86
  chdir: File.expand_path(config.root_path)
87
87
  )
88
88
 
@@ -104,28 +104,13 @@ class Shakapacker::Compiler
104
104
  def webpack_env
105
105
  return env unless defined?(ActionController::Base)
106
106
 
107
- Shakapacker.set_shakapacker_env_variables_for_backward_compatibility
108
-
109
107
  env.merge(
110
- "SHAKAPACKER_ASSET_HOST" => instance.config.asset_host,
111
- "SHAKAPACKER_RELATIVE_URL_ROOT" => instance.config.relative_url_root,
112
- "SHAKAPACKER_CONFIG" => instance.config_path.to_s
108
+ "SHAKAPACKER_ASSET_HOST" => instance.config.asset_host,
109
+ "SHAKAPACKER_CONFIG" => instance.config.config_path.to_s
113
110
  )
114
111
  end
115
112
 
116
113
  def bin_shakapacker_path
117
- if File.exist?(config.root_path.join("bin/shakapacker"))
118
- config.root_path.join("bin/shakapacker")
119
- elsif File.exist?(config.root_path.join("bin/webpacker"))
120
- Shakapacker.puts_deprecation_message(
121
- Shakapacker.short_deprecation_message(
122
- "bin/webpacker",
123
- "bin/shakapacker"
124
- )
125
- )
126
- config.root_path.join("bin/webpacker")
127
- else
128
- config.root_path.join("bin/shakapacker")
129
- end
114
+ config.root_path.join("bin/shakapacker")
130
115
  end
131
116
  end
@@ -12,10 +12,7 @@ class Shakapacker::Configuration
12
12
  def initialize(root_path:, config_path:, env:)
13
13
  @root_path = root_path
14
14
  @env = env
15
-
16
- # For backward compatibility
17
- Shakapacker.set_shakapacker_env_variables_for_backward_compatibility
18
- @config_path = Pathname.new(ENV["SHAKAPACKER_CONFIG"] || config_path)
15
+ @config_path = config_path
19
16
  end
20
17
 
21
18
  def dev_server
@@ -43,17 +40,6 @@ class Shakapacker::Configuration
43
40
  fetch(:shakapacker_precompile)
44
41
  end
45
42
 
46
- def webpacker_precompile?
47
- Shakapacker.puts_deprecation_message(
48
- Shakapacker.short_deprecation_message(
49
- "webpacker_precompile?",
50
- "shakapacker_precompile?"
51
- )
52
- )
53
-
54
- shakapacker_precompile?
55
- end
56
-
57
43
  def source_path
58
44
  root_path.join(fetch(:source_path))
59
45
  end
@@ -103,17 +89,7 @@ class Shakapacker::Configuration
103
89
  end
104
90
 
105
91
  def fetch(key)
106
- return data.fetch(key, defaults[key]) unless key == :webpacker_precompile
107
-
108
- # for backward compatibility
109
- Shakapacker.puts_deprecation_message(
110
- Shakapacker.short_deprecation_message(
111
- "webpacker_precompile",
112
- "shakapacker_precompile"
113
- )
114
- )
115
-
116
- data.fetch(key, defaults[:shakapacker_precompile])
92
+ data.fetch(key, defaults[key])
117
93
  end
118
94
 
119
95
  def asset_host
@@ -123,19 +99,6 @@ class Shakapacker::Configuration
123
99
  )
124
100
  end
125
101
 
126
- def relative_url_root
127
- result = ENV.fetch(
128
- "SHAKAPACKER_RELATIVE_URL_ROOT",
129
- fetch(:relative_url_root) || ActionController::Base.relative_url_root
130
- )
131
-
132
- if result
133
- Shakapacker.puts_deprecation_message("The usage of relative_url_root is deprecated in Shakapacker and will be removed in v8.")
134
- end
135
-
136
- result
137
- end
138
-
139
102
  private
140
103
  def data
141
104
  @data ||= load
@@ -149,13 +112,6 @@ class Shakapacker::Configuration
149
112
  end
150
113
  symbolized_config = config[env].deep_symbolize_keys
151
114
 
152
- # For backward compatibility
153
- if symbolized_config.key?(:shakapacker_precompile) && !symbolized_config.key?(:webpacker_precompile)
154
- symbolized_config[:webpacker_precompile] = symbolized_config[:shakapacker_precompile]
155
- elsif !symbolized_config.key?(:shakapacker_precompile) && symbolized_config.key?(:webpacker_precompile)
156
- symbolized_config[:shakapacker_precompile] = symbolized_config[:webpacker_precompile]
157
- end
158
-
159
115
  return symbolized_config
160
116
  rescue Errno::ENOENT => e
161
117
  if self.class.installing