shakapacker 7.1.0 → 7.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/dummy.yml +3 -0
  3. data/.github/workflows/generator.yml +3 -0
  4. data/.github/workflows/ruby-backward-compatibility.yml +6 -0
  5. data/.github/workflows/ruby.yml +7 -0
  6. data/CHANGELOG.md +23 -6
  7. data/README.md +29 -3
  8. data/docs/v6_upgrade.md +2 -2
  9. data/lib/install/config/shakapacker.yml +5 -2
  10. data/lib/install/template.rb +56 -16
  11. data/lib/shakapacker/compiler.rb +5 -3
  12. data/lib/shakapacker/configuration.rb +16 -0
  13. data/lib/shakapacker/dev_server.rb +7 -1
  14. data/lib/shakapacker/dev_server_runner.rb +16 -7
  15. data/lib/shakapacker/digest_strategy.rb +3 -1
  16. data/lib/shakapacker/runner.rb +19 -1
  17. data/lib/shakapacker/utils/misc.rb +12 -0
  18. data/lib/shakapacker/version.rb +1 -1
  19. data/lib/shakapacker/webpack_runner.rb +12 -5
  20. data/lib/tasks/shakapacker/binstubs.rake +1 -1
  21. data/lib/tasks/shakapacker/check_manager.rake +27 -0
  22. data/lib/tasks/shakapacker/check_yarn.rake +2 -1
  23. data/lib/tasks/shakapacker/info.rake +20 -3
  24. data/lib/tasks/shakapacker/install.rake +1 -1
  25. data/lib/tasks/shakapacker/verify_install.rake +1 -1
  26. data/lib/tasks/shakapacker.rake +2 -2
  27. data/lib/tasks/webpacker/check_yarn.rake +1 -1
  28. data/package.json +51 -51
  29. data/shakapacker.gemspec +1 -0
  30. data/spec/backward_compatibility_specs/dev_server_runner_spec.rb +156 -30
  31. data/spec/backward_compatibility_specs/digest_strategy_spec.rb +1 -0
  32. data/spec/backward_compatibility_specs/engine_rake_tasks_spec.rb +29 -7
  33. data/spec/backward_compatibility_specs/webpack_runner_spec.rb +44 -10
  34. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker.yml +1 -1
  35. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker_other_location.yml +1 -1
  36. data/spec/dummy/Gemfile +0 -2
  37. data/spec/dummy/package.json +1 -2
  38. data/spec/generator_specs/e2e_template/template.rb +31 -10
  39. data/spec/generator_specs/fake-bin/bun +10 -0
  40. data/spec/generator_specs/fake-bin/npm +10 -0
  41. data/spec/generator_specs/fake-bin/pnpm +10 -0
  42. data/spec/generator_specs/fake-bin/yarn +10 -0
  43. data/spec/generator_specs/generator_spec.rb +167 -17
  44. data/spec/mounted_app/package.json +1 -0
  45. data/spec/shakapacker/compiler_spec.rb +2 -2
  46. data/spec/shakapacker/configuration_spec.rb +89 -0
  47. data/spec/shakapacker/dev_server_runner_spec.rb +170 -29
  48. data/spec/shakapacker/digest_strategy_spec.rb +22 -0
  49. data/spec/shakapacker/engine_rake_tasks_spec.rb +27 -6
  50. data/spec/shakapacker/rake_tasks_spec.rb +6 -5
  51. data/spec/shakapacker/test_app/config/shakapacker.yml +1 -1
  52. data/spec/shakapacker/test_app/config/shakapacker_other_location.yml +1 -1
  53. data/spec/shakapacker/webpack_runner_spec.rb +44 -10
  54. data/spec/spec_helper.rb +2 -0
  55. data/spec/support/package_json_helpers.rb +16 -0
  56. metadata +24 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b52fbe18d6cc69e790f1432b93a8badee45c64229b7e0b5ad9bf6e7645c1c28c
4
- data.tar.gz: 94ba2b7a48537e4b3ece200e2983975b444a340edff45b9af3544b1709375c38
3
+ metadata.gz: ae742f19f77532f469332730b57bb1e381c6f238ace6cd5eb113a95f607bde68
4
+ data.tar.gz: 246d1e8dba39d501d219d1e37f4665c8b9f1ab401825173f22cbe270bf95a933
5
5
  SHA512:
