Skip to content

Commit 318c164

Browse files
committed
refactor(@angular/build): define an internal ngHmrMode value
An `ngHmrMode` boolean value will now be defined within application bundles. The value is based on the development server's `hmr` option with `true` when HMR is enabled and `false` when disabled. For all application builds, the value will be `false`. `ngHmrMode` is similar in behavior to `ngServeMode` or `ngDevMode`. It will not be present in the output code unless referenced and in those cases only the final boolean value will be present if not optimized out of the final code. The value is not considered part of the public API and may change in the future.
1 parent f780e8b commit 318c164

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/angular/build/src/builders/dev-server/tests/behavior/component-updates_spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,39 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
4747
expect(response?.headers.get('Cache-Control')).toEqual('no-cache');
4848
expect(output).toBe('');
4949
});
50+
51+
it('sets ngHmrMode define to true when HMR is enabled', async () => {
52+
harness.useTarget('serve', {
53+
...BASE_OPTIONS,
54+
hmr: true,
55+
});
56+
57+
await harness.writeFile(
58+
'src/main.ts',
59+
'declare const ngHmrMode: boolean; console.log(`HMR=${ngHmrMode}`);',
60+
);
61+
62+
const { result, content } = await executeOnceAndFetch(harness, 'main.js');
63+
64+
expect(result?.success).toBeTrue();
65+
expect(content).toContain('HMR=${true}');
66+
});
67+
68+
it('sets ngHmrMode define to false when HMR is disabled', async () => {
69+
harness.useTarget('serve', {
70+
...BASE_OPTIONS,
71+
hmr: false,
72+
});
73+
74+
await harness.writeFile(
75+
'src/main.ts',
76+
'declare const ngHmrMode: boolean; console.log(`HMR=${ngHmrMode}`);',
77+
);
78+
79+
const { result, content } = await executeOnceAndFetch(harness, 'main.js');
80+
81+
expect(result?.success).toBeTrue();
82+
expect(content).toContain('HMR=${false}');
83+
});
5084
});
5185
});

packages/angular/build/src/tools/esbuild/application-code-bundle.ts

+1
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu
600600
...(optimizationOptions.scripts ? { 'ngDevMode': 'false' } : undefined),
601601
'ngJitMode': jit ? 'true' : 'false',
602602
'ngServerMode': 'false',
603+
'ngHmrMode': options.templateUpdates ? 'true' : 'false',
603604
},
604605
loader: loaderExtensions,
605606
footer,

0 commit comments

Comments
 (0)