shakapacker 6.6.0 → 7.0.0.rc.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (197) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby-backward-compatibility.yml +43 -0
  3. data/CHANGELOG.md +50 -4
  4. data/CONTRIBUTING.md +4 -4
  5. data/Gemfile.development_dependencies +1 -1
  6. data/README.md +113 -88
  7. data/Rakefile +5 -0
  8. data/config/shakapacker.yml +1 -0
  9. data/docs/deployment.md +12 -10
  10. data/docs/developing_webpacker.md +5 -5
  11. data/docs/react.md +10 -6
  12. data/docs/style_loader_vs_mini_css.md +2 -2
  13. data/docs/troubleshooting.md +22 -18
  14. data/docs/using_esbuild_loader.md +5 -3
  15. data/docs/using_swc_loader.md +2 -2
  16. data/docs/v6_upgrade.md +2 -2
  17. data/docs/v7_upgrade.md +56 -0
  18. data/lib/install/bin/shakapacker +13 -0
  19. data/lib/install/bin/shakapacker-dev-server +13 -0
  20. data/lib/install/config/{webpacker.yml → shakapacker.yml} +13 -8
  21. data/lib/install/template.rb +7 -7
  22. data/lib/{webpacker → shakapacker}/base_strategy.rb +2 -2
  23. data/lib/{webpacker → shakapacker}/commands.rb +4 -4
  24. data/lib/{webpacker → shakapacker}/compiler.rb +43 -15
  25. data/lib/{webpacker → shakapacker}/compiler_strategy.rb +6 -6
  26. data/lib/{webpacker → shakapacker}/configuration.rb +52 -21
  27. data/lib/shakapacker/deprecation_helper.rb +88 -0
  28. data/lib/{webpacker → shakapacker}/dev_server.rb +27 -4
  29. data/lib/{webpacker → shakapacker}/dev_server_proxy.rb +4 -4
  30. data/lib/shakapacker/dev_server_runner.rb +104 -0
  31. data/lib/{webpacker → shakapacker}/digest_strategy.rb +6 -6
  32. data/lib/{webpacker → shakapacker}/env.rb +8 -8
  33. data/lib/{webpacker → shakapacker}/helper.rb +20 -20
  34. data/lib/{webpacker → shakapacker}/instance.rb +13 -10
  35. data/lib/{webpacker → shakapacker}/manifest.rb +14 -14
  36. data/lib/{webpacker → shakapacker}/mtime_strategy.rb +5 -5
  37. data/lib/shakapacker/railtie.rb +70 -0
  38. data/lib/shakapacker/runner.rb +28 -0
  39. data/lib/shakapacker/utils/version_syntax_converter.rb +2 -2
  40. data/lib/{webpacker → shakapacker}/version.rb +2 -2
  41. data/lib/{webpacker → shakapacker}/version_checker.rb +8 -8
  42. data/lib/shakapacker/webpack_runner.rb +67 -0
  43. data/lib/shakapacker.rb +51 -1
  44. data/lib/tasks/shakapacker/binstubs.rake +15 -0
  45. data/lib/tasks/shakapacker/check_binstubs.rake +25 -0
  46. data/lib/tasks/shakapacker/check_node.rake +31 -0
  47. data/lib/tasks/shakapacker/check_yarn.rake +33 -0
  48. data/lib/tasks/shakapacker/clean.rake +23 -0
  49. data/lib/tasks/shakapacker/clobber.rake +18 -0
  50. data/lib/tasks/shakapacker/compile.rake +31 -0
  51. data/lib/tasks/shakapacker/info.rake +21 -0
  52. data/lib/tasks/shakapacker/install.rake +17 -0
  53. data/lib/tasks/shakapacker/verify_config.rake +12 -0
  54. data/lib/tasks/shakapacker/verify_install.rake +4 -0
  55. data/lib/tasks/shakapacker/yarn_install.rake +24 -0
  56. data/lib/tasks/shakapacker.rake +18 -0
  57. data/lib/tasks/webpacker/binstubs.rake +5 -11
  58. data/lib/tasks/webpacker/check_binstubs.rake +6 -9
  59. data/lib/tasks/webpacker/check_node.rake +5 -27
  60. data/lib/tasks/webpacker/check_yarn.rake +5 -29
  61. data/lib/tasks/webpacker/clean.rake +5 -19
  62. data/lib/tasks/webpacker/clobber.rake +6 -13
  63. data/lib/tasks/webpacker/compile.rake +5 -33
  64. data/lib/tasks/webpacker/info.rake +5 -17
  65. data/lib/tasks/webpacker/install.rake +5 -13
  66. data/lib/tasks/webpacker/verify_config.rake +6 -11
  67. data/lib/tasks/webpacker/verify_install.rake +7 -2
  68. data/lib/tasks/webpacker/yarn_install.rake +5 -20
  69. data/lib/tasks/webpacker.rake +15 -13
  70. data/lib/webpacker/dev_server_runner.rb +9 -96
  71. data/lib/webpacker/webpack_runner.rb +9 -58
  72. data/lib/webpacker.rb +3 -47
  73. data/package/__tests__/config-bc.js +27 -0
  74. data/package/__tests__/config.js +6 -5
  75. data/package/__tests__/dev_server-bc.js +46 -0
  76. data/package/__tests__/dev_server.js +9 -8
  77. data/package/__tests__/development-bc.js +66 -0
  78. data/package/__tests__/development.js +36 -5
  79. data/package/__tests__/env-bc.js +59 -0
  80. data/package/__tests__/env.js +3 -2
  81. data/package/__tests__/index.js +13 -0
  82. data/package/__tests__/production-bc.js +51 -0
  83. data/package/__tests__/production.js +25 -3
  84. data/package/__tests__/staging-bc.js +53 -0
  85. data/package/__tests__/staging.js +27 -4
  86. data/package/__tests__/test-bc.js +43 -0
  87. data/package/__tests__/test.js +22 -4
  88. data/package/babel/preset.js +1 -4
  89. data/package/config.js +25 -7
  90. data/package/dev_server.js +7 -4
  91. data/package/env.js +22 -3
  92. data/package/environments/__tests__/base-bc.js +107 -0
  93. data/package/environments/__tests__/base.js +13 -13
  94. data/package/environments/base.js +1 -1
  95. data/package/environments/development.js +4 -46
  96. data/package/environments/production.js +1 -1
  97. data/package/index.js +11 -4
  98. data/package/rules/__tests__/__utils__/webpack.js +1 -1
  99. data/package/rules/__tests__/file.js +27 -0
  100. data/package/rules/__tests__/swc.js +1 -2
  101. data/package/rules/file.js +2 -2
  102. data/package/utils/configPath.js +19 -0
  103. data/package/utils/defaultConfigPath.js +2 -0
  104. data/package/utils/get_style_rule.js +5 -2
  105. data/package/utils/helpers.js +24 -1
  106. data/package/utils/inliningCss.js +7 -0
  107. data/package/utils/snakeToCamelCase +7 -0
  108. data/package/webpackDevServerConfig.js +68 -0
  109. data/package.json +2 -2
  110. data/{webpacker.gemspec → shakapacker.gemspec} +3 -3
  111. data/spec/backward_compatibility_specs/command_spec_bc.rb +116 -0
  112. data/spec/backward_compatibility_specs/compiler_spec_bc.rb +59 -0
  113. data/spec/backward_compatibility_specs/compiler_strategy_spec_bc.rb +22 -0
  114. data/spec/backward_compatibility_specs/configuration_spec_bc.rb +286 -0
  115. data/spec/backward_compatibility_specs/dev_server_runner_spec_bc.rb +79 -0
  116. data/spec/backward_compatibility_specs/dev_server_spec_bc.rb +47 -0
  117. data/spec/backward_compatibility_specs/digest_strategy_spec_bc.rb +35 -0
  118. data/spec/backward_compatibility_specs/engine_rake_tasks_spec_bc.rb +44 -0
  119. data/spec/backward_compatibility_specs/env_spec_bc.rb +23 -0
  120. data/spec/backward_compatibility_specs/helper_spec_bc.rb +243 -0
  121. data/spec/backward_compatibility_specs/instance_spec_bc.rb +31 -0
  122. data/spec/backward_compatibility_specs/manifest_spec_bc.rb +100 -0
  123. data/spec/backward_compatibility_specs/mtime_strategy_spec_bc.rb +55 -0
  124. data/spec/backward_compatibility_specs/rake_tasks_spec_bc.rb +37 -0
  125. data/spec/backward_compatibility_specs/spec_helper_initializer.rb +24 -0
  126. data/spec/backward_compatibility_specs/webpack_runner_spec_bc.rb +56 -0
  127. data/spec/backward_compatibility_specs/webpacker_spec_bc.rb +41 -0
  128. data/spec/backward_compatibility_specs/webpacker_test_app/.gitignore +2 -0
  129. data/spec/backward_compatibility_specs/webpacker_test_app/Rakefile +3 -0
  130. data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/app/packs/entrypoints/application.js +1 -1
  131. data/{lib/install → spec/backward_compatibility_specs/webpacker_test_app}/bin/webpacker +0 -0
  132. data/spec/backward_compatibility_specs/webpacker_test_app/config/application.rb +11 -0
  133. data/spec/backward_compatibility_specs/webpacker_test_app/config/environment.rb +4 -0
  134. data/spec/backward_compatibility_specs/webpacker_test_app/config/initializers/inspect_autoload_paths.rb +1 -0
  135. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpack/webpack.config.js +0 -0
  136. data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker.yml +1 -1
  137. data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_css_extract_ignore_order_warnings.yml +1 -1
  138. data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_defaults_fallback.yml +1 -1
  139. data/spec/backward_compatibility_specs/webpacker_test_app/config/webpacker_no_precompile.yml +7 -0
  140. data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_other_location.yml +1 -1
  141. data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_public_root.yml +1 -1
  142. data/spec/backward_compatibility_specs/webpacker_test_app/config.ru +5 -0
  143. data/spec/backward_compatibility_specs/webpacker_test_app/package.json +13 -0
  144. data/spec/backward_compatibility_specs/webpacker_test_app/public/packs/manifest.json +58 -0
  145. data/spec/backward_compatibility_specs/webpacker_test_app/some.config.js +0 -0
  146. data/spec/backward_compatibility_specs/webpacker_test_app/tmp/shakapacker/last-compilation-digest-production +1 -0
  147. data/spec/backward_compatibility_specs/webpacker_test_app/yarn.lock +11 -0
  148. data/spec/command_spec.rb +18 -16
  149. data/spec/compiler_spec.rb +19 -17
  150. data/spec/compiler_strategy_spec.rb +9 -7
  151. data/spec/configuration_spec.rb +108 -66
  152. data/spec/dev_server_runner_spec.rb +9 -8
  153. data/spec/dev_server_spec.rb +97 -9
  154. data/spec/digest_strategy_spec.rb +4 -2
  155. data/spec/engine_rake_tasks_spec.rb +12 -16
  156. data/spec/env_spec.rb +7 -5
  157. data/spec/helper_spec.rb +4 -2
  158. data/spec/instance_spec.rb +31 -0
  159. data/spec/manifest_spec.rb +21 -19
  160. data/spec/mounted_app/test/dummy/config/application.rb +1 -1
  161. data/spec/mounted_app/test/dummy/config/webpacker.yml +3 -3
  162. data/spec/mtime_strategy_spec.rb +4 -2
  163. data/spec/rake_tasks_spec.rb +20 -18
  164. data/spec/spec_helper.rb +0 -25
  165. data/spec/spec_helper_initializer.rb +24 -0
  166. data/spec/test_app/.gitignore +2 -0
  167. data/spec/test_app/app/javascript/entrypoints/application.js +10 -0
  168. data/spec/test_app/app/javascript/entrypoints/generated/something.js +2 -0
  169. data/spec/test_app/app/javascript/entrypoints/multi_entry.css +4 -0
  170. data/spec/test_app/app/javascript/entrypoints/multi_entry.js +4 -0
  171. data/spec/test_app/bin/{webpacker-dev-server → shakapacker} +3 -3
  172. data/spec/test_app/bin/{webpacker → shakapacker-dev-server} +3 -3
  173. data/spec/test_app/config/application.rb +1 -1
  174. data/spec/test_app/config/shakapacker.yml +82 -0
  175. data/spec/test_app/config/shakapacker_css_extract_ignore_order_warnings.yml +84 -0
  176. data/spec/test_app/config/shakapacker_defaults_fallback.yml +11 -0
  177. data/spec/test_app/config/shakapacker_manifest_path.yml +80 -0
  178. data/spec/test_app/config/shakapacker_nested_entries.yml +83 -0
  179. data/spec/test_app/config/shakapacker_no_precompile.yml +7 -0
  180. data/spec/test_app/config/shakapacker_other_location.yml +85 -0
  181. data/spec/test_app/config/shakapacker_public_root.yml +18 -0
  182. data/spec/version_checker_spec.rb +16 -15
  183. data/spec/webpack_runner_spec.rb +4 -3
  184. data/spec/webpacker_spec.rb +9 -29
  185. metadata +119 -44
  186. data/config/webpacker.yml +0 -1
  187. data/lib/webpacker/railtie.rb +0 -70
  188. data/lib/webpacker/runner.rb +0 -23
  189. data/package/configPath.js +0 -3
  190. data/package/inliningCss.js +0 -7
  191. data/spec/test_app/config/webpacker_no_precompile.yml +0 -7
  192. /data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/app/packs/entrypoints/generated/something.js +0 -0
  193. /data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/app/packs/entrypoints/multi_entry.css +0 -0
  194. /data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/app/packs/entrypoints/multi_entry.js +0 -0
  195. /data/{lib/install → spec/backward_compatibility_specs/webpacker_test_app}/bin/webpacker-dev-server +0 -0
  196. /data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_manifest_path.yml +0 -0
  197. /data/spec/{test_app → backward_compatibility_specs/webpacker_test_app}/config/webpacker_nested_entries.yml +0 -0
