webpacker 6.0.0.beta.7 → 6.0.0.rc.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/jest.yml +5 -2
  3. data/.github/workflows/js-lint.yml +4 -1
  4. data/.github/workflows/rubocop.yml +1 -1
  5. data/.github/workflows/ruby.yml +12 -12
  6. data/.node-version +1 -1
  7. data/.rubocop.yml +1 -0
  8. data/CHANGELOG.md +21 -10
  9. data/Gemfile.lock +83 -83
  10. data/README.md +160 -157
  11. data/config/webpacker.yml +1 -1
  12. data/docs/developing_webpacker.md +29 -0
  13. data/docs/troubleshooting.md +53 -23
  14. data/docs/v6_upgrade.md +31 -42
  15. data/gemfiles/Gemfile-rails-edge +1 -1
  16. data/gemfiles/Gemfile-rails.6.1.x +12 -0
  17. data/lib/install/bin/yarn +18 -0
  18. data/lib/install/config/webpacker.yml +14 -17
  19. data/lib/install/package.json +17 -0
  20. data/lib/install/packs/entrypoints/application.js +3 -4
  21. data/lib/install/template.rb +28 -15
  22. data/lib/tasks/webpacker/check_node.rake +3 -1
  23. data/lib/tasks/webpacker/check_yarn.rake +4 -2
  24. data/lib/tasks/webpacker/clobber.rake +1 -1
  25. data/lib/tasks/webpacker/verify_config.rake +14 -0
  26. data/lib/tasks/webpacker/verify_install.rake +1 -11
  27. data/lib/tasks/yarn.rake +36 -0
  28. data/lib/webpacker/commands.rb +19 -15
  29. data/lib/webpacker/configuration.rb +15 -4
  30. data/lib/webpacker/dev_server.rb +6 -0
  31. data/lib/webpacker/dev_server_runner.rb +6 -3
  32. data/lib/webpacker/env.rb +5 -1
  33. data/lib/webpacker/helper.rb +14 -8
  34. data/lib/webpacker/instance.rb +4 -0
  35. data/lib/webpacker/manifest.rb +1 -2
  36. data/lib/webpacker/railtie.rb +1 -2
  37. data/lib/webpacker/runner.rb +1 -1
  38. data/lib/webpacker/version.rb +1 -1
  39. data/lib/webpacker.rb +1 -1
  40. data/package/__tests__/development.js +4 -4
  41. data/package/__tests__/env.js +8 -4
  42. data/package/babel/preset.js +0 -1
  43. data/package/config.js +3 -3
  44. data/package/env.js +6 -3
  45. data/package/environments/__tests__/base.js +3 -3
  46. data/package/environments/base.js +12 -7
  47. data/package/environments/development.js +37 -33
  48. data/package/index.js +2 -0
  49. data/package/inliningCss.js +7 -0
  50. data/package/rules/file.js +1 -1
  51. data/package/rules/sass.js +1 -2
  52. data/package/utils/get_style_rule.js +4 -2
  53. data/package.json +25 -29
  54. data/test/command_test.rb +76 -0
  55. data/test/configuration_test.rb +1 -1
  56. data/test/dev_server_runner_test.rb +5 -2
  57. data/test/helper_test.rb +48 -34
  58. data/test/manifest_test.rb +10 -2
  59. data/test/mounted_app/test/dummy/config/webpacker.yml +1 -1
  60. data/test/test_app/config/webpacker.yml +1 -3
  61. data/test/test_app/config/webpacker_other_location.yml +79 -0
  62. data/test/test_app/public/packs/manifest.json +12 -5
  63. data/test/webpacker_test.rb +17 -0
  64. data/webpacker.gemspec +1 -1
  65. data/yarn.lock +2202 -2680
  66. metadata +18 -9
