webpacker 5.4.0 → 6.0.0.rc.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +1 -1
  3. data/.github/workflows/jest.yml +7 -15
  4. data/.github/workflows/js-lint.yml +7 -15
  5. data/.github/workflows/rubocop.yml +1 -1
  6. data/.github/workflows/ruby.yml +18 -36
  7. data/.gitignore +2 -0
  8. data/.node-version +1 -1
  9. data/.rubocop.yml +5 -4
  10. data/CHANGELOG.md +45 -9
  11. data/CONTRIBUTING.md +20 -1
  12. data/Gemfile.lock +83 -83
  13. data/README.md +394 -180
  14. data/config/README.md +3 -0
  15. data/config/webpacker.yml +1 -0
  16. data/docs/deployment.md +9 -11
  17. data/docs/developing_webpacker.md +29 -0
  18. data/docs/troubleshooting.md +61 -25
  19. data/docs/v6_upgrade.md +113 -0
  20. data/gemfiles/Gemfile-rails-edge +1 -1
  21. data/gemfiles/Gemfile-rails.6.1.x +12 -0
  22. data/lib/install/{javascript/packs/application.js → application.js} +2 -5
  23. data/lib/install/bin/webpack +4 -7
  24. data/lib/install/bin/yarn +18 -0
  25. data/lib/install/config/webpack/base.js +3 -0
  26. data/lib/install/config/webpack/development.js +2 -2
  27. data/lib/install/config/webpack/production.js +2 -2
  28. data/lib/install/config/webpack/test.js +2 -2
  29. data/lib/install/config/webpacker.yml +21 -49
  30. data/lib/install/package.json +15 -0
  31. data/lib/install/template.rb +63 -28
  32. data/lib/tasks/webpacker/binstubs.rake +6 -4
  33. data/lib/tasks/webpacker/check_binstubs.rake +4 -4
  34. data/lib/tasks/webpacker/check_node.rake +3 -0
  35. data/lib/tasks/webpacker/check_yarn.rake +4 -1
  36. data/lib/tasks/webpacker/clobber.rake +1 -1
  37. data/lib/tasks/webpacker/compile.rake +4 -2
  38. data/lib/tasks/webpacker/info.rake +12 -10
  39. data/lib/tasks/webpacker/install.rake +6 -4
  40. data/lib/tasks/webpacker/verify_config.rake +14 -0
  41. data/lib/tasks/webpacker/verify_install.rake +1 -10
  42. data/lib/tasks/webpacker/yarn_install.rake +9 -7
  43. data/lib/tasks/webpacker.rake +2 -11
  44. data/lib/tasks/yarn.rake +38 -0
  45. data/lib/webpacker/commands.rb +21 -16
  46. data/lib/webpacker/compiler.rb +16 -9
  47. data/lib/webpacker/configuration.rb +8 -32
  48. data/lib/webpacker/dev_server.rb +6 -0
  49. data/lib/webpacker/dev_server_runner.rb +28 -4
  50. data/lib/webpacker/helper.rb +47 -81
  51. data/lib/webpacker/instance.rb +4 -0
  52. data/lib/webpacker/manifest.rb +2 -3
  53. data/lib/webpacker/railtie.rb +8 -2
  54. data/lib/webpacker/runner.rb +1 -1
  55. data/lib/webpacker/version.rb +1 -1
  56. data/lib/webpacker/webpack_runner.rb +29 -3
  57. data/lib/webpacker.rb +1 -1
  58. data/package/__tests__/config.js +5 -37
  59. data/package/__tests__/development.js +13 -21
  60. data/package/__tests__/env.js +16 -4
  61. data/package/__tests__/index.js +9 -0
  62. data/package/__tests__/production.js +6 -6
  63. data/package/__tests__/staging.js +7 -6
  64. data/package/__tests__/test.js +4 -5
  65. data/package/babel/preset.js +54 -0
  66. data/package/config.js +6 -14
  67. data/package/env.js +13 -4
  68. data/package/environments/__tests__/base.js +20 -52
  69. data/package/environments/base.js +68 -128
  70. data/package/environments/development.js +49 -47
  71. data/package/environments/production.js +66 -64
  72. data/package/environments/test.js +2 -2
  73. data/package/index.js +15 -8
  74. data/package/inliningCss.js +7 -0
  75. data/package/rules/babel.js +10 -8
  76. data/package/rules/coffee.js +6 -0
  77. data/package/rules/erb.js +15 -0
  78. data/package/rules/file.js +21 -19
  79. data/package/rules/index.js +16 -18
  80. data/package/rules/less.js +22 -0
  81. data/package/rules/raw.js +5 -0
  82. data/package/rules/sass.js +9 -10
  83. data/package/rules/stylus.js +26 -0
  84. data/package/utils/get_style_rule.js +28 -36
  85. data/package/utils/helpers.js +28 -35
  86. data/package.json +23 -43
  87. data/test/command_test.rb +76 -0
  88. data/test/compiler_test.rb +0 -12
  89. data/test/configuration_test.rb +4 -35
  90. data/test/dev_server_runner_test.rb +36 -6
  91. data/test/engine_rake_tasks_test.rb +39 -0
  92. data/test/helper_test.rb +76 -62
  93. data/test/manifest_test.rb +16 -0
  94. data/test/mounted_app/Rakefile +4 -0
  95. data/test/mounted_app/test/dummy/Rakefile +3 -0
  96. data/test/mounted_app/test/dummy/bin/rails +3 -0
  97. data/test/mounted_app/test/dummy/bin/rake +3 -0
  98. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  99. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  100. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  101. data/test/mounted_app/test/dummy/config.ru +5 -0
  102. data/test/mounted_app/test/dummy/package.json +7 -0
  103. data/test/rake_tasks_test.rb +1 -10
  104. data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
  105. data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
  106. data/test/test_app/config/webpacker.yml +5 -31
  107. data/test/test_app/config/webpacker_other_location.yml +79 -0
  108. data/test/test_app/config/webpacker_public_root.yml +0 -1
  109. data/test/test_app/public/packs/manifest.json +36 -18
  110. data/test/test_app/some.config.js +0 -0
  111. data/test/webpack_runner_test.rb +9 -3
  112. data/test/webpacker_test.rb +21 -0
  113. data/webpacker.gemspec +2 -2
  114. data/yarn.lock +2183 -6524
  115. metadata +63 -93
  116. data/.travis.yml +0 -43
  117. data/docs/assets.md +0 -119
  118. data/docs/cloud9.md +0 -310
  119. data/docs/css.md +0 -308
  120. data/docs/docker.md +0 -68
  121. data/docs/engines.md +0 -213
  122. data/docs/env.md +0 -63
  123. data/docs/es6.md +0 -72
  124. data/docs/folder-structure.md +0 -66
  125. data/docs/integrations.md +0 -220
  126. data/docs/misc.md +0 -23
  127. data/docs/props.md +0 -223
  128. data/docs/target.md +0 -22
  129. data/docs/testing.md +0 -136
  130. data/docs/typescript.md +0 -190
  131. data/docs/v4-upgrade.md +0 -142
  132. data/docs/webpack-dev-server.md +0 -92
  133. data/docs/webpack.md +0 -364
  134. data/docs/yarn.md +0 -23
  135. data/lib/install/angular.rb +0 -23
  136. data/lib/install/coffee.rb +0 -25
  137. data/lib/install/config/.browserslistrc +0 -1
  138. data/lib/install/config/babel.config.js +0 -70
  139. data/lib/install/config/postcss.config.js +0 -12
  140. data/lib/install/config/webpack/environment.js +0 -3
  141. data/lib/install/elm.rb +0 -39
  142. data/lib/install/erb.rb +0 -25
  143. data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
  144. data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
  145. data/lib/install/examples/angular/hello_angular/index.ts +0 -8
  146. data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
  147. data/lib/install/examples/angular/hello_angular.js +0 -7
  148. data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
  149. data/lib/install/examples/elm/Main.elm +0 -55
  150. data/lib/install/examples/elm/hello_elm.js +0 -16
  151. data/lib/install/examples/erb/hello_erb.js.erb +0 -6
  152. data/lib/install/examples/react/babel.config.js +0 -87
  153. data/lib/install/examples/react/hello_react.jsx +0 -26
  154. data/lib/install/examples/react/tsconfig.json +0 -21
  155. data/lib/install/examples/stimulus/application.js +0 -1
  156. data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
  157. data/lib/install/examples/stimulus/controllers/index.js +0 -9
  158. data/lib/install/examples/svelte/app.svelte +0 -11
  159. data/lib/install/examples/svelte/hello_svelte.js +0 -20
  160. data/lib/install/examples/typescript/hello_typescript.ts +0 -4
  161. data/lib/install/examples/typescript/tsconfig.json +0 -24
  162. data/lib/install/examples/vue/app.vue +0 -22
  163. data/lib/install/examples/vue/hello_vue.js +0 -72
  164. data/lib/install/loaders/coffee.js +0 -6
  165. data/lib/install/loaders/elm.js +0 -25
  166. data/lib/install/loaders/erb.js +0 -11
  167. data/lib/install/loaders/svelte.js +0 -9
  168. data/lib/install/loaders/vue.js +0 -6
  169. data/lib/install/react.rb +0 -18
  170. data/lib/install/stimulus.rb +0 -12
  171. data/lib/install/svelte.rb +0 -29
  172. data/lib/install/typescript.rb +0 -39
  173. data/lib/install/vue.rb +0 -49
  174. data/lib/tasks/installers.rake +0 -42
  175. data/package/config_types/__tests__/config_list.js +0 -118
  176. data/package/config_types/__tests__/config_object.js +0 -43
  177. data/package/config_types/config_list.js +0 -75
  178. data/package/config_types/config_object.js +0 -55
  179. data/package/config_types/index.js +0 -7
  180. data/package/rules/module.css.js +0 -3
  181. data/package/rules/module.sass.js +0 -8
  182. data/package/rules/node_modules.js +0 -22
  183. data/package/utils/__tests__/deep_assign.js +0 -32
  184. data/package/utils/__tests__/deep_merge.js +0 -10
  185. data/package/utils/__tests__/get_style_rule.js +0 -65
  186. data/package/utils/__tests__/objectify.js +0 -9
  187. data/package/utils/deep_assign.js +0 -22
  188. data/package/utils/deep_merge.js +0 -22
  189. data/package/utils/objectify.js +0 -3
  190. /data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.css +0 -0
  191. /data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.js +0 -0
