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.profile) {
|
58 | result.profile = argv.profile;
|
59 | }
|
60 |
|
61 | if (argv.progress) {
|
62 | result.progress = argv.progress;
|
63 | }
|
64 |
|
65 | if (argv.reporter) {
|
66 | result.reporter = argv.reporter;
|
67 | }
|
68 |
|
69 | if (argv.watch) {
|
70 | result.watch = true;
|
71 | }
|
72 |
|
73 | return merge(options, result, plugins.length ? { plugins } : {});
|
74 | },
|
75 |
|
76 | flags: {
|
77 | context: {
|
78 | desc: 'The root directory for resolving entry point and stats',
|
79 | type: 'string',
|
80 | },
|
81 | debug: {
|
82 | desc: 'Switch loaders to debug mode',
|
83 | type: 'boolean',
|
84 | },
|
85 | devtool: {
|
86 | desc: chalk`Enable devtool for better debugging experience.
|
87 | {dim e.g. --devtool eval-cheap-module-source-map}`,
|
88 | type: 'string',
|
89 | },
|
90 | entry: {
|
91 | desc: 'The entry point',
|
92 | type: ['array', 'object', 'string'],
|
93 | },
|
94 | help: {
|
95 | desc: 'Show usage information and the options listed here',
|
96 | },
|
97 | 'log-level': {
|
98 | desc: chalk`Limit all process console messages to a specific level and above
|
99 | {dim Levels: trace, debug, info, warn, error, silent}`,
|
100 | type: 'string',
|
101 | },
|
102 | 'log-time': {
|
103 | desc: 'Instruct the logger and dependencies to display a timestamp',
|
104 | },
|
105 | progress: {
|
106 | desc: chalk`Instructs webpack to track and display build progress
|
107 | {dim This is often used with --profile}`,
|
108 | type: 'boolean',
|
109 | },
|
110 | reporter: {
|
111 | desc:
|
112 | 'Specifies the reporter to use for generating console output for a build',
|
113 | type: 'string',
|
114 | },
|
115 | require: {
|
116 | desc: `Preload one or more modules before loading the webpack configuration
|
117 | Typically used for language-specific require hooks`,
|
118 | type: ['string', 'array'],
|
119 | },
|
120 | 'run-dev': {
|
121 | alias: 'd',
|
122 | desc: `An alias for --debug --devtool eval-cheap-module-source-map
|
123 | --mode development --output-pathinfo`,
|
124 | type: 'boolean',
|
125 | },
|
126 | 'run-prod': {
|
127 | alias: 'p',
|
128 | desc: `An alias for --optimize-minimize --mode production, and defines
|
129 | process.env.NODE_ENV="production"`,
|
130 | type: 'boolean',
|
131 | },
|
132 | version: {
|
133 | desc: 'Display the webpack-command version',
|
134 | },
|
135 | watch: {
|
136 | alias: 'w',
|
137 | desc: 'Watch the filesystem for changes',
|
138 | type: 'boolean',
|
139 | },
|
140 | },
|
141 |
|
142 | name: 'General',
|
143 | };
|