webpack-auto-inject-version/dist/main.js

54 lines
1.9 KiB
JavaScript
Raw Normal View History

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-21 18:32:35 +11:00
var log = require('./core/log');
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;
2016-10-21 18:32:35 +11:00
log.call('info', 'AIS_START');
this.executeNoneWebpackComponents();
2016-10-20 17:58:19 +11:00
}
WebpackAutoInject.prototype.apply = function (compiler) {
this.compiler = compiler;
2016-10-21 18:32:35 +11:00
this.executeWebpackComponents();
2016-10-20 19:32:26 +11:00
};
2016-10-21 18:32:35 +11:00
WebpackAutoInject.prototype.executeNoneWebpackComponents = function () {
this.executeComponents(config.NON_WEBPACK_COMPONENTS, function () {
});
};
WebpackAutoInject.prototype.executeWebpackComponents = function () {
this.executeComponents(config.WEBPACK_COMPONENTS, function () {
});
};
WebpackAutoInject.prototype.executeComponents = function (components, done) {
2016-10-20 19:32:26 +11:00
var _this = this;
2016-10-21 18:32:35 +11:00
if (!components.length) {
done();
2016-10-20 19:32:26 +11:00
return;
}
2016-10-21 18:32:35 +11:00
var comp = components.shift();
if (!this.options[comp.option]) {
this.executeComponents(components, done);
return;
2016-10-20 17:58:19 +11:00
}
2016-10-21 18:32:35 +11:00
var inst = new (require(comp.path))(this);
inst.apply().then(function () {
_this.executeComponents(components, done);
}, function (err) { _this.executeComponents(components, done); });
2016-10-20 17:58:19 +11:00
};
WebpackAutoInject.options = {
autoIncrease: true,
2016-10-21 18:32:35 +11:00
injectAsComment: true,
injectByTag: true,
injectByTagFileRegex: /^index\.html$/
2016-10-20 17:58:19 +11:00
};
return WebpackAutoInject;
}());
module.exports = WebpackAutoInject;