@@ -1,29 +1,23 @@
1
1
  # Install Webpacker
2
2
  copy_file "#{__dir__}/config/webpacker.yml", "config/webpacker.yml"
3
+ copy_file "#{__dir__}/package.json", "package.json"
3
4
 
4
5
  say "Copying webpack core config"
5
6
  directory "#{__dir__}/config/webpack", "config/webpack"
6
7
 
7
- say "Copying postcss.config.js to app root directory"
8
- copy_file "#{__dir__}/config/postcss.config.js", "postcss.config.js"
9
-
10
- say "Copying babel.config.js to app root directory"
11
- copy_file "#{__dir__}/config/babel.config.js", "babel.config.js"
12
-
13
- say "Copying .browserslistrc to app root directory"
14
- copy_file "#{__dir__}/config/.browserslistrc", ".browserslistrc"
15
-
16
8
  if Dir.exists?(Webpacker.config.source_path)
17
- say "The JavaScript app source directory already exists"
9
+ say "The packs app source directory already exists"
18
10
  else
19
- say "Creating JavaScript app source directory"
20
- directory "#{__dir__}/javascript", Webpacker.config.source_path
11
+ say "Creating packs app source directory"
12
+ empty_directory "app/javascript"
13
+ copy_file "#{__dir__}/application.js", "app/javascript/application.js"
21
14
  end
