webpacker 4.3.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 (200) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +8 -8
  3. data/.github/workflows/jest.yml +30 -0
  4. data/.github/workflows/js-lint.yml +31 -0
  5. data/.github/workflows/rubocop.yml +39 -0
  6. data/.github/workflows/ruby.yml +48 -0
  7. data/.gitignore +2 -0
  8. data/.node-version +1 -1
  9. data/.rubocop.yml +117 -16
  10. data/CHANGELOG.md +91 -5
  11. data/CONTRIBUTING.md +20 -1
  12. data/Gemfile +1 -0
  13. data/Gemfile.lock +121 -109
  14. data/README.md +365 -387
  15. data/config/README.md +3 -0
  16. data/config/webpacker.yml +1 -0
  17. data/docs/deployment.md +9 -11
  18. data/docs/developing_webpacker.md +29 -0
  19. data/docs/troubleshooting.md +64 -26
  20. data/docs/v6_upgrade.md +113 -0
  21. data/gemfiles/Gemfile-rails-edge +1 -1
  22. data/gemfiles/{Gemfile-rails.4.2.x → Gemfile-rails.6.1.x} +4 -1
  23. data/lib/install/{javascript/packs/application.js → application.js} +2 -5
  24. data/lib/install/bin/webpack +4 -7
  25. data/lib/install/bin/yarn +18 -0
  26. data/lib/install/config/webpack/base.js +3 -0
  27. data/lib/install/config/webpack/development.js +2 -2
  28. data/lib/install/config/webpack/production.js +2 -2
  29. data/lib/install/config/webpack/test.js +2 -2
  30. data/lib/install/config/webpacker.yml +22 -54
  31. data/lib/install/package.json +15 -0
  32. data/lib/install/template.rb +66 -23
  33. data/lib/tasks/webpacker/binstubs.rake +6 -4
  34. data/lib/tasks/webpacker/check_binstubs.rake +4 -4
  35. data/lib/tasks/webpacker/check_node.rake +18 -8
  36. data/lib/tasks/webpacker/check_yarn.rake +19 -10
  37. data/lib/tasks/webpacker/clean.rake +12 -6
  38. data/lib/tasks/webpacker/clobber.rake +9 -5
  39. data/lib/tasks/webpacker/compile.rake +4 -2
  40. data/lib/tasks/webpacker/info.rake +12 -10
  41. data/lib/tasks/webpacker/install.rake +6 -4
  42. data/lib/tasks/webpacker/verify_config.rake +14 -0
  43. data/lib/tasks/webpacker/verify_install.rake +1 -10
  44. data/lib/tasks/webpacker/yarn_install.rake +13 -16
  45. data/lib/tasks/webpacker.rake +2 -11
  46. data/lib/tasks/yarn.rake +38 -0
  47. data/lib/webpacker/commands.rb +40 -11
  48. data/lib/webpacker/compiler.rb +23 -12
  49. data/lib/webpacker/configuration.rb +24 -33
  50. data/lib/webpacker/dev_server.rb +6 -0
  51. data/lib/webpacker/dev_server_proxy.rb +3 -1
  52. data/lib/webpacker/dev_server_runner.rb +34 -10
  53. data/lib/webpacker/env.rb +5 -1
  54. data/lib/webpacker/helper.rb +72 -87
  55. data/lib/webpacker/instance.rb +4 -0
  56. data/lib/webpacker/manifest.rb +6 -7
  57. data/lib/webpacker/railtie.rb +8 -45
  58. data/lib/webpacker/runner.rb +1 -0
  59. data/lib/webpacker/version.rb +1 -1
  60. data/lib/webpacker/webpack_runner.rb +30 -4
  61. data/lib/webpacker.rb +1 -1
  62. data/package/__tests__/config.js +6 -27
  63. data/package/__tests__/dev_server.js +2 -0
  64. data/package/__tests__/development.js +18 -13
  65. data/package/__tests__/env.js +16 -4
  66. data/package/__tests__/index.js +9 -0
  67. data/package/__tests__/production.js +6 -6
  68. data/package/__tests__/staging.js +7 -6
  69. data/package/__tests__/test.js +4 -5
  70. data/package/babel/preset.js +54 -0
  71. data/package/config.js +7 -12
  72. data/package/configPath.js +3 -0
  73. data/package/dev_server.js +1 -1
  74. data/package/env.js +14 -6
  75. data/package/environments/__tests__/base.js +26 -31
  76. data/package/environments/base.js +78 -128
  77. data/package/environments/development.js +47 -43
  78. data/package/environments/production.js +66 -66
  79. data/package/environments/test.js +2 -2
  80. data/package/index.js +15 -8
  81. data/package/inliningCss.js +7 -0
  82. data/package/rules/babel.js +20 -11
  83. data/package/rules/coffee.js +6 -0
  84. data/package/rules/erb.js +15 -0
  85. data/package/rules/file.js +21 -18
  86. data/package/rules/index.js +16 -18
  87. data/package/rules/less.js +22 -0
  88. data/package/rules/raw.js +5 -0
  89. data/package/rules/sass.js +14 -6
  90. data/package/rules/stylus.js +26 -0
  91. data/package/utils/get_style_rule.js +28 -36
  92. data/package/utils/helpers.js +28 -35
  93. data/package.json +26 -46
  94. data/test/command_test.rb +76 -0
  95. data/test/compiler_test.rb +4 -11
  96. data/test/configuration_test.rb +5 -35
  97. data/test/dev_server_runner_test.rb +36 -6
  98. data/test/engine_rake_tasks_test.rb +39 -0
  99. data/test/helper_test.rb +77 -60
  100. data/test/manifest_test.rb +53 -6
  101. data/test/mounted_app/Rakefile +4 -0
  102. data/test/mounted_app/test/dummy/Rakefile +3 -0
  103. data/test/mounted_app/test/dummy/bin/rails +3 -0
  104. data/test/mounted_app/test/dummy/bin/rake +3 -0
  105. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  106. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  107. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  108. data/test/mounted_app/test/dummy/config.ru +5 -0
  109. data/test/mounted_app/test/dummy/package.json +7 -0
  110. data/test/rake_tasks_test.rb +12 -10
  111. data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
  112. data/test/test_app/app/packs/entrypoints/multi_entry.css +4 -0
  113. data/test/test_app/app/packs/entrypoints/multi_entry.js +4 -0
  114. data/test/test_app/config/application.rb +0 -1
  115. data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
  116. data/test/test_app/config/webpacker.yml +7 -27
  117. data/test/test_app/config/webpacker_other_location.yml +79 -0
  118. data/test/test_app/config/webpacker_public_root.yml +0 -1
  119. data/test/test_app/public/packs/manifest.json +36 -17
  120. data/test/test_app/some.config.js +0 -0
  121. data/test/webpack_runner_test.rb +10 -4
  122. data/test/webpacker_test.rb +21 -0
  123. data/webpacker.gemspec +5 -4
  124. data/yarn.lock +3387 -6898
  125. metadata +90 -99
  126. data/.travis.yml +0 -57
  127. data/docs/assets.md +0 -119
  128. data/docs/cloud9.md +0 -310
  129. data/docs/css.md +0 -254
  130. data/docs/docker.md +0 -68
  131. data/docs/engines.md +0 -200
  132. data/docs/env.md +0 -65
  133. data/docs/es6.md +0 -72
  134. data/docs/folder-structure.md +0 -66
  135. data/docs/misc.md +0 -23
  136. data/docs/props.md +0 -223
  137. data/docs/testing.md +0 -137
  138. data/docs/typescript.md +0 -126
  139. data/docs/v4-upgrade.md +0 -142
  140. data/docs/webpack-dev-server.md +0 -92
  141. data/docs/webpack.md +0 -364
  142. data/docs/yarn.md +0 -23
  143. data/gemfiles/Gemfile-rails.5.0.x +0 -9
  144. data/gemfiles/Gemfile-rails.5.1.x +0 -9
  145. data/lib/install/angular.rb +0 -23
  146. data/lib/install/coffee.rb +0 -25
  147. data/lib/install/config/.browserslistrc +0 -1
  148. data/lib/install/config/babel.config.js +0 -72
  149. data/lib/install/config/postcss.config.js +0 -12
  150. data/lib/install/config/webpack/environment.js +0 -3
  151. data/lib/install/elm.rb +0 -39
  152. data/lib/install/erb.rb +0 -25
  153. data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
  154. data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
  155. data/lib/install/examples/angular/hello_angular/index.ts +0 -8
  156. data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
  157. data/lib/install/examples/angular/hello_angular.js +0 -7
  158. data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
  159. data/lib/install/examples/elm/Main.elm +0 -55
  160. data/lib/install/examples/elm/hello_elm.js +0 -16
  161. data/lib/install/examples/erb/hello_erb.js.erb +0 -6
  162. data/lib/install/examples/react/babel.config.js +0 -87
  163. data/lib/install/examples/react/hello_react.jsx +0 -26
  164. data/lib/install/examples/react/tsconfig.json +0 -20
  165. data/lib/install/examples/stimulus/application.js +0 -1
  166. data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
  167. data/lib/install/examples/stimulus/controllers/index.js +0 -9
  168. data/lib/install/examples/svelte/app.svelte +0 -11
  169. data/lib/install/examples/svelte/hello_svelte.js +0 -20
  170. data/lib/install/examples/typescript/hello_typescript.ts +0 -4
  171. data/lib/install/examples/typescript/tsconfig.json +0 -23
  172. data/lib/install/examples/vue/app.vue +0 -22
  173. data/lib/install/examples/vue/hello_vue.js +0 -72
  174. data/lib/install/loaders/coffee.js +0 -6
  175. data/lib/install/loaders/elm.js +0 -25
  176. data/lib/install/loaders/erb.js +0 -11
  177. data/lib/install/loaders/svelte.js +0 -9
  178. data/lib/install/loaders/typescript.js +0 -11
  179. data/lib/install/loaders/vue.js +0 -6
  180. data/lib/install/react.rb +0 -18
  181. data/lib/install/stimulus.rb +0 -12
  182. data/lib/install/svelte.rb +0 -29
  183. data/lib/install/typescript.rb +0 -46
  184. data/lib/install/vue.rb +0 -49
  185. data/lib/tasks/installers.rake +0 -42
  186. data/package/config_types/__tests__/config_list.js +0 -118
  187. data/package/config_types/__tests__/config_object.js +0 -43
  188. data/package/config_types/config_list.js +0 -75
  189. data/package/config_types/config_object.js +0 -55
  190. data/package/config_types/index.js +0 -7
  191. data/package/rules/module.css.js +0 -3
  192. data/package/rules/module.sass.js +0 -8
  193. data/package/rules/node_modules.js +0 -24
  194. data/package/utils/__tests__/deep_assign.js +0 -32
  195. data/package/utils/__tests__/deep_merge.js +0 -10
  196. data/package/utils/__tests__/get_style_rule.js +0 -65
  197. data/package/utils/__tests__/objectify.js +0 -9
  198. data/package/utils/deep_assign.js +0 -22
  199. data/package/utils/deep_merge.js +0 -22
  200. data/package/utils/objectify.js +0 -3
