Skip to content

Commit a05901d

Browse files
committed
support incremental build
1 parent f379ee1 commit a05901d

File tree

4 files changed

+150
-126
lines changed

4 files changed

+150
-126
lines changed

package.json

Lines changed: 2 additions & 2 deletions
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

Lines changed: 36 additions & 0 deletions
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,17 @@ class CssModule extends webpack.Module {
6165
return resource;
6266
}
6367

68+
updateCacheModule(module) {
69+
console.log('updateCacheModule', module.content);
70+
this.content = module.content;
71+
this.media = module.media;
72+
this.sourceMap = module.sourceMap;
73+
}
74+
75+
needRebuild() {
76+
return true;
77+
}
78+
6479
build(options, compilation, resolver, fileSystem, callback) {
6580
this.buildInfo = {};
6681
this.buildMeta = {};
@@ -131,6 +146,7 @@ class MiniCssExtractPlugin {
131146
contentHashType: NS,
132147
},
133148
identifier: `mini-css-extract-plugin.${chunk.id}`,
149+
hash: chunk.contentHash[NS],
134150
});
135151
}
136152
});
@@ -145,9 +161,29 @@ class MiniCssExtractPlugin {
145161
contentHashType: NS,
146162
},
147163
identifier: `mini-css-extract-plugin.${chunk.id}`,
164+
hash: chunk.contentHash[NS],
148165
});
149166
}
150167
});
168+
compilation.mainTemplate.hooks.hashForChunk.tap(
169+
pluginName,
170+
(hash, chunk) => {
171+
const { chunkFilename } = this.options;
172+
if (REGEXP_CHUNKHASH.test(chunkFilename)) {
173+
hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
174+
}
175+
if (REGEXP_CONTENTHASH.test(chunkFilename)) {
176+
hash.update(
177+
JSON.stringify(
178+
chunk.getChunkMaps(true).contentHash[NS] || {},
179+
),
180+
);
181+
}
182+
if (REGEXP_NAME.test(chunkFilename)) {
183+
hash.update(JSON.stringify(chunk.getChunkMaps(true).name));
184+
}
185+
},
186+
);
151187
compilation.hooks.contentHash.tap(pluginName, (chunk) => {
152188
const { outputOptions } = compilation;
153189
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;

test/manual/webpack.config.js

Lines changed: 2 additions & 0 deletions
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)