webpacker 5.4.4 → 6.0.0.rc.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) 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 -16
  7. data/.gitignore +2 -0
  8. data/.node-version +1 -1
  9. data/.rubocop.yml +5 -7
  10. data/CHANGELOG.md +30 -12
  11. data/CONTRIBUTING.md +20 -1
  12. data/Gemfile.lock +83 -83
  13. data/README.md +389 -179
  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.6.1.x +12 -0
  21. data/lib/install/{javascript/packs/application.js → application.js} +2 -5
  22. data/lib/install/bin/webpack +4 -7
  23. data/lib/install/bin/yarn +18 -0
  24. data/lib/install/config/webpack/base.js +3 -0
  25. data/lib/install/config/webpack/development.js +2 -2
  26. data/lib/install/config/webpack/production.js +2 -2
  27. data/lib/install/config/webpack/test.js +2 -2
  28. data/lib/install/config/webpacker.yml +21 -49
  29. data/lib/install/package.json +15 -0
  30. data/lib/install/template.rb +64 -29
  31. data/lib/tasks/webpacker/binstubs.rake +6 -4
  32. data/lib/tasks/webpacker/check_binstubs.rake +4 -4
  33. data/lib/tasks/webpacker/check_node.rake +3 -0
  34. data/lib/tasks/webpacker/check_yarn.rake +3 -0
  35. data/lib/tasks/webpacker/clobber.rake +1 -1
  36. data/lib/tasks/webpacker/compile.rake +4 -2
  37. data/lib/tasks/webpacker/info.rake +12 -10
  38. data/lib/tasks/webpacker/install.rake +6 -4
  39. data/lib/tasks/webpacker/verify_config.rake +14 -0
  40. data/lib/tasks/webpacker/verify_install.rake +1 -10
  41. data/lib/tasks/webpacker/yarn_install.rake +9 -7
  42. data/lib/tasks/webpacker.rake +2 -11
  43. data/lib/tasks/yarn.rake +38 -0
  44. data/lib/webpacker/commands.rb +21 -16
  45. data/lib/webpacker/compiler.rb +16 -9
  46. data/lib/webpacker/configuration.rb +8 -32
  47. data/lib/webpacker/dev_server.rb +6 -0
  48. data/lib/webpacker/dev_server_runner.rb +28 -4
  49. data/lib/webpacker/helper.rb +47 -81
  50. data/lib/webpacker/instance.rb +4 -0
  51. data/lib/webpacker/manifest.rb +2 -3
  52. data/lib/webpacker/railtie.rb +8 -2
  53. data/lib/webpacker/runner.rb +1 -1
  54. data/lib/webpacker/version.rb +1 -1
  55. data/lib/webpacker/webpack_runner.rb +29 -3
  56. data/lib/webpacker.rb +1 -1
  57. data/package/__tests__/config.js +5 -37
  58. data/package/__tests__/development.js +13 -21
  59. data/package/__tests__/env.js +16 -4
  60. data/package/__tests__/index.js +9 -0
  61. data/package/__tests__/production.js +6 -6
  62. data/package/__tests__/staging.js +7 -6
  63. data/package/__tests__/test.js +4 -5
  64. data/package/babel/preset.js +54 -0
  65. data/package/config.js +6 -14
  66. data/package/env.js +13 -4
  67. data/package/environments/__tests__/base.js +20 -52
  68. data/package/environments/base.js +68 -128
  69. data/package/environments/development.js +49 -47
  70. data/package/environments/production.js +66 -64
  71. data/package/environments/test.js +2 -2
  72. data/package/index.js +15 -8
  73. data/package/inliningCss.js +7 -0
  74. data/package/rules/babel.js +10 -8
  75. data/package/rules/coffee.js +6 -0
  76. data/package/rules/erb.js +15 -0
  77. data/package/rules/file.js +21 -19
  78. data/package/rules/index.js +16 -18
  79. data/package/rules/less.js +22 -0
  80. data/package/rules/raw.js +5 -0
  81. data/package/rules/sass.js +9 -10
  82. data/package/rules/stylus.js +26 -0
  83. data/package/utils/get_style_rule.js +28 -36
  84. data/package/utils/helpers.js +28 -35
  85. data/package.json +18 -38
  86. data/test/command_test.rb +76 -0
  87. data/test/compiler_test.rb +0 -12
  88. data/test/configuration_test.rb +4 -35
  89. data/test/dev_server_runner_test.rb +36 -6
  90. data/test/engine_rake_tasks_test.rb +39 -0
  91. data/test/helper_test.rb +79 -77
  92. data/test/manifest_test.rb +16 -0
  93. data/test/mounted_app/Rakefile +4 -0
  94. data/test/mounted_app/test/dummy/Rakefile +3 -0
  95. data/test/mounted_app/test/dummy/bin/rails +3 -0
  96. data/test/mounted_app/test/dummy/bin/rake +3 -0
  97. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  98. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  99. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  100. data/test/mounted_app/test/dummy/config.ru +5 -0
  101. data/test/mounted_app/test/dummy/package.json +7 -0
  102. data/test/rake_tasks_test.rb +1 -10
  103. data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
  104. data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
  105. data/test/test_app/config/webpacker.yml +5 -31
  106. data/test/test_app/config/webpacker_other_location.yml +79 -0
  107. data/test/test_app/config/webpacker_public_root.yml +0 -1
  108. data/test/test_app/public/packs/manifest.json +36 -18
  109. data/test/test_app/some.config.js +0 -0
  110. data/test/webpack_runner_test.rb +9 -3
  111. data/test/webpacker_test.rb +21 -0
  112. data/webpacker.gemspec +2 -2
  113. data/yarn.lock +1277 -4620
  114. metadata +63 -92
  115. data/docs/assets.md +0 -119
  116. data/docs/cloud9.md +0 -310
  117. data/docs/css.md +0 -308
  118. data/docs/docker.md +0 -68
  119. data/docs/engines.md +0 -213
  120. data/docs/env.md +0 -63
  121. data/docs/es6.md +0 -72
  122. data/docs/folder-structure.md +0 -66
  123. data/docs/integrations.md +0 -220
  124. data/docs/misc.md +0 -23
  125. data/docs/props.md +0 -223
  126. data/docs/target.md +0 -22
  127. data/docs/testing.md +0 -136
  128. data/docs/typescript.md +0 -190
  129. data/docs/v4-upgrade.md +0 -142
  130. data/docs/webpack-dev-server.md +0 -92
  131. data/docs/webpack.md +0 -364
  132. data/docs/yarn.md +0 -23
  133. data/lib/install/angular.rb +0 -23
  134. data/lib/install/coffee.rb +0 -25
  135. data/lib/install/config/.browserslistrc +0 -1
  136. data/lib/install/config/babel.config.js +0 -82
  137. data/lib/install/config/postcss.config.js +0 -12
  138. data/lib/install/config/webpack/environment.js +0 -3
  139. data/lib/install/elm.rb +0 -39
  140. data/lib/install/erb.rb +0 -25
  141. data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
  142. data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
  143. data/lib/install/examples/angular/hello_angular/index.ts +0 -8
  144. data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
  145. data/lib/install/examples/angular/hello_angular.js +0 -7
  146. data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
  147. data/lib/install/examples/elm/Main.elm +0 -55
  148. data/lib/install/examples/elm/hello_elm.js +0 -16
  149. data/lib/install/examples/erb/hello_erb.js.erb +0 -6
  150. data/lib/install/examples/react/babel.config.js +0 -99
  151. data/lib/install/examples/react/hello_react.jsx +0 -26
  152. data/lib/install/examples/react/tsconfig.json +0 -21
  153. data/lib/install/examples/stimulus/application.js +0 -1
  154. data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
  155. data/lib/install/examples/stimulus/controllers/index.js +0 -9
  156. data/lib/install/examples/svelte/app.svelte +0 -11
  157. data/lib/install/examples/svelte/hello_svelte.js +0 -20
  158. data/lib/install/examples/typescript/hello_typescript.ts +0 -4
  159. data/lib/install/examples/typescript/tsconfig.json +0 -24
  160. data/lib/install/examples/vue/app.vue +0 -22
  161. data/lib/install/examples/vue/hello_vue.js +0 -72
  162. data/lib/install/loaders/coffee.js +0 -6
  163. data/lib/install/loaders/elm.js +0 -25
  164. data/lib/install/loaders/erb.js +0 -11
  165. data/lib/install/loaders/svelte.js +0 -9
  166. data/lib/install/loaders/vue.js +0 -6
  167. data/lib/install/react.rb +0 -18
  168. data/lib/install/stimulus.rb +0 -12
  169. data/lib/install/svelte.rb +0 -29
  170. data/lib/install/typescript.rb +0 -39
  171. data/lib/install/vue.rb +0 -49
  172. data/lib/tasks/installers.rake +0 -42
  173. data/package/config_types/__tests__/config_list.js +0 -118
  174. data/package/config_types/__tests__/config_object.js +0 -43
  175. data/package/config_types/config_list.js +0 -75
  176. data/package/config_types/config_object.js +0 -55
  177. data/package/config_types/index.js +0 -7
  178. data/package/rules/module.css.js +0 -3
  179. data/package/rules/module.sass.js +0 -8
  180. data/package/rules/node_modules.js +0 -22
  181. data/package/utils/__tests__/deep_assign.js +0 -32
  182. data/package/utils/__tests__/deep_merge.js +0 -10
  183. data/package/utils/__tests__/get_style_rule.js +0 -65
  184. data/package/utils/__tests__/objectify.js +0 -9
  185. data/package/utils/deep_assign.js +0 -22
  186. data/package/utils/deep_merge.js +0 -22
  187. data/package/utils/objectify.js +0 -3
  188. /data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.css +0 -0
  189. /data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.js +0 -0
