@mobileaction/ui-modules
Version:
Mobile Action common modules for Vue projects
72 lines (62 loc) • 2.24 kB
JavaScript
const webpack = require('webpack');
const { validate } = require('schema-utils');
const isWebpack4 = () => webpack.version[0] === '4';
// schema for options object
const schema = {
type: 'object',
properties: {
path: {
type: 'string',
},
appInfo: {
type: 'object',
},
},
};
class MaAppInfoPlugin {
constructor(options = {}) {
const _options = Object.assign({
path: 'app-info.json',
appInfo: null,
}, options);
validate(schema, _options, {
name: 'MaAppInfoPlugin',
baseDataPath: 'options',
});
this.options = _options;
}
apply(compiler) {
const { appInfo, path } = this.options;
if (!appInfo) { // if invalid app info or path is given don't emit anything but log it
console.warn('MaAppInfo: empty app info provided file will not be generated');
return;
}
if (!path) {
console.warn('MaAppInfo: Invalid app info path provided file will not be generated');
return;
}
const appInfoJson = JSON.stringify(appInfo);
if (isWebpack4()) {
// emit is asynchronous hook, tapping into it using tapAsync, you can use tapPromise/tap(synchronous) as well
compiler.hooks.emit.tapAsync(`MaAppInfoPlugin: ${ path } body=${ appInfoJson }`, (compilation, callback) => {
// Insert app info into the webpack build as a new file asset:
compilation.assets[path] = {
source: () => appInfoJson,
size: () => appInfoJson.length,
};
callback();
});
return;
}
// webpack export `webpack-sources` to avoid cache problems
const { sources } = webpack;
compiler.hooks.compilation.tap('MaAppInfo', (compilation) => {
compilation.hooks.additionalAssets.tapAsync('MaAppInfo', (callback) => {
compilation.assets[path] = new sources.RawSource(appInfoJson);
callback();
});
});
}
}
// webpack modules must be commonjs
module.exports = MaAppInfoPlugin;