@@ -4,6 +4,8 @@ require "digest/sha1"
4
4
  class Webpacker::Compiler
5
5
  # Additional paths that test compiler needs to watch
6
6
  # Webpacker::Compiler.watched_paths << 'bower_components'
7
+ #
8
+ # Deprecated. Use additional_paths in the YAML configuration instead.
7
9
  cattr_accessor(:watched_paths) { [] }
8
10
 
9
11
  # Additional environment variables that the compiler is being run with
@@ -20,21 +22,20 @@ class Webpacker::Compiler
20
22
  if stale?
21
23
  run_webpack.tap do |success|
22
24
  # We used to only record the digest on success
23
- # However, the output file is still written on error, (at least with ts-loader), meaning that the
24
- # digest should still be updated. If it's not, you can end up in a situation where a recompile doesn't
25
- # take place when it should.
25
+ # However, the output file is still written on error, meaning that the digest should still be updated.
26
+ # If it's not, you can end up in a situation where a recompile doesn't take place when it should.
26
27
  # See https://github.com/rails/webpacker/issues/2113
27
28
  record_compilation_digest
28
29
  end
29
30
  else
30
- logger.info "Everything's up-to-date. Nothing to do"
31
+ logger.debug "Everything's up-to-date. Nothing to do"
31
32
  true