@@ -8,17 +8,19 @@ 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
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
 
@@ -27,6 +29,8 @@ module Webpacker
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
36
  $stdout.puts "webpack dev_server configuration not found in #{@config.config_path}[#{ENV["RAILS_ENV"]}]."
@@ -34,6 +38,21 @@ module 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
@@ -46,20 +65,25 @@ module Webpacker
46
65
  def execute_cmd
47
66
  env = Webpacker::Compiler.env
48
67
  env["WEBPACKER_CONFIG"] = @webpacker_config
68
+ env["WEBPACK_SERVE"] = "true"
49
69
 
50
70
  cmd = if node_modules_bin_exist?
51
- ["#{@node_modules_bin_path}/webpack-dev-server"]
71
+ ["#{@node_modules_bin_path}/webpack", "serve"]
52
72
  else
53
- ["yarn", "webpack-dev-server"]
73
+ ["yarn", "webpack", "serve"]
54
74
  end
55
75
 
56
76
  if @argv.include?("--debug-webpacker")
57
- cmd = [ "node", "--inspect-brk"] + cmd
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
@@ -12,15 +12,9 @@ module Webpacker::Helper
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
- path_to_asset(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.
@@ -29,15 +23,24 @@ module Webpacker::Helper
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
- url_to_asset(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.
@@ -69,43 +72,31 @@ module Webpacker::Helper
69
72
  favicon_link_tag(resolve_path_to_image(name), options)
70
73
  end
71
74
 
72
- # Creates a script tag that references the named pack file, as compiled by webpack per the entries list
73
- # in package/environments/base.js. By default, this list is auto-generated to match everything in
74
- # app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
75
- #
76
- # Example:
77
- #
78
- # <%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
79
- # <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
80
- def javascript_pack_tag(*names, **options)
81
- javascript_include_tag(*sources_from_manifest_entries(names, type: :javascript), **options)
82
- end
83
-
84
75
  # Creates script tags that reference the js chunks from entrypoints when using split chunks API,
85
76
  # as compiled by webpack per the entries list in package/environments/base.js.
86
77
  # By default, this list is auto-generated to match everything in
87
- # 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.
88
79
  # See: https://webpack.js.org/plugins/split-chunks-plugin/
89
80
  #
90
81
  # Example:
91
82
  #
92
- # <%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %> # =>
93
- # <script src="/packs/vendor-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
94
- # <script src="/packs/calendar~runtime-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
95
- # <script src="/packs/calendar-1016838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
96
- # <script src="/packs/map~runtime-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
97
- # <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>
98
89
  #
99
90
  # DO:
100
91
  #
101
- # <%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
92
+ # <%= javascript_pack_tag 'calendar', 'map' %>
102
93
  #
103
94
  # DON'T:
104
95
  #
105
- # <%= javascript_packs_with_chunks_tag 'calendar' %>
106
- # <%= javascript_packs_with_chunks_tag 'map' %>
107
- def javascript_packs_with_chunks_tag(*names, **options)
108
- javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), **options)
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 })
109
100
  end
