webpacker 6.0.0.beta → 6.0.0.beta.5

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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/jest.yml +1 -1
  3. data/.github/workflows/js-lint.yml +1 -1
  4. data/.github/workflows/ruby.yml +9 -6
  5. data/6_0_upgrade.md +29 -10
  6. data/CHANGELOG.md +2 -0
  7. data/CONTRIBUTING.md +1 -1
  8. data/Gemfile.lock +7 -5
  9. data/README.md +115 -52
  10. data/config/README.md +3 -0
  11. data/config/webpacker.yml +1 -0
  12. data/docs/troubleshooting.md +160 -0
  13. data/lib/install/config/webpacker.yml +4 -2
  14. data/lib/install/{javascript/packs → packs/entrypoints}/application.js +2 -2
  15. data/lib/install/template.rb +3 -3
  16. data/lib/webpacker/commands.rb +2 -1
  17. data/lib/webpacker/compiler.rb +2 -2
  18. data/lib/webpacker/dev_server_runner.rb +2 -0
  19. data/lib/webpacker/helper.rb +13 -43
  20. data/lib/webpacker/version.rb +1 -1
  21. data/lib/webpacker/webpack_runner.rb +1 -0
  22. data/package.json +1 -1
  23. data/package/__tests__/development.js +2 -1
  24. data/package/__tests__/index.js +9 -0
  25. data/package/environments/__tests__/base.js +4 -4
  26. data/package/environments/base.js +7 -4
  27. data/package/environments/development.js +1 -0
  28. data/package/environments/production.js +29 -22
  29. data/package/index.js +3 -3
  30. data/package/rules/file.js +5 -3
  31. data/package/rules/index.js +3 -2
  32. data/package/rules/raw.js +5 -0
  33. data/package/rules/stylus.js +26 -0
  34. data/package/utils/helpers.js +4 -2
  35. data/test/configuration_test.rb +2 -2
  36. data/test/dev_server_runner_test.rb +10 -2
  37. data/test/helper_test.rb +33 -39
  38. data/test/mounted_app/test/dummy/config/webpacker.yml +2 -2
  39. data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
  40. data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.css +0 -0
  41. data/test/test_app/app/{javascript/packs → packs/entrypoints}/multi_entry.js +0 -0
  42. data/test/test_app/config/webpacker.yml +2 -2
  43. data/test/test_app/public/packs/manifest.json +7 -0
  44. metadata +17 -13
  45. data/lib/install/javascript/packs/application.css +0 -9
  46. data/package/rules/svg.js +0 -20
