1 | const path = require('path');
|
2 |
|
3 | const chalk = require('chalk');
|
4 | const merge = require('merge-options');
|
5 | const webpack = require('webpack');
|
6 |
|
7 | module.exports = {
|
8 | apply(argv, options) {
|
9 | let plugins = [];
|
10 | const result = {};
|
11 |
|
12 | if (options.plugins) {
|
13 | plugins = plugins.concat(options.plugins);
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | if (argv.runDev) {
|
21 | argv.debug = true;
|
22 | argv.outputPathinfo = true;
|
23 | if (!argv.devtool) {
|
24 | argv.devtool = 'eval-cheap-module-source-map';
|
25 | }
|
26 | if (!argv.mode) {
|
27 | argv.mode = 'development';
|
28 | }
|
29 | }
|
30 |
|
31 | if (argv.runProd) {
|
32 | argv.optimizeMinimize = true;
|
33 | argv.define = Object.assign({}, argv.define, {
|
34 | 'process.env.NODE_ENV': '"production"'
|
35 | });
|
36 | if (!argv.mode) {
|
37 | argv.mode = 'production';
|
38 | }
|
39 | }
|
40 |
|
41 | if (argv.context) {
|
42 | result.context = path.resolve(argv.context);
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 | if (argv.debug) {
|
48 | const { LoaderOptionsPlugin } = webpack;
|
49 | const plugin = new LoaderOptionsPlugin({ debug: true });
|
50 | plugins.unshift(plugin);
|
51 | }
|
52 |
|
53 | if (argv.devtool) {
|
54 | result.devtool = argv.devtool;
|
55 | }
|
56 |
|
57 | if (argv.progress) {
|
58 | result.progress = argv.progress;
|
59 | }
|
60 |
|
61 | if (argv.reporter) {
|
62 | result.reporter = argv.reporter;
|
63 | }
|
64 |
|
65 | if (argv.watch) {
|
66 | result.watch = true;
|
67 | }
|
68 |
|
69 | return merge(options, result, plugins.length ? { plugins } : {});
|
70 | },
|
71 |
|
72 | flags: {
|
73 | context: {
|
74 | desc: 'The root directory for resolving entry point and stats',
|
75 | type: 'string'
|
76 | },
|
77 | debug: {
|
78 | desc: 'Switch loaders to debug mode',
|
79 | type: 'boolean'
|
80 | },
|
81 | devtool: {
|
82 | desc: chalk`Enable devtool for better debugging experience.
|
83 | {dim e.g. --devtool eval-cheap-module-source-map}`,
|
84 | type: 'string'
|
85 | },
|
86 | entry: {
|
87 | desc: 'The entry point',
|
88 | type: ['array', 'object', 'string']
|
89 | },
|
90 | help: {
|
91 | desc: 'Show usage information and the options listed here'
|
92 | },
|
93 | 'log-level': {
|
94 | desc: chalk`Limit all process console messages to a specific level and above
|
95 | {dim Levels: trace, debug, info, warn, error, silent}`,
|
96 | type: 'string'
|
97 | },
|
98 | 'log-time': {
|
99 | desc: 'Instruct the logger and dependencies to display a timestamp'
|
100 | },
|
101 | progress: {
|
102 | desc: chalk`Instructs webpack to track and display build progress
|
103 | {dim This is often used with --profile}`,
|
104 | type: 'boolean'
|
105 | },
|
106 | reporter: {
|
107 | desc: 'Specifies the reporter to use for generating console output for a build',
|
108 | type: 'string'
|
109 | },
|
110 | require: {
|
111 | desc: `Preload one or more modules before loading the webpack configuration
|
112 | Typically used for language-specific require hooks`,
|
113 | type: ['string', 'array']
|
114 | },
|
115 | 'run-dev': {
|
116 | alias: 'd',
|
117 | desc: `An alias for --debug --devtool eval-cheap-module-source-map
|
118 | --mode development --output-pathinfo`,
|
119 | type: 'boolean'
|
120 | },
|
121 | 'run-prod': {
|
122 | alias: 'p',
|
123 | desc: `An alias for --optimize-minimize --mode production, and defines
|
124 | process.env.NODE_ENV="production"`,
|
125 | type: 'boolean'
|
126 | },
|
127 | version: {
|
128 | desc: 'Display the webpack-command version'
|
129 | },
|
130 | watch: {
|
131 | alias: 'w',
|
132 | desc: 'Watch the filesystem for changes',
|
133 | type: 'boolean'
|
134 | }
|
135 | },
|
136 |
|
137 | name: 'General'
|
138 | };
|