Skip to content

Commit 27198dd

Browse files
committed
feat(pack): 添加 beforePackCallbacks 回调
1 parent 6b02357 commit 27198dd

File tree

4 files changed

+41
-60
lines changed

4 files changed

+41
-60
lines changed

lib/models/Config.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,6 @@ var Config = function () {
189189
value: function getMiddlewares() {
190190
return this._config.middleware;
191191
}
192-
}, {
193-
key: 'applyBeforePack',
194-
value: function applyBeforePack(beforeCallback) {
195-
if (typeof beforeCallback === 'function') {
196-
this._config.beforePack = beforeCallback;
197-
}
198-
}
199192
}]);
200193

201194
return Config;

lib/models/Project.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var Project = function () {
3333
this.config = new Config(cwd);
3434
this.commands = Manager.getCommands();
3535
this.middlewares = [];
36+
this.beforePackCallbacks = [];
3637
this.packCallbacks = [];
3738
this.eslintConfig = require('../config/eslint.json');
3839
this.configFile = globby.sync(['ykit.*.js', 'ykit.js'], { cwd: this.cwd })[0] || '';
@@ -95,7 +96,8 @@ var Project = function () {
9596
config: this.config.getConfig(),
9697
commands: this.commands,
9798
middlewares: this.middlewares,
98-
applyBeforePack: this.config.applyBeforePack.bind(this.config),
99+
applyBeforePack: this.applyBeforePack.bind(this),
100+
beforePackCallbacks: this.beforePackCallbacks,
99101
packCallbacks: this.packCallbacks,
100102
eslintConfig: this.eslintConfig,
101103
applyMiddleware: this.config.applyMiddleware.bind(this.config),
@@ -294,6 +296,15 @@ var Project = function () {
294296

295297
callback(null, !report.errorCount);
296298
}
299+
}, {
300+
key: 'applyBeforePack',
301+
value: function applyBeforePack(nextBeforePackCB) {
302+
if (typeof nextBeforePackCB === 'function') {
303+
this.beforePackCallbacks.push(nextBeforePackCB);
304+
} else if (Array.isArray(nextBeforePackCB)) {
305+
this.beforePackCallbacks.concat(nextBeforePackCB);
306+
}
307+
}
297308
}, {
298309
key: 'pack',
299310
value: function pack(opt, callback) {
@@ -305,12 +316,6 @@ var Project = function () {
305316

306317
UtilFs.deleteFolderRecursive(this.cachePath);
307318

308-
if (!config.beforePack) {
309-
config.beforePack = function (done) {
310-
done();
311-
};
312-
}
313-
314319
var compilerProcess = function compilerProcess() {
315320
// 打包前设置
316321
if (opt.sourcemap) {
@@ -460,23 +465,14 @@ var Project = function () {
460465
});
461466
};
462467

463-
config.beforePack(function () {
464-
if (opt.lint) {
465-
async.series([function (callback) {
466-
return _this4.lint(callback);
467-
}], function (err, results) {
468-
if (!err) {
469-
if (results[0] && results[1]) {
470-
compilerProcess();
471-
}
472-
} else {
473-
error(err.stack);
474-
}
475-
});
476-
} else {
477-
compilerProcess();
478-
}
479-
}, opt);
468+
async.series(this.beforePackCallbacks.map(function (beforePackItem) {
469+
return function (callback) {
470+
beforePackItem();
471+
callback(null);
472+
};
473+
}), function (err) {
474+
compilerProcess();
475+
});
480476

481477
return this;
482478
}

src/models/Config.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,6 @@ class Config {
173173
return this._config.middleware;
174174
}
175175

176-
applyBeforePack(beforeCallback) {
177-
if (typeof beforeCallback === 'function') {
178-
this._config.beforePack = beforeCallback;
179-
}
180-
}
181176
}
182177

183178
module.exports = Config;

src/models/Project.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Project {
2525
this.config = new Config(cwd);
2626
this.commands = Manager.getCommands();
2727
this.middlewares = [];
28+
this.beforePackCallbacks = [];
2829
this.packCallbacks = [];
2930
this.eslintConfig = require('../config/eslint.json');
3031
this.configFile = globby.sync(['ykit.*.js', 'ykit.js'], { cwd: this.cwd })[0] || '';
@@ -85,7 +86,8 @@ class Project {
8586
config: this.config.getConfig(),
8687
commands: this.commands,
8788
middlewares: this.middlewares,
88-
applyBeforePack: this.config.applyBeforePack.bind(this.config),
89+
applyBeforePack: this.applyBeforePack.bind(this),
90+
beforePackCallbacks: this.beforePackCallbacks,
8991
packCallbacks: this.packCallbacks,
9092
eslintConfig: this.eslintConfig,
9193
applyMiddleware: this.config.applyMiddleware.bind(this.config),
@@ -291,17 +293,19 @@ class Project {
291293
callback(null, !report.errorCount);
292294
}
293295

296+
applyBeforePack(nextBeforePackCB) {
297+
if (typeof nextBeforePackCB === 'function') {
298+
this.beforePackCallbacks.push(nextBeforePackCB)
299+
} else if (Array.isArray(nextBeforePackCB)) {
300+
this.beforePackCallbacks.concat(nextBeforePackCB)
301+
}
302+
}
303+
294304
pack(opt, callback) {
295305
let self = this, packStartTime = Date.now(), config = this.config.getConfig();
296306

297307
UtilFs.deleteFolderRecursive(this.cachePath);
298308

299-
if (!config.beforePack) {
300-
config.beforePack = function(done) {
301-
done();
302-
};
303-
}
304-
305309
const compilerProcess = () => {
306310
// 打包前设置
307311
if (opt.sourcemap) {
@@ -471,23 +475,16 @@ class Project {
471475
});
472476
};
473477

474-
config.beforePack(
475-
() => {
476-
if (opt.lint) {
477-
async.series([callback => this.lint(callback)], (err, results) => {
478-
if (!err) {
479-
if (results[0] && results[1]) {
480-
compilerProcess();
481-
}
482-
} else {
483-
error(err.stack);
484-
}
485-
});
486-
} else {
487-
compilerProcess();
488-
}
489-
},
490-
opt
478+
async.series(
479+
this.beforePackCallbacks.map((beforePackItem) => {
480+
return function(callback) {
481+
beforePackItem();
482+
callback(null);
483+
};
484+
}),
485+
err => {
486+
compilerProcess();
487+
}
491488
);
492489

493490
return this;

0 commit comments

Comments
 (0)