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:
|
108 | 'Specifies the reporter to use for generating console output for a build',
|
109 | type: 'string',
|
110 | },
|
111 | require: {
|
112 | desc: `Preload one or more modules before loading the webpack configuration
|
113 | Typically used for language-specific require hooks`,
|
114 | type: ['string', 'array'],
|
115 | },
|
116 | 'run-dev': {
|
117 | alias: 'd',
|
118 | desc: `An alias for --debug --devtool eval-cheap-module-source-map
|
119 | --mode development --output-pathinfo`,
|
120 | type: 'boolean',
|
121 | },
|
122 | 'run-prod': {
|
123 | alias: 'p',
|
124 | desc: `An alias for --optimize-minimize --mode production, and defines
|
125 | process.env.NODE_ENV="production"`,
|
126 | type: 'boolean',
|
127 | },
|
128 | version: {
|
129 | desc: 'Display the webpack-command version',
|
130 | },
|
131 | watch: {
|
132 | alias: 'w',
|
133 | desc: 'Watch the filesystem for changes',
|
134 | type: 'boolean',
|
135 | },
|
136 | },
|
137 |
|
138 | name: 'General',
|
139 | };
|