2016-10-20 17:58:19 +11:00
|
|
|
var chalk = require('chalk');
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var config = require('./config');
|
2016-10-20 19:32:26 +11:00
|
|
|
var Promise = require('bluebird');
|
|
|
|
var u = require('./core/utils');
|
2016-10-20 17:58:19 +11:00
|
|
|
'use strict';
|
|
|
|
var WebpackAutoInject = (function () {
|
|
|
|
function WebpackAutoInject(options) {
|
2016-10-20 19:32:26 +11:00
|
|
|
this.options = u.merge(WebpackAutoInject.options, options);
|
2016-10-20 17:58:19 +11:00
|
|
|
var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
|
|
|
|
this.version = packageFile.version;
|
|
|
|
}
|
|
|
|
WebpackAutoInject.prototype.apply = function (compiler) {
|
|
|
|
this.compiler = compiler;
|
2016-10-20 19:32:26 +11:00
|
|
|
this.components = config.COMPONENTS;
|
|
|
|
this.executeComponents();
|
|
|
|
};
|
|
|
|
WebpackAutoInject.prototype.executeComponents = function () {
|
|
|
|
var _this = this;
|
|
|
|
if (!this.components.length) {
|
|
|
|
console.log(chalk.bgRed('AIS: DONE!'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var comp = this.components.shift();
|
|
|
|
if (this.options[comp.option]) {
|
|
|
|
var inst = new (require(comp.path))(this);
|
|
|
|
inst.apply().then(function () {
|
|
|
|
_this.executeComponents();
|
|
|
|
}, function (err) { console.log(err); });
|
2016-10-20 17:58:19 +11:00
|
|
|
}
|
2016-10-20 19:32:26 +11:00
|
|
|
else {
|
|
|
|
this.executeComponents();
|
2016-10-20 17:58:19 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
WebpackAutoInject.options = {
|
|
|
|
autoIncrease: true,
|
|
|
|
injectIntoHtml: true,
|
2016-10-20 19:32:26 +11:00
|
|
|
injectIntoHtmlRegex: /^index\.html$/,
|
|
|
|
injectIntoAnyFile: true
|
2016-10-20 17:58:19 +11:00
|
|
|
};
|
|
|
|
return WebpackAutoInject;
|
|
|
|
}());
|
|
|
|
module.exports = WebpackAutoInject;
|