data/config/webpacker.yml CHANGED
@@ -1 +1 @@
1
- config/lib/install/config/webpacker.yml
1
+ ../lib/install/config/webpacker.yml
@@ -0,0 +1,29 @@
1
+ # Developing Webpacker
2
+
3
+ It's a little trickier for Rails developers to work on the JS code of a project like rails/webpacker. So here are some tips!
4
+
5
+ ## Use some test app
6
+ For example, for React on Rails Changes, I'm using [shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh).
7
+ This directory is the `TEST_APP_DIR`.
8
+
9
+ ## Fork rails/webpacker
10
+ Let's call the rails/webpacker directory `WEBPACKER_DIR` which has rails/webpacker's `package.json`.
11
+
12
+ ## Changing the Package
13
+ ### Setup with Yalc
14
+ Use [`yalc`](https://github.com/wclr/yalc) unless you like yak shaving weird errors.
15
+ 1. In `WEBPACKER_DIR`, run `yalc publish`
16
+ 2. In `TEST_APP_DIR`, run `yalc link @rails/webpacker`
17
+
18
+ ## Update the Package Code
19
+ 1. Make some JS change in WEBPACKER_DIR
20
+ 2. Run `yalc push` and your changes will be pushed to your `TEST_APP_DIR`'s node_modules.
21
+ 3. You may need to run `yarn` in `TEST_APP_DIR` if you added or removed dependencies of rails/webpacker.
22
+
23
+ ## Updating the Ruby Code
24
+
25
+ For the Ruby part, just change the gem reference `TEST_APP_DIR`, like:
26
+
27
+ ```ruby
28
+ gem "webpacker", path: "../../forks/webpacker"
29
+ ```
@@ -4,14 +4,17 @@
4
4
 
5
5
  1. Read the error message carefully. The error message will tell you the precise key value
6
6
  that is not matching what Webpack expects.
7
+
7
8
  2. Put a `debugger` statement in your Webpack configuration and run `bin/webpack --debug-webpacker`.
8
9
  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
+ config. For example, install the Chrome extension
10
11
  [NiM](https://chrome.google.com/webstore/detail/nodejs-v8-inspector-manag/gnhhdgbaldcilmgcpfddgdbkhjohddkj) and
11
12
  set the option for the dev tools to open automatically. Or put `chrome://inspect` in the URL bar.
12
13
  For more details on debugging, see the official
13
14
  [Webpack docs on debugging](https://webpack.js.org/contribute/debugging/#devtools)
15
+
14
16
  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.
17
+
15
18
  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
19
 
17
20
  ## ENOENT: no such file or directory - node-sass
@@ -56,16 +59,15 @@ completed the compilation successfully before loading a view.
56
59
  * If you get this error while trying to use Elm, try rebuilding Elm. You can do
57
60
  so with a postinstall hook in your `package.json`:
58
61
 
59
- ```
62
+ ```json
60
63
  "scripts": {
61
64
  "postinstall": "npm rebuild elm"
62
65
  }
63
66
  ```
64
67
 
65
-
66
68
  ## webpack or webpack-dev-server not found
67
69
 
68
- * This could happen if `webpacker:install` step is skipped. Please run `bundle exec rails webpacker:install` to fix the issue.
70
+ * This could happen if `webpacker:install` step is skipped. Please run `bundle exec rails webpacker:install` to fix the issue.
69
71
 
70
72
  * 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
73
 
@@ -74,7 +76,6 @@ bundle config --delete bin
74
76
  ./bin/rails app:update:bin # or rails app:update:bin
75
77
  ```
76
78
 
77
-
78
79
  ## Running webpack on Windows
79
80
 
80
81
  If you are running webpack on Windows, your command shell may not be able to interpret the preferred interpreter
@@ -86,7 +87,6 @@ C:\path>ruby bin\webpack
86
87
  C:\path>ruby bin\webpack-dev-server
87
88
  ```
88
89
 
89
-
90
90
  ## Invalid configuration object. webpack has been initialised using a configuration object that does not match the API schema.
91
91
 
92
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`.
@@ -101,6 +101,7 @@ Basic fix involves:
101
101
 
102
102
  ```bash
103
103
  # install sysconfcpus on CI
104
+
104
105
  git clone https://github.com/obmarg/libsysconfcpus.git $HOME/dependencies/libsysconfcpus
105
106
  cd libsysconfcpus
106
107
  .configure --prefix=$HOME/dependencies/sysconfcpus
@@ -113,41 +114,39 @@ chmod +x $HOME/your_rails_app/node_modules/.bin/elm-make
113
114
  ```
114
115
 
115
116
  ## 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
+ 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
 
118
119
  ```ruby
119
- // production.rb
120
- # From
120
+ # In production.rb
121
121
 
122
+ # From
122
123
  Rails.application.config.assets.js_compressor = :uglifier
123
124
 
124
125
  # To
125
-
126
126
  Rails.application.config.assets.js_compressor = Uglifier.new(harmony: true)
127
-
128
127
  ```
129
128
 
130
129
  ### Angular: WARNING in ./node_modules/@angular/core/esm5/core.js, Critical dependency: the request of a dependency is an expression
131
130
 
132
- To silent these warnings, please update `config/webpack/environment.js`
133
-
131
+ To silent these warnings, please update `config/webpack/base.js`:
134
132
  ```js
135
- // environment.js
136
133
  const webpack = require('webpack')
137
134
  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
- )
135
+ const { webpackConfig, merge } = require('@rails/webpacker')
136
+
137
+ module.exports = merge(webpackConfig, {
138
+ plugins: [
139
+ new webpack.ContextReplacementPlugin(
140
+ /angular(\\|\/)core(\\|\/)(@angular|esm5)/,
141
+ resolve(config.source_path)
142
+ )
143
+ ]
144
+ })
146
145
  ```
147
146
 
148
147
  ### Compilation Fails Silently
149
148
 
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)
149
+ 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
150
 
152
151
  ```yml
153
152
  # webpacker.yml
@@ -158,3 +157,34 @@ default: &default
158
157
  public_output_path: complaints_packs
159
158
  webpack_compile_output: true
160
159
  ```
160
+
161
+ ### Using global variables for dependencies
162
+
163
+ If you want to access any dependency without importing it everywhere or use it directly in your dev tools, please check: [https://webpack.js.org/plugins/provide-plugin/](https://webpack.js.org/plugins/provide-plugin/)
164
+
165
+ **You don't need to assign dependencies on `window`.**
166
+
167
+ For instance, with [jQuery](https://jquery.com/):
168
+ ```diff
169
+ // app/packs/entrypoints/application.js
170
+
171
+ - import jQuery from 'jquery'
172
+ - window.jQuery = jQuery
173
+ ```
174
+
175
+ Instead do:
176
+ ```js
177
+ // config/webpack/base.js
178
+
179
+ const webpack = require('webpack')
180
+ const { webpackConfig, merge } = require('@rails/webpacker')
181
+
182
+ module.exports = merge(webpackConfig, {
183
+ plugins: [
184
+ new webpack.ProvidePlugin({
185
+ $: 'jquery',
186
+ jQuery: 'jquery',
187
+ })
188
+ ],
189
+ })
190
+ ```
data/docs/v6_upgrade.md CHANGED
@@ -1,45 +1,32 @@
1
- # To Webpacker v6 from v5
1
+ # Upgrading from Webpacker 5 to 6
2
2
 
3
- This guide aims to help you migrating to Webpacker 6. If you are using vanilla Webpacker install then hopefully, the upgrade should be really straightforward.
3
+ There are several substantial changes in Webpacker 6 that you need to manually account for when coming from Webpacker 5. This guide will help you through it.
4
4
 
5
- ## Main differences with v5
5
+ ## Webpacker has become a slimmer wrapper around Webpack
6
6
 
7
- The main goal for Webpacker v6 is to manage the JavaScript in your Rails application with Webpack. This will allows you, for example, to use JavaScript modules, automatic code splitting using multiple entry points, use PostCSS or use [Vue](https://vuejs.org/) or [React](https://reactjs.org/).
7
+ By default, Webpacker 6 is focused on compiling and bundling JavaScript. This pairs with the existing asset pipeline in Rails that's setup to transpile CSS and static images using [Sprockets](https://github.com/rails/sprockets). For most developers, that's the recommended combination. But if you'd like to use Webpacker for CSS and static assets as well, please see [integrations](https://github.com/rails/webpacker#integrations) for more information.
8
8
 
9
- You probably don't want to install Webpacker and Webpack if you only need some JavaScript Sprinkles, Sass integration, images and fonts support.
9
+ Webpacker used to configure Webpack indirectly, which lead to a [complicated secondary configuration process](https://github.com/rails/webpacker/blob/5-x-stable/docs/webpack.md). This was done in order to provide default configurations for the most popular frameworks, but ended up creating more complexity than it cured. So now Webpacker delegates all configuration directly to Webpack's default configuration setup.
10
10
 
11
- ### Default integrations
11
+ This means you have to configure integration with frameworks yourself, but webpack-merge helps with this. See this example for [Vue](https://github.com/rails/webpacker#other-frameworks).
12
12
 
13
- By default, Webpacker v6 out of the box supports JS and static assets (fonts, images etc.) compilation. Webpacker now detects automatically relevant packages to support more tools.
14
-
15
- See [Integrations](https://github.com/rails/webpacker#integrations) for more information.
16
-
17
- Why? Because most developers don't need to handle CSS, SASS or another tools with Webpack. [Sprockets](https://github.com/rails/sprockets) is probably enough and we don't want to make things harder.
18
-
19
- ### Simpler API
20
-
21
- Webpacker is still a wrapper around [Webpack](https://webpack.js.org/) to simplify the integration in your Rails application.
22
-
23
- But we noticed that the [Webpacker v5 configuration](https://github.com/rails/webpacker/blob/5-x-stable/docs/webpack.md) was a bit confusing mostly because Webpack is a complicated beast to manage.
24
-
25
- There are so many different toolchains in JavaScript these days, it would be impossible to create te perfect configuration for everybody. That is also why defaults installers have been removed.
26
-
27
- In order to simplify even more the configuration, the custom API to manage the Webpack configuration has been removed.
28
-
29
- Now you have a straight access to the Webpack configuration and you can change it very easily with webpack-merge. So now, you can refer to the documentation of the tools you want to install it with Webpack. Here is an example with [Vue](https://github.com/rails/webpacker#other-frameworks).
30
-
31
- ## How to upgrade to Webpacker v6
13
+ ## How to upgrade to Webpacker 6
32
14
 
33
15
  1. If your `source_path` is `app/javascript`, rename it to `app/packs`
34
16
  2. If your `source_entry_path` is `packs`, rename it to `entrypoints`
35
17
  3. Rename `config/webpack` to `config/webpack_old`
36
18
  4. Rename `config/webpacker.yml` to `config/webpacker_old.yml`
37
19
  5. Uninstall the current version of `webpack-dev-server`: `yarn remove webpack-dev-server`
38
- 6. Upgrade Webpacker
20
+ 6. Remove .browserslistrc from the root of your Rails app
21
+ 7. Upgrade the Webpacker Ruby gem and NPM package
22
+
23
+ Note: [Check the releases page to verify the latest version](https://github.com/rails/webpacker/releases), and make sure to install identical version numbers of webpacker gem and `@rails/webpacker` npm package. (Gems use a period and packages use a dot between the main version number and the beta version.)
24
+
25
+ Example going to a specific version:
39
26
 
40
27
  ```ruby
41
28
  # Gemfile
42
- gem 'webpacker', '~> 6.0.0.pre.2'
29
+ gem 'webpacker', '6.0.0.rc.5'
43
30
  ```
44
31
 
45
32
  ```bash
@@ -47,22 +34,16 @@ Now you have a straight access to the Webpack configuration and you can change i
47
34
  ```
48
35
 
49
36
  ```bash
50
- yarn add @rails/webpacker@next
37
+ yarn add @rails/webpacker@6.0.0-rc.5 --exact
51
38
  ```
52
39
 
53
40
  ```bash
54
41
  bundle exec rails webpacker:install
55
42
  ```
56
43
 
57
- - Change `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and
58
- `stylesheet_pack_tag`.
59
-
60
- 7. If you are using any integrations like `css`, `React` or `TypeScript`. Please see https://github.com/rails/webpacker#integrations section on how they work in v6.
61
-
62
- 8. Copy over any custom webpack config from `config/webpack_old`
63
-
64
- - Common code previously called 'environment' changed to 'base'
65
- - import `environment` changed name to `webpackConfig`.
44
+ 7. Update API usage of the view helpers by changing `javascript_packs_with_chunks_tag` and `stylesheet_packs_with_chunks_tag` to `javascript_pack_tag` and `stylesheet_pack_tag`. Ensure that your layouts and views will only have **at most one call** to `javascript_pack_tag` or `stylesheet_pack_tag`. You can now pass multiple bundles to these view helper methods. If you fail to changes this, you may experience performance issues, and other bugs related to multiple copies of React, like [issue 2932](https://github.com/rails/webpacker/issues/2932). If you expose jquery globally with `expose-loader,` by using `import $ from "expose-loader?exposes=$,jQuery!jquery"` in your `app/packs/entrypoints/application.js`, pass the option `defer: false` to your `javascript_pack_tag`.
45
+ 8. If you are using any integrations like `css`, `React` or `TypeScript`. Please see https://github.com/rails/webpacker#integrations section on how they work in v6.
46
+ 9. Copy over any custom webpack config from `config/webpack_old`. Common code previously called 'environment' should be changed to 'base', and import `environment` changed to `webpackConfig`.
66
47
 
67
48
  ```js
68
49
  // config/webpack/base.js
@@ -72,15 +53,23 @@ Now you have a straight access to the Webpack configuration and you can change i
72
53
  module.exports = merge(webpackConfig, customConfig)
73
54
  ```
74
55
 
75
- 9. Copy over custom browserlist config from `.browserlistrc` if it exists into the `"browserlist"` key in `package.json` and remove `.browserslistrc`.
56
+ 10. Copy over custom browserlist config from `.browserslistrc` if it exists into the `"browserslist"` key in `package.json` and remove `.browserslistrc`.
57
+
58
+ 11. Remove `babel.config.js` if you never changed it. Be sure to have this config in your `package.json`:
59
+ ```json
60
+ "babel": {
61
+ "presets": [
62
+ "./node_modules/@rails/webpacker/package/babel/preset.js"
63
+ ]
64
+ }
65
+ ```
66
+ 12. Remove `postcss.config.js` if you don't use `PostCSS`.
67
+ 13. `extensions` was removed from the `webpacker.yml` file. Move custom extensions to your configuration by merging an object like this. For more details, see docs for [Webpack Configuration](https://github.com/rails/webpacker/blob/master/README.md#webpack-configuration)
76
68
 
77
- 10. `extensions` was removed from the webpacker.yml file. Move custom extensions to
78
- your configuration by by merging an object like this. For more details, see docs for
79
- [Webpack Configuration](https://github.com/rails/webpacker/blob/master/README.md#webpack-configuration)
80
69
  ```js
81
70
  {
82
71
  resolve: {
83
- extensions: ['.ts', '.tsx']
72
+ extensions: ['.ts', '.tsx', '.vue', '.css']
84
73
  }
85
74
  }
86
75
  ```
@@ -4,7 +4,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
4
4
 
5
5
  gemspec path: "../"
6
6
 
7
- gem "rails", github: "rails/rails"
7
+ gem "rails", github: "rails/rails", branch: "main"
8
8
  gem "arel", github: "rails/arel"
9
9
  gem "rake", ">= 11.1"
10
10
  gem "rack-proxy", require: false
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
4
+
5
+ gemspec path: "../"
6
+
7
+ gem "rails", '~>6.1.0'
8
+ gem "arel", github: "rails/arel"
9
+ gem "rake", ">= 11.1"
10
+ gem "rack-proxy", require: false
11
+ gem "minitest", "~> 5.0"
12
+ gem "byebug"
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ APP_ROOT = File.expand_path("..", __dir__)
4
+ Dir.chdir(APP_ROOT) do
5
+ yarn = ENV["PATH"].split(File::PATH_SEPARATOR).
6
+ select { |dir| File.expand_path(dir) != __dir__ }.
7
+ product(["yarn", "yarnpkg", "yarn.cmd", "yarn.ps1"]).
8
+ map { |dir, file| File.expand_path(file, dir) }.
9
+ find { |file| File.executable?(file) }
10
+
11
+ if yarn
12
+ exec yarn, *ARGV
13
+ else
14
+ $stderr.puts "Yarn executable was not detected in the system."
15
+ $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
16
+ exit 1
17
+ end
18
+ end
@@ -5,7 +5,7 @@ default: &default
5
5
  source_entry_path: entrypoints
6
6
  public_root_path: public
7
7
  public_output_path: packs
8
- cache_path: tmp/cache/webpacker
8
+ cache_path: tmp/webpacker
9
9
  webpack_compile_output: true
10
10
 
11
11
  # Additional paths webpack should look up modules
@@ -24,29 +24,26 @@ development:
24
24
  https: false
25
25
  host: localhost
26
26
  port: 3035
27
- public: localhost:3035
28
- # Inject browserside javascript that required by both HMR and Live(full) reload
29
- inject_client: true
30
27
  # Hot Module Replacement updates modules while the application is running without a full reload
31
28
  hmr: false
32
- # Inline should be set to true if using HMR; it inserts a script to take care of live reloading
33
- inline: true
34
- # Should we show a full-screen overlay in the browser when there are compiler errors or warnings?
35
- overlay: true
29
+ client:
30
+ # Should we show a full-screen overlay in the browser when there are compiler errors or warnings?
31
+ overlay: true
32
+ # May also be a string
33
+ # webSocketURL:
34
+ # hostname: "0.0.0.0"
35
+ # pathname: "/ws"
36
+ # port: 8080
36
37
  # Should we use gzip compression?
37
38
  compress: true
38
39
  # Note that apps that do not check the host are vulnerable to DNS rebinding attacks
39
- disable_host_check: true
40
- # This option lets the browser open with your local IP
41
- use_local_ip: false
42
- # When enabled, nothing except the initial startup information will be written to the console.
43
- # This also means that errors or warnings from webpack are not visible.
44
- quiet: false
45
- pretty: false
40
+ allowed_hosts: "all"
41
+ pretty: true
46
42
  headers:
47
43
  'Access-Control-Allow-Origin': '*'
48
- watch_options:
49
- ignored: '**/node_modules/**'
44
+ static:
45
+ watch:
46
+ ignored: '**/node_modules/**'
50
47
 
51
48
  test:
52
49
  <<: *default
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "app",
3
+ "private": true,
4
+ "dependencies": {
5
+ "@rails/activestorage": "latest",
6
+ "@rails/actioncable": "latest"
7
+ },
8
+ "version": "0.1.0",
9
+ "babel": {
10
+ "presets": [
11
+ "./node_modules/@rails/webpacker/package/babel/preset.js"
12
+ ]
13
+ },
14
+ "browserslist": [
15
+ "defaults"
16
+ ]
17
+ }
@@ -14,7 +14,6 @@
14
14
  // const images = require.context('../images', true)
15
15
  // const imagePath = (name) => images(name, true)
16
16
 
17
- import 'core-js/stable'
18
- import 'regenerator-runtime/runtime'
19
-
20
- console.log('Hello World from Webpacker')
17
+ // Activate Active Storage
18
+ // import * as ActiveStorage from "@rails/activestorage"
19
+ // ActiveStorage.start()
@@ -1,5 +1,6 @@
1
1
  # Install Webpacker
2
2
  copy_file "#{__dir__}/config/webpacker.yml", "config/webpacker.yml"
3
+ copy_file "#{__dir__}/package.json", "package.json"
3
4
 
4
5
  say "Copying webpack core config"
5
6
  directory "#{__dir__}/config/webpack", "config/webpack"
@@ -26,6 +27,32 @@ if File.exists?(git_ignore_path)
26
27
  end
27
28
  end
28
29
 
30
+ if (app_layout_path = Rails.root.join("app/views/layouts/application.html.erb")).exist?
31
+ say "Add JavaScript include tag in application layout"
32
+ insert_into_file app_layout_path.to_s, %(\n <%= javascript_pack_tag "application" %>), before: /\s*<\/head>/
33
+ else
34
+ say "Default application.html.erb is missing!", :red
35
+ say %( Add <%= javascript_pack_tag "application" %> within the <head> tag in your custom layout.)
36
+ end
37
+
38
+ if (setup_path = Rails.root.join("bin/setup")).exist?
39
+ say "Run bin/yarn during bin/setup"
40
+ insert_into_file setup_path.to_s, <<-RUBY, after: %( system("bundle check") || system!("bundle install")\n)
41
+
42
+ # Install JavaScript dependencies
43
+ system! "bin/yarn"
44
+ RUBY
45
+ end
46
+
47
+ if (asset_config_path = Rails.root.join("config/initializers/assets.rb")).exist?
48
+ say "Add node_modules to the asset load path"
49
+ append_to_file asset_config_path, <<-RUBY
50
+
51
+ # Add node_modules folder to the asset load path.
52
+ Rails.application.config.assets.paths << Rails.root.join("node_modules")
53
+ RUBY
54
+ end
55
+
29
56
  results = []
30
57
 
31
58
  Dir.chdir(Rails.root) do
@@ -49,18 +76,6 @@ Dir.chdir(Rails.root) do
49
76
  results << run("yarn add --dev webpack-dev-server @webpack-cli/serve")
50
77
  end
51
78
 
52
- insert_into_file Rails.root.join("package.json").to_s, before: /\n}\n*$/ do
53
- <<~JSON.chomp
54
- ,
55
- "babel": {
56
- "presets": ["./node_modules/@rails/webpacker/package/babel/preset.js"]
57
- },
58
- "browserslist": [
59
- "defaults"
60
- ]
61
- JSON
62
- end
63
-
64
79
  if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1
65
80
  say "You need to allow webpack-dev-server host as allowed origin for connect-src.", :yellow
66
81
  say "This can be done in Rails 5.2+ for development environment in the CSP initializer", :yellow
@@ -68,9 +83,7 @@ if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1
68
83
  say "policy.connect_src :self, :https, \"http://localhost:3035\", \"ws://localhost:3035\" if Rails.env.development?", :yellow
69
84
  end
70
85
 
71
- if results.all?
72
- say "Webpacker successfully installed 🎉 🍰", :green
73
- else
86
+ unless results.all?
74
87
  say "Webpacker installation failed 😭 See above for details.", :red
75
88
  exit 1
76
89
  end