32
33
  end
33
34
  end
34
35
 
35
36
  # Returns true if all the compiled packs are up to date with the underlying asset files.
36
37
  def fresh?
37
- watched_files_digest == last_compilation_digest
38
+ last_compilation_digest&.== watched_files_digest
38
39
  end
39
40
 
40
41
  # Returns true if the compiled packs are out of date with the underlying asset files.
@@ -51,9 +52,12 @@ class Webpacker::Compiler
51
52
  end
52
53
 
53
54
  def watched_files_digest
54
- files = Dir[*default_watched_paths, *watched_paths].reject { |f| File.directory?(f) }
55
- file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
56
- Digest::SHA1.hexdigest(file_ids.join("/"))
55
+ warn "Webpacker::Compiler.watched_paths has been deprecated. Set additional_paths in webpacker.yml instead." unless watched_paths.empty?
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
57
61
  end
58
62
 
59
63
  def record_compilation_digest
@@ -61,12 +65,18 @@ class Webpacker::Compiler
61
65
  compilation_digest_path.write(watched_files_digest)
62
66
  end
63
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
+
64
74
  def run_webpack
65
75
  logger.info "Compiling..."
66
76
 
67
77
  stdout, stderr, status = Open3.capture3(
68
78
  webpack_env,
69
- "#{RbConfig.ruby} ./bin/webpack",
79
+ "#{optionalRubyRunner} ./bin/webpack",
70
80
  chdir: File.expand_path(config.root_path)
71
81
  )
72
82
 