110
101
 
111
102
  # Creates a link tag, for preloading, that references a given Webpacker asset.
@@ -124,72 +115,47 @@ module Webpacker::Helper
124
115
  end
125
116
  end
126
117
 
127
- # Creates a link tag that references the named pack file, as compiled by webpack per the entries list
128
- # in package/environments/base.js. By default, this list is auto-generated to match everything in
129
- # app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
130
- #
131
- # Note: If the development server is running and hot module replacement is active, this will return nothing.
132
- # In that setup you need to configure your styles to be inlined in your JavaScript for hot reloading.
133
- #
134
- # Examples:
135
- #
136
- # # When extract_css is false in webpacker.yml:
137
- # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
138
- # nil
139
- #
140
- # # When extract_css is true in webpacker.yml:
141
- # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
142
- # <link rel="stylesheet" media="screen" href="/packs/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />
143
- def stylesheet_pack_tag(*names, **options)
144
- if current_webpacker_instance.config.extract_css?
145
- stylesheet_link_tag(*sources_from_manifest_entries(names, type: :stylesheet), **options)
146
- end
147
- end
148
-
149
118
  # Creates link tags that reference the css chunks from entrypoints when using split chunks API,
150
119
  # as compiled by webpack per the entries list in package/environments/base.js.
151
120
  # By default, this list is auto-generated to match everything in
