Skip to content

Commit c9aed02

Browse files
committed
Run commands via yarn when not exist node_modules
Currently, commands(`webpack` and `webpacker-dev-server`) are need to exist under the `node_modules/.bin` directory. But when Yarn Plug'n'Play using, `node_modules/.bin` is not created. Ref: https://github.com/yarnpkg/rfcs/blob/master/accepted/0000-plug-an-play.md In this case, need to execute the command via `yarn`. So if `node_modules` does not exist, executes the command via `yarn`.
1 parent a7f7f53 commit c9aed02

File tree

5 files changed

+129
-7
lines changed

5 files changed

+129
-7
lines changed

lib/webpacker/dev_server_runner.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,22 @@ def detect_port!
4545

4646
def execute_cmd
4747
env = Webpacker::Compiler.env
48-
cmd = [
49-
"#{@node_modules_bin_path}/webpack-dev-server",
50-
"--config", @webpack_config
51-
]
48+
49+
cmd = if node_modules_bin_exist?
50+
["#{@node_modules_bin_path}/webpack-dev-server"]
51+
else
52+
["yarn", "webpack-dev-server"]
53+
end
54+
cmd += ["--config", @webpack_config]
5255
cmd += ["--progress", "--color"] if @pretty
5356

5457
Dir.chdir(@app_path) do
55-
exec env, *cmd
58+
Kernel.exec env, *cmd
5659
end
5760
end
61+
62+
def node_modules_bin_exist?
63+
File.exist?("#{@node_modules_bin_path}/webpack-dev-server")
64+
end
5865
end
5966
end

lib/webpacker/webpack_runner.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@ module Webpacker
55
class WebpackRunner < Webpacker::Runner
66
def run
77
env = Webpacker::Compiler.env
8-
cmd = [ "#{@node_modules_bin_path}/webpack", "--config", @webpack_config ] + @argv
8+
9+
cmd = if node_modules_bin_exist?
10+
["#{@node_modules_bin_path}/webpack"]
11+
else
12+
["yarn", "webpack"]
13+
end
14+
cmd += ["--config", @webpack_config] + @argv
915

1016
Dir.chdir(@app_path) do
11-
exec env, *cmd
17+
Kernel.exec env, *cmd
1218
end
1319
end
20+
21+
private
22+
def node_modules_bin_exist?
23+
File.exist?("#{@node_modules_bin_path}/webpack")
24+
end
1425
end
1526
end

test/dev_server_runner_test.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require "test_helper"
2+
require "webpacker/dev_server_runner"
3+
4+
class DevServerRunnerTest < Webpacker::Test
5+
def setup
6+
@original_node_env, ENV["NODE_ENV"] = ENV["NODE_ENV"], "development"
7+
@original_rails_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "development"
8+
end
9+
10+
def teardown
11+
ENV["NODE_ENV"] = @original_node_env
12+
ENV["RAILS_ENV"] = @original_rails_env
13+
end
14+
15+
def test_run_cmd_via_node_modules
16+
app_path = File.expand_path(".", Dir.pwd)
17+
cmd = ["#{app_path}/node_modules/.bin/webpack-dev-server", "--config", "#{test_app_path}/config/webpack/development.js"]
18+
19+
verify_command(cmd, use_node_modules: true)
20+
end
21+
22+
def test_run_cmd_via_yarn
23+
cmd = ["yarn", "webpack-dev-server", "--config", "#{test_app_path}/config/webpack/development.js"]
24+
25+
verify_command(cmd, use_node_modules: false)
26+
end
27+
28+
private
29+
def test_app_path
30+
File.expand_path("test_app", __dir__)
31+
end
32+
33+
def verify_command(cmd, use_node_modules: true)
34+
cwd = Dir.pwd
35+
Dir.chdir(test_app_path)
36+
37+
klass = Webpacker::DevServerRunner
38+
instance = klass.new([])
39+
mock = Minitest::Mock.new
40+
mock.expect(:call, nil, [{}, *cmd])
41+
42+
klass.stub(:new, instance) do
43+
instance.stub(:node_modules_bin_exist?, use_node_modules) do
44+
Kernel.stub(:exec, mock) { klass.run([]) }
45+
end
46+
end
47+
48+
mock.verify
49+
ensure
50+
Dir.chdir(cwd)
51+
end
52+
end

test/test_app/config/webpack/development.js

Whitespace-only changes.

test/webpack_runner_test.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require "test_helper"
2+
require "webpacker/webpack_runner"
3+
4+
class WebpackRunnerTest < Webpacker::Test
5+
def setup
6+
@original_node_env, ENV["NODE_ENV"] = ENV["NODE_ENV"], "development"
7+
@original_rails_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "development"
8+
end
9+
10+
def teardown
11+
ENV["NODE_ENV"] = @original_node_env
12+
ENV["RAILS_ENV"] = @original_rails_env
13+
end
14+
15+
def test_run_cmd_via_node_modules
16+
app_path = File.expand_path(".", Dir.pwd)
17+
cmd = ["#{app_path}/node_modules/.bin/webpack", "--config", "#{test_app_path}/config/webpack/development.js"]
18+
19+
verify_command(cmd, use_node_modules: true)
20+
end
21+
22+
def test_run_cmd_via_yarn
23+
cmd = ["yarn", "webpack", "--config", "#{test_app_path}/config/webpack/development.js"]
24+
25+
verify_command(cmd, use_node_modules: false)
26+
end
27+
28+
private
29+
def test_app_path
30+
File.expand_path("test_app", __dir__)
31+
end
32+
33+
def verify_command(cmd, use_node_modules: true)
34+
cwd = Dir.pwd
35+
Dir.chdir(test_app_path)
36+
37+
klass = Webpacker::WebpackRunner
38+
instance = klass.new([])
39+
mock = Minitest::Mock.new
40+
mock.expect(:call, nil, [{}, *cmd])
41+
42+
klass.stub(:new, instance) do
43+
instance.stub(:node_modules_bin_exist?, use_node_modules) do
44+
Kernel.stub(:exec, mock) { klass.run([]) }
45+
end
46+
end
47+
48+
mock.verify
49+
ensure
50+
Dir.chdir(cwd)
51+
end
52+
end

0 commit comments

Comments
 (0)