Skip to content

Commit e5ce2f8

Browse files
Disable HMR by default
Agent-Logs-Url: https://github.com/NativeScript/nativescript-cli/sessions/58c98aaf-7d9e-410b-b92b-0708f4ff4655 Co-authored-by: NathanWalker <457187+NathanWalker@users.noreply.github.com>
1 parent 229c727 commit e5ce2f8

4 files changed

Lines changed: 62 additions & 19 deletions

File tree

lib/data/prepare-data.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ export class PrepareData extends ControllerDataBase {
3333
Object.assign(env, data.env);
3434
}
3535

36+
const hmr =
37+
data.hmr ||
38+
data.useHotModuleReload ||
39+
env.hmr === true ||
40+
env.hmr === "true";
41+
3642
this.release = data.release;
37-
this.hmr = data.hmr || data.useHotModuleReload;
43+
this.hmr = hmr;
3844
this.env = {
3945
...env,
40-
hmr: data.hmr || data.useHotModuleReload,
46+
hmr,
4147
};
4248
this.watch = data.watch;
4349
if (_.isBoolean(data.watchNative)) {

lib/options.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,16 @@ export class Options {
5050
}
5151

5252
this.argv.bundle = "webpack";
53+
const isHmrEnabled = this.isHmrEnabled();
5354

5455
// Check if the user has explicitly provide --hmr and --release options from command line
55-
if (this.initialArgv.release && this.initialArgv.hmr) {
56+
if (this.argv.release && isHmrEnabled) {
5657
this.$errors.fail(
5758
"The options --release and --hmr cannot be used simultaneously."
5859
);
5960
}
6061

61-
if (this.argv.hmr) {
62-
this.argv.hmr = !this.argv.release;
63-
}
62+
this.argv.hmr = isHmrEnabled && !this.argv.release;
6463

6564
if (this.argv.debugBrk) {
6665
// we cannot use HMR along with debug-brk because we have to restart the app
@@ -168,7 +167,7 @@ export class Options {
168167
hmr: {
169168
type: OptionType.Boolean,
170169
hasSensitiveValue: false,
171-
default: true,
170+
default: false,
172171
},
173172
collection: {
174173
type: OptionType.String,
@@ -379,6 +378,21 @@ export class Options {
379378
return optionName;
380379
}
381380

381+
private isHmrEnabled(): boolean {
382+
if (process.argv.includes("--no-hmr")) {
383+
return false;
384+
}
385+
386+
return (
387+
this.isTruthyBooleanValue(this.argv.hmr) ||
388+
this.isTruthyBooleanValue(_.get(this.argv, "env.hmr"))
389+
);
390+
}
391+
392+
private isTruthyBooleanValue(value: unknown): boolean {
393+
return value === true || value === "true";
394+
}
395+
382396
private setArgv(): void {
383397
const opts: IDictionary<IDashedOption> = <IDictionary<IDashedOption>>{};
384398
_.each(this.options, (value: IDashedOption, key: string) => {

package-lock.json

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/options.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ describe("options", () => {
319319
expectedError:
320320
"The options --release and --hmr cannot be used simultaneously.",
321321
},
322+
{
323+
name: "--release --env.hmr",
324+
args: ["--release", "--env.hmr"],
325+
expectedError:
326+
"The options --release and --hmr cannot be used simultaneously.",
327+
},
322328
];
323329

324330
_.each(testCasesExpectingToThrow, (testCase) => {
@@ -341,14 +347,29 @@ describe("options", () => {
341347
describe("hmr option", () => {
342348
const testCases = [
343349
{
344-
name: "should set hmr to true by default",
350+
name: "should set hmr to false by default",
351+
expectedHmrValue: false,
352+
},
353+
{
354+
name: "should set hmr to true when --hmr is provided",
355+
args: ["--hmr"],
345356
expectedHmrValue: true,
346357
},
347358
{
348359
name: "should set hmr to false when --no-hmr is provided",
349360
args: ["--no-hmr"],
350361
expectedHmrValue: false,
351362
},
363+
{
364+
name: "should set hmr to true when --env.hmr is provided",
365+
args: ["--env.hmr"],
366+
expectedHmrValue: true,
367+
},
368+
{
369+
name: "should keep hmr disabled when --no-hmr and --env.hmr are provided",
370+
args: ["--no-hmr", "--env.hmr"],
371+
expectedHmrValue: false,
372+
},
352373
{
353374
name:
354375
"should set hmr to false when provided through dashed options from command",

0 commit comments

Comments
 (0)