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
@@ -1,87 +1,9 @@
1
1
  require "thor"
2
2
 
3
3
  module Shakapacker
4
- DEPRECATION_GUIDE_URL = "https://github.com/shakacode/shakapacker/blob/main/docs/v7_upgrade.md"
5
- DEPRECATION_MESSAGE = <<~MSG
6
- DEPRECATION NOTICE:
7
-
8
- Using webpacker spelling is deprecated in Shakapacker.
9
- Update your project with the new spelling.
10
-
11
- For more information about this process, check:
12
- #{DEPRECATION_GUIDE_URL}
13
- MSG
14
4
  SHELL = Thor::Shell::Color.new
15
5
 
16
- def get_config_file_path_with_backward_compatibility(config_path)
17
- if config_path.to_s.end_with?("shakapacker.yml") && !File.exist?(config_path)
18
- webpacker_config_path = if config_path.class == Pathname
19
- Pathname.new(config_path.to_s.gsub("shakapacker.yml", "webpacker.yml"))
20
- else
21
- config_path.gsub("shakapacker.yml", "webpacker.yml")
22
- end
23
-
24
- if File.exist?(webpacker_config_path)
25
- puts_deprecation_message(
26
- short_deprecation_message(
27
- "config/webpacker.yml",
28
- "config/shakapacker.yml"
29
- )
30
- )
31
- return webpacker_config_path
32
- end
33
- end
34
-
35
- config_path
36
- end
37
-
38
- def set_shakapacker_env_variables_for_backward_compatibility
39
- webpacker_env_variables = ENV.select { |key| key.start_with?("WEBPACKER_") }
40
-
41
- deprecation_message_body = ""
42
-
43
- webpacker_env_variables.each do |webpacker_key, _|
44
- shakapacker_key = webpacker_key.gsub("WEBPACKER_", "SHAKAPACKER_")
45
- next if ENV.key?(shakapacker_key)
46
-
47
- deprecation_message_body += <<~MSG
48
- Use `#{shakapacker_key}` instead of the deprecated `#{webpacker_key}`.
49
- MSG
50
-
51
- ENV[shakapacker_key] = ENV[webpacker_key]
52
- end
53
-
54
- if deprecation_message_body.present?
55
- Shakapacker.puts_deprecation_message(
56
- <<~MSG
57
- DEPRECATION NOTICE:
58
-
59
- #{deprecation_message_body}
60
- Read more: #{Shakapacker::DEPRECATION_GUIDE_URL}
61
- MSG
62
- )
63
- end
64
- end
65
-
66
- def short_deprecation_message(old_usage, new_usage)
67
- <<~MSG
68
- DEPRECATION NOTICE:
69
-
70
- Consider using `#{new_usage}` instead of the deprecated `#{old_usage}`.
71
- Read more: #{DEPRECATION_GUIDE_URL}
72
- MSG
73
- end
74
-
75
6
  def puts_deprecation_message(message)
76
7
  SHELL.say "\n#{message}\n", :yellow
77
8
  end
78
-
79
- def puts_rake_deprecation_message(webpacker_task_name)
80
- Shakapacker.puts_deprecation_message(
81
- Shakapacker.short_deprecation_message(
82
- "rake #{webpacker_task_name}",
83
- "rake #{webpacker_task_name.gsub("webpacker", "shakapacker")}"
84
- )
85
- )
86
- end
87
9
  end
@@ -30,21 +30,6 @@ class Shakapacker::DevServer
30
30
  fetch(:port)
31
31
  end
32
32
 
33
- def 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
41
- when true, "true", Hash
42
- true
43
- else
44
- false
45
- end
46
- end
47
-
48
33
  def server
49
34
  server_value = fetch(:server)
50
35
  server_type = server_value.is_a?(Hash) ? server_value[:type] : server_value
@@ -65,7 +50,7 @@ class Shakapacker::DevServer
65
50
  end
66
51
 
67
52
  def protocol
68
- return "https" if server == "https" || https? == true
53
+ return "https" if server == "https"
69
54
 
70
55
  "http"
71
56
  end
@@ -70,16 +70,7 @@ module Shakapacker
70
70
 
71
71
  cmd = build_cmd
72
72
 
