webpack-auto-inject-version/src/webpack-auto-inject.js

124 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-03-15 22:07:22 +11:00
/* global define */
2017-04-11 06:33:32 +10:00
import fs from 'fs';
import path from 'path';
2017-04-11 06:33:32 +10:00
import config from 'config';
import log from 'core/log';
import merge from 'lodash/merge';
import transform from 'lodash/transform';
2017-04-11 09:11:09 +10:00
// import sub components
2017-04-13 08:40:22 +10:00
import AutoIncreaseVersion from 'components/auto-increase-version/auto-increase-version';
import InjectAsComment from 'components/inject-as-comment/inject-as-comment';
import InjectByTag from 'components/inject-by-tag/inject-by-tag';
2017-04-11 06:33:32 +10:00
2017-04-13 07:32:39 +10:00
export default class WebpackAutoInject {
2017-04-11 06:33:32 +10:00
/**
* Constructor,
* called on webpack config load
2017-04-11 09:11:09 +10:00
* @param userConfig - config from the webpack config file
2017-04-11 06:33:32 +10:00
*/
2017-04-11 09:11:09 +10:00
constructor(userConfig) {
this.setConfig(userConfig);
const packageFile = JSON.parse(
2017-04-11 09:11:09 +10:00
fs.readFileSync(path.resolve(this.config.PACKAGE_JSON_PATH), 'utf8')
);
2017-04-11 06:33:32 +10:00
this.version = packageFile.version;
log.call('info', 'AIS_START');
2018-03-15 22:07:22 +11:00
this._executeNoneWebpackComponents();
2017-04-11 06:33:32 +10:00
}
2017-04-13 07:32:39 +10:00
/**
* Set config
* - merge userConfig with default config
* - merge above with a protected config
* @param userConfig
*/
2017-04-11 09:11:09 +10:00
setConfig(userConfig) {
this.config = merge(config, userConfig);
2018-03-15 22:07:22 +11:00
2017-04-11 09:11:09 +10:00
// lets convert all components names to lowercase - to prevent issues
2017-04-13 07:32:39 +10:00
this.config.components = transform(this.config.components, (result, val, key) => {
2017-04-11 09:11:09 +10:00
result[key.toLowerCase()] = val;
});
}
2017-04-11 06:33:32 +10:00
/**
* Webpack apply call,
* when webpack is initialized and
* plugin has been called by webpack
2018-03-15 22:07:22 +11:00
*
2017-04-13 07:32:39 +10:00
* @protected
2018-03-15 22:07:22 +11:00
*
* @param compiler
2017-04-11 06:33:32 +10:00
*/
2018-03-15 22:07:22 +11:00
apply(compiler) {
2017-04-11 06:33:32 +10:00
this.compiler = compiler;
2018-03-15 22:07:22 +11:00
this._executeWebpackComponents();
2017-04-11 06:33:32 +10:00
}
/**
* Execute none webpack components
* - runs as soon as possible,
* > without waiting for webpack init
*/
2018-03-15 22:07:22 +11:00
_executeNoneWebpackComponents() {
this._executeComponent([AutoIncreaseVersion]);
2017-04-11 06:33:32 +10:00
}
/**
* Execute webpack components
* - runs when webpack is initialized
* and plugins is called by webpack
*/
2018-03-15 22:07:22 +11:00
_executeWebpackComponents() {
if (config.componentsOptions.AutoIncreaseVersion.runInWatchMode) {
2018-03-15 22:07:22 +11:00
this._executeComponent([AutoIncreaseVersion]);
}
2018-03-15 22:07:22 +11:00
this._executeComponent([InjectAsComment, InjectByTag]);
2017-04-11 06:33:32 +10:00
}
/**
* Execute components,
* - general layer for comp execution
* - used for both, webpack and non webpack comp
2018-03-15 22:07:22 +11:00
*
* @private
*
* @param components
2017-04-11 06:33:32 +10:00
*/
2018-03-15 22:07:22 +11:00
_executeComponent(components) {
2017-04-11 06:33:32 +10:00
// no more components,
// finish
2017-04-13 07:32:39 +10:00
if (!components.length) {
2017-04-11 09:11:09 +10:00
return;
}
2017-04-11 06:33:32 +10:00
2017-04-11 09:11:09 +10:00
// take first component class
2018-03-15 22:07:22 +11:00
const ComponentClass = components.shift();
2017-04-11 06:33:32 +10:00
// if component is disabled, call next component
2017-04-11 09:11:09 +10:00
if (!this.config.components[ComponentClass.componentName.toLowerCase()]) {
2018-03-15 22:07:22 +11:00
this._executeComponent(components);
2017-04-11 06:33:32 +10:00
return;
}
// execute component
2018-03-15 22:07:22 +11:00
const inst = new ComponentClass(this);
2017-04-11 09:11:09 +10:00
// await for apply to finish
2018-03-15 22:07:22 +11:00
inst.apply();
2017-04-11 09:11:09 +10:00
// call next tick
2018-03-15 22:07:22 +11:00
this._executeComponent(components);
2017-04-11 06:33:32 +10:00
}
}
// webpack hack to export class directly,
// - instead of using 'new WebpackAutoInject.default()',
// - with this you can just use WebpackAutoInject();
define(() => {
return WebpackAutoInject;
});