Skip to content

Commit 06039bf

Browse files
committed
fix(pack): 修复 chunk 版本号问题
1 parent 5d5a7f5 commit 06039bf

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

lib/modules/minWorker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ process.on('message', function (m) {
3838

3939
// 重新生成版本号, webpack 打的样式文件 hash 会根据所在目录不同而不同,造成 beta/prd 环境下版本号不一致
4040
if (assetName.indexOf(HASH_PLACEHOLDER) > -1) {
41-
var version = md5(minifiedCode).slice(0, 16);
41+
var version = md5(minifiedCode).slice(0, 20); // 和 webpack hash 长度保持一致
4242
var nextName = assetName.replace(HASH_PLACEHOLDER, version);
4343
fs.renameSync(path.resolve(cwd, assetName), path.resolve(cwd, nextName));
4444
replacedAssets = [assetName, nextName];
45+
} else {
46+
replacedAssets = [assetName, assetName];
4547
}
4648
}
4749
}

lib/plugins/hashPlaceholderPlugin.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
'use strict';
22

3+
/*
4+
* 解决样式文件的版本号生成问题,先用 placeholder 占位
5+
* 待压缩资源过后再重新生成版本号,替换占位符
6+
*/
7+
8+
var styleExtNames = ['.css', '.scss', '.sass', '.less'];
9+
310
function HashPlaceholder() {}
411

512
HashPlaceholder.prototype.apply = function (compiler) {
613
compiler.plugin('compilation', function (compilation) {
714
compilation.plugin('chunk-hash', function (chunk, chunkHash) {
8-
chunkHash.digest = function () {
9-
return '[hashPlaceholder]';
10-
};
15+
if (chunk.name && styleExtNames.indexOf(sysPath.extname(chunk.name)) > -1) {
16+
chunkHash.digest = function () {
17+
return '[hashPlaceholder]';
18+
};
19+
}
1120
});
1221
});
1322
};

src/modules/minWorker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ process.on('message', function(m) {
3636

3737
// 重新生成版本号, webpack 打的样式文件 hash 会根据所在目录不同而不同,造成 beta/prd 环境下版本号不一致
3838
if(assetName.indexOf(HASH_PLACEHOLDER) > -1) {
39-
const version = md5(minifiedCode).slice(0, 16);
39+
const version = md5(minifiedCode).slice(0, 20); // 和 webpack hash 长度保持一致
4040
const nextName = assetName.replace(HASH_PLACEHOLDER, version);
4141
fs.renameSync(path.resolve(cwd, assetName), path.resolve(cwd, nextName));
4242
replacedAssets = [assetName, nextName];
43+
} else {
44+
replacedAssets = [assetName, assetName];
4345
}
4446
}
4547
}

src/plugins/hashPlaceholderPlugin.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
'use strict';
22

3+
/*
4+
* 解决样式文件的版本号生成问题,先用 placeholder 占位
5+
* 待压缩资源过后再重新生成版本号,替换占位符
6+
*/
7+
8+
const styleExtNames = ['.css', '.scss', '.sass', '.less'];
9+
310
function HashPlaceholder () {
411

512
}
613

714
HashPlaceholder.prototype.apply = function(compiler) {
815
compiler.plugin('compilation', function(compilation) {
916
compilation.plugin('chunk-hash', function(chunk, chunkHash) {
10-
chunkHash.digest = function () {
11-
return '[hashPlaceholder]';
12-
};
17+
if(chunk.name && styleExtNames.indexOf(sysPath.extname(chunk.name)) > -1) {
18+
chunkHash.digest = function () {
19+
return '[hashPlaceholder]';
20+
};
21+
}
1322
});
1423
});
1524
};

0 commit comments

Comments
 (0)