73
- if @argv.include?("--debug-webpacker")
74
- Shakapacker.puts_deprecation_message(
75
- Shakapacker.short_deprecation_message(
76
- "--debug-webpacker",
77
- "--debug-shakapacker"
78
- )
79
- )
80
- end
81
-
82
- if @argv.delete("--debug-shakapacker") || @argv.delete("--debug-webpacker")
73
+ if @argv.delete("--debug-shakapacker")
83
74
  env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --inspect-brk --trace-warnings"
84
75
  end
85
76
 
@@ -98,17 +89,7 @@ module Shakapacker
98
89
  end
99
90
 
100
91
  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
-
110
- def node_modules_bin_exist?
111
- File.exist?("#{@node_modules_bin_path}/webpack-dev-server")
92
+ package_json.manager.native_exec_command("webpack", ["serve"])
112
93
  end
113
94
  end
114
95
  end
@@ -20,7 +20,7 @@ class Shakapacker::Env
20
20
  end
21
21
 
22
22
  def fallback_env_warning
23
- logger.info "RAILS_ENV=#{Rails.env} environment is not defined in config/shakapacker.yml, falling back to #{Shakapacker::DEFAULT_ENV} environment"
23
+ logger.info "RAILS_ENV=#{Rails.env} environment is not defined in #{config_path}, falling back to #{Shakapacker::DEFAULT_ENV} environment"
24
24
  end
25
25
 
26
26
  def available_environments
@@ -7,9 +7,7 @@ class Shakapacker::Instance
7
7
 
8
8
  def initialize(root_path: Rails.root, config_path: Rails.root.join("config/shakapacker.yml"))
9
9
  @root_path = root_path
10
-
11
- # For backward compatibility
12
- @config_path = Shakapacker.get_config_file_path_with_backward_compatibility(config_path)
10
+ @config_path = Pathname.new(ENV["SHAKAPACKER_CONFIG"] || config_path)
13
11
  end
14
12
 
15
13
  def env
@@ -105,7 +105,7 @@ class Shakapacker::Manifest
105
105
  def missing_file_from_manifest_error(bundle_name)
106
106
  <<-MSG
107
107
  Shakapacker can't find #{bundle_name} in #{config.manifest_path}. Possible causes:
108
- 1. You forgot to install node packages (try `yarn install`) or are running an incompatible version of Node
108
+ 1. You forgot to install javascript packages or are running an incompatible javascript runtime version
109
109
  2. Your app has code with a non-standard extension (like a `.jsx` file) but the extension is not in the `extensions` config in `config/shakapacker.yml`
110
110
  3. You have set compile: false (see `config/shakapacker.yml`) for this environment
