Skip to content

Commit 1c8eb1f

Browse files
authored
fix: exclude mini-css loader modules (#382)
* fix: exclude mini-css loader modules mini-css does not emit assets when built from webpack's memory cache or hard-source's disk cache. Exclude the modules that lead to the child compilations that emit the assets so the assets are always emitted. This has a minor performance change instead of a large performance hit because the bulk of the css work is still cached outside of these excluded modules. * fix: text copy-webpack-plugin * fixup! fix: exclude mini-css loader modules * fixup! fix: exclude mini-css loader modules * fix: disable hard modules mini css test
1 parent 8a68f29 commit 1c8eb1f

File tree

12 files changed

+125
-1
lines changed

12 files changed

+125
-1
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,10 @@ class HardSourceWebpackPlugin {
433433
const TransformModuleErrorsPlugin = require('./lib/TransformModuleErrorsPlugin');
434434
const SupportExtractTextPlugin = require('./lib/SupportExtractTextPlugin');
435435
let SupportMiniCssExtractPlugin;
436+
let ExcludeMiniCssModulePlugin;
436437
if (webpackFeatures.generator) {
437438
SupportMiniCssExtractPlugin = require('./lib/SupportMiniCssExtractPlugin');
439+
ExcludeMiniCssModulePlugin = require('./lib/ExcludeMiniCssModulePlugin');
438440
}
439441
const TransformDependencyBlockPlugin = require('./lib/TransformDependencyBlockPlugin');
440442
const TransformBasicDependencyPlugin = require('./lib/TransformBasicDependencyPlugin');
@@ -477,6 +479,7 @@ class HardSourceWebpackPlugin {
477479

478480
if (SupportMiniCssExtractPlugin) {
479481
new SupportMiniCssExtractPlugin().apply(compiler);
482+
new ExcludeMiniCssModulePlugin().apply(compiler);
480483
}
481484

482485
new TransformDependencyBlockPlugin({

lib/ExcludeMiniCssModulePlugin.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const pluginCompat = require('./util/plugin-compat');
2+
3+
/**
4+
* Exclude modules with CssDependency. These modules are what mini-css keys
5+
* child compilations on. Excluding them the child compilations and their
6+
* assets are built every build. This has a minor performance cost as the bulk
7+
* of the work for css is still cached.
8+
*/
9+
class ExcludeMiniCssModulePlugin {
10+
apply(compiler) {
11+
let CssDependency;
12+
13+
pluginCompat.tap(
14+
compiler,
15+
'make',
16+
'SupportMiniCssExtractPlugin',
17+
({ dependencyFactories }) => {
18+
const Dependencies = dependencyFactories.keys();
19+
for (const Dep of Dependencies) {
20+
if (Dep.name === 'CssDependency') {
21+
CssDependency = Dep;
22+
break;
23+
}
24+
}
25+
},
26+
);
27+
28+
pluginCompat.tap(
29+
compiler,
30+
'_hardSourceAfterFreezeModule',
31+
'HardMiniCssExtractPlugin',
32+
(frozen, module, extra) => {
33+
if (
34+
CssDependency &&
35+
module.dependencies.some(dep => dep instanceof CssDependency)
36+
) {
37+
return null;
38+
}
39+
return frozen;
40+
},
41+
);
42+
}
43+
}
44+
45+
module.exports = ExcludeMiniCssModulePlugin;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"babel-preset-env": "^1.6.1",
4444
"cacache": "^11.0.2",
4545
"chai": "^3.5.0",
46+
"copy-webpack-plugin": "^4.5.1",
4647
"css-loader": "^0.27.0",
4748
"extract-text-webpack-plugin": "^3.0.0 || ^2.0.0 || ^1.0.1",
4849
"file-loader": "^1.0.0 || ^0.11.0 || ^0.10.1",
11.9 KB
Loading

tests/fixtures/plugin-copy/index.js

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var HardSourceWebpackPlugin = require('../../..');
2+
3+
var CopyWebpackPlugin = require('copy-webpack-plugin');
4+
5+
module.exports = {
6+
context: __dirname,
7+
entry: './index.js',
8+
output: {
9+
path: __dirname + '/tmp',
10+
filename: 'main.js',
11+
},
12+
module: {
13+
rules: [
14+
{
15+
test: /\.png$/,
16+
loader: 'file-loader',
17+
},
18+
],
19+
},
20+
plugins: [
21+
new HardSourceWebpackPlugin({
22+
cacheDirectory: 'cache',
23+
}),
24+
new CopyWebpackPlugin(['images']),
25+
],
26+
};
11.9 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.hello {
2+
color: blue;
3+
background: url('image.png');
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./index.css');
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var MiniCssExtractPlugin = require('mini-css-extract-plugin');
2+
3+
var HardSourceWebpackPlugin = require('../../..');
4+
5+
module.exports = {
6+
context: __dirname,
7+
entry: './index.js',
8+
output: {
9+
path: __dirname + '/tmp',
10+
filename: 'main.js',
11+
},
12+
module: {
13+
rules: [
14+
{
15+
test: /\.css$/,
16+
use: [
17+
MiniCssExtractPlugin.loader,
18+
'css-loader'
19+
]
20+
},
21+
{
22+
test: /\.png$/,
23+
use: [
24+
'file-loader'
25+
]
26+
}
27+
]
28+
},
29+
plugins: [
30+
new MiniCssExtractPlugin(),
31+
new HardSourceWebpackPlugin({
32+
cacheDirectory: 'cache',
33+
environmentHash: {
34+
root: __dirname + '/../../..',
35+
},
36+
}),
37+
],
38+
};

0 commit comments

Comments
 (0)