@@ -4,14 +4,14 @@
4
4
  # "/packs/calendar-1016838bab065ae1e314.css".
5
5
  #
6
6
  # When the configuration is set to on-demand compilation, with the `compile: true` option in
7
- # the webpacker.yml file, any lookups will be preceded by a compilation if one is needed.
8
- class Webpacker::Manifest
7
+ # the shakapacker.yml file, any lookups will be preceded by a compilation if one is needed.
8
+ class Shakapacker::Manifest
9
9
  class MissingEntryError < StandardError; end
10
10
 
11
- delegate :config, :compiler, :dev_server, to: :@webpacker
11
+ delegate :config, :compiler, :dev_server, to: :@instance
12
12
 
13
- def initialize(webpacker)
14
- @webpacker = webpacker
13
+ def initialize(instance)
14
+ @instance = instance
15
15
  end
16
16
 
17
17
  def refresh
@@ -32,19 +32,19 @@ class Webpacker::Manifest
32
32
  lookup_pack_with_chunks(name, pack_type) || handle_missing_entry(name, pack_type)
33
33
  end
34
34
 
35
- # Computes the relative path for a given Webpacker asset using manifest.json.
35
+ # Computes the relative path for a given Shakapacker asset using manifest.json.
36
36
  # If no asset is found, returns nil.
