Skip to content

Commit c2d032c

Browse files
lsylvestergauravtiwari
authored andcommitted
move integrety check config to yaml (#1669)
1 parent 52a21f3 commit c2d032c

File tree

9 files changed

+42
-35
lines changed

9 files changed

+42
-35
lines changed

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,20 @@ yarn add @rails/[email protected]
278278

279279
By default, in development, webpacker runs a yarn integrity check to ensure that all local JavaScript packages are up-to-date. This is similar to what bundler does currently in Rails, but for JavaScript packages. If your system is out of date, then Rails will not initialize. You will be asked to upgrade your local JavaScript packages by running `yarn install`.
280280

281-
To turn off this option, you will need to override the default by adding a new config option to your Rails development environment configuration file (`config/environment/development.rb`):
281+
To turn off this option, you will need to change the default setting in `config/webpacker.yml`:
282282

283-
```
284-
config.webpacker.check_yarn_integrity = false
283+
```yaml
284+
# config/webpacker.yml
285+
development:
286+
...
287+
# Verifies that versions and hashed value of the package contents in the project's package.json
288+
check_yarn_integrity: false
285289
```
286290

287-
You may also turn on this feature by adding the config option to any Rails environment configuration file:
291+
You may also turn on this feature by adding the config option for any Rails environment in `config/webpacker.yml`:
288292

289-
```
290-
config.webpacker.check_yarn_integrity = true
293+
```yaml
294+
check_yarn_integrity: true
291295
```
292296

293297
## Integrations

lib/install/config/webpacker.yml

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ default: &default
55
source_entry_path: packs
66
public_output_path: packs
77
cache_path: tmp/cache/webpacker
8+
check_yarn_integrity: false
89

910
# Additional paths webpack should lookup modules
1011
# ['app/assets', 'engine/foo/app/assets']
@@ -31,6 +32,9 @@ development:
3132
<<: *default
3233
compile: true
3334

35+
# Verifies that versions and hashed value of the package contents in the project's package.json
36+
check_yarn_integrity: true
37+
3438
# Reference: https://webpack.js.org/configuration/dev-server/
3539
dev_server:
3640
https: false

lib/install/template.rb

-15
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,6 @@
2222

2323
apply "#{__dir__}/binstubs.rb"
2424

25-
say "Adding configurations"
26-
27-
check_yarn_integrity_config = ->(value) { <<CONFIG.indent(2) }
28-
# Verifies that versions and hashed value of the package contents in the project's package.json
29-
config.webpacker.check_yarn_integrity = #{value}
30-
CONFIG
31-
32-
if Rails::VERSION::MAJOR >= 5
33-
environment check_yarn_integrity_config.call("true"), env: :development
34-
environment check_yarn_integrity_config.call("false"), env: :production
35-
else
36-
inject_into_file "config/environments/development.rb", "\n#{check_yarn_integrity_config.call("true")}", after: "Rails.application.configure do", verbose: false
37-
inject_into_file "config/environments/production.rb", "\n#{check_yarn_integrity_config.call("false")}", after: "Rails.application.configure do", verbose: false
38-
end
39-
4025
if File.exists?(".gitignore")
4126
append_to_file ".gitignore", <<-EOS
4227
/public/packs

lib/webpacker/commands.rb

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def clobber
1111
end
1212

1313
def bootstrap
14-
config.refresh
1514
manifest.refresh
1615
end
1716

lib/webpacker/configuration.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ def initialize(root_path:, config_path:, env:)
1111
@env = env
1212
end
1313

14-
def refresh
15-
@data = load
16-
end
17-
1814
def dev_server
1915
fetch(:dev_server)
2016
end
@@ -63,6 +59,14 @@ def extensions
6359
fetch(:extensions)
6460
end
6561

62+
def check_yarn_integrity=(value)
63+
data[:check_yarn_integrity] = value
64+
end
65+
66+
def check_yarn_integrity?
67+
fetch(:check_yarn_integrity)
68+
end
69+
6670
private
6771
def fetch(key)
6872
data.fetch(key, defaults[key])

lib/webpacker/railtie.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
class Webpacker::Engine < ::Rails::Engine
77
# Allows Webpacker config values to be set via Rails env config files
88
config.webpacker = ActiveSupport::OrderedOptions.new
9-
config.webpacker.check_yarn_integrity = false
9+
10+
initializer "webpacker.set_configs" do |app|
11+
if app.config.webpacker.key?(:check_yarn_integrity)
12+
Webpacker.config.check_yarn_integrity = app.config.webpacker.check_yarn_integrity
13+
end
14+
end
1015

1116
# ================================
1217
# Check Yarn Integrity Initializer
@@ -24,7 +29,7 @@ class Webpacker::Engine < ::Rails::Engine
2429
# - edit config/environments/production.rb
2530
# - add `config.webpacker.check_yarn_integrity = true`
2631
initializer "webpacker.yarn_check" do |app|
27-
if File.exist?("yarn.lock") && app.config.webpacker.check_yarn_integrity
32+
if File.exist?("yarn.lock") && Webpacker.config.check_yarn_integrity?
2833
output = `yarn check --integrity 2>&1`
2934

3035
unless $?.success?
@@ -34,8 +39,8 @@ class Webpacker::Engine < ::Rails::Engine
3439
$stderr.puts " Please run `yarn install` to update."
3540
$stderr.puts "========================================"
3641
$stderr.puts "\n\n"
37-
$stderr.puts "To disable this check, please add `config.webpacker.check_yarn_integrity = false`"
38-
$stderr.puts "to your Rails development config file (config/environments/development.rb)."
42+
$stderr.puts "To disable this check, please change `check_yarn_integrity`"
43+
$stderr.puts "to `false` in your webpacker config file (config/webpacker.yml)."
3944
$stderr.puts "\n\n"
4045
$stderr.puts output
4146
$stderr.puts "\n\n"
@@ -77,7 +82,10 @@ class Webpacker::Engine < ::Rails::Engine
7782
initializer "webpacker.bootstrap" do
7883
if defined?(Rails::Server) || defined?(Rails::Console)
7984
Webpacker.bootstrap
80-
Spring.after_fork { Webpacker.bootstrap } if defined?(Spring)
85+
if defined?(Spring)
86+
Spring.after_fork { Webpacker.bootstrap }
87+
Spring.watch(Webpacker.config.config_path)
88+
end
8189
end
8290
end
8391
end

test/compiler_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_default_watched_paths
2525
assert_equal Webpacker.compiler.send(:default_watched_paths), [
2626
"app/assets/**/*",
2727
"/etc/yarn/**/*",
28-
"test/test_app/app/javascript/**/*",
28+
"app/javascript/**/*",
2929
"yarn.lock",
3030
"package.json",
3131
"config/webpack/**/*"

test/test_app/config.ru

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file allows the `Rails.root` to be correctly determined.
2+
3+
require_relative "config/environment"
4+
5+
run Rails.application

test/test_helper.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
Rails.env = "production"
1010

11-
Webpacker.instance = Webpacker::Instance.new \
12-
root_path: Pathname.new(File.expand_path("test_app", __dir__)),
13-
config_path: Pathname.new(File.expand_path("./test_app/config/webpacker.yml", __dir__))
11+
Webpacker.instance = ::Webpacker::Instance.new
1412

1513
class Webpacker::Test < Minitest::Test
1614
private

0 commit comments

Comments
 (0)