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