@@ -87,8 +97,8 @@ class Webpacker::Compiler
87
97
 
88
98
  def default_watched_paths
89
99
  [
90
- *config.resolved_paths_globbed,
91
- config.source_path_globbed,
100
+ *config.additional_paths,
101
+ "#{config.source_path}/**/*",
92
102
  "yarn.lock", "package.json",
93
103
  "config/webpack/**/*"
94
104
  ].freeze
@@ -102,6 +112,7 @@ class Webpacker::Compiler
102
112
  return env unless defined?(ActionController::Base)
103
113
 
104
114
  env.merge("WEBPACKER_ASSET_HOST" => ENV.fetch("WEBPACKER_ASSET_HOST", ActionController::Base.helpers.compute_asset_host),
105
- "WEBPACKER_RELATIVE_URL_ROOT" => ENV.fetch("WEBPACKER_RELATIVE_URL_ROOT", ActionController::Base.relative_url_root))
115
+ "WEBPACKER_RELATIVE_URL_ROOT" => ENV.fetch("WEBPACKER_RELATIVE_URL_ROOT", ActionController::Base.relative_url_root),
116
+ "WEBPACKER_CONFIG" => webpacker.config_path.to_s)
106
117
  end
107
118
  end
@@ -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
- def resolved_paths
31
- fetch(:resolved_paths)
32
- end
33
-
34
- def resolved_paths_globbed
35
- resolved_paths.map { |p| globbed_path_with_extensions(p) }
26
+ def additional_paths
27
+ fetch(:additional_paths)
36
28
  end
37
29
 
38
30
  def source_entry_path
@@ -59,38 +51,34 @@ 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
- data[:check_yarn_integrity] = value
68
- end
69
-
70
- def check_yarn_integrity?
71
- fetch(:check_yarn_integrity)
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
72
60
  end
73
61
 
74
62
  def webpack_compile_output?
75
63
  fetch(:webpack_compile_output)
76
64
  end
77
65
 
78
- def extract_css?
79
- fetch(:extract_css)
66
+ def fetch(key)
67
+ data.fetch(key, defaults[key])
80
68
  end
81
69
 
82
70
  private
83
- def fetch(key)
84
- data.fetch(key, defaults[key])
85
- end
86
-
87
71
  def data
88
72
  @data ||= load
89
73
  end
90
74
 
91
75
  def load
92
- YAML.load(config_path.read)[env].deep_symbolize_keys
93
-
76
+ config = begin
77
+ YAML.load_file(config_path.to_s, aliases: true)
78
+ rescue ArgumentError
79
+ YAML.load_file(config_path.to_s)
80
+ end
81
+ config[env].deep_symbolize_keys
94
82
  rescue Errno::ENOENT => e
95
83
  raise "Webpacker configuration file not found #{config_path}. " \
96
84
  "Please run rails webpacker:install " \
@@ -103,11 +91,14 @@ class Webpacker::Configuration
103
91
  end
104
92
 
105
93
  def defaults
106
- @defaults ||= \
107
- HashWithIndifferentAccess.new(YAML.load_file(File.expand_path("../../install/config/webpacker.yml", __FILE__))[env])
108
- end
109
-
110
- def globbed_path_with_extensions(path)
111
- "#{path}/**/*{#{extensions.join(',')}}"
94
+ @defaults ||= begin
95
+ path = File.expand_path("../../install/config/webpacker.yml", __FILE__)
96
+ config = begin
97
+ YAML.load_file(path, aliases: true)
98
+ rescue ArgumentError
99
+ YAML.load_file(path)
100
+ end
101
+ HashWithIndifferentAccess.new(config[env])
102
+ end
112
103
  end
113
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
 
@@ -11,7 +11,9 @@ class Webpacker::DevServerProxy < Rack::Proxy
11
11
 
12
12
  def perform_request(env)
13
13
  if env["PATH_INFO"].start_with?("/#{public_output_uri_path}") && dev_server.running?
14
- env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = env["HTTP_X_FORWARDED_SERVER"] = dev_server.host_with_port
14
+ env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = dev_server.host
15
+ env["HTTP_X_FORWARDED_SERVER"] = dev_server.host_with_port
16
+ env["HTTP_PORT"] = env["HTTP_X_FORWARDED_PORT"] = dev_server.port.to_s
15
17
  env["HTTP_X_FORWARDED_PROTO"] = env["HTTP_X_FORWARDED_SCHEME"] = dev_server.protocol
16
18
  unless dev_server.https?
17
19
  env["HTTPS"] = env["HTTP_X_FORWARDED_SSL"] = "off"
@@ -8,58 +8,82 @@ module Webpacker
8
8
  class DevServerRunner < Webpacker::Runner
9
9
  def run