6
- metadata.gz: ade899e22f00c9863302d2a9aa7e58549d057b07be50d34d853751cabf59a9d318a637240734854059b352110b20245361ec7d427488166b4e481639b90cb380
7
- data.tar.gz: 6364f8d109c008da994d01f7ad553472a90d5392511b796ba7286502d5f3b6969ad225cbd8e3364fb86da6f2dc5b19524631399eebae982b43558444cb886a0e
6
+ metadata.gz: d57fc47ab68b52626fa66d643d5d62ceeb62fd27b707844c5b187c5ef4e29a15cbb8296a3bbb9204740ecd3769371c05c1ea6046ac2deffc7771bbf2b0839484
7
+ data.tar.gz: 5af5dc61cf4d3f01b17eaca403a8f09ae8a63fe3d2adffdb3dd91969c0e71684118ae0365a893a27690ed4275c03fb1d46a20b30fcb56f8821fdae44110b212e
@@ -10,6 +10,9 @@ jobs:
10
10
  - uses: actions/checkout@v4
11
11
  with:
12
12
  persist-credentials: false
13
+ - uses: actions/setup-node@v3
14
+ with:
15
+ node-version: '16'
13
16
  - uses: ruby/setup-ruby@v1
14
17
  with:
15
18
  ruby-version: '3.1.2'
@@ -10,6 +10,7 @@ jobs:
10
10
  matrix:
11
11
  os: [ubuntu-latest]
12
12
  ruby: ['2.6', '2.7', '3.0']
13
+ use_package_json_gem: ['true', 'false']
13
14
  gemfile:
14
15
  - gemfiles/Gemfile-rails.6.0.x
15
16
  - gemfiles/Gemfile-rails.6.1.x
@@ -37,3 +38,5 @@ jobs:
37
38
  - name: Install dependencies
38
39
  run: bundle install
39
40
  - run: bundle exec rake run_spec:generator
41
+ env:
42
+ SHAKAPACKER_USE_PACKAGE_JSON_GEM: ${{ matrix.use_package_json_gem }}
@@ -39,3 +39,9 @@ jobs:
39
39
 
40
40
  - name: Ruby specs - Backward compatibility
41
41
  run: bundle exec rake run_spec:gem_bc
42
+ env:
43
+ SHAKAPACKER_USE_PACKAGE_JSON_GEM: "false"
44
+ - name: Ruby specs - Backward compatibility
45
+ run: bundle exec rake run_spec:gem_bc
46
+ env:
47
+ SHAKAPACKER_USE_PACKAGE_JSON_GEM: "true"
@@ -39,3 +39,10 @@ jobs:
39
39
 
40
40
  - name: Ruby specs
41
41
  run: bundle exec rake run_spec:gem
42
+ env:
43
+ SHAKAPACKER_USE_PACKAGE_JSON_GEM: "false"
44
+
45
+ - name: Ruby specs
46
+ run: bundle exec rake run_spec:gem
47
+ env:
48
+ SHAKAPACKER_USE_PACKAGE_JSON_GEM: "true"
data/CHANGELOG.md CHANGED
@@ -5,20 +5,36 @@
5
5
 
6
6
  ## Versions
7
7
  ## [Unreleased]
8
- Changes since last non-beta release.
8
+ Changes since the last non-beta release.
9
9
 
10
10
  _Please add entries here for your pull requests that are not yet released._
11
11
 
12
12
  ### Added
