UNPKG

5.64 kBJavaScriptView Raw
1require('v8-compile-cache');
2const chalk = require('chalk');
3const program = require('commander');
4const version = require('../package.json').version;
5
6program.version(version);
7
8program
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 '--hmr-port <port>',
18 'set the port to serve HMR websockets, defaults to random',
19 parseInt
20 )
21 .option(
22 '--hmr-hostname <hostname>',
23 'set the hostname of HMR websockets, defaults to location.hostname of current window'
24 )
25 .option('--https', 'serves files over HTTPS')
26 .option('--cert <path>', 'path to certificate to use with HTTPS')
27 .option('--key <path>', 'path to private key to use with HTTPS')
28 .option(
29 '--open [browser]',
30 'automatically open in specified browser, defaults to default browser'
31 )
32 .option(
33 '-d, --out-dir <path>',
34 'set the output directory. defaults to "dist"'
35 )
36 .option(
37 '-o, --out-file <filename>',
38 'set the output filename for the application entry point.'
39 )
40 .option(
41 '--public-url <url>',
42 'set the public URL to serve on. defaults to the same as the --out-dir option'
43 )
44 .option('--no-hmr', 'disable hot module replacement')
45 .option('--no-cache', 'disable the filesystem cache')
46 .option('--no-source-maps', 'disable sourcemaps')
47 .option('--no-autoinstall', 'disable autoinstall')
48 .option(
49 '-t, --target [target]',
50 'set the runtime environment, either "node", "browser" or "electron". defaults to "browser"',
51 /^(node|browser|electron)$/
52 )
53 .option('-V, --version', 'output the version number')
54 .option(
55 '--log-level <level>',
56 'set the log level, either "0" (no output), "1" (errors), "2" (warnings + errors) or "3" (all).',
57 /^([0-3])$/
58 )
59 .action(bundle);
60
61program
62 .command('watch [input]')
63 .description('starts the bundler in watch mode')
64 .option(
65 '-d, --out-dir <path>',
66 'set the output directory. defaults to "dist"'
67 )
68 .option(
69 '-o, --out-file <filename>',
70 'set the output filename for the application entry point.'
71 )
72 .option(
73 '--public-url <url>',
74 'set the public URL to serve on. defaults to the same as the --out-dir option'
75 )
76 .option(
77 '--hmr-port <port>',
78 'set the port to serve HMR websockets, defaults to random',
79 parseInt
80 )
81 .option(
82 '--hmr-hostname <hostname>',
83 'set the hostname of HMR websockets, defaults to location.hostname of current window'
84 )
85 .option('--no-hmr', 'disable hot module replacement')
86 .option('--no-cache', 'disable the filesystem cache')
87 .option('--no-source-maps', 'disable sourcemaps')
88 .option('--no-autoinstall', 'disable autoinstall')
89 .option(
90 '-t, --target [target]',
91 'set the runtime environment, either "node", "browser" or "electron". defaults to "browser"',
92 /^(node|browser|electron)$/
93 )
94 .option(
95 '--log-level <level>',
96 'set the log level, either "0" (no output), "1" (errors), "2" (warnings + errors) or "3" (all).',
97 /^([0-3])$/
98 )
99 .action(bundle);
100
101program
102 .command('build [input]')
103 .description('bundles for production')
104 .option(
105 '-d, --out-dir <path>',
106 'set the output directory. defaults to "dist"'
107 )
108 .option(
109 '-o, --out-file <filename>',
110 'set the output filename for the application entry point.'
111 )
112 .option(
113 '--public-url <url>',
114 'set the public URL to serve on. defaults to the same as the --out-dir option'
115 )
116 .option('--no-minify', 'disable minification')
117 .option('--no-cache', 'disable the filesystem cache')
118 .option('--no-source-maps', 'disable sourcemaps')
119 .option(
120 '-t, --target <target>',
121 'set the runtime environment, either "node", "browser" or "electron". defaults to "browser"',
122 /^(node|browser|electron)$/
123 )
124 .option(
125 '--detailed-report',
126 'print a detailed build report after a completed build'
127 )
128 .option(
129 '--log-level <level>',
130 'set the log level, either "0" (no output), "1" (errors), "2" (warnings + errors) or "3" (all).',
131 /^([0-3])$/
132 )
133 .action(bundle);
134
135program
136 .command('help [command]')
137 .description('display help information for a command')
138 .action(function(command) {
139 let cmd = program.commands.find(c => c.name() === command) || program;
140 cmd.help();
141 });
142
143program.on('--help', function() {
144 console.log('');
145 console.log(
146 ' Run `' +
147 chalk.bold('parcel help <command>') +
148 '` for more information on specific commands'
149 );
150 console.log('');
151});
152
153// Make serve the default command except for --help
154var args = process.argv;
155if (args[2] === '--help' || args[2] === '-h') args[2] = 'help';
156if (!args[2] || !program.commands.some(c => c.name() === args[2])) {
157 args.splice(2, 0, 'serve');
158}
159
160program.parse(args);
161
162async function bundle(main, command) {
163 // Require bundler here so the help command is fast
164 const Bundler = require('../');
165
166 if (command.name() === 'build') {
167 process.env.NODE_ENV = 'production';
168 } else {
169 process.env.NODE_ENV = process.env.NODE_ENV || 'development';
170 }
171
172 if (command.cert && command.key) {
173 command.https = {
174 cert: command.cert,
175 key: command.key
176 };
177 }
178
179 const bundler = new Bundler(main, command);
180
181 if (command.name() === 'serve') {
182 const server = await bundler.serve(command.port || 1234, command.https);
183 if (command.open) {
184 await require('./utils/openInBrowser')(
185 `${command.https ? 'https' : 'http'}://localhost:${
186 server.address().port
187 }`,
188 command.open
189 );
190 }
191 } else {
192 bundler.bundle();
193 }
194}