10
10
  load_config
11
+ detect_unsupported_switches!
11
12
  detect_port!
12
13
  execute_cmd
13
14
  end
14
15
 
15
16
  private
17
+
16
18
  def load_config
17
19
  app_root = Pathname.new(@app_path)
18
20
 
19
- config = Configuration.new(
21
+ @config = Configuration.new(
20
22
  root_path: app_root,
21
- config_path: app_root.join("config/webpacker.yml"),
23
+ config_path: Pathname.new(@webpacker_config),
22
24
  env: ENV["RAILS_ENV"]
23
25
  )
24
26
 
25
- dev_server = DevServer.new(config)
27
+ dev_server = DevServer.new(@config)
26
28
 
27
29
  @hostname = dev_server.host
28
30
  @port = dev_server.port
29
31
  @pretty = dev_server.pretty?
32
+ @https = dev_server.https?
33
+ @hot = dev_server.hmr?
30
34
 
31
35
  rescue Errno::ENOENT, NoMethodError
32
- $stdout.puts "webpack dev_server configuration not found in #{config.config_path}[#{ENV["RAILS_ENV"]}]."
36
+ $stdout.puts "webpack dev_server configuration not found in #{@config.config_path}[#{ENV["RAILS_ENV"]}]."
33
37
  $stdout.puts "Please run bundle exec rails webpacker:install to install Webpacker"
34
38
  exit!
35
39
  end
36
40
 
41
+ UNSUPPORTED_SWITCHES = %w[--host --port]
42
+ private_constant :UNSUPPORTED_SWITCHES
43
+ def detect_unsupported_switches!
44
+ unsupported_switches = UNSUPPORTED_SWITCHES & @argv
45
+ if unsupported_switches.any?
46
+ $stdout.puts "The following CLI switches are not supported by Webpacker: #{unsupported_switches.join(' ')}. Please edit your command and try again."
47
+ exit!
48
+ end
49
+
50
+ if @argv.include?("--https") && !@https
51
+ $stdout.puts "Please set https: true in webpacker.yml to use the --https command line flag."
52
+ exit!
53
+ end
54
+ end
55
+
37
56
  def detect_port!
38
57
  server = TCPServer.new(@hostname, @port)
39
58
  server.close
40
59
 
41
60
  rescue Errno::EADDRINUSE
42
- $stdout.puts "Another program is running on port #{@port}. Set a new port in #{@config_file} for dev_server"
61
+ $stdout.puts "Another program is running on port #{@port}. Set a new port in #{@config.config_path} for dev_server"
43
62
  exit!
44
63
  end
45
64
 
46
65
  def execute_cmd
47
66
  env = Webpacker::Compiler.env
67
+ env["WEBPACKER_CONFIG"] = @webpacker_config
68
+ env["WEBPACK_SERVE"] = "true"
48
69
 
49
70
  cmd = if node_modules_bin_exist?
50
- ["#{@node_modules_bin_path}/webpack-dev-server"]
71
+ ["#{@node_modules_bin_path}/webpack", "serve"]
51
72
  else
52
- ["yarn", "webpack-dev-server"]
73
+ ["yarn", "webpack", "serve"]
53
74
  end
54
75
 
55
- if ARGV.include?("--debug")
56
- cmd = [ "node", "--inspect-brk"] + cmd
57
- ARGV.delete("--debug")
76
+ if @argv.include?("--debug-webpacker")
77
+ cmd = [ "node", "--inspect-brk", "--trace-warnings" ] + cmd
78
+ @argv.delete "--debug-webpacker"
58
79
  end
59
80
 
60
81
  cmd += ["--config", @webpack_config]
61
82
  cmd += ["--progress", "--color"] if @pretty
62
83
 
84
+ cmd += ["--hot"] if @hot
85
+ cmd += @argv
86
+
63
87
  Dir.chdir(@app_path) do
64
88
  Kernel.exec env, *cmd
65
89
  end
data/lib/webpacker/env.rb CHANGED
@@ -27,7 +27,11 @@ class Webpacker::Env
27
27
 
28
28
  def available_environments
29
29
  if config_path.exist?
30
- YAML.load(config_path.read).keys
30
+ begin
31
+ YAML.load_file(config_path.to_s, aliases: true)
32
+ rescue ArgumentError
33
+ YAML.load_file(config_path.to_s)
34
+ end
31
35
  else
32
36
  [].freeze
33
37
  end
@@ -7,37 +7,40 @@ module Webpacker::Helper
7
7
  end
8
8
 
9
9
  # Computes the relative path for a given Webpacker asset.
10
- # Returns the relative path using manifest.json and passes it to asset_path helper.
11
- # This will use asset_path internally, so most of their behaviors will be the same.
10
+ # Returns the relative path using manifest.json and passes it to path_to_asset helper.
11
+ # This will use path_to_asset internally, so most of their behaviors will be the same.
12
12
  #
13
13
  # Example:
14
14
  #
15
- # # When extract_css is false in webpacker.yml and the file is a css:
16
- # <%= asset_pack_path 'calendar.css' %> # => nil
17
- #
18
- # # When extract_css is true in webpacker.yml or the file is not a css:
19
15
  # <%= asset_pack_path 'calendar.css' %> # => "/packs/calendar-1016838bab065ae1e122.css"
20
16
  def asset_pack_path(name, **options)
21
- if current_webpacker_instance.config.extract_css? || !stylesheet?(name)
22
- asset_path(current_webpacker_instance.manifest.lookup!(name), options)
23
- end
17
+ path_to_asset(current_webpacker_instance.manifest.lookup!(name), options)
24
18
  end
25
19
 
26
20
  # Computes the absolute path for a given Webpacker asset.
27
- # Returns the absolute path using manifest.json and passes it to asset_url helper.
28
- # This will use asset_url internally, so most of their behaviors will be the same.
21
+ # Returns the absolute path using manifest.json and passes it to url_to_asset helper.
22
+ # This will use url_to_asset internally, so most of their behaviors will be the same.
29
23
  #
30
24
  # Example:
31
25
  #
32
- # # When extract_css is false in webpacker.yml and the file is a css:
33
- # <%= asset_pack_url 'calendar.css' %> # => nil
34
- #
35
- # # When extract_css is true in webpacker.yml or the file is not a css:
36
26
  # <%= asset_pack_url 'calendar.css' %> # => "http://example.com/packs/calendar-1016838bab065ae1e122.css"
37
27
  def asset_pack_url(name, **options)
38
- if current_webpacker_instance.config.extract_css? || !stylesheet?(name)
39
- asset_url(current_webpacker_instance.manifest.lookup!(name), options)
40
- end
28
+ url_to_asset(current_webpacker_instance.manifest.lookup!(name), options)
29
+ end
30
+
31
+ # Computes the relative path for a given Webpacker image with the same automated processing as image_pack_tag.
32
+ # Returns the relative path using manifest.json and passes it to path_to_asset helper.
33
+ # This will use path_to_asset internally, so most of their behaviors will be the same.
34
+ def image_pack_path(name, **options)
35
+ resolve_path_to_image(name, **options)
36
+ end
37
+
38
+ # Computes the absolute path for a given Webpacker image with the same automated
39
+ # processing as image_pack_tag. Returns the relative path using manifest.json
40
+ # and passes it to path_to_asset helper. This will use path_to_asset internally,
41
+ # so most of their behaviors will be the same.
42
+ def image_pack_url(name, **options)
43
+ resolve_path_to_image(name, **options.merge(protocol: :request))
41
44
  end
42
45
 
43
46
  # Creates an image tag that references the named pack file.
@@ -46,7 +49,16 @@ module Webpacker::Helper
46
49
  #
47
50
  # <%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
48
51
  # <img alt='Edit Entry' src='/packs/application-k344a6d59eef8632c9d1.png' width='16' height='10' />
52
+ #
53
+ # <%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
54
+ # <img srcset= "/packs/picture-2x-7cca48e6cae66ec07b8e.png 2x" src="/packs/picture-c38deda30895059837cf.png" >
49
55
  def image_pack_tag(name, **options)
56
+ if options[:srcset] && !options[:srcset].is_a?(String)
57
+ options[:srcset] = options[:srcset].map do |src_name, size|
58
+ "#{resolve_path_to_image(src_name)} #{size}"
59
+ end.join(", ")
60
+ end
61
+
50
62
  image_tag(resolve_path_to_image(name), options)
51
63
  end
52
64
 
@@ -60,43 +72,37 @@ module Webpacker::Helper
60
72
  favicon_link_tag(resolve_path_to_image(name), options)
61
73
  end
62
74
 
63
- # Creates a script tag that references the named pack file, as compiled by webpack per the entries list
64
- # in config/webpack/shared.js. By default, this list is auto-generated to match everything in
65
- # app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
66
- #
67
- # Example:
68
- #
69
- # <%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
70
- # <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
71
- def javascript_pack_tag(*names, **options)
72
- javascript_include_tag(*sources_from_manifest_entries(names, type: :javascript), **options)
73
- end
74
-
75
75
  # Creates script tags that reference the js chunks from entrypoints when using split chunks API,
76
- # as compiled by webpack per the entries list in config/webpack/shared.js.
76
+ # as compiled by webpack per the entries list in package/environments/base.js.
77
77
  # By default, this list is auto-generated to match everything in
78
- # app/javascript/packs/*.js and all the dependent chunks. In production mode, the digested reference is automatically looked up.
78
+ # app/packs/entrypoints/*.js and all the dependent chunks. In production mode, the digested reference is automatically looked up.
79
79
  # See: https://webpack.js.org/plugins/split-chunks-plugin/
80
+ #
80
81
  # Example:
81
82
  #
82
- # <%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %> # =>
83
- # <script src="/packs/vendor-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
84
- # <script src="/packs/calendar~runtime-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
85
- # <script src="/packs/calendar-1016838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
86
- # <script src="/packs/map~runtime-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
87
- # <script src="/packs/map-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
83
+ # <%= javascript_pack_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %> # =>
84
+ # <script src="/packs/vendor-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload" defer="true"></script>
85
+ # <script src="/packs/calendar~runtime-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload" defer="true"></script>
86
+ # <script src="/packs/calendar-1016838bab065ae1e314.chunk.js" data-turbolinks-track="reload" defer="true"></script>
87
+ # <script src="/packs/map~runtime-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload" defer="true"></script>
88
+ # <script src="/packs/map-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload" defer="true"></script>
89
+ #
88
90
  # DO:
89
- # <%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
91
+ #
92
+ # <%= javascript_pack_tag 'calendar', 'map' %>
93
+ #
90
94
  # DON'T:
91
- # <%= javascript_packs_with_chunks_tag 'calendar' %>
92
- # <%= javascript_packs_with_chunks_tag 'map' %>
93
- def javascript_packs_with_chunks_tag(*names, **options)
94
- javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), **options)
95
+ #
96
+ # <%= javascript_pack_tag 'calendar' %>
97
+ # <%= javascript_pack_tag 'map' %>
98
+ def javascript_pack_tag(*names, defer: true, **options)
99
+ javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), **options.tap { |o| o[:defer] = defer })
95
100
  end