152
- # 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.
153
122
  # See: https://webpack.js.org/plugins/split-chunks-plugin/
154
123
  #
155
124
  # Examples:
156
125
  #
157
- # <%= stylesheet_packs_with_chunks_tag 'calendar', 'map' %> # =>
126
+ # <%= stylesheet_pack_tag 'calendar', 'map' %> # =>
158
127
  # <link rel="stylesheet" media="screen" href="/packs/3-8c7ce31a.chunk.css" />
159
128
  # <link rel="stylesheet" media="screen" href="/packs/calendar-8c7ce31a.chunk.css" />
160
129
  # <link rel="stylesheet" media="screen" href="/packs/map-8c7ce31a.chunk.css" />
161
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
+ #
162
135
  # DO:
163
136
  #
164
- # <%= stylesheet_packs_with_chunks_tag 'calendar', 'map' %>
137
+ # <%= stylesheet_pack_tag 'calendar', 'map' %>
165
138
  #
166
139
  # DON'T:
167
140
  #
168
- # <%= stylesheet_packs_with_chunks_tag 'calendar' %>
169
- # <%= stylesheet_packs_with_chunks_tag 'map' %>
170
- def stylesheet_packs_with_chunks_tag(*names, **options)
171
- if current_webpacker_instance.config.extract_css?
172
- stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
173
- end
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)
174
147
  end
175
148
 
176
149
  private
177
- def stylesheet?(name)
178
- File.extname(name) == ".css"
179
- end
180
-
181
- def sources_from_manifest_entries(names, type:)
182
- names.map { |name| current_webpacker_instance.manifest.lookup!(name, type: type) }.flatten
183
- end
184
150
 
185
151
  def sources_from_manifest_entrypoints(names, type:)
186
- 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
187
153
  end
188
154
 
189
- def resolve_path_to_image(name)
190
- path = name.starts_with?("media/images/") ? name : "media/images/#{name}"
191
- path_to_asset(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)
192
158
  rescue
193
- path_to_asset(current_webpacker_instance.manifest.lookup!(name))
159
+ path_to_asset(current_webpacker_instance.manifest.lookup!(name), options)
194
160
  end
195
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,7 +23,7 @@ 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
@@ -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)
@@ -8,8 +8,7 @@ class Webpacker::Engine < ::Rails::Engine
8
8
  config.webpacker = ActiveSupport::OrderedOptions.new
9
9
 
10
10
  initializer "webpacker.proxy" do |app|
11
- insert_middleware = Webpacker.config.dev_server.present? rescue nil
12
- if insert_middleware
11
+ if (Webpacker.config.dev_server.present? rescue nil)
13
12
  app.middleware.insert_before 0,
14
13
  Rails::VERSION::MAJOR >= 5 ?
15
14
  Webpacker::DevServerProxy : "Webpacker::DevServerProxy", ssl_verify_none: true
