Skip to content

Commit fbe68b4

Browse files
webpack: Add css-hot-loader to remove flash on unstyled content.
This commit removes the flash on unstyled content while in dev mode that was caused by the use of style-loader. Instead it enables mini-css-extract-plugin in dev in combination with css-hot-loader which enables HMR for development. This is because mini-css-extract-plugin does not currently support HMR out of the box. It also adds a SourceMapDevtoolPlugin to enable sourcemaps with css since mini-css breaks sourcemaps when used in combination with the cheap-module-evel-source-map setting. Related issues: webpack-contrib/mini-css-extract-plugin#34 webpack-contrib/mini-css-extract-plugin#29
1 parent 1ce18a4 commit fbe68b4

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@types/webpack": "4.1.3",
1010
"blueimp-md5": "2.10.0",
1111
"clipboard": "2.0.0",
12+
"css-hot-loader": "1.3.9",
1213
"css-loader": "0.28.11",
1314
"emoji-datasource-apple": "3.0.0",
1415
"emoji-datasource-emojione": "3.0.0",

tools/webpack.config.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,14 @@ const MiniCssExtractPlugin = require("mini-css-extract-plugin");
55

66
const assets = require('./webpack.assets.json');
77

8-
// Currently we're using style-loader for local dev
9-
// because MiniCssExtractPlugin doesn't support
10-
// HMR as yet. When this changes we can switch
11-
// over to MiniCssExtractPlugin which will Also
12-
// solve the flash of unstsyled elements on page load.
13-
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/34
14-
function getCSSLoader(isProd: boolean) {
8+
// Adds on css-hot-loader in dev mode
9+
function getHotCSS(bundle:any[], isProd:boolean) {
1510
if(isProd) {
16-
return MiniCssExtractPlugin.loader
17-
}
18-
return {
19-
loader: 'style-loader',
20-
options: {
21-
sourceMap: true
22-
}
11+
return bundle;
2312
}
13+
return [
14+
'css-hot-loader',
15+
].concat(bundle);
2416
}
2517
export default (env?: string) : webpack.Configuration => {
2618
const production: boolean = env === "production";
@@ -83,21 +75,21 @@ export default (env?: string) : webpack.Configuration => {
8375
// regular css files
8476
{
8577
test: /\.css$/,
86-
use: [
87-
getCSSLoader(production),
78+
use: getHotCSS([
79+
MiniCssExtractPlugin.loader,
8880
{
8981
loader: 'css-loader',
9082
options: {
9183
sourceMap: true
9284
}
9385
},
94-
],
86+
], production),
9587
},
9688
// sass / scss loader
9789
{
9890
test: /\.(sass|scss)$/,
99-
use: [
100-
getCSSLoader(production),
91+
use: getHotCSS([
92+
MiniCssExtractPlugin.loader,
10193
{
10294
loader: 'css-loader',
10395
options: {
@@ -110,7 +102,7 @@ export default (env?: string) : webpack.Configuration => {
110102
sourceMap: true
111103
}
112104
}
113-
],
105+
], production),
114106
},
115107
// load fonts and files
116108
{
@@ -171,6 +163,14 @@ export default (env?: string) : webpack.Configuration => {
171163
new MiniCssExtractPlugin({
172164
filename: "[name].css",
173165
chunkFilename: "[id].css"
166+
}),
167+
// We use SourceMapDevToolPlugin in order to enable SourceMaps
168+
// in combination with mini-css-extract-plugin and
169+
// the devtool setting of cheap-module-eval-source-map.
170+
// Without this plugin source maps won't work with that combo.
171+
// See https://github.com/webpack-contrib/mini-css-extract-plugin/issues/29
172+
new webpack.SourceMapDevToolPlugin({
173+
filename: "[file].map"
174174
})
175175
];
176176

yarn.lock

+9-1
Original file line numberDiff line numberDiff line change
@@ -2054,6 +2054,14 @@ [email protected]:
20542054
version "0.0.4"
20552055
resolved "https://registry.yarnpkg.com/css-color-names/-/css-color-names-0.0.4.tgz#808adc2e79cf84738069b646cb20ec27beb629e0"
20562056

2057+
css-hot-loader@^1.3.9:
2058+
version "1.3.9"
2059+
resolved "https://registry.yarnpkg.com/css-hot-loader/-/css-hot-loader-1.3.9.tgz#ed22b41126920134a4a2246d7d32113e2425c754"
2060+
dependencies:
2061+
loader-utils "^1.1.0"
2062+
lodash "^4.17.5"
2063+
normalize-url "^1.9.1"
2064+
20572065
20582066
version "0.28.11"
20592067
resolved "https://registry.yarnpkg.com/css-loader/-/css-loader-0.28.11.tgz#c3f9864a700be2711bb5a2462b2389b1a392dab7"
@@ -6282,7 +6290,7 @@ [email protected]:
62826290
query-string "^5.0.1"
62836291
sort-keys "^2.0.0"
62846292

6285-
normalize-url@^1.4.0:
6293+
normalize-url@^1.4.0, normalize-url@^1.9.1:
62866294
version "1.9.1"
62876295
resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-1.9.1.tgz#2cc0d66b31ea23036458436e3620d85954c66c3c"
62886296
dependencies:

0 commit comments

Comments
 (0)