111
111
  (unless you are using the `bin/shakapacker -w` or the `bin/shakapacker-dev-server`, in which case maybe you aren't running the dev server in the background?)
@@ -3,6 +3,7 @@ require "rails/railtie"
3
3
  require "shakapacker/helper"
4
4
  require "shakapacker/dev_server_proxy"
5
5
  require "shakapacker/version_checker"
6
+ require "shakapacker/utils/manager"
6
7
 
7
8
  class Shakapacker::Engine < ::Rails::Engine
8
9
  # Allows Shakapacker config values to be set via Rails env config files
@@ -14,6 +15,12 @@ class Shakapacker::Engine < ::Rails::Engine
14
15
  end
15
16
  end
16
17
 
18
+ initializer "shakapacker.manager_checker" do
19
+ if File.exist?(Shakapacker::VersionChecker::NodePackageVersion.package_json_path)
20
+ Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious!
21
+ end
22
+ end
23
+
17
24
  initializer "shakapacker.proxy" do |app|
18
25
  if (Shakapacker.config.dev_server.present? rescue nil)
19
26
  app.middleware.insert_before 0,
@@ -1,4 +1,6 @@
1
1
  require "shakapacker/utils/misc"
2
+ require "shakapacker/utils/manager"
3
+ require "package_json"
2
4
 
3
5
  module Shakapacker
4
6
  class Runner
@@ -13,34 +15,18 @@ module Shakapacker
13
15
 
14
16
  @app_path = File.expand_path(".", Dir.pwd)
15
17
  @webpack_config = File.join(@app_path, "config/webpack/webpack.config.js")
16
-
17
- Shakapacker.set_shakapacker_env_variables_for_backward_compatibility
18
-
19
- @node_modules_bin_path = fetch_node_modules_bin_path
20
18
  @shakapacker_config = ENV["SHAKAPACKER_CONFIG"] || File.join(@app_path, "config/shakapacker.yml")
21
19
 
22
- @shakapacker_config = Shakapacker.get_config_file_path_with_backward_compatibility(@shakapacker_config)
23
-
24
20
  unless File.exist?(@webpack_config)
25
21
  $stderr.puts "webpack config #{@webpack_config} not found, please run 'bundle exec rails shakapacker:install' to install Shakapacker with default configs or add the missing config file for your custom environment."
26
22
  exit!
27
23
  end
28
- end
29
24
 
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
25
+ Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious!
34
26
  end
35
27
 
36
28
  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
29
+ @package_json ||= PackageJson.read(@app_path)
44
30
  end
45
31
  end
46
32
  end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "package_json"
4
+
5
+ module Shakapacker
6
+ module Utils
7
+ class Manager
8
+ class Error < StandardError; end
9
+
10
+ MANAGER_LOCKS = {
11
+ "bun" => "bun.lockb",
12
+ "npm" => "package-lock.json",
13
+ "pnpm" => "pnpm-lock.yaml",
14
+ "yarn" => "yarn.lock"
15
+ }
16
+
17
+ # Emits a warning if it's not obvious what package manager to use
18
+ def self.error_unless_package_manager_is_obvious!
19
+ return unless PackageJson.read.fetch("packageManager", nil).nil?
20
+
21
+ guessed_binary = guess_binary
22
+
23
+ return if guessed_binary == "npm"
24
+
25
+ raise Error, <<~MSG
26
+ You don't have "packageManager" set in your package.json
27
+ meaning that Shakapacker will use npm but you've got a #{MANAGER_LOCKS[guessed_binary]}
28
+ file meaning you probably want to be using #{guessed_binary} instead.
29
+
30
+ To make this happen, set "packageManager" in your package.json to #{guessed_binary}@#{guess_version}
31
+ MSG
32
+ end
33
+
34
+ # Guesses the binary of the package manager to use based on what lockfiles exist
35
+ #
36
+ # @return [String]
37
+ def self.guess_binary
38
+ MANAGER_LOCKS.find { |_, lock| File.exist?(lock) }&.first || "npm"
39
+ end
40
+
41
+ # Guesses the version of the package manager to use by calling `<manager> --version`
42
+ #
43
+ # @return [String]
44
+ def self.guess_version
45
+ require "open3"
46
+
47
+ command = "#{guess_binary} --version"
48
+ stdout, stderr, status = Open3.capture3(command)
49
+
50
+ unless status.success?
51
+ raise Error, "#{command} failed with exit code #{status.exitstatus}: #{stderr}"
52
+ end
53
+
54
+ stdout.chomp
55
+ end
56
+ end
57
+ end
58
+ end
@@ -8,18 +8,6 @@ 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
-
23
11
  def self.uncommitted_changes?(message_handler)
24
12
  return false if ENV["COVERAGE"] == "true"
25
13
 
@@ -1,4 +1,4 @@
1
1
  module Shakapacker
2
2
  # Change the version in package.json too, please!
3
- VERSION = "7.2.3".freeze
3
+ VERSION = "8.0.0-rc.1".freeze
4
4
  end
@@ -17,41 +17,19 @@ module Shakapacker
17
17
 
18
18
  def raise_if_gem_and_node_package_versions_differ
19
19
  # Skip check if package is not in package.json or listed from relative path, git repo or github URL
20
- return if node_package_version.skip_processing?
20
+ # or if consistent version checking is not enabled
21
+ return if node_package_version.skip_processing? || !Shakapacker.config.ensure_consistent_versioning?
21
22
 
22
23
  node_major_minor_patch = node_package_version.major_minor_patch
23
24
  gem_major_minor_patch = gem_major_minor_patch_version
24
- versions_match = node_major_minor_patch[0] == gem_major_minor_patch[0] &&
25
- node_major_minor_patch[1] == gem_major_minor_patch[1] &&
26
- node_major_minor_patch[2] == gem_major_minor_patch[2]
27
25
 
28
- uses_wildcard = node_package_version.semver_wildcard?
26
+ raise_differing_versions_warning unless (
27
+ node_major_minor_patch[0] == gem_major_minor_patch[0] &&
28
+ node_major_minor_patch[1] == gem_major_minor_patch[1] &&
29
+ node_major_minor_patch[2] == gem_major_minor_patch[2]
30
+ )
29
31
 
30
- if !Shakapacker.config.ensure_consistent_versioning? && (uses_wildcard || !versions_match)
31
- check_failed = if uses_wildcard
32
- "Semver wildcard without a lockfile detected"
33
- else
34
- "Version mismatch detected"
35
- end
36
-
37
- warn <<-MSG.strip_heredoc
38
- Shakapacker::VersionChecker - #{check_failed}
39
-
40
- You are currently not checking for consistent versions of shakapacker gem and npm package. A version mismatch or usage of semantic versioning wildcard (~ or ^) without a lockfile has been detected.
41
-
42
- Version mismatch can lead to incorrect behavior and bugs. You should ensure that both the gem and npm package dependencies are locked to the same version.
43
-
44
- You can enable the version check by setting `ensure_consistent_versioning: true` in your `shakapacker.yml` file.
45
-
46
- Checking for gem and npm package versions mismatch or wildcard will be enabled by default in the next major version of shakapacker.
47
- MSG
48
-
49
- return
50
- end
51
-
52
- raise_differing_versions_warning unless versions_match
53
-
54
- raise_node_semver_version_warning if uses_wildcard
32
+ raise_node_semver_version_warning if node_package_version.semver_wildcard?
55
33
  end
56
34
 
57
35
  private
@@ -87,6 +65,7 @@ module Shakapacker
87
65
  [match[1], match[2], match[3]]
88
66
  end
89
67
 
68
+ # TODO: this might as well use package_json
90
69
  class NodePackageVersion
91
70
  attr_reader :package_json
92
71
 
@@ -23,16 +23,7 @@ module Shakapacker
23
23
 
24
24
  cmd = build_cmd
25
25
 
26
- if @argv.include?("--debug-webpacker")
27
- Shakapacker.puts_deprecation_message(
28
- Shakapacker.short_deprecation_message(
29
- "--debug-webpacker",
30
- "--debug-shakapacker"
31
- )
32
- )
33
- end
34
-
35
- if @argv.delete("--debug-shakapacker") || @argv.delete("--debug-webpacker")
26
+ if @argv.delete("--debug-shakapacker")
36
27
  env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --inspect-brk"
37
28
  end
38
29
 
@@ -59,17 +50,7 @@ module Shakapacker
59
50
  private
60
51
 
61
52
  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
-
71
- def node_modules_bin_exist?
72
- File.exist?("#{@node_modules_bin_path}/webpack")
53
+ package_json.manager.native_exec_command("webpack")
73
54
  end
74
55
  end
75
56
  end
data/lib/shakapacker.rb CHANGED
@@ -47,5 +47,3 @@ require "shakapacker/dev_server"
47
47
  require "shakapacker/deprecation_helper"
48
48
 
49
49
  require "shakapacker/railtie" if defined?(Rails)
50
-
51
- Webpacker = Shakapacker
@@ -1,34 +1,20 @@
1
1
  namespace :shakapacker do
2
2
  desc "Verifies that bin/shakapacker is present"
3
3
  task :check_binstubs do
4
- verify_file_existence("bin/shakapacker", "bin/webpacker")
5
- verify_file_existence("bin/shakapacker-dev-server", "bin/webpacker-dev-server")
4
+ verify_file_existence("bin/shakapacker")
5
+ verify_file_existence("bin/shakapacker-dev-server")
6
6
  end
7
7
  end
8
8
 
9
- def verify_file_existance(main_file, alternative_file)
10
- puts "verify_file_existance is deprecated - use verify_file_existence instead"
11
- verify_file_existence(main_file, alternative_file)
12
- end
13
-
14
- def verify_file_existence(main_file, alternative_file)
15
- unless File.exist?(Rails.root.join(main_file))
16
- if File.exist?(Rails.root.join(alternative_file))
17
- Shakapacker.puts_deprecation_message(
18
- Shakapacker.short_deprecation_message(
19
- alternative_file,
20
- main_file
21
- )
22
- )
23
- else
24
- puts <<~MSG
25
- Couldn't find shakapacker binstubs!
26
- Possible solutions:
27
- - Ensure you have run `rails shakapacker:install`.
28
- - Run `rails shakapacker:binstubs` if you have already installed shakapacker.
29
- - Ensure the `bin` directory, `bin/shakapacker`, and `bin/shakapacker-dev-server` are not included in .gitignore.
30
- MSG
31
- exit!
32
- end
9
+ def verify_file_existence(binstub_file)
10
+ unless File.exist?(Rails.root.join(binstub_file))
11
+ puts <<~MSG
12
+ Couldn't find shakapacker binstubs!
13
+ Possible solutions:
14
+ - Ensure you have run `rails shakapacker:install`.
15
+ - Run `rails shakapacker:binstubs` if you have already installed shakapacker.
16
+ - Ensure the `bin` directory, `bin/shakapacker`, and `bin/shakapacker-dev-server` are not included in .gitignore.
17
+ MSG
18
+ exit!
33
19
  end
34
20
  end
@@ -1,15 +1,10 @@
1
1
  require "shakapacker/utils/misc"
2
+ require "shakapacker/utils/manager"
2
3
 
3
4
  namespace :shakapacker do
4
5
  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"
6
+ task :check_manager do
7
+ Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious!
13
8
 
14
9
  package_json = PackageJson.read
15
10
  pm = package_json.manager.binary
@@ -1,5 +1,6 @@
1
1
  require "shakapacker/version"
2
2
  require "shakapacker/utils/misc"
3
+ require "shakapacker/utils/manager"
3
4
 
4
5
  namespace :shakapacker do
5
6
  desc "Provide information on Shakapacker's environment"
@@ -9,30 +10,20 @@ namespace :shakapacker do
9
10
  $stdout.puts "Rails: #{Rails.version}"
10
11
  $stdout.puts "Shakapacker: #{Shakapacker::VERSION}"
11
12
  $stdout.puts "Node: #{`node --version`}"
12
- if Shakapacker::Utils::Misc.use_package_json_gem
13
- require "package_json"
14
13
 
15
- pj_manager = PackageJson.read.manager
14
+ Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious!
16
15
 
17
- $stdout.puts "#{pj_manager.binary}: #{pj_manager.version}"
18
- else
19
- $stdout.puts "Yarn: #{`yarn --version`}"
20
- end
16
+ pj_manager = PackageJson.read.manager
21
17
 
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
18
+ $stdout.puts "#{pj_manager.binary}: #{pj_manager.version}"
19
+
20
+ node_package_version = Shakapacker::VersionChecker.build.node_package_version.raw
27
21
 
28
22
  $stdout.puts "\n"
29
23
  $stdout.puts "shakapacker: #{node_package_version}"
30
24
 
31
25
  $stdout.puts "Is bin/shakapacker present?: #{File.exist? 'bin/shakapacker'}"
32
26
  $stdout.puts "Is bin/shakapacker-dev-server present?: #{File.exist? 'bin/shakapacker-dev-server'}"
33
- unless Shakapacker::Utils::Misc.use_package_json_gem
34
- $stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}"
35
- end
36
27
  end