96
101
 
97
102
  # Creates a link tag, for preloading, that references a given Webpacker asset.
98
103
  # In production mode, the digested reference is automatically looked up.
99
104
  # See: https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
105
+ #
100
106
  # Example:
101
107
  #
102
108
  # <%= preload_pack_asset 'fonts/fa-regular-400.woff2' %> # =>
@@ -109,68 +115,47 @@ module Webpacker::Helper
109
115
  end
110
116
  end
111
117
 
112
- # Creates a link tag that references the named pack file, as compiled by webpack per the entries list
113
- # in config/webpack/shared.js. By default, this list is auto-generated to match everything in
114
- # app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
115
- #
116
- # Note: If the development server is running and hot module replacement is active, this will return nothing.
117
- # In that setup you need to configure your styles to be inlined in your JavaScript for hot reloading.
118
- #
119
- # Examples:
120
- #
121
- # # When extract_css is false in webpacker.yml:
122
- # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
123
- # nil
124
- #
125
- # # When extract_css is true in webpacker.yml:
126
- # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
127
- # <link rel="stylesheet" media="screen" href="/packs/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />
128
- def stylesheet_pack_tag(*names, **options)
129
- if current_webpacker_instance.config.extract_css?
130
- stylesheet_link_tag(*sources_from_manifest_entries(names, type: :stylesheet), **options)
131
- end
132
- end
133
-
134
118
  # Creates link tags that reference the css chunks from entrypoints when using split chunks API,