37
37
  #
38
38
  # Example:
39
39
  #
40
- # Webpacker.manifest.lookup('calendar.js') # => "/packs/calendar-1016838bab065ae1e122.js"
40
+ # Shakapacker.manifest.lookup('calendar.js') # => "/packs/calendar-1016838bab065ae1e122.js"
41
41
  def lookup(name, pack_type = {})
42
42
  compile if compiling?
43
43
 
44
44
  find(full_pack_name(name, pack_type[:type]))
45
45
  end
46
46
 
47
- # Like lookup, except that if no asset is found, raises a Webpacker::Manifest::MissingEntryError.
47
+ # Like lookup, except that if no asset is found, raises a Shakapacker::Manifest::MissingEntryError.
48
48
  def lookup!(name, pack_type = {})
49
49
  lookup(name, pack_type) || handle_missing_entry(name, pack_type)
50
50
  end
@@ -55,7 +55,7 @@ class Webpacker::Manifest
55
55
  end
56
56
 
57
57
  def compile
58
- Webpacker.logger.tagged("Webpacker") { compiler.compile }
58
+ Shakapacker.logger.tagged("Shakapacker") { compiler.compile }
59
59
  end
60
60
 
61
61
  def data
@@ -76,7 +76,7 @@ class Webpacker::Manifest
76
76
  end
