Skip to content

Commit 24fedb2

Browse files
committed
feat(@angular-devkit/build-angular): enable HMR for extracted CSS
The latest versions of mini-css-extract-plugin support HMR, see: https://github.com/webpack-contrib/mini-css-extract-plugin#hot-module-reloading-hmr for more details
1 parent fb351a4 commit 24fedb2

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/models/build-options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface BuildOptions {
4747
bundleDependencies?: boolean;
4848
externalDependencies?: string[];
4949
watch?: boolean;
50+
hmr?: boolean;
5051
outputHashing?: string;
5152
poll?: number;
5253
deleteOutputPath?: boolean;

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/styles.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,14 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
218218
include: globalStylePaths,
219219
test,
220220
use: [
221-
buildOptions.extractCss ? MiniCssExtractPlugin.loader : require.resolve('style-loader'),
221+
buildOptions.extractCss
222+
? {
223+
loader: MiniCssExtractPlugin.loader,
224+
options: {
225+
hmr: buildOptions.hmr,
226+
},
227+
}
228+
: require.resolve('style-loader'),
222229
{
223230
loader: require.resolve('css-loader'),
224231
options: {

packages/angular_devkit/build_angular/src/dev-server/index.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as ts from 'typescript';
2222
import * as url from 'url';
2323
import * as webpack from 'webpack';
2424
import * as WebpackDevServer from 'webpack-dev-server';
25+
import { normalizeExtraEntryPoints } from '../angular-cli-files/models/webpack-configs/utils';
2526
import { IndexHtmlWebpackPlugin } from '../angular-cli-files/plugins/index-html-webpack-plugin';
2627
import { checkPort } from '../angular-cli-files/utilities/check-port';
2728
import { IndexHtmlTransform } from '../angular-cli-files/utilities/index-file/write-index-html';
@@ -175,7 +176,7 @@ export function serveWebpackBrowser(
175176

176177
// Add live reload config.
177178
if (options.liveReload) {
178-
_addLiveReload(options, browserOptions, webpackConfig, clientAddress, context.logger);
179+
_addLiveReload(root, options, browserOptions, webpackConfig, clientAddress, context.logger);
179180
} else if (options.hmr) {
180181
context.logger.warn('Live reload is disabled. HMR option ignored.');
181182
}
@@ -493,6 +494,7 @@ export function buildServePath(
493494
* @private
494495
*/
495496
function _addLiveReload(
497+
root: string,
496498
options: DevServerBuilderOptions,
497499
browserOptions: BrowserBuilderSchema,
498500
webpackConfig: webpack.Configuration,
@@ -557,7 +559,6 @@ function _addLiveReload(
557559
const entryPoints = [`${webpackDevServerPath}?${url.format(clientAddress)}${sockjsPath}`];
558560
if (options.hmr) {
559561
const webpackHmrLink = 'https://webpack.js.org/guides/hot-module-replacement';
560-
561562
logger.warn(tags.oneLine`NOTICE: Hot Module Replacement (HMR) is enabled for the dev server.`);
562563

563564
const showWarning = options.hmrWarning;
@@ -574,11 +575,15 @@ function _addLiveReload(
574575
);
575576
}
576577
entryPoints.push('webpack/hot/dev-server');
577-
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
578-
if (browserOptions.extractCss) {
579-
logger.warn(tags.oneLine`NOTICE: (HMR) does not allow for CSS hot reload
580-
when used together with '--extract-css'.`);
578+
if (browserOptions.styles?.length) {
579+
// When HMR is enabled we need to add the css paths as part of the entrypoints
580+
// because otherwise no JS bundle will contain the HMR accept code.
581+
const normalizedStyles = normalizeExtraEntryPoints(browserOptions.styles, 'styles')
582+
.map(style => path.resolve(root, style.input));
583+
entryPoints.push(...normalizedStyles);
581584
}
585+
586+
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
582587
}
583588
if (typeof webpackConfig.entry !== 'object' || Array.isArray(webpackConfig.entry)) {
584589
webpackConfig.entry = {};

0 commit comments

Comments
 (0)