webpacker 6.0.0.rc.1 → 6.0.0.rc.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/jest.yml +6 -17
- data/.github/workflows/js-lint.yml +6 -17
- data/.github/workflows/ruby.yml +13 -38
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +15 -3
- data/CONTRIBUTING.md +19 -0
- data/Gemfile.lock +84 -84
- data/README.md +118 -164
- data/docs/developing_webpacker.md +4 -4
- data/docs/troubleshooting.md +5 -1
- data/docs/v6_upgrade.md +72 -34
- data/lib/install/{packs/entrypoints/application.js → application.js} +3 -7
- data/lib/install/bin/webpack +4 -7
- data/lib/install/bin/yarn +18 -0
- data/lib/install/config/webpacker.yml +16 -13
- data/lib/install/package.json +15 -0
- data/lib/install/template.rb +37 -15
- data/lib/tasks/yarn.rake +38 -0
- data/lib/webpacker/commands.rb +19 -15
- data/lib/webpacker/dev_server_runner.rb +1 -1
- data/lib/webpacker/helper.rb +2 -2
- data/lib/webpacker/railtie.rb +7 -0
- data/lib/webpacker/version.rb +1 -1
- data/lib/webpacker/webpack_runner.rb +27 -7
- data/package/__tests__/development.js +4 -11
- data/package/config.js +3 -3
- data/package/env.js +3 -6
- data/package/environments/base.js +1 -1
- data/package/environments/development.js +34 -32
- data/package/rules/file.js +1 -1
- data/package.json +15 -15
- data/test/command_test.rb +76 -0
- data/test/dev_server_runner_test.rb +1 -1
- data/test/helper_test.rb +13 -4
- data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
- data/test/webpacker_test.rb +4 -0
- data/webpacker.gemspec +2 -2
- data/yarn.lock +1392 -2825
- metadata +16 -11
data/docs/v6_upgrade.md
CHANGED
@@ -10,40 +10,56 @@ Webpacker used to configure Webpack indirectly, which lead to a [complicated sec
|
|
10
10
|
|
11
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
|
-
## How to upgrade to Webpacker
|
13
|
+
## How to upgrade to Webpacker v6 from v5
|
14
14
|
|
15
|
-
1. If
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
1. If you are changing from the v5 default for `source_entry_path`:
|
16
|
+
```yml
|
17
|
+
source_path: app/javascript
|
18
|
+
source_entry_path: packs
|
19
|
+
```
|
20
|
+
to the v6 default:
|
21
|
+
```yml
|
22
|
+
source_path: app/javascript
|
23
|
+
source_entry_path: /
|
24
|
+
```
|
25
|
+
Then move your `app/javascript/packs/*` (including `application.js`) to `app/javascript/`.
|
26
|
+
|
27
|
+
Check if you had any entry point files in child directories of your `source_entry_path`. Files for entry points in child directories are not supported by rails/webpacker v6.
|
28
|
+
|
29
|
+
2. Rename `config/webpack` to `config/webpack_old`
|
30
|
+
|
31
|
+
3. Rename `config/webpacker.yml` to `config/webpacker_old.yml`
|
32
|
+
|
33
|
+
4. Uninstall the current version of `webpack-dev-server`: `yarn remove webpack-dev-server`
|
34
|
+
|
35
|
+
5. Upgrade the Webpacker Ruby gem and NPM package
|
21
36
|
|
22
37
|
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.)
|
23
38
|
|
24
|
-
Example going to a specific
|
39
|
+
Example going to a specific version:
|
25
40
|
|
26
41
|
```ruby
|
27
42
|
# Gemfile
|
28
|
-
gem 'webpacker', '6.0.0.
|
43
|
+
gem 'webpacker', '6.0.0.rc.5'
|
29
44
|
```
|
30
45
|
|
31
|
-
Do *not* specify `'^6.0.0.beta.7'` (with a caret) or you will accidentally install `6.0.0.pre.2` which is months older than `beta.7`.
|
32
|
-
|
33
46
|
```bash
|
34
47
|
bundle install
|
35
48
|
```
|
36
49
|
|
37
50
|
```bash
|
38
|
-
yarn add @rails/webpacker@6.0.0-
|
51
|
+
yarn add @rails/webpacker@6.0.0-rc.5 --exact
|
39
52
|
```
|
40
53
|
|
41
54
|
```bash
|
42
55
|
bundle exec rails webpacker:install
|
43
56
|
```
|
44
|
-
|
45
|
-
|
46
|
-
|
57
|
+
|
58
|
+
6. 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` and **at most one call** to `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/javascript/application.js`, pass the option `defer: false` to your `javascript_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`. Common code previously called 'environment' should be changed to 'base', and import `environment` changed to `webpackConfig`.
|
47
63
|
|
48
64
|
```js
|
49
65
|
// config/webpack/base.js
|
@@ -53,23 +69,45 @@ Do *not* specify `'^6.0.0.beta.7'` (with a caret) or you will accidentally insta
|
|
53
69
|
module.exports = merge(webpackConfig, customConfig)
|
54
70
|
```
|
55
71
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
"
|
62
|
-
"
|
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)
|
68
|
-
|
69
|
-
```js
|
70
|
-
{
|
71
|
-
resolve: {
|
72
|
-
extensions: ['.ts', '.tsx', '.vue', '.css']
|
72
|
+
9. Copy over custom browserlist config from `.browserslistrc` if it exists into the `"browserslist"` key in `package.json` and remove `.browserslistrc`.
|
73
|
+
|
74
|
+
10. Remove `babel.config.js` if you never changed it. Be sure to have this config in your `package.json`:
|
75
|
+
|
76
|
+
```json
|
77
|
+
"babel": {
|
78
|
+
"presets": [
|
79
|
+
"./node_modules/@rails/webpacker/package/babel/preset.js"
|
80
|
+
]
|
73
81
|
}
|
74
|
-
|
75
|
-
|
82
|
+
```
|
83
|
+
|
84
|
+
11. Remove `postcss.config.js` if you don't use `PostCSS`.
|
85
|
+
|
86
|
+
12. `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)
|
87
|
+
|
88
|
+
```js
|
89
|
+
{
|
90
|
+
resolve: {
|
91
|
+
extensions: ['.ts', '.tsx', '.vue', '.css']
|
92
|
+
}
|
93
|
+
}
|
94
|
+
```
|
95
|
+
|
96
|
+
13. Some dependencies were removed in [PR 3056](https://github.com/rails/webpacker/pull/3056). If you see the error: `Error: Cannot find module 'babel-plugin-macros'`, or similar, then you need to `yarn add <dependency>` where <dependency> might include: `babel-plugin-macros`, `case-sensitive-paths-webpack-plugin`, `core-js`, `regenerator-runtime`. Or you might want to remove your dependency on those.
|
97
|
+
|
98
|
+
14. If `bin/yarn` does not exist, create an executable [yarn](https://github.com/rails/webpacker/blob/master/lib/install/bin/yarn) file in your `/bin` directory.
|
99
|
+
|
100
|
+
15. Remove overlapping dependencies from your `package.json` and rails/webpacker's `package.json`. For example, don't include `webpack` directly as that's a dependency of rails/webpacker.
|
101
|
+
|
102
|
+
16. Review the new default's changes to `webpacker.yml` and `config/webpack`. Consider each suggested change carefully, especially the change to have your `source_entry_path` be at the top level of your `source_path`.
|
103
|
+
|
104
|
+
17. Make sure that you can run `bin/webpack` without errors.
|
105
|
+
|
106
|
+
18. Try running `RAILS_ENV=production bin/rails assets:precompile`. If all goes well, don't forget to clean the generated assets with `bin/rails assets:clobber`.
|
107
|
+
|
108
|
+
19. Try your app!
|
109
|
+
|
110
|
+
## Examples of v5 to v6
|
111
|
+
|
112
|
+
1. [React on Rails Project with HMR and SSR](https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/compare/webpacker-5.x...master)
|
113
|
+
2. [Vue and Sass Example](https://github.com/guillaumebriday/upgrade-webpacker-5-to-6)
|
@@ -1,19 +1,15 @@
|
|
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/
|
4
|
+
// a relevant structure within app/javascript and only use these pack files to reference
|
5
5
|
// that code so it'll be compiled.
|
6
6
|
//
|
7
7
|
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
|
8
8
|
// layout file, like app/views/layouts/application.html.erb
|
9
9
|
|
10
|
-
// Uncomment to copy all static images under
|
10
|
+
// Uncomment to copy all static images under ./images to the output folder and reference
|
11
11
|
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
|
12
12
|
// or the `imagePath` JavaScript helper below.
|
13
13
|
//
|
14
|
-
// const images = require.context('
|
14
|
+
// const images = require.context('./images', true)
|
15
15
|
// const imagePath = (name) => images(name, true)
|
16
|
-
|
17
|
-
// Activate Active Storage
|
18
|
-
// import * as ActiveStorage from "@rails/activestorage"
|
19
|
-
// ActiveStorage.start()
|
data/lib/install/bin/webpack
CHANGED
@@ -1,17 +1,14 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
|
4
|
-
ENV["NODE_ENV"] ||= "development"
|
5
|
-
|
6
3
|
require "pathname"
|
7
|
-
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
8
|
-
Pathname.new(__FILE__).realpath)
|
9
|
-
|
10
4
|
require "bundler/setup"
|
11
|
-
|
12
5
|
require "webpacker"
|
13
6
|
require "webpacker/webpack_runner"
|
14
7
|
|
8
|
+
ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
|
9
|
+
ENV["NODE_ENV"] ||= "development"
|
10
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", Pathname.new(__FILE__).realpath)
|
11
|
+
|
15
12
|
APP_ROOT = File.expand_path("..", __dir__)
|
16
13
|
Dir.chdir(APP_ROOT) do
|
17
14
|
Webpacker::WebpackRunner.run(ARGV)
|
@@ -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
|
@@ -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/
|
5
|
-
source_entry_path:
|
4
|
+
source_path: app/javascript
|
5
|
+
source_entry_path: /
|
6
6
|
public_root_path: public
|
7
7
|
public_output_path: packs
|
8
8
|
cache_path: tmp/webpacker
|
@@ -24,25 +24,28 @@ development:
|
|
24
24
|
https: false
|
25
25
|
host: localhost
|
26
26
|
port: 3035
|
27
|
-
public: localhost:3035
|
28
27
|
# Hot Module Replacement updates modules while the application is running without a full reload
|
29
28
|
hmr: false
|
30
|
-
#
|
31
|
-
|
29
|
+
# Defaults to the inverse of hmr. Uncomment to manually set this.
|
30
|
+
# live_reload: true
|
31
|
+
client:
|
32
|
+
# Should we show a full-screen overlay in the browser when there are compiler errors or warnings?
|
33
|
+
overlay: true
|
34
|
+
# May also be a string
|
35
|
+
# webSocketURL:
|
36
|
+
# hostname: "0.0.0.0"
|
37
|
+
# pathname: "/ws"
|
38
|
+
# port: 8080
|
32
39
|
# Should we use gzip compression?
|
33
40
|
compress: true
|
34
41
|
# Note that apps that do not check the host are vulnerable to DNS rebinding attacks
|
35
|
-
|
36
|
-
# This option lets the browser open with your local IP
|
37
|
-
use_local_ip: false
|
38
|
-
# When enabled, nothing except the initial startup information will be written to the console.
|
39
|
-
# This also means that errors or warnings from webpack are not visible.
|
40
|
-
quiet: false
|
42
|
+
allowed_hosts: "all"
|
41
43
|
pretty: true
|
42
44
|
headers:
|
43
45
|
'Access-Control-Allow-Origin': '*'
|
44
|
-
|
45
|
-
|
46
|
+
static:
|
47
|
+
watch:
|
48
|
+
ignored: '**/node_modules/**'
|
46
49
|
|
47
50
|
test:
|
48
51
|
<<: *default
|
data/lib/install/template.rb
CHANGED
@@ -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"
|
@@ -8,7 +9,8 @@ if Dir.exists?(Webpacker.config.source_path)
|
|
8
9
|
say "The packs app source directory already exists"
|
9
10
|
else
|
10
11
|
say "Creating packs app source directory"
|
11
|
-
|
12
|
+
empty_directory "app/javascript"
|
13
|
+
copy_file "#{__dir__}/application.js", "app/javascript/application.js"
|
12
14
|
end
|
13
15
|
|
14
16
|
apply "#{__dir__}/binstubs.rb"
|
@@ -26,6 +28,40 @@ if File.exists?(git_ignore_path)
|
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
31
|
+
if (app_layout_path = Rails.root.join("app/views/layouts/application.html.erb")).exist?
|
32
|
+
say "Add JavaScript include tag in application layout"
|
33
|
+
insert_into_file app_layout_path.to_s, %(\n <%= javascript_pack_tag "application" %>), before: /\s*<\/head>/
|
34
|
+
else
|
35
|
+
say "Default application.html.erb is missing!", :red
|
36
|
+
say %( Add <%= javascript_pack_tag "application" %> within the <head> tag in your custom layout.)
|
37
|
+
end
|
38
|
+
|
39
|
+
if (setup_path = Rails.root.join("bin/setup")).exist?
|
40
|
+
say "Run bin/yarn during bin/setup"
|
41
|
+
insert_into_file setup_path.to_s, <<-RUBY, after: %( system("bundle check") || system!("bundle install")\n)
|
42
|
+
|
43
|
+
# Install JavaScript dependencies
|
44
|
+
system! "bin/yarn"
|
45
|
+
RUBY
|
46
|
+
end
|
47
|
+
|
48
|
+
if (asset_config_path = Rails.root.join("config/initializers/assets.rb")).exist?
|
49
|
+
say "Add node_modules to the asset load path"
|
50
|
+
append_to_file asset_config_path, <<-RUBY
|
51
|
+
|
52
|
+
# Add node_modules folder to the asset load path.
|
53
|
+
Rails.application.config.assets.paths << Rails.root.join("node_modules")
|
54
|
+
RUBY
|
55
|
+
end
|
56
|
+
|
57
|
+
if (csp_config_path = Rails.root.join("config/initializers/content_security_policy.rb")).exist?
|
58
|
+
say "Make note of webpack-dev-server exemption needed to csp"
|
59
|
+
insert_into_file csp_config_path, <<-RUBY, after: %(# Rails.application.config.content_security_policy do |policy|)
|
60
|
+
# # If you are using webpack-dev-server then specify webpack-dev-server host
|
61
|
+
# policy.connect_src :self, :https, "http://localhost:3035", "ws://localhost:3035" if Rails.env.development?
|
62
|
+
RUBY
|
63
|
+
end
|
64
|
+
|
29
65
|
results = []
|
30
66
|
|
31
67
|
Dir.chdir(Rails.root) do
|
@@ -49,20 +85,6 @@ Dir.chdir(Rails.root) do
|
|
49
85
|
results << run("yarn add --dev webpack-dev-server @webpack-cli/serve")
|
50
86
|
end
|
51
87
|
|
52
|
-
insert_into_file Rails.root.join("package.json").to_s, before: /\n}\n*$/ do
|
53
|
-
<<~JSON.chomp
|
54
|
-
,
|
55
|
-
"babel": {
|
56
|
-
"presets": [
|
57
|
-
"./node_modules/@rails/webpacker/package/babel/preset.js"
|
58
|
-
]
|
59
|
-
},
|
60
|
-
"browserslist": [
|
61
|
-
"defaults"
|
62
|
-
]
|
63
|
-
JSON
|
64
|
-
end
|
65
|
-
|
66
88
|
if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1
|
67
89
|
say "You need to allow webpack-dev-server host as allowed origin for connect-src.", :yellow
|
68
90
|
say "This can be done in Rails 5.2+ for development environment in the CSP initializer", :yellow
|
data/lib/tasks/yarn.rake
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Duplicate of the yarn tasks still present in Rails until Webpacker <5 have been deprecated
|
4
|
+
|
5
|
+
namespace :yarn do
|
6
|
+
desc "Install all JavaScript dependencies as specified via Yarn"
|
7
|
+
task :install do
|
8
|
+
begin
|
9
|
+
# Install only production deps when for not usual envs.
|
10
|
+
valid_node_envs = %w[test development production]
|
11
|
+
node_env = ENV.fetch("NODE_ENV") do
|
12
|
+
valid_node_envs.include?(Rails.env) ? Rails.env : "production"
|
13
|
+
end
|
14
|
+
|
15
|
+
yarn_flags =
|
16
|
+
if `#{RbConfig.ruby} "#{Rails.root}/bin/yarn" --version`.start_with?("1")
|
17
|
+
"--no-progress --frozen-lockfile"
|
18
|
+
else
|
19
|
+
"--immutable"
|
20
|
+
end
|
21
|
+
|
22
|
+
system(
|
23
|
+
{ "NODE_ENV" => node_env },
|
24
|
+
"#{RbConfig.ruby} \"#{Rails.root}/bin/yarn\" install #{yarn_flags}",
|
25
|
+
exception: true
|
26
|
+
)
|
27
|
+
rescue Errno::ENOENT
|
28
|
+
$stderr.puts "bin/yarn was not found."
|
29
|
+
$stderr.puts "Please run `bundle exec rails app:update:bin` to create it."
|
30
|
+
exit 1
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Run Yarn prior to Sprockets assets precompilation, so dependencies are available for use.
|
36
|
+
if Rake::Task.task_defined?("assets:precompile") && File.exist?(Rails.root.join("bin", "yarn"))
|
37
|
+
Rake::Task["assets:precompile"].enhance [ "yarn:install" ]
|
38
|
+
end
|
data/lib/webpacker/commands.rb
CHANGED
@@ -17,20 +17,21 @@ class Webpacker::Commands
|
|
17
17
|
#
|
18
18
|
def clean(count = 2, age = 3600)
|
19
19
|
if config.public_output_path.exist? && config.public_manifest_path.exist?
|
20
|
-
|
21
|
-
.
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
20
|
+
packs
|
21
|
+
.map do |paths|
|
22
|
+
paths.map { |path| [Time.now - File.mtime(path), path] }
|
23
|
+
.sort
|
24
|
+
.reject.with_index do |(file_age, _), index|
|
25
|
+
file_age < age || index < count
|
26
|
+
end
|
27
|
+
.map { |_, path| path }
|
27
28
|
end
|
28
|
-
.
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
.flatten
|
30
|
+
.compact
|
31
|
+
.each do |file|
|
32
|
+
if File.file?(file)
|
33
|
+
File.delete(file)
|
34
|
+
logger.info "Removed #{file}"
|
34
35
|
end
|
35
36
|
end
|
36
37
|
end
|
@@ -54,12 +55,15 @@ class Webpacker::Commands
|
|
54
55
|
end
|
55
56
|
|
56
57
|
private
|
57
|
-
def
|
58
|
+
def packs
|
58
59
|
all_files = Dir.glob("#{config.public_output_path}/**/*")
|
59
60
|
manifest_config = Dir.glob("#{config.public_manifest_path}*")
|
60
61
|
|
61
62
|
packs = all_files - manifest_config - current_version
|
62
|
-
packs.reject { |file| File.directory?(file) }.group_by
|
63
|
+
packs.reject { |file| File.directory?(file) }.group_by do |path|
|
64
|
+
base, _, ext = File.basename(path).scan(/(.*)(-[\da-f]+)(\.\w+)/).flatten
|
65
|
+
"#{File.dirname(path)}/#{base}#{ext}"
|
66
|
+
end.values
|
63
67
|
end
|
64
68
|
|
65
69
|
def current_version
|
@@ -65,7 +65,7 @@ module Webpacker
|
|
65
65
|
def execute_cmd
|
66
66
|
env = Webpacker::Compiler.env
|
67
67
|
env["WEBPACKER_CONFIG"] = @webpacker_config
|
68
|
-
env["
|
68
|
+
env["WEBPACK_SERVE"] = "true"
|
69
69
|
|
70
70
|
cmd = if node_modules_bin_exist?
|
71
71
|
["#{@node_modules_bin_path}/webpack", "serve"]
|
data/lib/webpacker/helper.rb
CHANGED
@@ -95,8 +95,8 @@ module Webpacker::Helper
|
|
95
95
|
#
|
96
96
|
# <%= javascript_pack_tag 'calendar' %>
|
97
97
|
# <%= javascript_pack_tag 'map' %>
|
98
|
-
def javascript_pack_tag(*names, **options)
|
99
|
-
javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), defer
|
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 })
|
100
100
|
end
|
101
101
|
|
102
102
|
# Creates a link tag, for preloading, that references a given Webpacker asset.
|
data/lib/webpacker/railtie.rb
CHANGED
@@ -51,4 +51,11 @@ class Webpacker::Engine < ::Rails::Engine
|
|
51
51
|
app.config.javascript_path = Webpacker.config.source_path.relative_path_from(Rails.root.join("app")).to_s
|
52
52
|
end
|
53
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
|
54
61
|
end
|
data/lib/webpacker/version.rb
CHANGED
@@ -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,17 +26,24 @@ module Webpacker
|
|
13
26
|
["yarn", "webpack"]
|
14
27
|
end
|
15
28
|
|
16
|
-
if @argv.
|
17
|
-
cmd = [
|
18
|
-
|
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
|
19
39
|
end
|
20
40
|
|
21
|
-
|
22
|
-
|
23
|
-
|
41
|
+
# Webpack commands are not compatible with --config option.
|
42
|
+
if (@argv & WEBPACK_COMMANDS).empty?
|
43
|
+
cmd += ["--config", @webpack_config]
|
24
44
|
end
|
25
45
|
|
26
|
-
cmd +=
|
46
|
+
cmd += @argv
|
27
47
|
|
28
48
|
Dir.chdir(@app_path) do
|
29
49
|
Kernel.exec env, *cmd
|
@@ -11,27 +11,20 @@ describe('Development environment', () => {
|
|
11
11
|
describe('webpackConfig', () => {
|
12
12
|
beforeEach(() => jest.resetModules())
|
13
13
|
|
14
|
-
test('should use development config and environment including devServer if
|
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.
|
17
|
+
process.env.WEBPACK_SERVE = 'true'
|
18
18
|
const { webpackConfig } = require('../index')
|
19
19
|
|
20
20
|
expect(webpackConfig.output.path).toEqual(resolve('public', 'packs'))
|
21
21
|
expect(webpackConfig.output.publicPath).toEqual('/packs/')
|
22
|
-
expect(webpackConfig).toMatchObject({
|
23
|
-
devServer: {
|
24
|
-
host: 'localhost',
|
25
|
-
port: 3035,
|
26
|
-
injectClient: false
|
27
|
-
}
|
28
|
-
})
|
29
22
|
})
|
30
23
|
|
31
|
-
test('should use development config and environment if
|
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.
|
27
|
+
process.env.WEBPACK_SERVE = undefined
|
35
28
|
const { webpackConfig } = require('../index')
|
36
29
|
|
37
30
|
expect(webpackConfig.output.path).toEqual(resolve('public', 'packs'))
|
data/package/config.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
const { resolve } = require('path')
|
2
|
-
const {
|
2
|
+
const { load } = require('js-yaml')
|
3
3
|
const { readFileSync } = require('fs')
|
4
4
|
const { merge } = require('webpack-merge')
|
5
5
|
const { ensureTrailingSlash } = require('./utils/helpers')
|
@@ -9,12 +9,12 @@ const configPath = require('./configPath')
|
|
9
9
|
const defaultConfigPath = require.resolve('../lib/install/config/webpacker.yml')
|
10
10
|
|
11
11
|
const getDefaultConfig = () => {
|
12
|
-
const defaultConfig =
|
12
|
+
const defaultConfig = load(readFileSync(defaultConfigPath), 'utf8')
|
13
13
|
return defaultConfig[railsEnv] || defaultConfig.production
|
14
14
|
}
|
15
15
|
|
16
16
|
const defaults = getDefaultConfig()
|
17
|
-
const app =
|
17
|
+
const app = load(readFileSync(configPath), 'utf8')[railsEnv]
|
18
18
|
|
19
19
|
const config = merge(defaults, app)
|
20
20
|
config.outputPath = resolve(config.public_root_path, config.public_output_path)
|
data/package/env.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
const {
|
1
|
+
const { load } = require('js-yaml')
|
2
2
|
const { readFileSync } = require('fs')
|
3
3
|
|
4
4
|
const NODE_ENVIRONMENTS = ['development', 'production', 'test']
|
@@ -12,14 +12,11 @@ const nodeEnv
|
|
12
12
|
const isProduction = nodeEnv === 'production'
|
13
13
|
const isDevelopment = nodeEnv === 'development'
|
14
14
|
|
15
|
-
const config =
|
15
|
+
const config = load(readFileSync(configPath), 'utf8')
|
16
16
|
const availableEnvironments = Object.keys(config).join('|')
|
17
17
|
const regex = new RegExp(`^(${availableEnvironments})$`, 'g')
|
18
18
|
|
19
|
-
|
20
|
-
// https://github.com/rails/webpacker/issues/3057
|
21
|
-
const runningWebpackDevServer = process.env.WEBPACK_DEV_SERVER === 'true' ||
|
22
|
-
process.env.WEBPACK_DEV_SERVE === 'true'
|
19
|
+
const runningWebpackDevServer = process.env.WEBPACK_SERVE === 'true'
|
23
20
|
|
24
21
|
module.exports = {
|
25
22
|
railsEnv: railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT,
|
@@ -16,7 +16,7 @@ const getEntryObject = () => {
|
|
16
16
|
const entries = {}
|
17
17
|
const rootPath = join(config.source_path, config.source_entry_path)
|
18
18
|
|
19
|
-
globSync(`${rootPath}
|
19
|
+
globSync(`${rootPath}/*.*`).forEach((path) => {
|
20
20
|
const namespace = relative(join(rootPath), dirname(path))
|
21
21
|
const name = join(namespace, basename(path, extname(path)))
|
22
22
|
let assetPaths = resolve(path)
|