77
77
 
78
78
  def handle_missing_entry(name, pack_type)
79
- raise Webpacker::Manifest::MissingEntryError, missing_file_from_manifest_error(full_pack_name(name, pack_type[:type]))
79
+ raise Shakapacker::Manifest::MissingEntryError, missing_file_from_manifest_error(full_pack_name(name, pack_type[:type]))
80
80
  end
81
81
 
82
82
  def load
@@ -106,11 +106,11 @@ class Webpacker::Manifest
106
106
  <<-MSG
107
107
  Shakapacker can't find #{bundle_name} in #{config.manifest_path}. Possible causes:
108
108
  1. You forgot to install node packages (try `yarn install`) or are running an incompatible version of Node
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/webpacker.yml`
110
- 3. You have set compile: false (see `config/webpacker.yml`) for this environment
111
- (unless you are using the `bin/webpacker -w` or the `bin/webpacker-dev-server`, in which case maybe you aren't running the dev server in the background?)
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
+ 3. You have set compile: false (see `config/shakapacker.yml`) for this environment
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?)
112
112
  4. webpack has not yet FINISHED running to reflect updates.
113
- 5. You have misconfigured Webpacker's `config/webpacker.yml` file.
113
+ 5. You have misconfigured Shakapacker's `config/shakapacker.yml` file.
114
114
  6. Your webpack configuration is not creating a manifest.
115
115
 
116
116
  Your manifest contains:
@@ -1,6 +1,6 @@
1
- require "webpacker/base_strategy"
1
+ require "shakapacker/base_strategy"
2
2
 
3
- module Webpacker
3
+ module Shakapacker
4
4
  class MtimeStrategy < BaseStrategy
5
5
  # Returns true if manifest file mtime is newer than the timestamp of the last modified watched file
6
6
  def fresh?
@@ -21,11 +21,11 @@ module Webpacker
21
21
  def latest_modified_timestamp
22
22
  if Rails.env.development?
23
23
  warn <<~MSG.strip
24
- Webpacker::Compiler - Slow setup for development
24
+ Shakapacker::Compiler - Slow setup for development
25
25
 
26
26
  Prepare JS assets with either:
27
- 1. Running `bin/webpacker-dev-server`
28
- 2. Set `compile` to false in webpacker.yml and run `bin/webpacker -w`
27
+ 1. Running `bin/shakapacker-dev-server`
28
+ 2. Set `compile` to false in shakapacker.yml and run `bin/shakapacker -w`
29
29
  MSG
30
30
  end
31
31
 
@@ -0,0 +1,70 @@
1
+ require "rails/railtie"
2
+
3
+ require "shakapacker/helper"
4
+ require "shakapacker/dev_server_proxy"
5
+ require "shakapacker/version_checker"
6
+
7
+ class Shakapacker::Engine < ::Rails::Engine
8
+ # Allows Shakapacker config values to be set via Rails env config files
9
+ config.shakapacker = ActiveSupport::OrderedOptions.new
10
+
11
+ initializer "shakapacker.version_checker" do
12
+ if File.exist?(Shakapacker::VersionChecker::NodePackageVersion.package_json_path)
13
+ Shakapacker::VersionChecker.build.raise_if_gem_and_node_package_versions_differ
14
+ end
15
+ end
16
+
17
+ initializer "shakapacker.proxy" do |app|
18
+ if (Shakapacker.config.dev_server.present? rescue nil)
19
+ app.middleware.insert_before 0,
20
+ Rails::VERSION::MAJOR >= 5 ?
21
+ Shakapacker::DevServerProxy : "Shakapacker::DevServerProxy", ssl_verify_none: true
22
+ end
23
+ end
24
+
25
+ initializer "shakapacker.helper" do
26
+ ActiveSupport.on_load :action_controller do
27
+ ActionController::Base.helper Shakapacker::Helper
28
+ end
29
+
30
+ ActiveSupport.on_load :action_view do
31
+ include Shakapacker::Helper
32
+ end
33
+ end
34
+
35
+ initializer "shakapacker.logger" do
36
+ config.after_initialize do
37
+ if ::Rails.logger.respond_to?(:tagged)
38
+ Shakapacker.logger = ::Rails.logger
39
+ else
40
+ Shakapacker.logger = ActiveSupport::TaggedLogging.new(::Rails.logger)
41
+ end
42
+ end
43
+ end
44
+
45
+ initializer "shakapacker.bootstrap" do
46
+ if defined?(Rails::Server) || defined?(Rails::Console)
47
+ Shakapacker.bootstrap
48
+ if defined?(Spring)
49
+ require "spring/watcher"
50
+ Spring.after_fork { Shakapacker.bootstrap }
51
+ Spring.watch(Shakapacker.config.config_path)
52
+ end
53
+ end
54
+ end
55
+
56
+ initializer "shakapacker.set_source" do |app|
57
+ if Shakapacker.config.config_path.exist?
58
+ app.config.javascript_path = Shakapacker.config.source_path.relative_path_from(Rails.root.join("app")).to_s
59
+ end
60
+ end
61
+
62
+ initializer "shakapacker.remove_app_packs_from_the_autoload_paths" do
63
+ Rails.application.config.before_initialize do
64
+ if Shakapacker.config.config_path.exist?
65
+ source_path = Shakapacker.config.source_path.to_s
66
+ ActiveSupport::Dependencies.autoload_paths.delete(source_path)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,28 @@
1
+ module Shakapacker
2
+ class Runner
3
+ def self.run(argv)
4
+ $stdout.sync = true
5
+ ENV["NODE_ENV"] ||= (ENV["RAILS_ENV"] == "production") ? "production" : "development"
6
+ new(argv).run
7
+ end
8
+
9
+ def initialize(argv)
10
+ @argv = argv
11
+
12
+ @app_path = File.expand_path(".", Dir.pwd)
13
+ @webpack_config = File.join(@app_path, "config/webpack/webpack.config.js")
14
+
15
+ Shakapacker.set_shakapacker_env_variables_for_backward_compatibility
16
+
17
+ @node_modules_bin_path = ENV["SHAKAPACKER_NODE_MODULES_BIN_PATH"] || `yarn bin`.chomp
18
+ @shakapacker_config = ENV["SHAKAPACKER_CONFIG"] || File.join(@app_path, "config/shakapacker.yml")
19
+
20
+ @shakapacker_config = Shakapacker.get_config_file_path_with_backward_compatibility(@shakapacker_config)
21
+
22
+ unless File.exist?(@webpack_config)
23
+ $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."
24
+ exit!
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../../webpacker/version"
3
+ require_relative "../../shakapacker/version"
4
4
 
5
5
  module Shakapacker
6
6
  module Utils
7
7
  class VersionSyntaxConverter
8
- def rubygem_to_npm(rubygem_version = Webpacker::VERSION)
8
+ def rubygem_to_npm(rubygem_version = Shakapacker::VERSION)
9
9
  regex_match = rubygem_version.match(/(\d+\.\d+\.\d+)[.\-]?(.+)?/)
10
10
  return "#{regex_match[1]}-#{regex_match[2]}" if regex_match[2]
11
11
 
@@ -1,4 +1,4 @@
1
- module Webpacker
1
+ module Shakapacker
2
2
  # Change the version in package.json too, please!
3
- VERSION = "6.6.0".freeze
3
+ VERSION = "7.0.0.rc.0".freeze
4
4
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
- require "webpacker/version"
2
+ require "shakapacker/version"
3
3
 
4
- module Webpacker
4
+ module Shakapacker
5
5
  class VersionChecker
6
6
  attr_reader :node_package_version
7
7
 
@@ -27,7 +27,7 @@ module Webpacker
27
27
 
28
28
  uses_wildcard = node_package_version.semver_wildcard?
29
29
 
30
- if !Webpacker.config.ensure_consistent_versioning? && (uses_wildcard || !versions_match)
30
+ if !Shakapacker.config.ensure_consistent_versioning? && (uses_wildcard || !versions_match)
31
31
  check_failed = if uses_wildcard
32
32
  "Semver wildcard without a lockfile detected"
33
33
  else
@@ -35,13 +35,13 @@ module Webpacker
35
35
  end
36
36
 
37
37
  warn <<-MSG.strip_heredoc
38
- Webpacker::VersionChecker - #{check_failed}
38
+ Shakapacker::VersionChecker - #{check_failed}
39
39
 
40
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
41
 
42
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
43
 
44
- You can enable the version check by setting `ensure_consistent_versioning: true` in your `webpacker.yml` file.
44
+ You can enable the version check by setting `ensure_consistent_versioning: true` in your `shakapacker.yml` file.
45
45
 
46
46
  Checking for gem and npm package versions mismatch or wildcard will be enabled by default in the next major version of shakapacker.
47
47
  MSG
@@ -68,18 +68,18 @@ module Webpacker
68
68
  end
69
69
 
70
70
  def raise_differing_versions_warning
71
- msg = "**ERROR** Webpacker: Webpacker gem and node package versions do not match\n#{common_error_msg}"
71
+ msg = "**ERROR** Shakapacker: Shakapacker gem and node package versions do not match\n#{common_error_msg}"
72
72
  raise msg
73
73
  end
74
74
 
75
75
  def raise_node_semver_version_warning
76
- msg = "**ERROR** Webpacker: Your node package version for shakapacker contains a "\
76
+ msg = "**ERROR** Shakapacker: Your node package version for shakapacker contains a "\
77
77
  "^ or ~\n#{common_error_msg}"
78
78
  raise msg
79
79
  end
80
80
 
81
81
  def gem_version
82
- Webpacker::VERSION
82
+ Shakapacker::VERSION
83
83
  end
84
84
 
85
85
  def gem_major_minor_patch_version
@@ -0,0 +1,67 @@
1
+ require "shellwords"
2
+ require "shakapacker/runner"
3
+
4
+ module Shakapacker
5
+ class WebpackRunner < Shakapacker::Runner
6
+ WEBPACK_COMMANDS = [
7
+ "help",
8
+ "h",
9
+ "--help",
10
+ "-h",
11
+ "version",
12
+ "v",
13
+ "--version",
14
+ "-v",
15
+ "info",
16
+ "i"
17
+ ].freeze
18
+
19
+ def run
20
+ env = Shakapacker::Compiler.env
21
+ env["SHAKAPACKER_CONFIG"] = @shakapacker_config
22
+
23
+ cmd = if node_modules_bin_exist?
24
+ ["#{@node_modules_bin_path}/webpack"]
25
+ else
26
+ ["yarn", "webpack"]
27
+ end
28
+
29
+ if @argv.include?("--debug-webpacker")
30
+ Shakapacker.puts_deprecation_message(
31
+ Shakapacker.short_deprecation_message(
32
+ "--debug-webpacker",
33
+ "--debug-shakapacker"
34
+ )
35
+ )
36
+ end
37
+
38
+ if @argv.delete("--debug-shakapacker") || @argv.delete("--debug-webpacker")
39
+ cmd = ["node", "--inspect-brk"] + cmd
40
+ end
41
+
42
+ if @argv.delete "--trace-deprecation"
43
+ cmd = ["node", "--trace-deprecation"] + cmd
44
+ end
45
+
46
+ if @argv.delete "--no-deprecation"
47
+ cmd = ["node", "--no-deprecation"] + cmd
48
+ end
49
+
50
+ # Webpack commands are not compatible with --config option.
51
+ if (@argv & WEBPACK_COMMANDS).empty?
52
+ cmd += ["--config", @webpack_config]
53
+ end
54
+
55
+ cmd += @argv
56
+
57
+ Dir.chdir(@app_path) do
58
+ Kernel.exec env, *cmd
59
+ end
60
+ end
61
+
62
+ private
63
+ def node_modules_bin_exist?
64
+ File.exist?("#{@node_modules_bin_path}/webpack")
65
+ end
66
+ end
67
+ end
data/lib/shakapacker.rb CHANGED
@@ -1 +1,51 @@
1
- require "webpacker"
1
+ require "active_support/core_ext/module/attribute_accessors"
2
+ require "active_support/core_ext/string/inquiry"
3
+ require "active_support/logger"
4
+ require "active_support/tagged_logging"
5
+
6
+ module Shakapacker
7
+ extend self
8
+
9
+ DEFAULT_ENV = "production".freeze
10
+
11
+ def instance=(instance)
12
+ @instance = instance
13
+ end
14
+
15
+ def instance
16
+ @instance ||= Shakapacker::Instance.new
17
+ end
18
+
19
+ def with_node_env(env)
20
+ original = ENV["NODE_ENV"]
21
+ ENV["NODE_ENV"] = env
22
+ yield
23
+ ensure
24
+ ENV["NODE_ENV"] = original
25
+ end
26
+
27
+ def ensure_log_goes_to_stdout
28
+ old_logger = Shakapacker.logger
29
+ Shakapacker.logger = Logger.new(STDOUT)
30
+ yield
31
+ ensure
32
+ Shakapacker.logger = old_logger
33
+ end
34
+
35
+ delegate :logger, :logger=, :env, :inlining_css?, to: :instance
36
+ delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance
37
+ delegate :bootstrap, :clean, :clobber, :compile, to: :commands
38
+ end
39
+
40
+ require "shakapacker/instance"
41
+ require "shakapacker/env"
42
+ require "shakapacker/configuration"
43
+ require "shakapacker/manifest"
44
+ require "shakapacker/compiler"
45
+ require "shakapacker/commands"
46
+ require "shakapacker/dev_server"
47
+ require "shakapacker/deprecation_helper"
48
+
49
+ require "shakapacker/railtie" if defined?(Rails)
50
+
51
+ Webpacker = Shakapacker
@@ -0,0 +1,15 @@
1
+ binstubs_template_path = File.expand_path("../../install/binstubs.rb", __dir__).freeze
2
+ bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin")
3
+
4
+ namespace :shakapacker do
5
+ desc "Installs Shakapacker binstubs in this application"
6
+ task binstubs: [:check_node, :check_yarn] do |task|
7
+ prefix = task.name.split(/#|shakapacker:binstubs/).first
8
+
9
+ if Rails::VERSION::MAJOR >= 5
10
+ exec "#{RbConfig.ruby} #{bin_path}/rails #{prefix}app:template LOCATION='#{binstubs_template_path}'"
11
+ else
12
+ exec "#{RbConfig.ruby} #{bin_path}/rake #{prefix}rails:template LOCATION='#{binstubs_template_path}'"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ namespace :shakapacker do
2
+ desc "Verifies that bin/shakapacker is present"
3
+ task :check_binstubs do
4
+ if File.exist?(Rails.root.join("bin/shakapacker"))
5
+ exit
6
+ elsif File.exist?(Rails.root.join("bin/webpacker"))
7
+ Shakapacker.puts_deprecation_message(
8
+ Shakapacker.short_deprecation_message(
9
+ "bin/webpacker",
10
+ "bin/shakapacker"
11
+ )
12
+ )
13
+ exit
14
+ else
15
+ puts <<~MSG
16
+ Could't find shakapacker binstubs!
17
+ Possible solutions:
18
+ - Ensure you have run `rails shakapacker:install`.
19
+ - Run `rails shakapacker:binstubs` if you have already installed shakapacker.
20
+ - Ensure the `bin` directory and `bin/shakapacker` are not included in .gitignore.
21
+ MSG
22
+ exit!
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ require "semantic_range"
2
+ namespace :shakapacker do
3
+ desc "Verifies if Node.js is installed"
4
+ task :check_node do
5
+ begin
6
+ node_version = `node -v || nodejs -v`.strip
7
+ raise Errno::ENOENT if node_version.blank?
8
+
9
+ pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath
10
+ node_range = JSON.parse(pkg_path.read)["engines"]["node"]
11
+ is_valid = SemanticRange.satisfies?(node_version, node_range) rescue false
12
+ semver_major = node_version[/\d+/] rescue nil
13
+ is_unstable = semver_major.to_i.odd? rescue false
14
+
15
+ if is_unstable
16
+ $stderr.puts "Warning: you are using an unstable release of Node.js (#{node_version}). If you encounter issues with Node.js, consider switching to an Active LTS release. More info: https://docs.npmjs.com/try-the-latest-stable-version-of-node"
17
+ end
18
+
19
+ unless is_valid
20
+ $stderr.puts "Shakapacker requires Node.js \"#{node_range}\" and you are using #{node_version}"
21
+ $stderr.puts "Please upgrade Node.js https://nodejs.org/en/download/"
22
+ $stderr.puts "Exiting!"
23
+ exit!
24
+ end
25
+ rescue Errno::ENOENT
26
+ $stderr.puts "Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/"
27
+ $stderr.puts "Exiting!"
28
+ exit!
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ require "semantic_range"
2
+ namespace :shakapacker do
3
+ desc "Verifies if Yarn is installed"
4
+ task :check_yarn do
5
+ begin
6
+ which_command = Gem.win_platform? ? "where" : "which"
7
+ raise Errno::ENOENT if `#{which_command} yarn`.strip.empty?
8
+
9
+ yarn_version = `yarn --version`.strip
10
+ raise Errno::ENOENT if yarn_version.blank?
11
+
12
+ pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath
13
+ yarn_range = JSON.parse(pkg_path.read)["engines"]["yarn"]
14
+ is_valid = SemanticRange.satisfies?(yarn_version, yarn_range) rescue false
15
+ is_unsupported = SemanticRange.satisfies?(yarn_version, ">=4.0.0") rescue false
16
+
17
+ unless is_valid
18
+ $stderr.puts "Shakapacker requires Yarn \"#{yarn_range}\" and you are using #{yarn_version}"
19
+ if is_unsupported
20
+ $stderr.puts "This version of Shakapacker does not support Yarn #{yarn_version}. Please downgrade to a supported version of Yarn https://yarnpkg.com/lang/en/docs/install/"
21
+ else
22
+ $stderr.puts "Please upgrade Yarn https://yarnpkg.com/lang/en/docs/install/"
23
+ end
24
+ $stderr.puts "Exiting!"
25
+ exit!
26
+ end
27
+ rescue Errno::ENOENT
28
+ $stderr.puts "Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/"
29
+ $stderr.puts "Exiting!"
30
+ exit!
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ $stdout.sync = true
2
+
3
+ require "shakapacker/configuration"
4
+
5
+ namespace :shakapacker do
6
+ desc "Remove old compiled bundles"
7
+ task :clean, [:keep, :age] => ["shakapacker:verify_install", :environment] do |_, args|
8
+ Shakapacker.ensure_log_goes_to_stdout do
9
+ Shakapacker.clean(Integer(args.keep || 2), Integer(args.age || 3600))
10
+ end
11
+ end
12
+ end
13
+
14
+ if Shakapacker.config.shakapacker_precompile?
15
+ # Run clean if the assets:clean is run
16
+ if Rake::Task.task_defined?("assets:clean")
17
+ Rake::Task["assets:clean"].enhance do
18
+ Rake::Task["shakapacker:clean"].invoke
19
+ end
20
+ else
21
+ Rake::Task.define_task("assets:clean" => "shakapacker:clean")
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ require "shakapacker/configuration"
2
+
3
+ namespace :shakapacker do
4
+ desc "Remove the webpack compiled output directory"
5
+ task clobber: ["shakapacker:verify_config", :environment] do
6
+ Shakapacker.clobber
7
+ $stdout.puts "Removed webpack output path directory #{Shakapacker.config.public_output_path}"
8
+ end
9
+ end
10
+
11
+ if Shakapacker.config.shakapacker_precompile?
12
+ # Run clobber if the assets:clobber is run
13
+ if Rake::Task.task_defined?("assets:clobber")
14
+ Rake::Task["assets:clobber"].enhance do
15
+ Rake::Task["shakapacker:clobber"].invoke
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ $stdout.sync = true
2
+
3
+ namespace :shakapacker do
4
+ desc "Compile JavaScript packs using webpack for production with digests"
5
+ task compile: ["shakapacker:verify_install", :environment] do
6
+ Shakapacker.with_node_env(ENV.fetch("NODE_ENV", "production")) do
7
+ Shakapacker.ensure_log_goes_to_stdout do
8
+ if Shakapacker.compile
9
+ # Successful compilation!
10
+ else
11
+ # Failed compilation
12
+ exit!
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ def invoke_shakapacker_compile_in_assets_precompile_task
20
+ Rake::Task["assets:precompile"].enhance do |task|
21
+ prefix = task.name.split(/#|assets:precompile/).first
22
+
23
+ Rake::Task["#{prefix}shakapacker:compile"].invoke
24
+ end
25
+ end
26
+
27
+ if Shakapacker.config.shakapacker_precompile?
28
+ if Rake::Task.task_defined?("assets:precompile")
29
+ invoke_shakapacker_compile_in_assets_precompile_task
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ require "shakapacker/version"
2
+
3
+ namespace :shakapacker do
4
+ desc "Provide information on Shakapacker's environment"
5
+ task :info do
6
+ Dir.chdir(Rails.root) do
7
+ $stdout.puts "Ruby: #{`ruby --version`}"
8
+ $stdout.puts "Rails: #{Rails.version}"
9
+ $stdout.puts "Shakapacker: #{Shakapacker::VERSION}"
10
+ $stdout.puts "Node: #{`node --version`}"
11
+ $stdout.puts "Yarn: #{`yarn --version`}"
12
+
13
+ $stdout.puts "\n"
14
+ $stdout.puts "shakapacker: \n#{`npm list shakapacker version`}"
15
+
16
+ $stdout.puts "Is bin/shakapacker present?: #{File.exist? 'bin/shakapacker'}"
17
+ $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'}"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ install_template_path = File.expand_path("../../install/template.rb", __dir__).freeze
2
+ bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin")
3
+
4
+ namespace :shakapacker do
5
+ desc "Install Shakapacker in this application"
6
+ task install: [:check_node, :check_yarn] do |task|
7
+ Shakapacker::Configuration.installing = true
8
+
9
+ prefix = task.name.split(/#|shakapacker:install/).first
10
+
11
+ if Rails::VERSION::MAJOR >= 5
12
+ exec "#{RbConfig.ruby} #{bin_path}/rails #{prefix}app:template LOCATION='#{install_template_path}'"
13
+ else
14
+ exec "#{RbConfig.ruby} #{bin_path}/rake #{prefix}rails:template LOCATION='#{install_template_path}'"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ namespace :shakapacker do
2
+ desc "Verifies if the Shakapacker config is present"
3
+ task :verify_config do
4
+ unless Shakapacker.config.config_path.exist?
5
+ path = Shakapacker.config.config_path.relative_path_from(Pathname.new(pwd)).to_s
6
+ $stderr.puts "Configuration #{path} file not found. \n"\
7
+ "Make sure shakapacker:install is run successfully before " \
8
+ "running dependent tasks"
9
+ exit!
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ namespace :shakapacker do
2
+ desc "Verifies if Shakapacker is installed"
3
+ task verify_install: [:verify_config, :check_node, :check_yarn, :check_binstubs]
4
+ end
@@ -0,0 +1,24 @@
1
+ namespace :shakapacker do
2
+ desc "Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn"
3
+ task :yarn_install do
4
+ warn <<~MSG.strip
5
+ Shakapacker - Automatic installation of yarn packages is deprecated
6
+ Automatic installation of yarn packages when assets are precompiled is deprecated and will be removed in Shakapacker v7.
7
+ Please ensure you are installing yarn packages explicitly before the asset compilation.
8
+ MSG
9
+
10
+ valid_node_envs = %w[test development production]
11
+ node_env = ENV.fetch("NODE_ENV") do
12
+ valid_node_envs.include?(Rails.env) ? Rails.env : "production"
13
+ end
14
+ Dir.chdir(Rails.root) do
15
+ yarn_flags =
16
+ if `yarn --version`.start_with?("1")
17
+ "--no-progress --frozen-lockfile"
18
+ else
19
+ "--immutable"
20
+ end
21
+ system({ "NODE_ENV" => node_env }, "yarn install #{yarn_flags}")
22
+ end
23
+ end
24
+ end