Skip to content

Commit 117b40d

Browse files
committed
support incremental build
1 parent f379ee1 commit 117b40d

File tree

4 files changed

+149
-126
lines changed

4 files changed

+149
-126
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"nsp": "^3.1.0",
4545
"pre-commit": "^1.2.2",
4646
"standard-version": "^4.3.0",
47-
"webpack": "^4.3.0",
47+
"webpack": "^4.4.0",
4848
"webpack-cli": "^2.0.13",
4949
"webpack-defaults": "^1.6.0",
5050
"webpack-dev-server": "^3.1.1"
@@ -56,7 +56,7 @@
5656
"node": ">= 6.11.5"
5757
},
5858
"peerDependencies": {
59-
"webpack": "^4.3.0"
59+
"webpack": "^4.4.0"
6060
},
6161
"pre-commit": "lint-staged",
6262
"lint-staged": {

src/index.js

+35
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const NS = path.dirname(fs.realpathSync(__filename));
1010

1111
const pluginName = 'mini-css-extract-plugin';
1212

13+
const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
14+
const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
15+
const REGEXP_NAME = /\[name\]/i;
16+
1317
class CssDependency extends webpack.Dependency {
1418
constructor({ identifier, content, media, sourceMap }, context, identifierIndex) {
1519
super();
@@ -61,6 +65,16 @@ class CssModule extends webpack.Module {
6165
return resource;
6266
}
6367

68+
updateCacheModule(module) {
69+
this.content = module.content;
70+
this.media = module.media;
71+
this.sourceMap = module.sourceMap;
72+
}
73+
74+
needRebuild() {
75+
return true;
76+
}
77+
6478
build(options, compilation, resolver, fileSystem, callback) {
6579
this.buildInfo = {};
6680
this.buildMeta = {};
@@ -131,6 +145,7 @@ class MiniCssExtractPlugin {
131145
contentHashType: NS,
132146
},
133147
identifier: `mini-css-extract-plugin.${chunk.id}`,
148+
hash: chunk.contentHash[NS],
134149
});
135150
}
136151
});
@@ -145,9 +160,29 @@ class MiniCssExtractPlugin {
145160
contentHashType: NS,
146161
},
147162
identifier: `mini-css-extract-plugin.${chunk.id}`,
163+
hash: chunk.contentHash[NS],
148164
});
149165
}
150166
});
167+
compilation.mainTemplate.hooks.hashForChunk.tap(
168+
pluginName,
169+
(hash, chunk) => {
170+
const { chunkFilename } = this.options;
171+
if (REGEXP_CHUNKHASH.test(chunkFilename)) {
172+
hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
173+
}
174+
if (REGEXP_CONTENTHASH.test(chunkFilename)) {
175+
hash.update(
176+
JSON.stringify(
177+
chunk.getChunkMaps(true).contentHash[NS] || {},
178+
),
179+
);
180+
}
181+
if (REGEXP_NAME.test(chunkFilename)) {
182+
hash.update(JSON.stringify(chunk.getChunkMaps(true).name));
183+
}
184+
},
185+
);
151186
compilation.hooks.contentHash.tap(pluginName, (chunk) => {
152187
const { outputOptions } = compilation;
153188
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;

test/manual/webpack.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Self = require('../../');
33
module.exports = {
44
mode: 'development',
55
output: {
6+
chunkFilename: "[contenthash].js",
67
publicPath: '/dist/',
78
},
89
module: {
@@ -19,6 +20,7 @@ module.exports = {
1920
plugins: [
2021
new Self({
2122
filename: '[name].css',
23+
chunkFilename: "[contenthash].css",
2224
}),
2325
],
2426
devServer: {

0 commit comments

Comments
 (0)