@@ -52,4 +51,11 @@ class Webpacker::Engine < ::Rails::Engine
52
51
  app.config.javascript_path = Webpacker.config.source_path.relative_path_from(Rails.root.join("app")).to_s
53
52
  end
54
53
  end
54
+
55
+ initializer "webpacker.remove_app_packs_from_the_autoload_paths" do
56
+ Rails.application.config.before_initialize do
57
+ source_path = Webpacker.config.source_path.to_s
58
+ ActiveSupport::Dependencies.autoload_paths.delete(source_path)
59
+ end
60
+ end
55
61
  end
@@ -12,7 +12,7 @@ module Webpacker
12
12
  @app_path = File.expand_path(".", Dir.pwd)
13
13
  @node_modules_bin_path = ENV["WEBPACKER_NODE_MODULES_BIN_PATH"] || `yarn bin`.chomp
14
14
  @webpack_config = File.join(@app_path, "config/webpack/#{ENV["NODE_ENV"]}.js")
15
- @webpacker_config = File.join(@app_path, "config/webpacker.yml")
15
+ @webpacker_config = ENV["WEBPACKER_CONFIG"] || File.join(@app_path, "config/webpacker.yml")
16
16
 
17
17
  unless File.exist?(@webpack_config)
18
18
  $stderr.puts "webpack config #{@webpack_config} not found, please run 'bundle exec rails webpacker:install' to install Webpacker with default configs or add the missing config file for your custom environment."
@@ -1,4 +1,4 @@
1
1
  module Webpacker
2
2
  # Change the version in package.json too, please!
3
- VERSION = "5.4.4".freeze
3
+ VERSION = "6.0.0.rc.6".freeze
4
4
  end
@@ -3,6 +3,19 @@ require "webpacker/runner"
3
3
 
4
4
  module Webpacker
5
5
  class WebpackRunner < Webpacker::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
+
6
19
  def run
7
20
  env = Webpacker::Compiler.env
8
21
  env["WEBPACKER_CONFIG"] = @webpacker_config
@@ -13,11 +26,24 @@ module Webpacker
13
26
  ["yarn", "webpack"]
14
27
  end
15
28
 
16
- if @argv.include?("--debug-webpacker")
17
- cmd = [ "node", "--inspect-brk"] + cmd
29
+ if @argv.delete "--debug-webpacker"
30
+ cmd = ["node", "--inspect-brk"] + cmd
31
+ end
32
+
33
+ if @argv.delete "--trace-deprecation"
34
+ cmd = ["node", "--trace-deprecation"] + cmd
35
+ end
36
+
37
+ if @argv.delete "--no-deprecation"
38
+ cmd = ["node", "--no-deprecation"] + cmd
39
+ end
40
+
41
+ # Webpack commands are not compatible with --config option.
42
+ if (@argv & WEBPACK_COMMANDS).empty?
43
+ cmd += ["--config", @webpack_config]
18
44
  end
19
45
 
20
- cmd += ["--config", @webpack_config] + @argv
46
+ cmd += @argv
21
47
 
22
48
  Dir.chdir(@app_path) do
23
49
  Kernel.exec env, *cmd
data/lib/webpacker.rb CHANGED
@@ -30,7 +30,7 @@ module Webpacker
30
30
  Webpacker.logger = old_logger
31
31
  end
32
32
 
33
- delegate :logger, :logger=, :env, to: :instance
33
+ delegate :logger, :logger=, :env, :inlining_css?, to: :instance
34
34
  delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance
35
35
  delegate :bootstrap, :clean, :clobber, :compile, to: :commands
36
36
  end
@@ -24,43 +24,11 @@ describe('Config', () => {
24
24
  })
25
25
 
