Skip to content

Commit

Permalink
fix: hmr: false doesn't disable Hot Module Replacement
Browse files Browse the repository at this point in the history
* webpacker-dev-server >= 4 enables HMR by default, so explicitly disable when `hmr: false`
  • Loading branch information
thedanbob committed Dec 22, 2023
1 parent 0f89275 commit ec1d7a2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ _Please add entries here for your pull requests that are not yet released._
- Recommend `server` option instead of deprecated `https` option when `--https` is provided [PR 380](https://github.com/shakacode/shakapacker/pull/380) by [G-Rath](https://github.com/g-rath)
- Recompile assets on asset host change [PR 364](https://github.com/shakacode/shakapacker/pull/364) by [ahangarha](https://github.com/ahangarha).
- Add deprecation warning for `https` option in `shakapacker.yml` (use `server: 'https'` instead) [PR 382](https://github.com/shakacode/shakapacker/pull/382) by [G-Rath](https://github.com/g-rath).
- Disable Hot Module Replacement in `webpack-dev-server` when `hmr: false` [PR 392](https://github.com/shakacode/shakapacker/pull/392) by [thedanbob](https://github.com/thedanbob).

## [v7.1.0] - September 30, 2023

Expand Down
8 changes: 6 additions & 2 deletions lib/shakapacker/dev_server_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ def execute_cmd
cmd += ["--config", @webpack_config]
cmd += ["--progress", "--color"] if @pretty

cmd += ["--hot"] if @hot
cmd += ["only"] if @hot == "only"
if @hot
cmd += ["--hot"]
cmd += ["only"] if @hot == "only"
else
cmd += ["--no-hot"]
end

cmd += @argv

Expand Down
24 changes: 12 additions & 12 deletions spec/backward_compatibility_specs/dev_server_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@
require "package_json"

it "uses the expected package manager", unless: fallback_manager == "yarn_classic" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"])

manager_name = fallback_manager.split("_")[0]

expect(cmd).to start_with(manager_name)
end

it "runs the command using the manager" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"])

verify_command(cmd)
end

it "passes on arguments" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--quiet"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot", "--quiet"])

verify_command(cmd, argv: (["--quiet"]))
end

it "does not automatically pass the --https flag" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"])

allow(Shakapacker::DevServer).to receive(:new).and_return(
double(
Expand All @@ -62,7 +62,7 @@
end

it "supports the https flag" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--https"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot", "--https"])

allow(Shakapacker::DevServer).to receive(:new).and_return(
double(
Expand Down Expand Up @@ -94,7 +94,7 @@
end

it "accepts environment variables" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"])
env = Webpacker::Compiler.env.dup

# ENV["WEBPACKER_CONFIG"] is the interface and env["SHAKAPACKER_CONFIG"] is internal
Expand All @@ -110,25 +110,25 @@
with_use_package_json_gem(enabled: false)

it "supports running via node modules" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"]

verify_command(cmd, use_node_modules: true)
end

it "supports running via yarn" do
cmd = ["yarn", "webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
cmd = ["yarn", "webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"]

verify_command(cmd, use_node_modules: false)
end

it "passes on arguments" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--quiet"]
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot", "--quiet"]

verify_command(cmd, argv: (["--quiet"]))
end

it "does not automatically pass the --https flag" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"]

allow(Shakapacker::DevServer).to receive(:new).and_return(
double(
Expand All @@ -144,7 +144,7 @@
end

it "supports the https flag" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--https"]
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot", "--https"]

allow(Shakapacker::DevServer).to receive(:new).and_return(
double(
Expand Down Expand Up @@ -176,7 +176,7 @@
end

it "accepts environment variables" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"]
env = Webpacker::Compiler.env.dup

# ENV["WEBPACKER_CONFIG"] is the interface and env["SHAKAPACKER_CONFIG"] is internal
Expand Down
24 changes: 12 additions & 12 deletions spec/shakapacker/dev_server_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@
require "package_json"

it "uses the expected package manager", unless: fallback_manager == "yarn_classic" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"])

manager_name = fallback_manager.split("_")[0]

expect(cmd).to start_with(manager_name)
end

it "runs the command using the manager" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"])

verify_command(cmd)
end

it "passes on arguments" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--quiet"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot", "--quiet"])

verify_command(cmd, argv: (["--quiet"]))
end

it "does not automatically pass the --https flag" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"])

allow(Shakapacker::DevServer).to receive(:new).and_return(
double(
Expand All @@ -61,7 +61,7 @@
end

it "supports the https flag" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--https"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot", "--https"])

allow(Shakapacker::DevServer).to receive(:new).and_return(
double(
Expand Down Expand Up @@ -93,7 +93,7 @@
end

it "accepts environment variables" do
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"])

env = Shakapacker::Compiler.env.dup
ENV["SHAKAPACKER_CONFIG"] = env["SHAKAPACKER_CONFIG"] = "#{test_app_path}/config/shakapacker_other_location.yml"
Expand All @@ -108,25 +108,25 @@
with_use_package_json_gem(enabled: false)

it "supports running via node modules" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"]

verify_command(cmd, use_node_modules: true)
end

it "supports running via yarn" do
cmd = ["yarn", "webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
cmd = ["yarn", "webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"]

verify_command(cmd, use_node_modules: false)
end

it "passes on arguments" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--quiet"]
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot", "--quiet"]

verify_command(cmd, argv: (["--quiet"]))
end

it "does not automatically pass the --https flag" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"]

allow(Shakapacker::DevServer).to receive(:new).and_return(
double(
Expand All @@ -142,7 +142,7 @@
end

it "supports the https flag" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--https"]
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot", "--https"]

allow(Shakapacker::DevServer).to receive(:new).and_return(
double(
Expand Down Expand Up @@ -190,7 +190,7 @@
end

it "accepts environment variables" do
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--no-hot"]
env = Shakapacker::Compiler.env.dup

ENV["SHAKAPACKER_CONFIG"] = env["SHAKAPACKER_CONFIG"] = "#{test_app_path}/config/shakapacker_other_location.yml"
Expand Down

0 comments on commit ec1d7a2

Please sign in to comment.