/
resolveAdvanced.js
63 lines (58 loc) · 2.33 KB
/
resolveAdvanced.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { packageExists, promptInstallation } = require('@webpack-cli/package-utils');
const { yellow } = require('colorette');
const { error, info } = require('../utils/logger');
/**
* Resolve advanced flags
* @param {args} args - Parsed args passed to CLI
*/
const resolveAdvanced = async (args) => {
const { target, prefetch, hot, analyze } = args;
const finalOptions = {
options: {},
outputOptions: {},
};
if (hot) {
const { HotModuleReplacementPlugin } = require('webpack');
const hotModuleVal = new HotModuleReplacementPlugin();
if (finalOptions.options && finalOptions.options.plugins) {
finalOptions.options.plugins.unshift(hotModuleVal);
} else {
finalOptions.options.plugins = [hotModuleVal];
}
}
if (prefetch) {
const { PrefetchPlugin } = require('webpack');
const prefetchVal = new PrefetchPlugin(null, args.prefetch);
if (finalOptions.options && finalOptions.options.plugins) {
finalOptions.options.plugins.unshift(prefetchVal);
} else {
finalOptions.options.plugins = [prefetchVal];
}
}
if (analyze) {
if (packageExists('webpack-bundle-analyzer')) {
// eslint-disable-next-line node/no-extraneous-require
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const bundleAnalyzerVal = new BundleAnalyzerPlugin();
if (finalOptions.options && finalOptions.options.plugins) {
finalOptions.options.plugins.unshift(bundleAnalyzerVal);
} else {
finalOptions.options.plugins = [bundleAnalyzerVal];
}
} else {
await promptInstallation('webpack-bundle-analyzer', () => {
error(`It looks like ${yellow('webpack-bundle-analyzer')} is not installed.`);
})
.then(() => info(`${yellow('webpack-bundle-analyzer')} was installed sucessfully.`))
.catch(() => {
error(`Action Interrupted, Please try once again or install ${yellow('webpack-bundle-analyzer')} manually.`);
process.exit(2);
});
}
}
if (target) {
finalOptions.options.target = args.target;
}
return finalOptions;
};
module.exports = resolveAdvanced;