13
- - Support passing custom webpack config directly to `generateWebpackConfig` for merging [PR 343](https://github.com/shakacode/shakapacker/pull/343) by [G-Rath](https://github.com/g-rath)
13
+ - Experimental support for other JS package managers using `package_json` gem [PR 349](https://github.com/shakacode/shakapacker/pull/349) by [G-Rath](https://github.com/g-rath).
14
+ - Support `hmr: only` configuration [PR 378](https://github.com/shakacode/shakapacker/pull/378) by [SimenB](https://github.com/SimenB).
15
+ - Use `config/shakapacker.yml` as the secondary source for `asset_host` and `relative_url_root` configurations [PR 376](https://github.com/shakacode/shakapacker/pull/376) by [ahangarha](https://github.com/ahangarha).
14
16
 
15
17
  ### Fixed
16
- - Use `NODE_OPTIONS` to enable Node-specific debugging flags [PR 350](https://github.com/shakacode/shakapacker/pull/350)
17
- - Add the boilplate `application.js` into `packs/` [PR 363](https://github.com/shakacode/shakapacker/pull/363)
18
+ - Recommend `server` option instead of deprecated `https` option when `--https` is provided [PR 380](https://github.com/shakacode/shakapacker/pull/380) by [G-Rath](https://github.com/g-rath)
19
+ - Recompile assets on asset host change [PR 364](https://github.com/shakacode/shakapacker/pull/364) by [ahangarha](https://github.com/ahangarha).
20
+ - Add deprecation warning for `https` option in `shakapacker.yml` (use `server: 'https'` instead) [PR 382](https://github.com/shakacode/shakapacker/pull/382) by [G-Rath](https://github.com/g-rath).
21
+ - Disable Hot Module Replacement in `webpack-dev-server` when `hmr: false` [PR 392](https://github.com/shakacode/shakapacker/pull/392) by [thedanbob](https://github.com/thedanbob).
22
+
23
+ ### Deprecated
24
+ - The usage of relative_url_root is deprecated in Shakapacker and will be removed in v8. [PR 376](https://github.com/shakacode/shakapacker/pull/376) by [ahangarha](https://github.com/ahangarha).
25
+
26
+ ## [v7.1.0] - September 30, 2023
27
+
28
+ ### Added
29
+ - Support passing custom webpack config directly to `generateWebpackConfig` for merging [PR 343](https://github.com/shakacode/shakapacker/pull/343) by [G-Rath](https://github.com/g-rath).
30
+
31
+ ### Fixed
32
+ - Use `NODE_OPTIONS` to enable Node-specific debugging flags [PR 350](https://github.com/shakacode/shakapacker/pull/350).
33
+ - Add the boilerplate `application.js` into `packs/` [PR 363](https://github.com/shakacode/shakapacker/pull/363).
18
34
 
19
35
  ## [v7.0.3] - July 7, 2023
20
36
  ### Fixed
21
- - Fixed commands execution for projects with space in absolute path [PR 322](https://github.com/shakacode/shakapacker/pull/322) by [kukicola](https://github.com/kukicola).
37
+ - Fixed commands execution for projects with space in the absolute path [PR 322](https://github.com/shakacode/shakapacker/pull/322) by [kukicola](https://github.com/kukicola).
22
38
 
23
39
  ## [v7.0.2] - July 3, 2023
24
40
  ### Fixed
@@ -274,7 +290,8 @@ Note: [Rubygem is 6.3.0.pre.rc.1](https://rubygems.org/gems/shakapacker/versions
274
290
  ## v5.4.3 and prior changes from rails/webpacker
275
291
  See [CHANGELOG.md in rails/webpacker (up to v5.4.3)](https://github.com/rails/webpacker/blob/master/CHANGELOG.md)
276
292
 
277
- [Unreleased]: https://github.com/shakacode/shakapacker/compare/v7.0.3...master
293
+ [Unreleased]: https://github.com/shakacode/shakapacker/compare/v7.1.0...master
294
+ [v7.1.0]: https://github.com/shakacode/shakapacker/compare/v7.0.3...v7.1.0
278
295
  [v7.0.3]: https://github.com/shakacode/shakapacker/compare/v7.0.2...v7.0.3
279
296
  [v7.0.2]: https://github.com/shakacode/shakapacker/compare/v7.0.1...v7.0.2
280
297
  [v7.0.1]: https://github.com/shakacode/shakapacker/compare/v7.0.0...v7.0.1
data/README.md CHANGED
@@ -51,6 +51,7 @@ Read the [full review here](https://clutch.co/profile/shakacode#reviews?sort_by=
51
51
  - [Optional support](#optional-support)
52
52
  - [Installation](#installation)
53
53
  - [Rails v6+](#rails-v6)
54
+ - [Using alternative package managers](#using-alternative-package-managers)
54
55
  - [Note for Yarn v2 usage](#note-for-yarn-v2-usage)
55
56
  - [Concepts](#concepts)
56
57
  - [Usage](#usage)
@@ -163,9 +164,21 @@ yarn add @babel/core @babel/plugin-transform-runtime @babel/preset-env @babel/ru
163
164
  Previously, these "webpack" and "babel" packages were direct dependencies for `shakapacker`. By
164
165
  making these peer dependencies, you have control over the versions used in your webpack and babel configs.
165
166
 
167
+ ### Using alternative package managers
168
+
169
+ There is experimental support for using package managers besides Yarn classic for managing JavaScript dependencies using the [`package_json`](https://github.com/G-Rath/package_json) gem.
170
+
171
+ This can be enabled by setting the environment variable `SHAKAPACKER_USE_PACKAGE_JSON_GEM` to `true`; Shakapacker will then use the `package_json` gem which in turn will look for the [`packageManager`](https://nodejs.org/api/packages.html#packagemanager) property in the `package.json` or otherwise the `PACKAGE_JSON_FALLBACK_MANAGER` environment variable to determine which manager to use, defaulting to `npm` if neither are found.
172
+
173
+ See [here](https://github.com/G-Rath/package_json#specifying-a-package-manager) for a list of the supported package managers and more information; note that `package_json` does not handle ensuring the manager is installed.
174
+
175
+ > **Note**
176
+ >
177
+ > The rest of the documentation assumes that `package_json` is not being used, and so always references `yarn` - you should instead use the package manager of your choice for these commands.
178
+
166
179
  ### Note for Yarn v2 usage
167
180
 
168
- If you are using Yarn v2 (berry), please note that PnP modules are not supported.
181
+ If you are using Yarn v2 (berry), please note that PnP modules are not supported unless you're using `SHAKAPACKER_USE_PACKAGE_JSON_GEM`.
169
182
 
170
183
  To use Shakapacker with Yarn v2, make sure you set `nodeLinker: node-modules` in your `.yarnrc.yml` file as per the [Yarn docs](https://yarnpkg.com/getting-started/migration#step-by-step) to opt out of Plug'n'Play behavior.
171
184
 
@@ -272,7 +285,11 @@ Note, the default of "defer" for the `javascript_pack_tag`. You can override tha
272
285
  <%= javascript_pack_tag 'calendar' %>
273
286
  <%= javascript_pack_tag 'map' %>
274
287
  ```
275
- While this also generally applies to `stylesheet_pack_tag`, you may use multiple calls to stylesheet_pack_tag if, say, you require multiple <style> tags for different output media:
288
+
289
+ While this also generally applies to `stylesheet_pack_tag`,
290
+ you may use multiple calls to stylesheet_pack_tag if,
291
+ say,
292
+ you require multiple `<style>` tags for different output media:
276
293
 
277
294
  ``` erb
278
295
  <%= stylesheet_pack_tag 'application', media: 'screen' %>
@@ -417,7 +434,16 @@ In production or CI environments, the `digest` strategy is more suitable, unless
417
434
 
418
435
  You can control what strategy is used by the `compiler_strategy` option in `shakapacker.yml` config file. By default `mtime` strategy is used in development environment, `digest` is used elsewhere.
419
436
 
420
- **Note:** If you are not using the `shakapacker-dev-server`, your packs will be served by the 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 the browser memory. For more details see [issue](https://github.com/shakacode/shakapacker/issues/88)[ #88](https://github.com/shakacode/shakapacker/issues/88)](https://github.com/shakacode/shakapacker/issues/88).
437
+ **Note:**
438
+ If you are not using the `shakapacker-dev-server`,
439
+ your packs will be served by the Rails public file server.
440
+ If you've enabled caching
441
+ (Rails application `config.action_controller.perform_caching` setting),
442
+ your changes will likely not be picked up due to `Cache-Control` header being set
443
+ and
444
+ assets being cached in the browser memory.
445
+ For more details see
446
+ [issue 88: Caching issues in Development since migrating to Shakapacker](https://github.com/shakacode/shakapacker/issues/88).
421
447
 
422
448
  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/shakapacker-dev-server`. This process will watch for changes in the relevant files, defined by `shakapacker.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/).
423
449
 
data/docs/v6_upgrade.md CHANGED
@@ -101,7 +101,7 @@ _If you're on webpacker v5, follow [how to upgrade to webpacker v6.0.0.rc.6 from
101
101
 
102
102
  1. Update `webpack-dev-server` to the current version, greater than 4.2, updating `package.json`.
103
103
 
104
- 1. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` and **at most one call** to `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932). If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/javascript/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
104
+ 1. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` and **at most one call** to `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932). If you expose jquery globally with `expose-loader` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/javascript/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
105
105
 
106
106
  1. If you are using any integrations like `css`, `postcss`, `React` or `TypeScript`. Please see https://github.com/shakacode/shakapacker#integrations section on how they work in v6.
107
107
 
@@ -168,5 +168,5 @@ _If you're on webpacker v5, follow [how to upgrade to webpacker v6.0.0.rc.6 from
168
168
 
169
169
  ## Examples of v5 to v6
170
170
 
171
- 1. [React on Rails Project with HMR and SSR](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/compare/webpacker-5.x...master)
171
+ 1. [React on Rails Project with HMR and SSR](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/compare/webpacker-5.x...61e897f2c604085f45b9ab5e23642501e430fb28)
172
172
  2. [Vue and Sass Example](https://github.com/guillaumebriday/upgrade-webpacker-5-to-6)
@@ -50,6 +50,11 @@ default: &default
50
50
  # https://webpack.js.org/guides/build-performance/#avoid-production-specific-tooling
51
51
  useContentHash: false
52
52
 
53
+ # Setting the asset host here will override Rails.application.config.asset_host.
54
+ # Here, you can set different asset_host per environment. Note that
55
+ # SHAKAPACKER_ASSET_HOST will override both configurations.
56
+ # asset_host: custom-path
57
+
53
58
  development:
54
59
  <<: *default
55
60
  compile: true
@@ -59,8 +64,6 @@ development:
59
64
  # Keys not described there are documented inline and in https://github.com/shakacode/shakapacker/
60
65
  dev_server:
61
66
  # For running dev server with https, set `server: https`.
62
- # You may use `https: true` instead but notice that it is deprecated in favor of `server: https`
63
- # Ensure only one of these entries is set.
64
67
  # server: https
65
68
 
66
69
  host: localhost
@@ -1,3 +1,4 @@
1
+ require "shakapacker/utils/misc"
1
2
  require "shakapacker/utils/version_syntax_converter"
2
3
 
3
4
  # Install Shakapacker
@@ -41,12 +42,28 @@ else
41
42
  say %( Add <%= javascript_pack_tag "application" %> within the <head> tag in your custom layout.)
42
43
  end
43
44
 
45
+ def package_json
46
+ if @package_json.nil?
47
+ Shakapacker::Utils::Misc.require_package_json_gem
48
+
49
+ @package_json = PackageJson.read
50
+ end
51
+
52
+ @package_json
53
+ end
54
+
44
55
  # Ensure there is `system!("bin/yarn")` command in `./bin/setup` file
45
56
  if (setup_path = Rails.root.join("bin/setup")).exist?
46
- say "Run bin/yarn during bin/setup"
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
62
+
63
+ say "Run #{native_install_command} during bin/setup"
47
64
 
48
65
  if File.read(setup_path).match? Regexp.escape(" # system('bin/yarn')\n")
49
- gsub_file(setup_path, "# system('bin/yarn')", "system!('bin/yarn')")
66
+ gsub_file(setup_path, "# system('bin/yarn')", "system!('#{native_install_command}')")
50
67
  else
51
68
  # Due to the inconsistency of quotation usage in Rails 7 compared to
52
69
  # earlier versions, we check both single and double quotations here.
@@ -55,30 +72,58 @@ if (setup_path = Rails.root.join("bin/setup")).exist?
55
72
  string_to_add = <<-RUBY
56
73
 
57
74
  # Install JavaScript dependencies
58
- system!("bin/yarn")
59
- RUBY
75
+ system!("#{native_install_command}")
76
+ RUBY
60
77
 
61
78
  if File.read(setup_path).match? pattern
62
79
  insert_into_file(setup_path, string_to_add, after: pattern)
63
80
  else
64
81
  say <<~MSG, :red
65
82
  It seems your `bin/setup` file doesn't have the expected content.
66
- Please review the file and manually add `system!("bin/yarn")` before any
83
+ Please review the file and manually add `system!("#{native_install_command}")` before any
67
84
  other command that requires JavaScript dependencies being already installed.
68
85
  MSG
69
86
  end
70
87
  end
71
88
  end
72
89
 
73
- results = []
90
+ 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
96
+ rescue PackageJson::Error
97
+ say "Shakapacker installation failed 😭 See above for details.", :red
98
+ exit 1
99
+ end
100
+
101
+ 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"]
108
+ end
74
109
 
75
110
  Dir.chdir(Rails.root) do
76
111
  npm_version = Shakapacker::Utils::VersionSyntaxConverter.new.rubygem_to_npm(Shakapacker::VERSION)
77
112
  say "Installing shakapacker@#{npm_version}"
78
- results << run("yarn add shakapacker@#{npm_version} --exact")
113
+ add_dependencies(["shakapacker@#{npm_version}"], :production)
114
+
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
124
+ end
79
125
 
80
- package_json = File.read("#{__dir__}/../../package.json")
81
- peers = JSON.parse(package_json)["peerDependencies"]
126
+ peers = fetch_peer_dependencies
82
127
  dev_dependency_packages = ["webpack-dev-server"]
83
128
 
84
129
  dependencies_to_add = []
@@ -96,13 +141,8 @@ Dir.chdir(Rails.root) do
96
141
  end
97
142
 
98
143
  say "Adding shakapacker peerDependencies"
99
- results << run("yarn add #{dependencies_to_add.join(' ')}")
144
+ add_dependencies(dependencies_to_add, :production)
100
145
 
101
146
  say "Installing webpack-dev-server for live reloading as a development dependency"
102
- results << run("yarn add --dev #{dev_dependencies_to_add.join(' ')}")
103
- end
104
-
105
- unless results.all?
106
- say "Shakapacker installation failed 😭 See above for details.", :red
107
- exit 1
147
+ add_dependencies(dev_dependencies_to_add, :dev)
108
148
  end
@@ -106,9 +106,11 @@ class Shakapacker::Compiler
106
106
 
107
107
  Shakapacker.set_shakapacker_env_variables_for_backward_compatibility
108
108
 
109
- env.merge("SHAKAPACKER_ASSET_HOST" => ENV.fetch("SHAKAPACKER_ASSET_HOST", ActionController::Base.helpers.compute_asset_host),
110
- "SHAKAPACKER_RELATIVE_URL_ROOT" => ENV.fetch("SHAKAPACKER_RELATIVE_URL_ROOT", ActionController::Base.relative_url_root),
111
- "SHAKAPACKER_CONFIG" => instance.config_path.to_s)
109
+ 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
113
+ )
112
114
  end
113
115
 
114
116
  def bin_shakapacker_path
@@ -116,6 +116,22 @@ class Shakapacker::Configuration
116
116
  data.fetch(key, defaults[:shakapacker_precompile])
117
117
  end
118
118
 
119
+ def asset_host
120
+ ENV.fetch(
121
+ "SHAKAPACKER_ASSET_HOST",
122
+ fetch(:asset_host) || ActionController::Base.helpers.compute_asset_host
123
+ )
124
+ end
125
+
126
+ def relative_url_root
127
+ Shakapacker.puts_deprecation_message "The usage of relative_url_root is deprecated in Shakapacker and will be removed in v8."
128
+
129
+ ENV.fetch(
130
+ "SHAKAPACKER_RELATIVE_URL_ROOT",
131
+ fetch(:relative_url_root) || ActionController::Base.relative_url_root
132
+ )
133
+ end
134
+
119
135
  private
120
136
  def data
121
137
  @data ||= load
@@ -31,7 +31,13 @@ class Shakapacker::DevServer
31
31
  end
32
32
 
33
33
  def https?
34
- case fetch(:https)
34
+ value = fetch(:https)
35
+
36
+ unless value.nil?
37
+ puts "WARNING: `https: true` has been deprecated in favor of `server: 'https'`"
38
+ end
39
+
40
+ case value
35
41
  when true, "true", Hash
36
42
  true
37
43
  else
@@ -48,7 +48,7 @@ module Shakapacker
48
48
  end
49
49
 
50
50
  if @argv.include?("--https") && !@https
51
- $stdout.puts "Please set https: true in shakapacker.yml to use the --https command line flag."
51
+ $stdout.puts "--https requires that 'server' in shakapacker.yml is set to 'https'"
52
52
  exit!
53
53
  end
54
54
  end
@@ -68,11 +68,7 @@ module Shakapacker
68
68
  env["WEBPACK_SERVE"] = "true"
69
69
  env["NODE_OPTIONS"] = ENV["NODE_OPTIONS"] || ""
70
70
 
71
- cmd = if node_modules_bin_exist?
72
- ["#{@node_modules_bin_path}/webpack", "serve"]
73
- else
74
- ["yarn", "webpack", "serve"]
75
- end
71
+ cmd = build_cmd
76
72
 
77
73
  if @argv.include?("--debug-webpacker")
78
74
  Shakapacker.puts_deprecation_message(
@@ -90,7 +86,10 @@ module Shakapacker
90
86
  cmd += ["--config", @webpack_config]
91
87
  cmd += ["--progress", "--color"] if @pretty
92
88
 
93
- cmd += ["--hot"] if @hot
89
+ # Default behavior of webpack-dev-server is @hot = true
90
+ cmd += ["--hot", "only"] if @hot == "only"
91
+ cmd += ["--no-hot"] if !@hot
92
+
94
93
  cmd += @argv
95
94
 
96
95
  Dir.chdir(@app_path) do
@@ -98,6 +97,16 @@ module Shakapacker
98
97
  end
99
98
  end
100
99
 
100
+ def build_cmd
101
+ if Shakapacker::Utils::Misc.use_package_json_gem
102
+ return package_json.manager.native_exec_command("webpack", ["serve"])
103
+ end
104
+
105
+ return ["#{@node_modules_bin_path}/webpack", "serve"] if node_modules_bin_exist?
106
+
107
+ ["yarn", "webpack", "serve"]
108
+ end
109
+
101
110
  def node_modules_bin_exist?
102
111
  File.exist?("#{@node_modules_bin_path}/webpack-dev-server")
103
112
  end
@@ -44,7 +44,9 @@ module Shakapacker
44
44
  end
45
45
  files = Dir[*expanded_paths].reject { |f| File.directory?(f) }
46
46
  file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
47
- Digest::SHA1.hexdigest(file_ids.join("/"))
47
+
48
+ asset_host = Shakapacker.config.asset_host.to_s
49
+ Digest::SHA1.hexdigest(file_ids.join("/").concat(asset_host))
48
50
  end
49
51
 
50
52
  def record_compilation_digest
@@ -1,3 +1,5 @@
1
+ require "shakapacker/utils/misc"
2
+
1
3
  module Shakapacker
2
4
  class Runner
3
5
  def self.run(argv)
@@ -14,7 +16,7 @@ module Shakapacker
14
16
 
15
17
  Shakapacker.set_shakapacker_env_variables_for_backward_compatibility
16
18
 
17
- @node_modules_bin_path = ENV["SHAKAPACKER_NODE_MODULES_BIN_PATH"] || `yarn bin`.chomp
19
+ @node_modules_bin_path = fetch_node_modules_bin_path
18
20
  @shakapacker_config = ENV["SHAKAPACKER_CONFIG"] || File.join(@app_path, "config/shakapacker.yml")
19
21
 
20
22
  @shakapacker_config = Shakapacker.get_config_file_path_with_backward_compatibility(@shakapacker_config)
@@ -24,5 +26,21 @@ module Shakapacker
24
26
  exit!
25
27
  end
26
28
  end
29
+
30
+ def fetch_node_modules_bin_path
31
+ return nil if Shakapacker::Utils::Misc.use_package_json_gem
32
+
33
+ ENV["SHAKAPACKER_NODE_MODULES_BIN_PATH"] || `yarn bin`.chomp
34
+ end
35
+
36
+ def package_json
37
+ if @package_json.nil?
38
+ Shakapacker::Utils::Misc.require_package_json_gem
39
+
40
+ @package_json = PackageJson.read(@app_path)
41
+ end
42
+
43
+ @package_json
44
+ end
27
45
  end
28
46
  end
@@ -8,6 +8,18 @@ module Shakapacker
8
8
  class Misc
9
9
  extend FileUtils
10
10
 
11
+ def self.use_package_json_gem
12
+ ENV.fetch("SHAKAPACKER_USE_PACKAGE_JSON_GEM", "false").casecmp("true").zero?
13
+ end
14
+
15
+ def self.require_package_json_gem
16
+ unless use_package_json_gem
17
+ raise "PackageJson should not be used unless SHAKAPACKER_USE_PACKAGE_JSON_GEM is true"
18
+ end
19
+
20
+ require "package_json"
21
+ end
22
+
11
23
  def self.uncommitted_changes?(message_handler)
12
24
  return false if ENV["COVERAGE"] == "true"
13
25
 
@@ -1,4 +1,4 @@
1
1
  module Shakapacker
2
2
  # Change the version in package.json too, please!
3
- VERSION = "7.1.0".freeze
3
+ VERSION = "7.2.0".freeze
4
4
  end
@@ -21,11 +21,7 @@ module Shakapacker
21
21
  env["SHAKAPACKER_CONFIG"] = @shakapacker_config
22
22
  env["NODE_OPTIONS"] = ENV["NODE_OPTIONS"] || ""
23
23
 
24
- cmd = if node_modules_bin_exist?
25
- ["#{@node_modules_bin_path}/webpack"]
26
- else
27
- ["yarn", "webpack"]
28
- end
24
+ cmd = build_cmd
29
25
 
30
26
  if @argv.include?("--debug-webpacker")
31
27
  Shakapacker.puts_deprecation_message(
@@ -61,6 +57,17 @@ module Shakapacker
61
57
  end
62
58
 
63
59
  private
60
+
61
+ def build_cmd
62
+ if Shakapacker::Utils::Misc.use_package_json_gem
63
+ return package_json.manager.native_exec_command("webpack")
64
+ end
65
+
66
+ return ["#{@node_modules_bin_path}/webpack"] if node_modules_bin_exist?
67
+
68
+ ["yarn", "webpack"]
69
+ end
70
+
64
71
  def node_modules_bin_exist?
65
72
  File.exist?("#{@node_modules_bin_path}/webpack")
66
73
  end
@@ -3,7 +3,7 @@ bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin")
3
3
 
4
4
  namespace :shakapacker do
5
5
  desc "Installs Shakapacker binstubs in this application"
6
- task binstubs: [:check_node, :check_yarn] do |task|
6
+ task binstubs: [:check_node, :check_manager] do |task|
7
7
  prefix = task.name.split(/#|shakapacker:binstubs/).first
8
8
 
9
9
  if Rails::VERSION::MAJOR >= 5
@@ -0,0 +1,27 @@
1
+ require "shakapacker/utils/misc"
2
+
3
+ namespace :shakapacker do
4
+ desc "Verifies if the expected JS package manager is installed"
5
+ task :check_manager do |task|
6
+ unless Shakapacker::Utils::Misc.use_package_json_gem
7
+ prefix = task.name.split(/#|shakapacker:/).first
8
+ Rake::Task["#{prefix}shakapacker:check_manager"].invoke
9
+ next
10
+ end
11
+
12
+ require "package_json"
13
+
14
+ package_json = PackageJson.read
15
+ pm = package_json.manager.binary
16
+
17
+ begin
18
+ version = package_json.manager.version
19
+
20
+ $stdout.puts "using #{pm}@#{version} to manage dependencies and scripts in package.json"
21
+ rescue PackageJson::Error
22
+ $stderr.puts "#{pm} not installed - please ensure it is installed before trying again"
23
+ $stderr.puts "Exiting!"
24
+ exit!
25
+ end
26
+ end
27
+ end
@@ -1,7 +1,8 @@
1
- require "semantic_range"
2
1
  namespace :shakapacker do
3
2
  desc "Verifies if Yarn is installed"
4
3
  task :check_yarn do
4
+ require "semantic_range"
5
+
5
6
  begin
6
7
  which_command = Gem.win_platform? ? "where" : "which"
7
8
  raise Errno::ENOENT if `#{which_command} yarn`.strip.empty?
@@ -1,4 +1,5 @@
1
1
  require "shakapacker/version"
2
+ require "shakapacker/utils/misc"
2
3
 
3
4
  namespace :shakapacker do
4
5
  desc "Provide information on Shakapacker's environment"
@@ -8,14 +9,30 @@ namespace :shakapacker do
8
9
  $stdout.puts "Rails: #{Rails.version}"
9
10
  $stdout.puts "Shakapacker: #{Shakapacker::VERSION}"
10
11
  $stdout.puts "Node: #{`node --version`}"
11
- $stdout.puts "Yarn: #{`yarn --version`}"
12
+ if Shakapacker::Utils::Misc.use_package_json_gem
13
+ require "package_json"
14
+
15
+ pj_manager = PackageJson.read.manager
16
+
17
+ $stdout.puts "#{pj_manager.binary}: #{pj_manager.version}"
18
+ else
19
+ $stdout.puts "Yarn: #{`yarn --version`}"
20
+ end
21
+
22
+ if Shakapacker::Utils::Misc.use_package_json_gem
23
+ node_package_version = Shakapacker::VersionChecker.build.node_package_version.raw
24
+ else
25
+ node_package_version = `npm list shakapacker version`
26
+ end
12
27
 
13
28
  $stdout.puts "\n"
14
- $stdout.puts "shakapacker: \n#{`npm list shakapacker version`}"
29
+ $stdout.puts "shakapacker: #{node_package_version}"
15
30
 
16
31
  $stdout.puts "Is bin/shakapacker present?: #{File.exist? 'bin/shakapacker'}"
17
32
  $stdout.puts "Is bin/shakapacker-dev-server present?: #{File.exist? 'bin/shakapacker-dev-server'}"
18
- $stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}"
33
+ unless Shakapacker::Utils::Misc.use_package_json_gem
34
+ $stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}"
35
+ end
19
36
  end
20
37
  end
21
38
  end
@@ -3,7 +3,7 @@ bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin")
3
3
 
4
4
  namespace :shakapacker do
5
5
  desc "Install Shakapacker in this application"
6
- task install: [:check_node, :check_yarn] do |task|
6
+ task install: [:check_node] do |task|
7
7
  Shakapacker::Configuration.installing = true
8
8
 
9
9
  prefix = task.name.split(/#|shakapacker:install/).first