Skip to content

Commit 6356468

Browse files
odlpgauravtiwari
authored andcommitted
Make "webpacker:yarn_install" task env aware (#1331)
* Make webpacker:yarn_install task env aware When webpacker compiles in a non-production environment it's removing node_modules which are dev dependencies. This is problematic on CI for example because these dependencies may be required. * Use alternative to Bundler.with_clean_env This was failing on CI presumably because BUNDLE_GEMFILE was being lost, causing the 4.2.x Gemfile tests to fail. * Remove explicit production flag from Yarn Install task If the NODE_ENV is set then Yarn will behave accordingly (e.g. skip devDependencies if NODE_ENV=production). Based on discussion: rails/webpacker#1331 (comment) * Use existing with_node_env helper
1 parent be7e9e9 commit 6356468

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

lib/tasks/webpacker/yarn_install.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace :webpacker do
22
desc "Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn"
33
task :yarn_install do
4-
system "yarn install --no-progress --frozen-lockfile --production"
4+
system "yarn install --no-progress"
55
end
66
end

test/rake_tasks_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,42 @@ def test_rake_task_webpacker_check_binstubs
2222
refute_includes output, "webpack binstubs not found."
2323
end
2424

25+
def test_rake_webpacker_yarn_install_in_non_production_environments
26+
assert_includes test_app_dev_dependencies, "right-pad"
27+
28+
Webpacker.with_node_env("test") do
29+
Dir.chdir(test_app_path) do
30+
`bundle exec rake webpacker:yarn_install`
31+
end
32+
end
33+
34+
assert_includes installed_node_module_names, "right-pad",
35+
"Expected dev dependencies to be installed"
36+
end
37+
38+
def test_rake_webpacker_yarn_install_in_production_environment
39+
Webpacker.with_node_env("production") do
40+
Dir.chdir(test_app_path) do
41+
`bundle exec rake webpacker:yarn_install`
42+
end
43+
end
44+
45+
refute_includes installed_node_module_names, "right-pad",
46+
"Expected only production dependencies to be installed"
47+
end
48+
2549
private
2650
def test_app_path
2751
File.expand_path("test_app", __dir__)
2852
end
53+
54+
def test_app_dev_dependencies
55+
package_json = File.expand_path("package.json", test_app_path)
56+
JSON.parse(File.read(package_json))["devDependencies"]
57+
end
58+
59+
def installed_node_module_names
60+
node_modules_path = File.expand_path("node_modules", test_app_path)
61+
Dir.chdir(node_modules_path) { Dir.glob("*") }
62+
end
2963
end

test/test_app/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "test_app",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"private": true,
7+
"dependencies": {
8+
"left-pad": "^1.2.0"
9+
},
10+
"devDependencies": {
11+
"right-pad": "^1.0.1"
12+
}
13+
}

test/test_app/yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
left-pad@^1.2.0:
6+
version "1.2.0"
7+
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.2.0.tgz#d30a73c6b8201d8f7d8e7956ba9616087a68e0ee"
8+
9+
right-pad@^1.0.1:
10+
version "1.0.1"
11+
resolved "https://registry.yarnpkg.com/right-pad/-/right-pad-1.0.1.tgz#8ca08c2cbb5b55e74dafa96bf7fd1a27d568c8d0"

0 commit comments

Comments
 (0)