22
15
 
23
16
  apply "#{__dir__}/binstubs.rb"
24
17
 
25
- if File.exists?(".gitignore")
26
- append_to_file ".gitignore" do
18
+ git_ignore_path = Rails.root.join(".gitignore")
19
+ if File.exists?(git_ignore_path)
20
+ append_to_file git_ignore_path do
27
21
  "\n" +
28
22
  "/public/packs\n" +
29
23
  "/public/packs-test\n" +
@@ -34,24 +28,62 @@ if File.exists?(".gitignore")
34
28
  end
35
29
  end
36
30
 
37
- if Webpacker::VERSION.match?(/^[0-9]+\.[0-9]+\.[0-9]+$/)
38
- say "Installing all JavaScript dependencies [#{Webpacker::VERSION}]"
39
- run "yarn add @rails/webpacker@#{Webpacker::VERSION}"
31
+ if (app_layout_path = Rails.root.join("app/views/layouts/application.html.erb")).exist?
32
+ say "Add JavaScript include tag in application layout"
33
+ insert_into_file app_layout_path.to_s, %(\n <%= javascript_pack_tag "application" %>), before: /\s*<\/head>/
40
34
  else
41
- say "Installing all JavaScript dependencies [from prerelease rails/webpacker]"
42
- run "yarn add @rails/webpacker@next"
35
+ say "Default application.html.erb is missing!", :red
36
+ say %( Add <%= javascript_pack_tag "application" %> within the <head> tag in your custom layout.)
37
+ end
38
+
39
+ if (setup_path = Rails.root.join("bin/setup")).exist?
40
+ say "Run bin/yarn during bin/setup"
41
+ insert_into_file setup_path.to_s, <<-RUBY, after: %( system("bundle check") || system!("bundle install")\n)
42
+
43
+ # Install JavaScript dependencies
44
+ system! "bin/yarn"
45
+ RUBY
46
+ end
47
+
48
+ if (asset_config_path = Rails.root.join("config/initializers/assets.rb")).exist?
49
+ say "Add node_modules to the asset load path"
50
+ append_to_file asset_config_path, <<-RUBY
51
+
52
+ # Add node_modules folder to the asset load path.
53
+ Rails.application.config.assets.paths << Rails.root.join("node_modules")
54
+ RUBY
55
+ end
56
+
57
+ if (csp_config_path = Rails.root.join("config/initializers/content_security_policy.rb")).exist?
58
+ say "Make note of webpack-dev-server exemption needed to csp"
59
+ insert_into_file csp_config_path, <<-RUBY, after: %(# Rails.application.config.content_security_policy do |policy|)
60
+ # # If you are using webpack-dev-server then specify webpack-dev-server host
61
+ # policy.connect_src :self, :https, "http://localhost:3035", "ws://localhost:3035" if Rails.env.development?
62
+ RUBY
43
63
  end
44
64
 
45
- package_json = File.read("#{__dir__}/../../package.json")
46
- webpack_version = package_json.match(/"webpack": "(.*)"/)[1]
47
- webpack_cli_version = package_json.match(/"webpack-cli": "(.*)"/)[1]
65
+ results = []
66
+
67
+ Dir.chdir(Rails.root) do
68
+ if Webpacker::VERSION.match?(/^[0-9]+\.[0-9]+\.[0-9]+$/)
69
+ say "Installing all JavaScript dependencies [#{Webpacker::VERSION}]"
70
+ results << run("yarn add @rails/webpacker@#{Webpacker::VERSION}")
71
+ else
72
+ say "Installing all JavaScript dependencies [from prerelease rails/webpacker]"
73
+ results << run("yarn add @rails/webpacker@next")
74
+ end
75
+
76
+ package_json = File.read("#{__dir__}/../../package.json")
77
+ webpack_version = package_json.match(/"webpack": "(.*)"/)[1]
78
+ webpack_cli_version = package_json.match(/"webpack-cli": "(.*)"/)[1]
48
79
 
49
- # needed for experimental Yarn 2 support and should not harm Yarn 1
50
- say "Installing webpack and webpack-cli as direct dependencies"
51
- run "yarn add webpack@#{webpack_version} webpack-cli@#{webpack_cli_version}"
80
+ # needed for experimental Yarn 2 support and should not harm Yarn 1
81
+ say "Installing webpack and webpack-cli as direct dependencies"
82
+ results << run("yarn add webpack@#{webpack_version} webpack-cli@#{webpack_cli_version}")
52
83
 
53
- say "Installing dev server for live reloading"
54
- run "yarn add --dev webpack-dev-server"
84
+ say "Installing dev server for live reloading"
85
+ results << run("yarn add --dev webpack-dev-server @webpack-cli/serve")
86
+ end
55
87
 
56
88
  if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1
57
89
  say "You need to allow webpack-dev-server host as allowed origin for connect-src.", :yellow
@@ -60,4 +92,7 @@ if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1
60
92
  say "policy.connect_src :self, :https, \"http://localhost:3035\", \"ws://localhost:3035\" if Rails.env.development?", :yellow
61
93
  end
62
94
 
63
- say "Webpacker successfully installed 🎉 🍰", :green
95
+ unless results.all?
96
+ say "Webpacker installation failed 😭 See above for details.", :red
97
+ exit 1
98
+ end
@@ -1,13 +1,15 @@
1
1
  binstubs_template_path = File.expand_path("../../install/binstubs.rb", __dir__).freeze
2
- bin_path = ENV["BUNDLE_BIN"] || "./bin"
2
+ bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin")
3
3
 
4
4
  namespace :webpacker do
5
5
  desc "Installs Webpacker binstubs in this application"
6
- task binstubs: [:check_node, :check_yarn] do
6
+ task binstubs: [:check_node, :check_yarn] do |task|
7
+ prefix = task.name.split(/#|webpacker:binstubs/).first
8
+
7
9
  if Rails::VERSION::MAJOR >= 5
8
- exec "#{RbConfig.ruby} #{bin_path}/rails app:template LOCATION=#{binstubs_template_path}"
10
+ exec "#{RbConfig.ruby} #{bin_path}/rails #{prefix}app:template LOCATION='#{binstubs_template_path}'"
9
11
  else
10
- exec "#{RbConfig.ruby} #{bin_path}/rake rails:template LOCATION=#{binstubs_template_path}"
12
+ exec "#{RbConfig.ruby} #{bin_path}/rake #{prefix}rails:template LOCATION='#{binstubs_template_path}'"
11
13
  end
12
14
  end
13
15
  end
@@ -1,10 +1,10 @@
1
1
  namespace :webpacker do
2
- desc "Verifies that webpack & webpack-dev-server are present."
2
+ desc "Verifies that bin/webpack is present"
3
3
  task :check_binstubs do
4
- unless File.exist?("bin/webpack")
5
- $stderr.puts "webpack binstubs not found.\n"\
4
+ unless File.exist?(Rails.root.join("bin/webpack"))
5
+ $stderr.puts "webpack binstub not found.\n"\
6
6
  "Have you run rails webpacker:install ?\n"\
7
- "Make sure the bin directory or binstubs are not included in .gitignore\n"\
7
+ "Make sure the bin directory and bin/webpack are not included in .gitignore\n"\
8
8
  "Exiting!"
9
9
  exit!
10
10
  end
@@ -3,6 +3,9 @@ namespace :webpacker do
3
3
  desc "Verifies if Node.js is installed"
4
4
  task :check_node do
5
5
  begin
6
+ which_command = Gem.win_platform? ? "where" : "which"
7
+ raise Errno::ENOENT if `#{which_command} node || #{which_command} nodejs`.strip.empty?
8
+
6
9
  node_version = `node -v || nodejs -v`.strip
7
10
  raise Errno::ENOENT if node_version.blank?
8
11
 
@@ -3,13 +3,16 @@ namespace :webpacker do
3
3
  desc "Verifies if Yarn is installed"
4
4
  task :check_yarn do
5
5
  begin
6
+ which_command = Gem.win_platform? ? "where" : "which"
7
+ raise Errno::ENOENT if `#{which_command} yarn`.strip.empty?
8
+
6
9
  yarn_version = `yarn --version`.strip
7
10
  raise Errno::ENOENT if yarn_version.blank?
8
11
 
9
12
  pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath
10
13
  yarn_range = JSON.parse(pkg_path.read)["engines"]["yarn"]
11
14
  is_valid = SemanticRange.satisfies?(yarn_version, yarn_range) rescue false
12
- is_unsupported = SemanticRange.satisfies?(yarn_version, ">=3.0.0") rescue false
15
+ is_unsupported = SemanticRange.satisfies?(yarn_version, ">=4.0.0") rescue false
13
16
 
14
17
  unless is_valid
15
18
  $stderr.puts "Webpacker requires Yarn \"#{yarn_range}\" and you are using #{yarn_version}"
@@ -2,7 +2,7 @@ require "webpacker/configuration"
2
2
 
3
3
  namespace :webpacker do
4
4
  desc "Remove the webpack compiled output directory"
5
- task clobber: ["webpacker:verify_install", :environment] do
5
+ task clobber: ["webpacker:verify_config", :environment] do
6
6
  Webpacker.clobber
7
7
  $stdout.puts "Removed webpack output path directory #{Webpacker.config.public_output_path}"
8
8
  end
@@ -10,8 +10,10 @@ end
10
10
  def enhance_assets_precompile
11
11
  # yarn:install was added in Rails 5.1
12
12
  deps = yarn_install_available? ? [] : ["webpacker:yarn_install"]
13
- Rake::Task["assets:precompile"].enhance(deps) do
14
- Rake::Task["webpacker:compile"].invoke
13
+ Rake::Task["assets:precompile"].enhance(deps) do |task|
14
+ prefix = task.name.split(/#|assets:precompile/).first
15
+
16
+ Rake::Task["#{prefix}webpacker:compile"].invoke
15
17
  end
16
18
  end
17
19
 
@@ -3,17 +3,19 @@ require "webpacker/version"
3
3
  namespace :webpacker do
4
4
  desc "Provide information on Webpacker's environment"
5
5
  task :info do
6
- $stdout.puts "Ruby: #{`ruby --version`}"
7
- $stdout.puts "Rails: #{Rails.version}"
8
- $stdout.puts "Webpacker: #{Webpacker::VERSION}"
9
- $stdout.puts "Node: #{`node --version`}"
10
- $stdout.puts "Yarn: #{`yarn --version`}"
6
+ Dir.chdir(Rails.root) do
7
+ $stdout.puts "Ruby: #{`ruby --version`}"
8
+ $stdout.puts "Rails: #{Rails.version}"
9
+ $stdout.puts "Webpacker: #{Webpacker::VERSION}"
10
+ $stdout.puts "Node: #{`node --version`}"
11
+ $stdout.puts "Yarn: #{`yarn --version`}"
11
12
 
12
- $stdout.puts "\n"
13
- $stdout.puts "@rails/webpacker: \n#{`npm list @rails/webpacker version`}"
13
+ $stdout.puts "\n"
14
+ $stdout.puts "@rails/webpacker: \n#{`npm list @rails/webpacker version`}"
14
15
 
15
- $stdout.puts "Is bin/webpack present?: #{File.exist? 'bin/webpack'}"
16
- $stdout.puts "Is bin/webpack-dev-server present?: #{File.exist? 'bin/webpack-dev-server'}"
17
- $stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}"
16
+ $stdout.puts "Is bin/webpack present?: #{File.exist? 'bin/webpack'}"
17
+ $stdout.puts "Is bin/webpack-dev-server present?: #{File.exist? 'bin/webpack-dev-server'}"
18
+ $stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}"
19
+ end
18
20
  end
19
21
  end
@@ -1,13 +1,15 @@
1
1
  install_template_path = File.expand_path("../../install/template.rb", __dir__).freeze
2
- bin_path = ENV["BUNDLE_BIN"] || "./bin"
2
+ bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin")
3
3
 
4
4
  namespace :webpacker do
5
5
  desc "Install Webpacker in this application"
6
- task install: [:check_node, :check_yarn] do
6
+ task install: [:check_node, :check_yarn] do |task|
7
+ prefix = task.name.split(/#|webpacker:install/).first
8
+
7
9
  if Rails::VERSION::MAJOR >= 5
8
- exec "#{RbConfig.ruby} #{bin_path}/rails app:template LOCATION=#{install_template_path}"
10
+ exec "#{RbConfig.ruby} #{bin_path}/rails #{prefix}app:template LOCATION='#{install_template_path}'"
9
11
  else
10
- exec "#{RbConfig.ruby} #{bin_path}/rake rails:template LOCATION=#{install_template_path}"
12
+ exec "#{RbConfig.ruby} #{bin_path}/rake #{prefix}rails:template LOCATION='#{install_template_path}'"
11
13
  end
12
14
  end
13
15
  end
@@ -0,0 +1,14 @@
1
+ require "webpacker/configuration"
2
+
3
+ namespace :webpacker do
4
+ desc "Verifies if the Webpacker config is present"
5
+ task :verify_config do
6
+ unless Webpacker.config.config_path.exist?
7
+ path = Webpacker.config.config_path.relative_path_from(Pathname.new(pwd)).to_s
8
+ $stderr.puts "Configuration #{path} file not found. \n"\
9
+ "Make sure webpacker:install is run successfully before " \
10
+ "running dependent tasks"
11
+ exit!
12
+ end
13
+ end
14
+ end
@@ -1,13 +1,4 @@
1
- require "webpacker/configuration"
2
-
3
1
  namespace :webpacker do
4
2
  desc "Verifies if Webpacker is installed"
5
- task verify_install: [:check_node, :check_yarn, :check_binstubs] do
6
- unless Webpacker.config.config_path.exist?
7
- $stderr.puts "Configuration config/webpacker.yml file not found. \n"\
8
- "Make sure webpacker:install is run successfully before " \
9
- "running dependent tasks"
10
- exit!
11
- end
12
- end
3
+ task verify_install: [:verify_config, :check_node, :check_yarn, :check_binstubs]
13
4
  end
@@ -5,12 +5,14 @@ namespace :webpacker do
5
5
  node_env = ENV.fetch("NODE_ENV") do
6
6
  valid_node_envs.include?(Rails.env) ? Rails.env : "production"
7
7
  end
8
- yarn_flags =
9
- if `yarn --version`.start_with?("1")
10
- "--no-progress --frozen-lockfile"
11
- else
12
- "--immutable"
13
- end
14
- system({ "NODE_ENV" => node_env }, "yarn install #{yarn_flags}")
8
+ Dir.chdir(Rails.root) do
9
+ yarn_flags =
10
+ if `yarn --version`.start_with?("1")
11
+ "--no-progress --frozen-lockfile"
12
+ else
13
+ "--immutable"
14
+ end
15
+ system({ "NODE_ENV" => node_env }, "yarn install #{yarn_flags}")
16
+ end
15
17
  end
16
18
  end
@@ -6,19 +6,10 @@ tasks = {
6
6
  "webpacker:clobber" => "Removes the webpack compiled output directory",
7
7
  "webpacker:check_node" => "Verifies if Node.js is installed",
8
8
  "webpacker:check_yarn" => "Verifies if Yarn is installed",
9
- "webpacker:check_binstubs" => "Verifies that webpack & webpack-dev-server are present",
9
+ "webpacker:check_binstubs" => "Verifies that bin/webpack is present",
10
10
  "webpacker:binstubs" => "Installs Webpacker binstubs in this application",
11
11
  "webpacker:verify_install" => "Verifies if Webpacker is installed",
12
- "webpacker:yarn_install" => "Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn",
13
- "webpacker:install:react" => "Installs and setup example React component",
14
- "webpacker:install:vue" => "Installs and setup example Vue component",
15
- "webpacker:install:angular" => "Installs and setup example Angular component",
16
- "webpacker:install:elm" => "Installs and setup example Elm component",
17
- "webpacker:install:svelte" => "Installs and setup example Svelte component",
18
- "webpacker:install:stimulus" => "Installs and setup example Stimulus component",
19
- "webpacker:install:erb" => "Installs Erb loader with an example",
20
- "webpacker:install:coffee" => "Installs CoffeeScript loader with an example",
21
- "webpacker:install:typescript" => "Installs Typescript loader with an example"
12
+ "webpacker:yarn_install" => "Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn"
22
13
  }.freeze
23
14
 
24
15
  desc "Lists all available tasks in Webpacker"
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Duplicate of the yarn tasks still present in Rails until Webpacker <5 have been deprecated
4
+
5
+ namespace :yarn do
6
+ desc "Install all JavaScript dependencies as specified via Yarn"
7
+ task :install do
8
+ begin
9
+ # Install only production deps when for not usual envs.
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
+
15
+ yarn_flags =
16
+ if `#{RbConfig.ruby} "#{Rails.root}/bin/yarn" --version`.start_with?("1")
17
+ "--no-progress --frozen-lockfile"
18
+ else
19
+ "--immutable"
20
+ end
21
+
22
+ system(
23
+ { "NODE_ENV" => node_env },
24
+ "#{RbConfig.ruby} \"#{Rails.root}/bin/yarn\" install #{yarn_flags}",
25
+ exception: true
26
+ )
27
+ rescue Errno::ENOENT
28
+ $stderr.puts "bin/yarn was not found."
29
+ $stderr.puts "Please run `bundle exec rails app:update:bin` to create it."
30
+ exit 1
31
+ end
32
+ end
33
+ end
34
+
35
+ # Run Yarn prior to Sprockets assets precompilation, so dependencies are available for use.
36
+ if Rake::Task.task_defined?("assets:precompile") && File.exist?(Rails.root.join("bin", "yarn"))
37
+ Rake::Task["assets:precompile"].enhance [ "yarn:install" ]
38
+ end
@@ -17,20 +17,21 @@ class Webpacker::Commands
17
17
  #
18
18
  def clean(count = 2, age = 3600)
19
19
  if config.public_output_path.exist? && config.public_manifest_path.exist?
20
- versions
21
- .sort
22
- .reverse
23
- .each_with_index
24
- .drop_while do |(mtime, _), index|
25
- max_age = [0, Time.now - Time.at(mtime)].max
26
- max_age < age || index < count
20
+ packs
21
+ .map do |paths|
22
+ paths.map { |path| [Time.now - File.mtime(path), path] }
23
+ .sort
24
+ .reject.with_index do |(file_age, _), index|
25
+ file_age < age || index < count
26
+ end
27
+ .map { |_, path| path }
27
28
  end
28
- .each do |(_, files), index|
29
- files.each do |file|
30
- if File.file?(file)
31
- File.delete(file)
32
- logger.info "Removed #{file}"
33
- end
29
+ .flatten
30
+ .compact
31
+ .each do |file|
32
+ if File.file?(file)
33
+ File.delete(file)
34
+ logger.info "Removed #{file}"
34
35
  end
35
36
  end
36
37
  end
@@ -54,17 +55,21 @@ class Webpacker::Commands
54
55
  end
55
56
 
56
57
  private
57
- def versions
58
+ def packs
58
59
  all_files = Dir.glob("#{config.public_output_path}/**/*")
59
60
  manifest_config = Dir.glob("#{config.public_manifest_path}*")
60
61
 
61
62
  packs = all_files - manifest_config - current_version
62
- packs.reject { |file| File.directory?(file) }.group_by { |file| File.mtime(file).utc.to_i }
63
+ packs.reject { |file| File.directory?(file) }.group_by do |path|
64
+ base, _, ext = File.basename(path).scan(/(.*)(-[\da-f]+)(\.\w+)/).flatten
65
+ "#{File.dirname(path)}/#{base}#{ext}"
66
+ end.values
63
67
  end
64
68
 
65
69
  def current_version
66
70
  packs = manifest.refresh.values.map do |value|
67
- next if value.is_a?(Hash)
71
+ value = value["src"] if value.is_a?(Hash)
72
+ next unless value.is_a?(String)
68
73
 
69
74
  File.join(config.root_path, "public", "#{value}*")
70
75
  end.compact
@@ -28,14 +28,14 @@ class Webpacker::Compiler
28
28
  record_compilation_digest
29
29
  end
30
30
  else
31
- logger.info "Everything's up-to-date. Nothing to do"
31
+ logger.debug "Everything's up-to-date. Nothing to do"
32
32
  true
33
33
  end
34
34
  end
35
35
 
36
36
  # Returns true if all the compiled packs are up to date with the underlying asset files.
37
37
  def fresh?
38
- watched_files_digest == last_compilation_digest
38
+ last_compilation_digest&.== watched_files_digest
39
39
  end
40
40
 
41
41
  # Returns true if the compiled packs are out of date with the underlying asset files.
@@ -53,10 +53,11 @@ class Webpacker::Compiler
53
53
 
54
54
  def watched_files_digest
55
55
  warn "Webpacker::Compiler.watched_paths has been deprecated. Set additional_paths in webpacker.yml instead." unless watched_paths.empty?
56
-
57
- files = Dir[*default_watched_paths, *watched_paths].reject { |f| File.directory?(f) }
58
- file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
59
- Digest::SHA1.hexdigest(file_ids.join("/"))
56
+ Dir.chdir File.expand_path(config.root_path) do
57
+ files = Dir[*default_watched_paths, *watched_paths].reject { |f| File.directory?(f) }
58
+ file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
59
+ Digest::SHA1.hexdigest(file_ids.join("/"))
60
+ end
60
61
  end
61
62
 
62
63
  def record_compilation_digest
@@ -64,12 +65,18 @@ class Webpacker::Compiler
64
65
  compilation_digest_path.write(watched_files_digest)
65
66
  end
66
67
 
68
+ def optionalRubyRunner
69
+ bin_webpack_path = config.root_path.join("bin/webpack")
70
+ first_line = File.readlines(bin_webpack_path).first.chomp
71
+ /ruby/.match?(first_line) ? RbConfig.ruby : ""
72
+ end
73
+
67
74
  def run_webpack
68
75
  logger.info "Compiling..."
69
76
 
70
77
  stdout, stderr, status = Open3.capture3(
71
78
  webpack_env,
72
- "#{RbConfig.ruby} ./bin/webpack",
79
+ "#{optionalRubyRunner} ./bin/webpack",
73
80
  chdir: File.expand_path(config.root_path)
74
81
  )
75
82
 
@@ -90,8 +97,8 @@ class Webpacker::Compiler
90
97
 
91
98
  def default_watched_paths
92
99
  [
93
- *config.additional_paths_globbed,
94
- config.source_path_globbed,
100
+ *config.additional_paths,
101
+ "#{config.source_path}/**/*",
95
102
  "yarn.lock", "package.json",
96
103
  "config/webpack/**/*"
97
104
  ].freeze
@@ -23,16 +23,8 @@ class Webpacker::Configuration
23
23
  root_path.join(fetch(:source_path))
24
24
  end
25
25
 
26
- def source_path_globbed
27
- globbed_path_with_extensions(source_path.relative_path_from(root_path))
28
- end
29
-
30
26
  def additional_paths
31
- fetch(:additional_paths) + resolved_paths
32
- end
33
-
34
- def additional_paths_globbed
35
- additional_paths.map { |p| globbed_path_with_extensions(p) }
27
+ fetch(:additional_paths)
36
28
  end
37
29
 
38
30
  def source_entry_path
@@ -59,35 +51,23 @@ class Webpacker::Configuration
59
51
  root_path.join(fetch(:cache_path))
60
52
  end
61
53
 
62
- def extensions
63
- fetch(:extensions)
64
- end
65
-
66
54
  def check_yarn_integrity=(value)
67
- warn "Webpacker::Configuration#check_yarn_integrity=(value) has been deprecated. The integrity check has been removed from Webpacker so changing this setting will have no effect."
55
+ warn <<~EOS
56
+ Webpacker::Configuration#check_yarn_integrity=(value) is obsolete. The integrity
57
+ check has been removed from Webpacker (https://github.com/rails/webpacker/pull/2518)
58
+ so changing this setting will have no effect.
59
+ EOS
68
60
  end
69
61
 
70
62
  def webpack_compile_output?
71
63
  fetch(:webpack_compile_output)
72
64
  end
73
65
 
74
- def extract_css?
75
- fetch(:extract_css)
66
+ def fetch(key)
67
+ data.fetch(key, defaults[key])
76
68
  end
77
69
 
78
70
  private
79
- def resolved_paths
80
- paths = data.fetch(:resolved_paths, [])
81
-
82
- warn "The resolved_paths option has been deprecated. Use additional_paths instead." unless paths.empty?
83
-
84
- paths
85
- end
86
-
87
- def fetch(key)
88
- data.fetch(key, defaults[key])
89
- end
90
-
91
71
  def data
92
72
  @data ||= load
93
73
  end
@@ -121,8 +101,4 @@ class Webpacker::Configuration
121
101
  HashWithIndifferentAccess.new(config[env])
122
102
  end
123
103
  end
124
-
125
- def globbed_path_with_extensions(path)
126
- "#{path}/**/*{#{extensions.join(',')}}"
127
- end
128
104
  end
@@ -51,12 +51,18 @@ class Webpacker::DevServer
51
51
  fetch(:pretty)
52
52
  end
53
53
 
54
+ def hmr?
55
+ fetch(:hmr)
56
+ end
57
+
54
58
  def env_prefix
55
59
  config.dev_server.fetch(:env_prefix, DEFAULT_ENV_PREFIX)
56
60
  end
57
61
 
58
62
  private
59
63
  def fetch(key)
64
+ return nil unless config.dev_server.present?
65
+
60
66
  ENV["#{env_prefix}_#{key.upcase}"] || config.dev_server.fetch(key, defaults[key])
61
67
  end
62
68