26
26
  test('should return additional paths as listed in app config, with resolved paths', () => {
27
- expect(config.additional_paths).toEqual(
28
- [
29
- 'app/assets',
30
- '/etc/yarn',
31
- 'app/elm'
32
- ]
33
- )
34
- })
35
-
36
- test('should return extensions as listed in app config', () => {
37
- expect(config.extensions).toEqual([
38
- '.mjs',
39
- '.js',
40
- '.sass',
41
- '.scss',
42
- '.css',
43
- '.module.sass',
44
- '.module.scss',
45
- '.module.css',
46
- '.png',
47
- '.svg',
48
- '.gif',
49
- '.jpeg',
50
- '.jpg',
51
- '.elm'
52
- ])
53
- })
54
-
55
- test('should return static assets extensions as listed in app config', () => {
56
- expect(config.static_assets_extensions).toEqual([
57
- '.jpg',
58
- '.jpeg',
59
- '.png',
60
- '.gif',
61
- '.tiff',
62
- '.ico',
63
- '.svg',
27
+ expect(config.additional_paths).toEqual([
28
+ 'app/assets',
29
+ '/etc/yarn',
30
+ 'some.config.js',
31
+ 'app/elm'
64
32
  ])
65
33
  })
66
34
  })
@@ -8,36 +8,28 @@ chdirTestApp()
8
8
  describe('Development environment', () => {
9
9
  afterAll(chdirCwd)
10
10
 
11
- describe('toWebpackConfig', () => {
11
+ describe('webpackConfig', () => {
12
12
  beforeEach(() => jest.resetModules())
13
13
 
14
- test('should use development config and environment including devServer if WEBPACK_DEV_SERVER', () => {
14
+ test('should use development config and environment including devServer if WEBPACK_SERVE', () => {
15
15
  process.env.RAILS_ENV = 'development'
16
16
  process.env.NODE_ENV = 'development'
17
- process.env.WEBPACK_DEV_SERVER = 'YES'
18
- const { environment } = require('../index')
19
-
20
- const config = environment.toWebpackConfig()
21
- expect(config.output.path).toEqual(resolve('public', 'packs'))
22
- expect(config.output.publicPath).toEqual('/packs/')
23
- expect(config).toMatchObject({
24
- devServer: {
25
- host: 'localhost',
26
- port: 3035
27
- }
28
- })
17
+ process.env.WEBPACK_SERVE = 'true'
18
+ const { webpackConfig } = require('../index')
19
+
20
+ expect(webpackConfig.output.path).toEqual(resolve('public', 'packs'))
21
+ expect(webpackConfig.output.publicPath).toEqual('/packs/')
29
22
  })
30
23
 
31
- test('should use development config and environment if WEBPACK_DEV_SERVER', () => {
24
+ test('should use development config and environment if WEBPACK_SERVE', () => {
32
25
  process.env.RAILS_ENV = 'development'
33
26
  process.env.NODE_ENV = 'development'
34
- process.env.WEBPACK_DEV_SERVER = undefined
35
- const { environment } = require('../index')
27
+ process.env.WEBPACK_SERVE = undefined
28
+ const { webpackConfig } = require('../index')
36
29
 
37
- const config = environment.toWebpackConfig()
38
- expect(config.output.path).toEqual(resolve('public', 'packs'))
39
- expect(config.output.publicPath).toEqual('/packs/')
40
- expect(config.devServer).toEqual(undefined)
30
+ expect(webpackConfig.output.path).toEqual(resolve('public', 'packs'))
31
+ expect(webpackConfig.output.publicPath).toEqual('/packs/')
32
+ expect(webpackConfig.devServer).toEqual(undefined)
41
33
  })
42
34
  })
43
35
  })
@@ -13,7 +13,10 @@ describe('Env', () => {
13
13
  process.env.NODE_ENV = 'development'
14
14
  expect(require('../env')).toEqual({
15
15
  railsEnv: 'development',
16
- nodeEnv: 'development'
16
+ nodeEnv: 'development',
17
+ isProduction: false,
18
+ isDevelopment: true,
19
+ runningWebpackDevServer: false
17
20
  })
18
21
  })
19
22
 
@@ -22,7 +25,10 @@ describe('Env', () => {
22
25
  delete process.env.NODE_ENV
23
26
  expect(require('../env')).toEqual({
24
27
  railsEnv: 'development',
25
- nodeEnv: 'production'
28
+ nodeEnv: 'production',
29
+ isProduction: true,
30
+ isDevelopment: false,
31
+ runningWebpackDevServer: false
26
32
  })
27
33
  })
28
34
 
@@ -31,7 +37,10 @@ describe('Env', () => {
31
37
  delete process.env.RAILS_ENV
32
38
  expect(require('../env')).toEqual({
33
39
  railsEnv: 'production',
34
- nodeEnv: 'production'
40
+ nodeEnv: 'production',
41
+ isProduction: true,
42
+ isDevelopment: false,
43
+ runningWebpackDevServer: false
35
44
  })
36
45
  })
37
46
 
@@ -40,7 +49,10 @@ describe('Env', () => {
40
49
  process.env.NODE_ENV = 'staging'
41
50
  expect(require('../env')).toEqual({
42
51
  railsEnv: 'staging',
43
- nodeEnv: 'production'
52
+ nodeEnv: 'production',
53
+ isProduction: true,
54
+ isDevelopment: false,
55
+ runningWebpackDevServer: false
44
56
  })
45
57
  })
46
58
  })
@@ -0,0 +1,9 @@
1
+ const index = require('../index')
2
+
3
+ describe('index', () => {
4
+ test('exports webpack-merge v5 functions', () => {
5
+ expect(index.merge).toBeInstanceOf(Function)
6
+ expect(index.mergeWithRules).toBeInstanceOf(Function)
7
+ expect(index.mergeWithCustomize).toBeInstanceOf(Function)
8
+ })
9
+ })
@@ -8,19 +8,19 @@ chdirTestApp()
8
8
  describe('Production environment', () => {
9
9
  afterAll(chdirCwd)
10
10
 
11
- describe('toWebpackConfig', () => {
11
+ describe('webpackConfig', () => {
12
12
  beforeEach(() => jest.resetModules())
13
13
 
14
14
  test('should use production config and environment', () => {
15
15
  process.env.RAILS_ENV = 'production'
16
16
  process.env.NODE_ENV = 'production'
17
17
 
18
- const { environment } = require('../index')
19
- const config = environment.toWebpackConfig()
18
+ const { webpackConfig } = require('../index')
20
19
 
21
- expect(config.output.path).toEqual(resolve('public', 'packs'))
22
- expect(config.output.publicPath).toEqual('/packs/')
23
- expect(config).toMatchObject({
20
+ expect(webpackConfig.output.path).toEqual(resolve('public', 'packs'))
21
+ expect(webpackConfig.output.publicPath).toEqual('/packs/')
22
+
23
+ expect(webpackConfig).toMatchObject({
24
24
  devtool: 'source-map',
25
25
  stats: 'normal'
26
26
  })
@@ -8,19 +8,20 @@ chdirTestApp()
8
8
  describe('Custom environment', () => {
9
9
  afterAll(chdirCwd)
10
10
 
11
- describe('toWebpackConfig', () => {
11
+ describe('webpackConfig', () => {
12
12
  beforeEach(() => jest.resetModules())
13
13
 
14
14
  test('should use staging config and default production environment', () => {
15
15
  process.env.RAILS_ENV = 'staging'
16
16
  delete process.env.NODE_ENV
17
17
 
18
- const { environment } = require('../index')
19
- const config = environment.toWebpackConfig()
18
+ const { webpackConfig } = require('../index')
20
19
 
21
- expect(config.output.path).toEqual(resolve('public', 'packs-staging'))
22
- expect(config.output.publicPath).toEqual('/packs-staging/')
23
- expect(config).toMatchObject({
20
+ expect(webpackConfig.output.path).toEqual(
21
+ resolve('public', 'packs-staging')
22
+ )
23
+ expect(webpackConfig.output.publicPath).toEqual('/packs-staging/')
24
+ expect(webpackConfig).toMatchObject({
24
25
  devtool: 'source-map',
25
26
  stats: 'normal'
26
27
  })
@@ -15,12 +15,11 @@ describe('Test environment', () => {
15
15
  process.env.RAILS_ENV = 'test'
16
16
  process.env.NODE_ENV = 'test'
17
17
 
18
- const { environment } = require('../index')
19
- const config = environment.toWebpackConfig()
18
+ const { webpackConfig } = require('../index')
20
19
 
21
- expect(config.output.path).toEqual(resolve('public', 'packs-test'))
22
- expect(config.output.publicPath).toEqual('/packs-test/')
23
- expect(config.devServer).toEqual(undefined)
20
+ expect(webpackConfig.output.path).toEqual(resolve('public', 'packs-test'))
21
+ expect(webpackConfig.output.publicPath).toEqual('/packs-test/')
22
+ expect(webpackConfig.devServer).toEqual(undefined)
24
23
  })
25
24
  })
26
25
  })