135
- # as compiled by webpack per the entries list in config/webpack/shared.js.
119
+ # as compiled by webpack per the entries list in package/environments/base.js.
136
120
  # By default, this list is auto-generated to match everything in
137
- # app/javascript/packs/*.js and all the dependent chunks. In production mode, the digested reference is automatically looked up.
121
+ # app/packs/entrypoints/*.js and all the dependent chunks. In production mode, the digested reference is automatically looked up.
138
122
  # See: https://webpack.js.org/plugins/split-chunks-plugin/
139
123
  #
140
124
  # Examples:
141
125
  #
142
- # <%= stylesheet_packs_with_chunks_tag 'calendar', 'map' %> # =>
126
+ # <%= stylesheet_pack_tag 'calendar', 'map' %> # =>
143
127
  # <link rel="stylesheet" media="screen" href="/packs/3-8c7ce31a.chunk.css" />
144
128
  # <link rel="stylesheet" media="screen" href="/packs/calendar-8c7ce31a.chunk.css" />
145
129
  # <link rel="stylesheet" media="screen" href="/packs/map-8c7ce31a.chunk.css" />
130
+ #
131
+ # When using the webpack-dev-server, CSS is inlined so HMR can be turned on for CSS,
132
+ # including CSS modules
133
+ # <%= stylesheet_pack_tag 'calendar', 'map' %> # => nil
134
+ #
146
135
  # DO:
