Skip to content

Commit f263f05

Browse files
committed
feat(pack): 允许在 packCallback 中更改 assetsInfo
1 parent 8164986 commit f263f05

File tree

2 files changed

+96
-94
lines changed

2 files changed

+96
-94
lines changed

lib/models/Project.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ var Project = function () {
289289
value: function pack(opt, callback) {
290290
var _this3 = this;
291291

292-
var config = this.config.getConfig();
292+
var self = this,
293+
config = this.config.getConfig();
294+
293295
UtilFs.deleteFolderRecursive(this.cachePath);
294296

295297
if (!config.beforePack) {
@@ -337,48 +339,49 @@ var Project = function () {
337339
}
338340

339341
webpack(config, function (err, stats) {
340-
globby.sync('**/*.cache', {
341-
cwd: config.output.path
342-
}).map(function (p) {
343-
return sysPath.join(config.output.path, p);
342+
globby.sync('**/*.cache', { cwd: config.output.path }).map(function (p) {
343+
sysPath.join(config.output.path, p);
344344
}).forEach(function (fp) {
345-
return fs.unlinkSync(fp);
345+
fs.unlinkSync(fp);
346346
});
347347

348348
if (!err) {
349-
var statsInfo = stats.toJson({
350-
errorDetails: false
351-
});
349+
async.series(_this3.packCallbacks.map(function (packCallback) {
350+
return function (callback) {
351+
packCallback(opt, stats);
352+
callback(null);
353+
};
354+
}), function (err) {
352355

353-
process.stdout.write(
354-
// clear bundle log
355-
' \n' + '\x1b[90m' + '-------------------------- YKIT PACKED ASSETS -------------------------- ' + '\x1b[0m \n\n');
356+
var statsInfo = stats.toJson({ errorDetails: false });
356357

357-
if (statsInfo.errors.length > 0) {
358-
statsInfo.errors.map(function (err) {
359-
error(err.red);
360-
info();
361-
});
362-
}
363-
if (statsInfo.warnings.length > 0) {
364-
statsInfo.warnings.map(function (warning) {
365-
warn(warning.yellow);
366-
info();
367-
});
368-
}
369-
statsInfo.assets.map(function (asset) {
370-
var size = asset.size > 1024 ? (asset.size / 1024).toFixed(2) + ' kB' : asset.size + ' bytes';
371-
if (!/\.cache$/.test(asset.name)) {
372-
log('- '.gray + asset.name + ' - ' + size);
358+
process.stdout.write('\n' + '\x1b[90m' + '-------------------------- YKIT PACKED ASSETS -------------------------- ' + '\x1b[0m \n\n');
359+
360+
if (statsInfo.errors.length > 0) {
361+
statsInfo.errors.map(function (err) {
362+
error(err.red);
363+
info();
364+
});
373365
}
374-
});
375-
info();
376-
_this3.packCallbacks.forEach(function (cb) {
377-
return cb(opt, stats);
366+
if (statsInfo.warnings.length > 0) {
367+
statsInfo.warnings.map(function (warning) {
368+
warn(warning.yellow);
369+
info();
370+
});
371+
}
372+
373+
var assetsInfo = self.config._config.assetsInfo || statsInfo.assets;
374+
assetsInfo.map(function (asset) {
375+
var size = asset.size > 1024 ? (asset.size / 1024).toFixed(2) + ' kB' : asset.size + ' bytes';
376+
if (!/\.cache$/.test(asset.name)) {
377+
log('- '.gray + asset.name + ' - ' + size);
378+
}
379+
});
380+
381+
info();
382+
callback(err, stats);
378383
});
379384
}
380-
381-
callback(err, stats);
382385
});
383386
};
384387

src/models/Project.js

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ class Project {
2727
this.middlewares = [];
2828
this.packCallbacks = [];
2929
this.eslintConfig = require('../config/eslint.json');
30-
this.configFile = globby.sync(['ykit.*.js', 'ykit.js'], { cwd: this.cwd })[0] || '';
31-
this.extendConfig = this.configFile &&
32-
this.configFile.match(/ykit\.([\w\.]+)\.js/) &&
33-
this.configFile.match(/ykit\.([\w\.]+)\.js/)[1] &&
34-
this.configFile.match(/ykit\.([\w\.]+)\.js/)[1].replace(/\./g, '-');
30+
this.configFile = globby.sync([
31+
'ykit.*.js', 'ykit.js'
32+
], {cwd: this.cwd})[0] || '';
33+
this.extendConfig = this.configFile && this.configFile.match(/ykit\.([\w\.]+)\.js/) && this.configFile.match(/ykit\.([\w\.]+)\.js/)[1] && this.configFile.match(/ykit\.([\w\.]+)\.js/)[1].replace(/\./g, '-');
3534
this.ignores = ['node_modules/**/*', 'bower_components/**/*', 'dev/**/*', 'prd/**/*', '.ykit_cache/**/*'];
3635
this.cachePath = this._isCacheDirExists(cwd) || '';
3736

@@ -189,7 +188,9 @@ class Project {
189188

190189
for (let key in entries) {
191190
const entryItem = entries[key],
192-
entry = Array.isArray(entryItem) ? entryItem[entryItem.length - 1] : entryItem,
191+
entry = Array.isArray(entryItem)
192+
? entryItem[entryItem.length - 1]
193+
: entryItem,
193194
extName = sysPath.extname(entry);
194195

195196
// 放在cache目录下
@@ -215,20 +216,12 @@ class Project {
215216
entryItem.forEach((cssPath) => {
216217
const originCssPath = sysPath.join(config.context, cssPath);
217218
const requiredPath = UtilPath.normalize(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
218-
fs.appendFileSync(
219-
cacheFilePath,
220-
'require("' + requiredPath + '");',
221-
'utf-8'
222-
);
219+
fs.appendFileSync(cacheFilePath, 'require("' + requiredPath + '");', 'utf-8');
223220
});
224221
} else {
225222
const originCssPath = sysPath.join(config.context, entry);
226223
const requiredPath = UtilPath.normalize(sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath));
227-
fs.writeFileSync(
228-
cacheFilePath,
229-
'require("' + requiredPath + '");',
230-
'utf-8'
231-
);
224+
fs.writeFileSync(cacheFilePath, 'require("' + requiredPath + '");', 'utf-8');
232225
}
233226

234227
fps.push(cacheFilePath);
@@ -272,11 +265,13 @@ class Project {
272265
}
273266

274267
pack(opt, callback) {
275-
let config = this.config.getConfig();
268+
let self = this,
269+
config = this.config.getConfig();
270+
276271
UtilFs.deleteFolderRecursive(this.cachePath);
277272

278273
if (!config.beforePack) {
279-
config.beforePack = function (done) {
274+
config.beforePack = function(done) {
280275
done();
281276
};
282277
}
@@ -320,58 +315,60 @@ class Project {
320315
}
321316

322317
webpack(config, (err, stats) => {
323-
globby.sync('**/*.cache', {
324-
cwd: config.output.path
325-
})
326-
.map((p) => sysPath.join(config.output.path, p))
327-
.forEach((fp) => fs.unlinkSync(fp));
318+
globby.sync('**/*.cache', {cwd: config.output.path}).map((p) => {
319+
sysPath.join(config.output.path, p);
320+
}).forEach((fp) => {
321+
fs.unlinkSync(fp);
322+
});
328323

329324
if (!err) {
330-
let statsInfo = stats.toJson({
331-
errorDetails: false
332-
});
325+
async.series(this.packCallbacks.map((packCallback) => {
326+
return function(callback) {
327+
packCallback(opt, stats);
328+
callback(null);
329+
};
330+
}), (err) => {
331+
332+
let statsInfo = stats.toJson({errorDetails: false});
333+
334+
process.stdout.write('\n' +
335+
'\x1b[90m' +
336+
'-------------------------- YKIT PACKED ASSETS -------------------------- ' +
337+
'\x1b[0m \n\n');
338+
339+
if (statsInfo.errors.length > 0) {
340+
statsInfo.errors.map((err) => {
341+
error(err.red);
342+
info();
343+
});
344+
}
345+
if (statsInfo.warnings.length > 0) {
346+
statsInfo.warnings.map((warning) => {
347+
warn(warning.yellow);
348+
info();
349+
});
350+
}
333351

334-
process.stdout.write(
335-
// clear bundle log
336-
' \n'
337-
+ '\x1b[90m'
338-
+ '-------------------------- YKIT PACKED ASSETS -------------------------- '
339-
+ '\x1b[0m \n\n'
340-
);
341-
342-
if (statsInfo.errors.length > 0) {
343-
statsInfo.errors.map((err) => {
344-
error(err.red);
345-
info();
346-
});
347-
}
348-
if (statsInfo.warnings.length > 0) {
349-
statsInfo.warnings.map((warning) => {
350-
warn(warning.yellow);
351-
info();
352+
const assetsInfo = self.config._config.assetsInfo || statsInfo.assets;
353+
assetsInfo.map((asset) => {
354+
const size = asset.size > 1024
355+
? (asset.size / 1024).toFixed(2) + ' kB'
356+
: asset.size + ' bytes';
357+
if (!/\.cache$/.test(asset.name)) {
358+
log('- '.gray + asset.name + ' - ' + size);
359+
}
352360
});
353-
}
354-
statsInfo.assets.map((asset) => {
355-
const size = asset.size > 1024 ?
356-
(asset.size / 1024).toFixed(2) + ' kB' :
357-
asset.size + ' bytes';
358-
if (!/\.cache$/.test(asset.name)) {
359-
log('- '.gray + asset.name + ' - ' + size);
360-
}
361+
362+
info();
363+
callback(err, stats);
361364
});
362-
info();
363-
this.packCallbacks.forEach(cb => cb(opt, stats));
364365
}
365-
366-
callback(err, stats);
367366
});
368367
};
369368

370369
config.beforePack(() => {
371370
if (opt.lint) {
372-
async.series([
373-
(callback) => this.lint(callback)
374-
], (err, results) => {
371+
async.series([(callback) => this.lint(callback)], (err, results) => {
375372
if (!err) {
376373
if (results[0] && results[1]) {
377374
compilerProcess();
@@ -414,7 +411,9 @@ class Project {
414411
if (dir) {
415412
dir = sysPath.resolve(this.cwd, dir);
416413
try {
417-
fs.statSync(dir).isDirectory() ? context = dir : lintPath = sysPath.relative(context, dir);
414+
fs.statSync(dir).isDirectory()
415+
? context = dir
416+
: lintPath = sysPath.relative(context, dir);
418417
} catch (e) {
419418
error(e);
420419
process.exit(1);

0 commit comments

Comments
 (0)