data/config/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Note
2
+
3
+ This directory exists for Jest specs that execute code expecting the Rails config directory at this path.
@@ -0,0 +1 @@
1
+ config/lib/install/config/webpacker.yml
@@ -0,0 +1,160 @@
1
+ # Troubleshooting
2
+
3
+ ## Debugging your webpack config
4
+
5
+ 1. Read the error message carefully. The error message will tell you the precise key value
6
+ that is not matching what Webpack expects.
7
+ 2. Put a `debugger` statement in your Webpack configuration and run `bin/webpack --debug-webpacker`.
8
+ If you have a node debugger installed, you'll see the Chrome debugger for your webpack
9
+ config. For example, install the Chrome extension
10
+ [NiM](https://chrome.google.com/webstore/detail/nodejs-v8-inspector-manag/gnhhdgbaldcilmgcpfddgdbkhjohddkj) and
11
+ set the option for the dev tools to open automatically. Or put `chrome://inspect` in the URL bar.
12
+ For more details on debugging, see the official
13
+ [Webpack docs on debugging](https://webpack.js.org/contribute/debugging/#devtools)
14
+ 3. Any arguments that you add to bin/webpack get sent to webpack. For example, you can pass `--debug` to switch loaders to debug mode. See [webpack CLI debug options](https://webpack.js.org/api/cli/#debug-options) for more information on the available options.
15
+ 4. You can also pass additional options to the command to run the webpack-dev-server and start the webpack-dev-server with the option `--debug-webpacker`
16
+
17
+ ## ENOENT: no such file or directory - node-sass
18
+
19
+ If you get the error `ENOENT: no such file or directory - node-sass` on deploy with
20
+ `assets:precompile` or `bundle exec rails webpacker:compile` you may need to
21
+ move Sass to production `dependencies`.
22
+
23
+ Move any packages that related to Sass (e.g. `node-sass` or `sass-loader`) from
24
+ `devDependencies` to `dependencies` in `package.json`. This is because
25
+ webpacker is running on a production system with the Rails workflow to build
26
+ the assets. Particularly on hosting providers that try to detect and do the right
27
+ thing, like Heroku.
28
+
29
+ However, if you get this on local development, or not during a deploy then you
30
+ may need to rebuild `node-sass`. It's a bit of a weird error; basically, it
31
+ can't find the `node-sass` binary. An easy solution is to create a postinstall
32
+ hook to ensure `node-sass` is rebuilt whenever new modules are installed.
33
+
34
+ In `package.json`:
35
+
36
+ ```json
37
+ "scripts": {
38
+ "postinstall": "npm rebuild node-sass"
39
+ }
40
+ ```
41
+
42
+ ## Can't find hello_react.js in manifest.json
43
+
44
+ * If you get this error `Can't find hello_react.js in manifest.json`
45
+ when loading a view in the browser it's because webpack is still compiling packs.
46
+ Webpacker uses a `manifest.json` file to keep track of packs in all environments,
47
+ however since this file is generated after packs are compiled by webpack. So,
48
+ if you load a view in browser whilst webpack is compiling you will get this error.
49
+ Therefore, make sure webpack
50
+ (i.e `./bin/webpack-dev-server`) is running and has
51
+ completed the compilation successfully before loading a view.
52
+
53
+
54
+ ## throw er; // Unhandled 'error' event
55
+
56
+ * If you get this error while trying to use Elm, try rebuilding Elm. You can do
57
+ so with a postinstall hook in your `package.json`:
58
+
59
+ ```
60
+ "scripts": {
61
+ "postinstall": "npm rebuild elm"
62
+ }
63
+ ```
64
+
65
+
66
+ ## webpack or webpack-dev-server not found
67
+
68
+ * This could happen if `webpacker:install` step is skipped. Please run `bundle exec rails webpacker:install` to fix the issue.
69
+
70
+ * If you encounter the above error on heroku after upgrading from Rails 4.x to 5.1.x, then the problem might be related to missing `yarn` binstub. Please run following commands to update/add binstubs:
71
+
72
+ ```bash
73
+ bundle config --delete bin
74
+ ./bin/rails app:update:bin # or rails app:update:bin
75
+ ```
76
+
77
+
78
+ ## Running webpack on Windows
79
+
80
+ If you are running webpack on Windows, your command shell may not be able to interpret the preferred interpreter
81
+ for the scripts generated in `bin/webpack` and `bin/webpack-dev-server`. Instead you'll want to run the scripts
82
+ manually with Ruby:
83
+
84
+ ```
85
+ C:\path>ruby bin\webpack
86
+ C:\path>ruby bin\webpack-dev-server
87
+ ```
88
+
89
+
90
+ ## Invalid configuration object. webpack has been initialised using a configuration object that does not match the API schema.
91
+
92
+ If you receive this error when running `$ ./bin/webpack-dev-server` ensure your configuration is correct; most likely the path to your "packs" folder is incorrect if you modified from the original "source_path" defined in `config/webpacker.yml`.
93
+
94
+ ## Running Elm on Continuous Integration (CI) services such as CircleCI, CodeShip, Travis CI
95
+
96
+ If your tests are timing out or erroring on CI it is likely that you are experiencing the slow Elm compilation issue described here: [elm-compiler issue #1473](https://github.com/elm-lang/elm-compiler/issues/1473)
97
+
98
+ The issue is related to CPU count exposed by the underlying service. The basic solution involves using [libsysconfcpus](https://github.com/obmarg/libsysconfcpus) to change the reported CPU count.
99
+
100
+ Basic fix involves:
101
+
102
+ ```bash
103
+ # install sysconfcpus on CI
104
+ git clone https://github.com/obmarg/libsysconfcpus.git $HOME/dependencies/libsysconfcpus
105
+ cd libsysconfcpus
106
+ .configure --prefix=$HOME/dependencies/sysconfcpus
107
+ make && make install
108
+
109
+ # use sysconfcpus with elm-make
110
+ mv $HOME/your_rails_app/node_modules/.bin/elm-make $HOME/your_rails_app/node_modules/.bin/elm-make-old
111
+ printf "#\041/bin/bash\n\necho \"Running elm-make with sysconfcpus -n 2\"\n\n$HOME/dependencies/sysconfcpus/bin/sysconfcpus -n 2 $HOME/your_rails_app/node_modules/.bin/elm-make-old \"\$@\"" > $HOME/your_rails_app/node_modules/.bin/elm-make
112
+ chmod +x $HOME/your_rails_app/node_modules/.bin/elm-make
113
+ ```
114
+
115
+ ## Rake assets:precompile fails. ExecJS::RuntimeError
116
+ This error occurs because you are trying to minify by terser a pack that's already been minified by Webpacker. To avoid this conflict and prevent appearing of ExecJS::RuntimeError error, you will need to disable uglifier from Rails config:
117
+
118
+ ```ruby
119
+ // production.rb
120
+ # From
121
+
122
+ Rails.application.config.assets.js_compressor = :uglifier
123
+
124
+ # To
125
+
126
+ Rails.application.config.assets.js_compressor = Uglifier.new(harmony: true)
127
+
128
+ ```
129
+
130
+ ### Angular: WARNING in ./node_modules/@angular/core/esm5/core.js, Critical dependency: the request of a dependency is an expression
131
+
132
+ To silent these warnings, please update `config/webpack/environment.js`
133
+
134
+ ```js
135
+ // environment.js
136
+ const webpack = require('webpack')
137
+ const { resolve } = require('path')
138
+ const { environment, config } = require('@rails/webpacker')
139
+
140
+ environment.plugins.append('ContextReplacement',
141
+ new webpack.ContextReplacementPlugin(
142
+ /angular(\\|\/)core(\\|\/)(@angular|esm5)/,
143
+ resolve(config.source_path)
144
+ )
145
+ )
146
+ ```
147
+
148
+ ### Compilation Fails Silently
149
+
150
+ If compiling is not producing output files and there are no error messages to help troubleshoot. Setting the webpack_compile_output configuration variable to 'true' in webpacker.yml may add some helpful error information to your log file (Rails log/development.log or log/production.log)
151
+
152
+ ```yml
153
+ # webpacker.yml
154
+ default: &default
155
+ source_path: app/javascript
156
+ source_entry_path: packs
157
+ public_root_path: public
158
+ public_output_path: complaints_packs
159
+ webpack_compile_output: true
160
+ ```
@@ -1,8 +1,8 @@
1
1
  # Note: You must restart bin/webpack-dev-server for changes to take effect
2
2
 
3
3
  default: &default
4
- source_path: app/javascript
5
- source_entry_path: packs
4
+ source_path: app/packs
5
+ source_entry_path: entrypoints
6
6
  public_root_path: public
7
7
  public_output_path: packs
8
8
  cache_path: tmp/cache/webpacker
@@ -25,6 +25,8 @@ development:
25
25
  host: localhost
26
26
  port: 3035
27
27
  public: localhost:3035
28
+ # Inject browserside javascript that required by both HMR and Live(full) reload
29
+ inject_client: true
28
30
  # Hot Module Replacement updates modules while the application is running without a full reload
29
31
  hmr: false
30
32
  # Inline should be set to true if using HMR; it inserts a script to take care of live reloading
@@ -1,10 +1,10 @@
1
1
  /* eslint no-console:0 */
2
2
  // This file is automatically compiled by Webpack, along with any other files
3
3
  // present in this directory. You're encouraged to place your actual application logic in
4
- // a relevant structure within app/javascript and only use these pack files to reference
4
+ // a relevant structure within app/packs and only use these pack files to reference
5
5
  // that code so it'll be compiled.
6
6
  //
7
- // To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
7
+ // To reference this file, add <%= javascript_packs_with_chunks_tag 'application' %> to the appropriate
8
8
  // layout file, like app/views/layouts/application.html.erb
9
9
 
10
10
  // Uncomment to copy all static images under ../images to the output folder and reference
@@ -5,10 +5,10 @@ say "Copying webpack core config"
5
5
  directory "#{__dir__}/config/webpack", "config/webpack"
6
6
 
7
7
  if Dir.exists?(Webpacker.config.source_path)
8
- say "The JavaScript app source directory already exists"
8
+ say "The packs app source directory already exists"
9
9
  else
10
- say "Creating JavaScript app source directory"
11
- directory "#{__dir__}/javascript", Webpacker.config.source_path
10
+ say "Creating packs app source directory"
11
+ directory "#{__dir__}/packs", Webpacker.config.source_path
12
12
  end
13
13
 
14
14
  apply "#{__dir__}/binstubs.rb"
@@ -64,7 +64,8 @@ class Webpacker::Commands
64
64
 
65
65
  def current_version
66
66
  packs = manifest.refresh.values.map do |value|
67
- next if value.is_a?(Hash)
67
+ value = value["src"] if value.is_a?(Hash)
68
+ next unless value.is_a?(String)
68
69
 
69
70
  File.join(config.root_path, "public", "#{value}*")
70
71
  end.compact
@@ -35,7 +35,7 @@ class Webpacker::Compiler
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.
@@ -69,7 +69,7 @@ class Webpacker::Compiler
69
69
  bin_webpack_path = config.root_path.join("bin/webpack")
70
70
  first_line = File.readlines(bin_webpack_path).first.chomp
71
71
  /ruby/.match?(first_line) ? RbConfig.ruby : ""
72
- end
72
+ end
73
73
 
74
74
  def run_webpack
75
75
  logger.info "Compiling..."
@@ -64,6 +64,7 @@ module Webpacker
64
64
  def execute_cmd
65
65
  env = Webpacker::Compiler.env
66
66
  env["WEBPACKER_CONFIG"] = @webpacker_config
67
+ env["WEBPACK_DEV_SERVER"] = "true"
67
68
 
68
69
  cmd = if node_modules_bin_exist?
69
70
  ["#{@node_modules_bin_path}/webpack", "serve"]
@@ -73,6 +74,7 @@ module Webpacker
73
74
 
74
75
  if @argv.include?("--debug-webpacker")
75
76
  cmd = [ "node", "--inspect-brk"] + cmd
77
+ @argv.delete "--debug-webpacker"
76
78
  end
77
79
 
78
80
  cmd += ["--config", @webpack_config]
@@ -72,27 +72,15 @@ module Webpacker::Helper
72
72
  favicon_link_tag(resolve_path_to_image(name), options)
73
73
  end
74
74
 
75
- # Creates a script tag that references the named pack file, as compiled by webpack per the entries list
76
- # in package/environments/base.js. By default, this list is auto-generated to match everything in
77
- # app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
78
- #
79
- # Example:
80
- #
81
- # <%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
82
- # <script src="/packs/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>
83
- def javascript_pack_tag(*names, **options)
84
- javascript_include_tag(*sources_from_manifest_entries(names, type: :javascript), **options)
85
- end
86
-
87
75
  # Creates script tags that reference the js chunks from entrypoints when using split chunks API,
88
76
  # as compiled by webpack per the entries list in package/environments/base.js.
89
77
  # By default, this list is auto-generated to match everything in
90
- # 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.
91
79
  # See: https://webpack.js.org/plugins/split-chunks-plugin/
92
80
  #
93
81
  # Example:
94
82
  #
95
- # <%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %> # =>
83
+ # <%= javascript_pack_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %> # =>
96
84
  # <script src="/packs/vendor-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
97
85
  # <script src="/packs/calendar~runtime-16838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
98
86
  # <script src="/packs/calendar-1016838bab065ae1e314.chunk.js" data-turbolinks-track="reload"></script>
@@ -101,13 +89,13 @@ module Webpacker::Helper
101
89
  #
102
90
  # DO:
103
91
  #
104
- # <%= javascript_packs_with_chunks_tag 'calendar', 'map' %>
92
+ # <%= javascript_pack_tag 'calendar', 'map' %>
105
93
  #
106
94
  # DON'T:
107
95
  #
108
- # <%= javascript_packs_with_chunks_tag 'calendar' %>
109
- # <%= javascript_packs_with_chunks_tag 'map' %>
110
- def javascript_packs_with_chunks_tag(*names, **options)
96
+ # <%= javascript_pack_tag 'calendar' %>
97
+ # <%= javascript_pack_tag 'map' %>
98
+ def javascript_pack_tag(*names, **options)
111
99
  javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), **options)
112
100
  end
113
101
 
@@ -127,53 +115,35 @@ module Webpacker::Helper
127
115
  end
128
116
  end
129
117
 
130
- # Creates a link tag that references the named pack file, as compiled by webpack per the entries list
131
- # in package/environments/base.js. By default, this list is auto-generated to match everything in
132
- # app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
133
- #
134
- # Note: If the development server is running and hot module replacement is active, this will return nothing.
135
- # In that setup you need to configure your styles to be inlined in your JavaScript for hot reloading.
136
- #
137
- # Examples:
138
- #
139
- # <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
140
- # <link rel="stylesheet" media="screen" href="/packs/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />
141
- def stylesheet_pack_tag(*names, **options)
142
- stylesheet_link_tag(*sources_from_manifest_entries(names, type: :stylesheet), **options)
143
- end
144
-
145
118
  # Creates link tags that reference the css chunks from entrypoints when using split chunks API,
146
119
  # as compiled by webpack per the entries list in package/environments/base.js.
147
120
  # By default, this list is auto-generated to match everything in
148
- # 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.
149
122
  # See: https://webpack.js.org/plugins/split-chunks-plugin/
150
123
  #
151
124
  # Examples:
152
125
  #
153
- # <%= stylesheet_packs_with_chunks_tag 'calendar', 'map' %> # =>
126
+ # <%= stylesheet_pack_tag 'calendar', 'map' %> # =>
154
127
  # <link rel="stylesheet" media="screen" href="/packs/3-8c7ce31a.chunk.css" />
155
128
  # <link rel="stylesheet" media="screen" href="/packs/calendar-8c7ce31a.chunk.css" />
156
129
  # <link rel="stylesheet" media="screen" href="/packs/map-8c7ce31a.chunk.css" />
157
130
  #
158
131
  # DO:
159
132
  #
160
- # <%= stylesheet_packs_with_chunks_tag 'calendar', 'map' %>
133
+ # <%= stylesheet_pack_tag 'calendar', 'map' %>
161
134
  #
162
135
  # DON'T:
163
136
  #
164
- # <%= stylesheet_packs_with_chunks_tag 'calendar' %>
165
- # <%= stylesheet_packs_with_chunks_tag 'map' %>
166
- def stylesheet_packs_with_chunks_tag(*names, **options)
137
+ # <%= stylesheet_pack_tag 'calendar' %>
138
+ # <%= stylesheet_pack_tag 'map' %>
139
+ def stylesheet_pack_tag(*names, **options)
167
140
  stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
168
141
  end
169
142
 
170
143
  private
171
- def sources_from_manifest_entries(names, type:)
172
- names.map { |name| current_webpacker_instance.manifest.lookup!(name, type: type) }.flatten
173
- end
174
144
 
175
145
  def sources_from_manifest_entrypoints(names, type:)
176
- names.map { |name| current_webpacker_instance.manifest.lookup_pack_with_chunks!(name, type: type) }.flatten.uniq
146
+ names.map { |name| current_webpacker_instance.manifest.lookup_pack_with_chunks!(name.to_s, type: type) }.flatten.uniq
177
147
  end
178
148
 
179
149
  def resolve_path_to_image(name, **options)
@@ -1,4 +1,4 @@
1
1
  module Webpacker
2
2
  # Change the version in package.json too, please!
3
- VERSION = "6.0.0.beta".freeze
3
+ VERSION = "6.0.0.beta.5".freeze
4
4
  end
@@ -15,6 +15,7 @@ module Webpacker
15
15
 
16
16
  if @argv.include?("--debug-webpacker")
17
17
  cmd = [ "node", "--inspect-brk"] + cmd
18
+ @argv.delete "--debug-webpacker"
18
19
  end
19
20
 
20
21
  if @argv.include?("--trace-deprecation")
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rails/webpacker",
3
- "version": "6.0.0-beta",
3
+ "version": "6.0.0-beta.5",
4
4
  "description": "Use webpack to manage app-like JavaScript modules in Rails",
5
5
  "main": "package/index.js",
6
6
  "files": [
@@ -22,7 +22,8 @@ describe('Development environment', () => {
22
22
  expect(webpackConfig).toMatchObject({
23
23
  devServer: {
24
24
  host: 'localhost',
25
- port: 3035
25
+ port: 3035,
26
+ injectClient: true
26
27
  }
27
28
  })
28
29
  })
@@ -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
+ })
@@ -17,14 +17,14 @@ describe('Base config', () => {
17
17
  describe('config', () => {
18
18
  test('should return entry', () => {
19
19
  expect(baseConfig.entry.application).toEqual(
20
- resolve('app', 'javascript', 'packs', 'application.js')
20
+ resolve('app', 'packs', 'entrypoints', 'application.js')
21
21
  )
22
22
  })
23
23
 
24
24
  test('should return multi file entry points', () => {
25
25
  expect(baseConfig.entry.multi_entry.sort()).toEqual([
26
- resolve('app', 'javascript', 'packs', 'multi_entry.css'),
27
- resolve('app', 'javascript', 'packs', 'multi_entry.js')
26
+ resolve('app', 'packs', 'entrypoints', 'multi_entry.css'),
27
+ resolve('app', 'packs', 'entrypoints', 'multi_entry.js')
28
28
  ])
29
29
  })
30
30
 
@@ -53,7 +53,7 @@ describe('Base config', () => {
53
53
 
54
54
  test('should return default resolve.modules with additions', () => {
55
55
  expect(baseConfig.resolve.modules).toEqual([
56
- resolve('app', 'javascript'),
56
+ resolve('app', 'packs'),
57
57
  resolve('app/assets'),
58
58
  resolve('/etc/yarn'),
59
59
  resolve('some.config.js'),