147
- # <%= stylesheet_packs_with_chunks_tag 'calendar', 'map' %>
136
+ #
137
+ # <%= stylesheet_pack_tag 'calendar', 'map' %>
138
+ #
148
139
  # DON'T:
149
- # <%= stylesheet_packs_with_chunks_tag 'calendar' %>
150
- # <%= stylesheet_packs_with_chunks_tag 'map' %>
151
- def stylesheet_packs_with_chunks_tag(*names, **options)
152
- if current_webpacker_instance.config.extract_css?
153
- stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
154
- end
140
+ #
141
+ # <%= stylesheet_pack_tag 'calendar' %>
142
+ # <%= stylesheet_pack_tag 'map' %>
143
+ def stylesheet_pack_tag(*names, **options)
144
+ return "" if Webpacker.inlining_css?
145
+
146
+ stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
155
147
  end
156
148
 
157
149
  private
158
- def stylesheet?(name)
159
- File.extname(name) == ".css"
160
- end
161
-
162
- def sources_from_manifest_entries(names, type:)
163
- names.map { |name| current_webpacker_instance.manifest.lookup!(name, type: type) }.flatten
164
- end
165
150
 
166
151
  def sources_from_manifest_entrypoints(names, type:)
167
- names.map { |name| current_webpacker_instance.manifest.lookup_pack_with_chunks!(name, type: type) }.flatten.uniq
152
+ names.map { |name| current_webpacker_instance.manifest.lookup_pack_with_chunks!(name.to_s, type: type) }.flatten.uniq
168
153
  end
169
154
 
170
- def resolve_path_to_image(name)
171
- path = name.starts_with?("media/images/") ? name : "media/images/#{name}"
172
- asset_path(current_webpacker_instance.manifest.lookup!(path))
155
+ def resolve_path_to_image(name, **options)
156
+ path = name.starts_with?("static/") ? name : "static/#{name}"
157
+ path_to_asset(current_webpacker_instance.manifest.lookup!(path), options)
173
158
  rescue
174
- asset_path(current_webpacker_instance.manifest.lookup!(name))
159
+ path_to_asset(current_webpacker_instance.manifest.lookup!(name), options)
175
160
  end
176
161
  end
@@ -34,4 +34,8 @@ class Webpacker::Instance
34
34
  def commands
35
35
  @commands ||= Webpacker::Commands.new self
36
36
  end
37
+
38
+ def inlining_css?
39
+ dev_server.hmr? && dev_server.running?
40
+ end
37
41
  end
@@ -23,13 +23,13 @@ class Webpacker::Manifest
23
23
 
24
24
  manifest_pack_type = manifest_type(pack_type[:type])
25
25
  manifest_pack_name = manifest_name(name, manifest_pack_type)
26
- find("entrypoints")[manifest_pack_name][manifest_pack_type]
26
+ find("entrypoints")[manifest_pack_name]["assets"][manifest_pack_type]
27
27
  rescue NoMethodError
28
28
  nil
29
29
  end
30
30
 
31
31
  def lookup_pack_with_chunks!(name, pack_type = {})
32
- lookup_pack_with_chunks(name, pack_type) || handle_missing_entry(name)
32
+ lookup_pack_with_chunks(name, pack_type) || handle_missing_entry(name, pack_type)
33
33
  end
34
34
 
35
35
  # Computes the relative path for a given Webpacker asset using manifest.json.
@@ -46,7 +46,7 @@ class Webpacker::Manifest
46
46
 
47
47
  # Like lookup, except that if no asset is found, raises a Webpacker::Manifest::MissingEntryError.
48
48
  def lookup!(name, pack_type = {})
49
- lookup(name, pack_type) || handle_missing_entry(name)
49
+ lookup(name, pack_type) || handle_missing_entry(name, pack_type)
50
50
  end
51
51
 
52
52
  private
@@ -75,8 +75,8 @@ class Webpacker::Manifest
75
75
  "#{name}.#{manifest_type(pack_type)}"
76
76
  end
77
77
 
78
- def handle_missing_entry(name)
79
- raise Webpacker::Manifest::MissingEntryError, missing_file_from_manifest_error(name)
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]))
80
80
  end
81
81
 
82
82
  def load
@@ -91,8 +91,7 @@ class Webpacker::Manifest
91
91
  # manifest hash the entrypoints are defined by their pack name without the extension.
92
92
  # When the user provides a name with a file extension, we want to try to strip it off.
93
93
  def manifest_name(name, pack_type)
94
- return name if File.extname(name.to_s).empty?
95
- File.basename(name, pack_type)
94
+ name.chomp(".#{pack_type}")
96
95
  end
97
96
 
98
97
  def manifest_type(pack_type)