Skip to content

Commit 8614cba

Browse files
committed
feat(@angular/build): enable component template hot replacement by default
When using the `application` builder (default for new projects) with the development server, component template only changes will now automatically replace the template within the running application without a full reload of the page. No application code changes are necessary and both file-based (`templateUrl`) and inline (`template`) component templates are supported. Additionally, changing a components styles in combination with a template change is also supported for hot replacement. This includes both inline and file-based changes. If any issues are encountered or it is preferred to not hot replace component templates, the `NG_HMR_TEMPLATES=0` environment variable can be used to disable the feature. Setting the `liveReload` option or `hmr` option to false will also disable all updates.
1 parent 10a5b8b commit 8614cba

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

packages/angular/build/src/builders/dev-server/vite-server.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,14 @@ export async function* serveWithVite(
138138
process.setSourceMapsEnabled(true);
139139
}
140140

141-
// Enable to support component style hot reloading (`NG_HMR_CSTYLES=0` can be used to disable selectively)
141+
// Enable to support link-based component style hot reloading (`NG_HMR_CSTYLES=0` can be used to disable selectively)
142142
browserOptions.externalRuntimeStyles =
143143
serverOptions.liveReload && serverOptions.hmr && useComponentStyleHmr;
144144

145-
// Enable to support component template hot replacement (`NG_HMR_TEMPLATE=1` can be used to enable)
146-
browserOptions.templateUpdates = !!serverOptions.liveReload && useComponentTemplateHmr;
147-
if (browserOptions.templateUpdates) {
148-
context.logger.warn(
149-
'Experimental support for component template hot replacement has been enabled via the "NG_HMR_TEMPLATE" environment variable.',
150-
);
151-
}
145+
// Enable to support component template hot replacement (`NG_HMR_TEMPLATE=0` can be used to disable selectively)
146+
// This will also replace file-based/inline styles as code if external runtime styles are not enabled.
147+
browserOptions.templateUpdates =
148+
serverOptions.liveReload && serverOptions.hmr && useComponentTemplateHmr;
152149

153150
// Setup the prebundling transformer that will be shared across Vite prebundling requests
154151
const prebundleTransformer = new JavaScriptTransformer(

packages/angular/build/src/utils/environment-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export const useComponentStyleHmr =
107107

108108
const hmrComponentTemplateVariable = process.env['NG_HMR_TEMPLATES'];
109109
export const useComponentTemplateHmr =
110-
isPresent(hmrComponentTemplateVariable) && isEnabled(hmrComponentTemplateVariable);
110+
!isPresent(hmrComponentTemplateVariable) || !isDisabled(hmrComponentTemplateVariable);
111111

112112
const partialSsrBuildVariable = process.env['NG_BUILD_PARTIAL_SSR'];
113113
export const usePartialSsrBuild =

0 commit comments

Comments
 (0)