Skip to content

Commit 57a77cc

Browse files
committed
fix(pack): 修复资源无版本号时编译错误
1 parent e67416e commit 57a77cc

File tree

10 files changed

+67
-32
lines changed

10 files changed

+67
-32
lines changed

lib/commands/pack.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ exports.setOptions = function (optimist) {
2121
exports.run = function (options) {
2222
var min = options.m || options.min || false,
2323
lint = options.l || options.lint || false,
24-
clean = options.c || options.clean || false,
24+
clean = options.c || options.clean || true,
2525
quiet = options.q || options.quiet || false,
2626
group = options.g || options.group,
2727
sourcemap = options.s || options.sourcemap,
@@ -40,7 +40,7 @@ exports.run = function (options) {
4040
lint: lint,
4141
min: min,
4242
sourcemap: sourcemap,
43-
clean: clean,
43+
clean: clean === 'false' ? false : true,
4444
quiet: quiet
4545
}, function (err) {
4646
if (err) {

lib/models/Config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var Config = function () {
6060
},
6161
plugins: [
6262
// local plugin
63-
require('../plugins/extTemplatedPathPlugin.js'), require('../plugins/requireModulePlugin.js')],
63+
require('../plugins/extTemplatedPathPlugin.js'), require('../plugins/requireModulePlugin.js'), require('../plugins/hashPlaceholderPlugin.js')],
6464
resolve: {
6565
root: [],
6666
extensions: ['', '.js', '.css', '.json', '.string', '.tpl'],

lib/models/Project.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ var Project = function () {
179179
this.setCommands(userConfigObj.command);
180180
}
181181
} else {
182-
error(this.configFile + ' 没有 exports 正确的方法!');
182+
error(this.configFile + ' 设置有误,请参考文档 ' + 'http://ued.qunar.com/ykit/docs-%E9%85%8D%E7%BD%AE.html'.underline);
183183
return this;
184184
}
185185
}
@@ -378,14 +378,16 @@ var Project = function () {
378378
if (processToRun === 0) {
379379
cc.exit();
380380
spinner.stop();
381+
381382
logTime('minify complete!');
383+
384+
// 更新 stats
385+
stats.compilation.assets = Object.keys(nextAssets).length > 0 ? nextAssets : originAssets;
386+
382387
afterPack();
383388
}
384389
});
385390
});
386-
387-
// 更新 stats
388-
stats.compilation.assets = nextAssets;
389391
})();
390392
} else {
391393
afterPack();

lib/modules/minWorker.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ var jsParser = require('uglify-js').parser;
77
var jsUglify = require('uglify-js').uglify;
88
var cssUglify = require('uglifycss');
99

10+
var HASH_PLACEHOLDER = '[hashPlaceholder]';
11+
1012
process.on('message', function (m) {
1113
var opt = m.opt;
1214
var cwd = m.cwd;
1315
var assetName = m.assetName;
14-
var nameReg = /^([^\@]*)\@?([^\.]+)(\.(js|css))$/;
1516
var replacedAssets = [];
1617

1718
if (/\.js$/.test(assetName) || /\.css$/.test(assetName)) {
@@ -36,14 +37,12 @@ process.on('message', function (m) {
3637
fs.writeFileSync(path.resolve(cwd, assetName), minifiedCode, { encoding: 'utf8' });
3738

3839
// 重新生成版本号, webpack 打的样式文件 hash 会根据所在目录不同而不同,造成 beta/prd 环境下版本号不一致
39-
var matchInfo = assetName.match(nameReg),
40-
version = matchInfo[2];
41-
42-
var nextVersion = md5(minifiedCode);
43-
var nextName = assetName.replace(version, nextVersion);
44-
fs.renameSync(path.resolve(cwd, assetName), path.resolve(cwd, nextName));
45-
46-
replacedAssets = [assetName, nextName];
40+
if (assetName.indexOf(HASH_PLACEHOLDER) > -1) {
41+
var version = md5(minifiedCode).slice(0, 16);
42+
var nextName = assetName.replace(HASH_PLACEHOLDER, version);
43+
fs.renameSync(path.resolve(cwd, assetName), path.resolve(cwd, nextName));
44+
replacedAssets = [assetName, nextName];
45+
}
4746
}
4847
}
4948

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
function HashPlaceholder() {}
4+
5+
HashPlaceholder.prototype.apply = function (compiler) {
6+
compiler.plugin('compilation', function (compilation) {
7+
compilation.plugin('chunk-hash', function (chunk, chunkHash) {
8+
chunkHash.digest = function () {
9+
return '[hashPlaceholder]';
10+
};
11+
});
12+
});
13+
};
14+
15+
module.exports = new HashPlaceholder();

src/commands/pack.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ exports.setOptions = (optimist) => {
2121
exports.run = function (options) {
2222
let min = options.m || options.min || false,
2323
lint = options.l || options.lint || false,
24-
clean = options.c || options.clean || false,
24+
clean = options.c || options.clean || true,
2525
quiet = options.q || options.quiet || false,
2626
group = options.g || options.group,
2727
sourcemap = options.s || options.sourcemap,
@@ -40,7 +40,7 @@ exports.run = function (options) {
4040
lint: lint,
4141
min: min,
4242
sourcemap: sourcemap,
43-
clean: clean,
43+
clean: clean === 'false' ? false : true,
4444
quiet: quiet
4545
}, (err) => {
4646
if (err) {

src/models/Config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class Config {
5555
plugins: [
5656
// local plugin
5757
require('../plugins/extTemplatedPathPlugin.js'),
58-
require('../plugins/requireModulePlugin.js')
58+
require('../plugins/requireModulePlugin.js'),
59+
require('../plugins/hashPlaceholderPlugin.js')
5960
],
6061
resolve: {
6162
root: [],

src/models/Project.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class Project {
162162
this.setCommands(userConfigObj.command);
163163
}
164164
} else {
165-
error(this.configFile + ' 没有 exports 正确的方法!');
165+
error(this.configFile + ' 设置有误,请参考文档 ' + 'http://ued.qunar.com/ykit/docs-%E9%85%8D%E7%BD%AE.html'.underline);
166166
return this;
167167
}
168168
}
@@ -353,14 +353,16 @@ class Project {
353353
if (processToRun === 0) {
354354
cc.exit();
355355
spinner.stop();
356+
356357
logTime('minify complete!');
358+
359+
// 更新 stats
360+
stats.compilation.assets = Object.keys(nextAssets).length > 0 ? nextAssets : originAssets;
361+
357362
afterPack();
358363
}
359364
});
360365
});
361-
362-
// 更新 stats
363-
stats.compilation.assets = nextAssets;
364366
} else {
365367
afterPack();
366368
}

src/modules/minWorker.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ const jsParser = require('uglify-js').parser;
55
const jsUglify = require('uglify-js').uglify;
66
const cssUglify = require('uglifycss');
77

8+
const HASH_PLACEHOLDER = '[hashPlaceholder]';
9+
810
process.on('message', function(m) {
911
const opt = m.opt;
1012
const cwd = m.cwd;
1113
const assetName = m.assetName;
12-
const nameReg = /^([^\@]*)\@?([^\.]+)(\.(js|css))$/;
1314
let replacedAssets = [];
1415

1516
if(/\.js$/.test(assetName) || /\.css$/.test(assetName)) {
@@ -34,14 +35,12 @@ process.on('message', function(m) {
3435
fs.writeFileSync(path.resolve(cwd, assetName), minifiedCode, {encoding: 'utf8'});
3536

3637
// 重新生成版本号, webpack 打的样式文件 hash 会根据所在目录不同而不同,造成 beta/prd 环境下版本号不一致
37-
var matchInfo = assetName.match(nameReg),
38-
version = matchInfo[2];
39-
40-
const nextVersion = md5(minifiedCode);
41-
const nextName = assetName.replace(version, nextVersion);
42-
fs.renameSync(path.resolve(cwd, assetName), path.resolve(cwd, nextName));
43-
44-
replacedAssets = [assetName, nextName];
38+
if(assetName.indexOf(HASH_PLACEHOLDER) > -1) {
39+
const version = md5(minifiedCode).slice(0, 16);
40+
const nextName = assetName.replace(HASH_PLACEHOLDER, version);
41+
fs.renameSync(path.resolve(cwd, assetName), path.resolve(cwd, nextName));
42+
replacedAssets = [assetName, nextName];
43+
}
4544
}
4645
}
4746

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
function HashPlaceholder () {
4+
5+
}
6+
7+
HashPlaceholder.prototype.apply = function(compiler) {
8+
compiler.plugin('compilation', function(compilation) {
9+
compilation.plugin('chunk-hash', function(chunk, chunkHash) {
10+
chunkHash.digest = function () {
11+
return '[hashPlaceholder]';
12+
};
13+
});
14+
});
15+
};
16+
17+
module.exports = new HashPlaceholder();

0 commit comments

Comments
 (0)