UNPKG

3.88 kBJavaScriptView Raw
1const { resolve } = require('path');
2
3const camelcase = require('camelcase');
4const chalk = require('chalk');
5const isObject = require('isobject');
6const merge = require('merge-options');
7const {
8 DefinePlugin,
9 HotModuleReplacementPlugin,
10 PrefetchPlugin,
11 ProvidePlugin,
12} = require('webpack');
13
14const { loadPlugin } = require('./util');
15
16module.exports = {
17 apply(argv, options) {
18 let plugins = [];
19 const output = { watchOptions: {} };
20
21 if (options.plugins) {
22 plugins = plugins.concat(options.plugins);
23 }
24
25 for (const flag of ['bail', 'cache', 'profile', 'target']) {
26 if (argv[flag]) {
27 output[flag] = argv[flag];
28 }
29 }
30
31 for (const name of [
32 'records-input-path',
33 'records-output-path',
34 'records-path',
35 ]) {
36 const flag = camelcase(name);
37 if (argv[flag]) {
38 output[flag] = resolve(argv[flag]);
39 }
40 }
41
42 // --define.test ok
43 if (argv.define && isObject(argv.define)) {
44 const plugin = new DefinePlugin(argv.define);
45 plugins.unshift(plugin);
46 }
47
48 if (argv.hot) {
49 const plugin = new HotModuleReplacementPlugin();
50 plugins.unshift(plugin);
51 }
52
53 if (argv.plugin) {
54 const plugin = loadPlugin(argv.plugin);
55 plugins.unshift(plugin);
56 }
57
58 if (argv.prefetch) {
59 const plugin = new PrefetchPlugin(argv.prefetch);
60 plugins.unshift(plugin);
61 }
62
63 if (argv.provide && isObject(argv.provide)) {
64 const plugin = new ProvidePlugin(argv.provide);
65 plugins.unshift(plugin);
66 }
67
68 if (argv.watchAggregateTimeout) {
69 output.watchOptions.aggregateTimeout = +argv.watchAggregateTimeout;
70 }
71
72 if (argv.watchPoll) {
73 const value =
74 typeof argv.watchPoll === 'boolean' ? true : +argv.watchPoll;
75 output.watchOptions.poll = value;
76 }
77
78 if (argv.watchStdin) {
79 output.watchOptions.stdin = true;
80 output.watch = true;
81 }
82
83 if (!Object.keys(output.watchOptions).length) {
84 delete output.watchOptions;
85 }
86
87 const result = merge(options, output, plugins.length ? { plugins } : {});
88
89 return result;
90 },
91
92 flags: {
93 bail: {
94 type: 'boolean',
95 desc: 'Abort the compilation on first error',
96 },
97 cache: {
98 type: 'boolean',
99 desc: 'Enable in memory caching',
100 },
101 define: {
102 type: 'object',
103 desc: `Used to redefine or replace a variable or key in the bundle. Complex
104keys should be set in config. {dim e.g. --define.batman robin}`,
105 },
106 hot: {
107 type: 'boolean',
108 desc: 'Enables Hot Module Replacement',
109 },
110 plugin: {
111 type: 'string',
112 desc: 'Load this plugin',
113 },
114 prefetch: {
115 desc: chalk`Prefetch this request
116{dim e.g. --prefetch ./file.js}`,
117 type: 'string',
118 },
119 profile: {
120 desc: 'Profile the compilation and include information in stats',
121 type: 'boolean',
122 },
123 provide: {
124 desc: chalk`Provide these modules as free vars in all modules
125{dim e.g. --provide.jQuery jquery}`,
126 type: 'object',
127 },
128 'records-input-path': {
129 desc: 'Path to the records file (reading)',
130 type: 'string',
131 },
132 'records-output-path': {
133 desc: 'Path to the records file (writing)',
134 type: 'string',
135 },
136 'records-path': {
137 desc: 'Path to the records file',
138 type: 'string',
139 },
140 target: {
141 desc: 'The targeted execution environment',
142 type: 'string',
143 },
144 'watch-aggregate-timeout': {
145 desc: 'Timeout for gathering changes while watching',
146 type: ['string', 'number'],
147 },
148 'watch-poll': {
149 desc: 'The polling interval for watching (also enable polling)',
150 type: ['string', 'number'],
151 },
152 'watch-stdin': {
153 alias: 'stdin',
154 desc: 'Exit the process when stdin is closed',
155 type: 'boolean',
156 },
157 },
158
159 name: 'Advanced',
160};