Skip to content

Commit

Permalink
feat(@angular-devkit/build-angular): enable HMR for extracted CSS
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alan-agius4 committed Aug 13, 2020
1 parent fb351a4 commit 24fedb2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface BuildOptions {
bundleDependencies?: boolean;
externalDependencies?: string[];
watch?: boolean;
hmr?: boolean;
outputHashing?: string;
poll?: number;
deleteOutputPath?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,14 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
include: globalStylePaths,
test,
use: [
buildOptions.extractCss ? MiniCssExtractPlugin.loader : require.resolve('style-loader'),
buildOptions.extractCss
? {
loader: MiniCssExtractPlugin.loader,
options: {
hmr: buildOptions.hmr,
},
}
: require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
Expand Down
17 changes: 11 additions & 6 deletions packages/angular_devkit/build_angular/src/dev-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as ts from 'typescript';
import * as url from 'url';
import * as webpack from 'webpack';
import * as WebpackDevServer from 'webpack-dev-server';
import { normalizeExtraEntryPoints } from '../angular-cli-files/models/webpack-configs/utils';
import { IndexHtmlWebpackPlugin } from '../angular-cli-files/plugins/index-html-webpack-plugin';
import { checkPort } from '../angular-cli-files/utilities/check-port';
import { IndexHtmlTransform } from '../angular-cli-files/utilities/index-file/write-index-html';
Expand Down Expand Up @@ -175,7 +176,7 @@ export function serveWebpackBrowser(

// Add live reload config.
if (options.liveReload) {
_addLiveReload(options, browserOptions, webpackConfig, clientAddress, context.logger);
_addLiveReload(root, options, browserOptions, webpackConfig, clientAddress, context.logger);
} else if (options.hmr) {
context.logger.warn('Live reload is disabled. HMR option ignored.');
}
Expand Down Expand Up @@ -493,6 +494,7 @@ export function buildServePath(
* @private
*/
function _addLiveReload(
root: string,
options: DevServerBuilderOptions,
browserOptions: BrowserBuilderSchema,
webpackConfig: webpack.Configuration,
Expand Down Expand Up @@ -557,7 +559,6 @@ function _addLiveReload(
const entryPoints = [`${webpackDevServerPath}?${url.format(clientAddress)}${sockjsPath}`];
if (options.hmr) {
const webpackHmrLink = 'https://webpack.js.org/guides/hot-module-replacement';

logger.warn(tags.oneLine`NOTICE: Hot Module Replacement (HMR) is enabled for the dev server.`);

const showWarning = options.hmrWarning;
Expand All @@ -574,11 +575,15 @@ function _addLiveReload(
);
}
entryPoints.push('webpack/hot/dev-server');
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
if (browserOptions.extractCss) {
logger.warn(tags.oneLine`NOTICE: (HMR) does not allow for CSS hot reload
when used together with '--extract-css'.`);
if (browserOptions.styles?.length) {
// When HMR is enabled we need to add the css paths as part of the entrypoints
// because otherwise no JS bundle will contain the HMR accept code.
const normalizedStyles = normalizeExtraEntryPoints(browserOptions.styles, 'styles')
.map(style => path.resolve(root, style.input));
entryPoints.push(...normalizedStyles);
}

webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
}
if (typeof webpackConfig.entry !== 'object' || Array.isArray(webpackConfig.entry)) {
webpackConfig.entry = {};
Expand Down

0 comments on commit 24fedb2

Please sign in to comment.