37
28
  end
38
29
  end
@@ -1,11 +1,11 @@
1
- const { moduleExists } = require('shakapacker')
1
+ const { moduleExists } = require("shakapacker")
2
2
 
3
3
  module.exports = function config(api) {
4
- const validEnv = ['development', 'test', 'production']
4
+ const validEnv = ["development", "test", "production"]
5
5
  const currentEnv = api.env()
6
- const isDevelopmentEnv = api.env('development')
7
- const isProductionEnv = api.env('production')
8
- const isTestEnv = api.env('test')
6
+ const isDevelopmentEnv = api.env("development")
7
+ const isProductionEnv = api.env("production")
8
+ const isTestEnv = api.env("test")
9
9
 
10
10
  if (!validEnv.includes(currentEnv)) {
11
11
  throw new Error(
@@ -17,21 +17,21 @@ module.exports = function config(api) {
17
17
 
18
18
  return {
19
19
  presets: [
20
- isTestEnv && ['@babel/preset-env', { targets: { node: 'current' } }],
20
+ isTestEnv && ["@babel/preset-env", { targets: { node: "current" } }],
21
21
  (isProductionEnv || isDevelopmentEnv) && [
22
- '@babel/preset-env',
22
+ "@babel/preset-env",
23
23
  {
24
- useBuiltIns: 'entry',
25
- corejs: '3.8',
26
- modules: 'auto',
24
+ useBuiltIns: "entry",
25
+ corejs: "3.8",
26
+ modules: "auto",
27
27
  bugfixes: true,
28
- exclude: ['transform-typeof-symbol']
28
+ exclude: ["transform-typeof-symbol"]
29
29
  }
30
30
  ],
31
- moduleExists('@babel/preset-typescript') && '@babel/preset-typescript'
31
+ moduleExists("@babel/preset-typescript") && "@babel/preset-typescript"
32
32
  ].filter(Boolean),
33
- plugins: [
34
- ['@babel/plugin-transform-runtime', { helpers: false }]
35
- ].filter(Boolean)
33
+ plugins: [["@babel/plugin-transform-runtime", { helpers: false }]].filter(
34
+ Boolean
35
+ )
36
36
  }
37
37
  }
data/package/config.js CHANGED
@@ -1,19 +1,16 @@
1
- const { resolve } = require('path')
2
- const { load } = require('js-yaml')
3
- const { existsSync, readFileSync } = require('fs')
1
+ const { resolve } = require("path")
2
+ const { load } = require("js-yaml")
3
+ const { existsSync, readFileSync } = require("fs")
4
4
 
5
- const { merge } = require('webpack-merge')
6
- const {
7
- ensureTrailingSlash,
8
- setShakapackerEnvVariablesForBackwardCompatibility
9
- } = require('./utils/helpers')
10
- const { railsEnv } = require('./env')
11
- const configPath = require('./utils/configPath')
5
+ const { merge } = require("webpack-merge")
6
+ const { ensureTrailingSlash } = require("./utils/helpers")
7
+ const { railsEnv } = require("./env")
8
+ const configPath = require("./utils/configPath")
12
9
 
13
- const defaultConfigPath = require('./utils/defaultConfigPath')
10
+ const defaultConfigPath = require("./utils/defaultConfigPath")
14
11
 
15
12
  const getDefaultConfig = () => {
16
- const defaultConfig = load(readFileSync(defaultConfigPath), 'utf8')
13
+ const defaultConfig = load(readFileSync(defaultConfigPath), "utf8")
17
14
  return defaultConfig[railsEnv] || defaultConfig.production
18
15
  }
19
16
 
@@ -21,12 +18,14 @@ const defaults = getDefaultConfig()
21
18
  let config
22
19
 
23
20
  if (existsSync(configPath)) {
24
- const appYmlObject = load(readFileSync(configPath), 'utf8')
21
+ const appYmlObject = load(readFileSync(configPath), "utf8")
25
22
  const envAppConfig = appYmlObject[railsEnv]
26
23
 
27
24
  if (!envAppConfig) {
28
25
  /* eslint no-console:0 */
29
- console.warn(`Warning: ${railsEnv} key not found in the configuration file. Using production configuration as a fallback.`)
26
+ console.warn(
27
+ `Warning: ${railsEnv} key not found in the configuration file. Using production configuration as a fallback.`
28
+ )
30
29
  }
31
30
 
32
31
  config = merge(defaults, envAppConfig || {})
@@ -39,8 +38,7 @@ config.outputPath = resolve(config.public_root_path, config.public_output_path)
39
38
  // Ensure that the publicPath includes our asset host so dynamic imports
40
39
  // (code-splitting chunks and static assets) load from the CDN instead of a relative path.
41
40
  const getPublicPath = () => {
42
- setShakapackerEnvVariablesForBackwardCompatibility()
43
- const rootUrl = ensureTrailingSlash(process.env.SHAKAPACKER_ASSET_HOST || '/')
41
+ const rootUrl = ensureTrailingSlash(process.env.SHAKAPACKER_ASSET_HOST || "/")
44
42
  return `${rootUrl}${config.public_output_path}/`
45
43
  }
46
44
 
@@ -50,7 +48,7 @@ config.publicPathWithoutCDN = `/${config.public_output_path}/`
50
48
  if (config.manifest_path) {
51
49
  config.manifestPath = resolve(config.manifest_path)
52
50
  } else {
53
- config.manifestPath = resolve(config.outputPath, 'manifest.json')
51
+ config.manifestPath = resolve(config.outputPath, "manifest.json")
54
52
  }
55
53
 
56
54
  module.exports = config