1 | require('v8-compile-cache');
|
2 | const chalk = require('chalk');
|
3 | const program = require('commander');
|
4 | const version = require('../package.json').version;
|
5 |
|
6 | program.version(version);
|
7 |
|
8 | program
|
9 | .command('serve [input...]')
|
10 | .description('starts a development server')
|
11 | .option(
|
12 | '-p, --port <port>',
|
13 | 'set the port to serve on. defaults to 1234',
|
14 | parseInt
|
15 | )
|
16 | .option(
|
17 | '--host <host>',
|
18 | 'set the host to listen on, defaults to listening on all interfaces'
|
19 | )
|
20 | .option(
|
21 | '--hmr-port <port>',
|
22 | 'set the port to serve HMR websockets, defaults to random',
|
23 | parseInt
|
24 | )
|
25 | .option(
|
26 | '--hmr-hostname <hostname>',
|
27 | 'set the hostname of HMR websockets, defaults to location.hostname of current window'
|
28 | )
|
29 | .option('--https', 'serves files over HTTPS')
|
30 | .option('--cert <path>', 'path to certificate to use with HTTPS')
|
31 | .option('--key <path>', 'path to private key to use with HTTPS')
|
32 | .option(
|
33 | '--open [browser]',
|
34 | 'automatically open in specified browser, defaults to default browser'
|
35 | )
|
36 | .option(
|
37 | '-d, --out-dir <path>',
|
38 | 'set the output directory. defaults to "dist"'
|
39 | )
|
40 | .option(
|
41 | '-o, --out-file <filename>',
|
42 | 'set the output filename for the application entry point.'
|
43 | )
|
44 | .option(
|
45 | '--public-url <url>',
|
46 | 'set the public URL to serve on. defaults to "/"'
|
47 | )
|
48 | .option('--global <variable>', 'expose your module through a global variable')
|
49 | .option('--no-hmr', 'disable hot module replacement')
|
50 | .option('--no-cache', 'disable the filesystem cache')
|
51 | .option('--no-source-maps', 'disable sourcemaps')
|
52 | .option('--no-autoinstall', 'disable autoinstall')
|
53 | .option(
|
54 | '-t, --target [target]',
|
55 | 'set the runtime environment, either "node", "browser" or "electron". defaults to "browser"',
|
56 | /^(node|browser|electron)$/
|
57 | )
|
58 | .option(
|
59 | '--bundle-node-modules',
|
60 | 'force bundling node modules, even on node/electron target'
|
61 | )
|
62 | .option('-V, --version', 'output the version number')
|
63 | .option(
|
64 | '--log-level <level>',
|
65 | 'set the log level, either "0" (no output), "1" (errors), "2" (warnings), "3" (info), "4" (verbose) or "5" (debug, creates a log file).',
|
66 | /^([0-5])$/
|
67 | )
|
68 | .option('--cache-dir <path>', 'set the cache directory. defaults to ".cache"')
|
69 | .action(bundle);
|
70 |
|
71 | program
|
72 | .command('watch [input...]')
|
73 | .description('starts the bundler in watch mode')
|
74 | .option(
|
75 | '-d, --out-dir <path>',
|
76 | 'set the output directory. defaults to "dist"'
|
77 | )
|
78 | .option(
|
79 | '-o, --out-file <filename>',
|
80 | 'set the output filename for the application entry point.'
|
81 | )
|
82 | .option(
|
83 | '--public-url <url>',
|
84 | 'set the public URL to serve on. defaults to "/"'
|
85 | )
|
86 | .option('--global <variable>', 'expose your module through a global variable')
|
87 | .option(
|
88 | '--hmr-port <port>',
|
89 | 'set the port to serve HMR websockets, defaults to random',
|
90 | parseInt
|
91 | )
|
92 | .option(
|
93 | '--hmr-hostname <hostname>',
|
94 | 'set the hostname of HMR websockets, defaults to location.hostname of current window'
|
95 | )
|
96 | .option('--https', 'listen on HTTPS for HMR connections')
|
97 | .option('--cert <path>', 'path to certificate to use with HTTPS')
|
98 | .option('--key <path>', 'path to private key to use with HTTPS')
|
99 | .option('--no-hmr', 'disable hot module replacement')
|
100 | .option('--no-cache', 'disable the filesystem cache')
|
101 | .option('--no-source-maps', 'disable sourcemaps')
|
102 | .option('--no-autoinstall', 'disable autoinstall')
|
103 | .option(
|
104 | '-t, --target [target]',
|
105 | 'set the runtime environment, either "node", "browser" or "electron". defaults to "browser"',
|
106 | /^(node|browser|electron)$/
|
107 | )
|
108 | .option(
|
109 | '--bundle-node-modules',
|
110 | 'force bundling node modules, even on node/electron target'
|
111 | )
|
112 | .option(
|
113 | '--log-level <level>',
|
114 | 'set the log level, either "0" (no output), "1" (errors), "2" (warnings), "3" (info), "4" (verbose) or "5" (debug, creates a log file).',
|
115 | /^([0-5])$/
|
116 | )
|
117 | .option('--cache-dir <path>', 'set the cache directory. defaults to ".cache"')
|
118 | .action(bundle);
|
119 |
|
120 | program
|
121 | .command('build [input...]')
|
122 | .description('bundles for production')
|
123 | .option(
|
124 | '-d, --out-dir <path>',
|
125 | 'set the output directory. defaults to "dist"'
|
126 | )
|
127 | .option(
|
128 | '-o, --out-file <filename>',
|
129 | 'set the output filename for the application entry point.'
|
130 | )
|
131 | .option(
|
132 | '--public-url <url>',
|
133 | 'set the public URL to serve on. defaults to "/"'
|
134 | )
|
135 | .option('--global <variable>', 'expose your module through a global variable')
|
136 | .option('--no-minify', 'disable minification')
|
137 | .option('--no-cache', 'disable the filesystem cache')
|
138 | .option('--no-source-maps', 'disable sourcemaps')
|
139 | .option('--no-autoinstall', 'disable autoinstall')
|
140 | .option('--no-content-hash', 'disable content hashing')
|
141 | .option(
|
142 | '--experimental-scope-hoisting',
|
143 | 'enable experimental scope hoisting/tree shaking support'
|
144 | )
|
145 | .option(
|
146 | '-t, --target <target>',
|
147 | 'set the runtime environment, either "node", "browser" or "electron". defaults to "browser"',
|
148 | /^(node|browser|electron)$/
|
149 | )
|
150 | .option(
|
151 | '--bundle-node-modules',
|
152 | 'force bundling node modules, even on node/electron target'
|
153 | )
|
154 | .option(
|
155 | '--detailed-report [depth]',
|
156 | 'print a detailed build report after a completed build. If enabled, defaults to depth "10"',
|
157 | /^([0-9]+|all)$/
|
158 | )
|
159 | .option(
|
160 | '--log-level <level>',
|
161 | 'set the log level, either "0" (no output), "1" (errors), "2" (warnings), "3" (info), "4" (verbose) or "5" (debug, creates a log file).',
|
162 | /^([0-5])$/
|
163 | )
|
164 | .option('--cache-dir <path>', 'set the cache directory. defaults to ".cache"')
|
165 | .action(bundle);
|
166 |
|
167 | program
|
168 | .command('help [command]')
|
169 | .description('display help information for a command')
|
170 | .action(function(command) {
|
171 | let cmd = program.commands.find(c => c.name() === command) || program;
|
172 | cmd.help();
|
173 | });
|
174 |
|
175 | program.on('--help', function() {
|
176 | console.log('');
|
177 | console.log(
|
178 | ' Run `' +
|
179 | chalk.bold('parcel help <command>') +
|
180 | '` for more information on specific commands'
|
181 | );
|
182 | console.log('');
|
183 | });
|
184 |
|
185 |
|
186 | var args = process.argv;
|
187 | if (args[2] === '--help' || args[2] === '-h') args[2] = 'help';
|
188 | if (!args[2] || !program.commands.some(c => c.name() === args[2])) {
|
189 | args.splice(2, 0, 'serve');
|
190 | }
|
191 |
|
192 | program.parse(args);
|
193 |
|
194 | async function bundle(main, command) {
|
195 |
|
196 | const Bundler = require('../');
|
197 |
|
198 | if (command.name() === 'watch') {
|
199 | command.watch = true;
|
200 | }
|
201 |
|
202 | if (command.name() === 'build') {
|
203 | command.production = true;
|
204 | process.env.NODE_ENV = process.env.NODE_ENV || 'production';
|
205 | } else {
|
206 | process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
207 | }
|
208 |
|
209 | if (command.cert && command.key) {
|
210 | command.https = {
|
211 | cert: command.cert,
|
212 | key: command.key
|
213 | };
|
214 | }
|
215 |
|
216 | command.throwErrors = false;
|
217 | command.scopeHoist = command.experimentalScopeHoisting || false;
|
218 |
|
219 | const bundler = new Bundler(main, command);
|
220 |
|
221 | command.target = command.target || 'browser';
|
222 | if (command.name() === 'serve' && command.target === 'browser') {
|
223 | const port = command.port || process.env.PORT || 1234;
|
224 | const server = await bundler.serve(port, command.https, command.host);
|
225 | if (server && command.open) {
|
226 | await require('./utils/openInBrowser')(
|
227 | `${command.https ? 'https' : 'http'}://${command.host || 'localhost'}:${
|
228 | server.address().port
|
229 | }`,
|
230 | command.open
|
231 | );
|
232 | }
|
233 | } else {
|
234 | bundler.bundle();
|
235 | }
|
236 | }
|