UNPKG

5.78 kBJavaScriptView Raw
1'use strict';
2
3let bundle = (() => {
4 var _ref = _asyncToGenerator(function* (main, command) {
5 // Require bundler here so the help command is fast
6 const Bundler = require('../');
7
8 if (command.name() === 'build') {
9 process.env.NODE_ENV = 'production';
10 } else {
11 process.env.NODE_ENV = process.env.NODE_ENV || 'development';
12 }
13
14 if (command.cert && command.key) {
15 command.https = {
16 cert: command.cert,
17 key: command.key
18 };
19 }
20
21 const bundler = new Bundler(main, command);
22
23 if (command.name() === 'serve') {
24 const server = yield bundler.serve(command.port || 1234, command.https);
25 if (command.open) {
26 yield require('./utils/openInBrowser')(`${command.https ? 'https' : 'http'}://localhost:${server.address().port}`, command.open);
27 }
28 } else {
29 bundler.bundle();
30 }
31 });
32
33 return function bundle(_x, _x2) {
34 return _ref.apply(this, arguments);
35 };
36})();
37
38function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
39
40require('v8-compile-cache');
41const chalk = require('chalk');
42const program = require('commander');
43const version = require('../package.json').version;
44
45program.version(version);
46
47program.command('serve [input]').description('starts a development server').option('-p, --port <port>', 'set the port to serve on. defaults to 1234', parseInt).option('--hmr-port <port>', 'set the port to serve HMR websockets, defaults to random', parseInt).option('--hmr-hostname <hostname>', 'set the hostname of HMR websockets, defaults to location.hostname of current window').option('--https', 'serves files over HTTPS').option('--cert <path>', 'path to certificate to use with HTTPS').option('--key <path>', 'path to private key to use with HTTPS').option('--open [browser]', 'automatically open in specified browser, defaults to default browser').option('-d, --out-dir <path>', 'set the output directory. defaults to "dist"').option('-o, --out-file <filename>', 'set the output filename for the application entry point.').option('--public-url <url>', 'set the public URL to serve on. defaults to the same as the --out-dir option').option('--no-hmr', 'disable hot module replacement').option('--no-cache', 'disable the filesystem cache').option('--no-source-maps', 'disable sourcemaps').option('--no-autoinstall', 'disable autoinstall').option('-t, --target [target]', 'set the runtime environment, either "node", "browser" or "electron". defaults to "browser"', /^(node|browser|electron)$/).option('-V, --version', 'output the version number').option('--log-level <level>', 'set the log level, either "0" (no output), "1" (errors), "2" (warnings + errors) or "3" (all).', /^([0-3])$/).action(bundle);
48
49program.command('watch [input]').description('starts the bundler in watch mode').option('-d, --out-dir <path>', 'set the output directory. defaults to "dist"').option('-o, --out-file <filename>', 'set the output filename for the application entry point.').option('--public-url <url>', 'set the public URL to serve on. defaults to the same as the --out-dir option').option('--hmr-port <port>', 'set the port to serve HMR websockets, defaults to random', parseInt).option('--hmr-hostname <hostname>', 'set the hostname of HMR websockets, defaults to location.hostname of current window').option('--no-hmr', 'disable hot module replacement').option('--no-cache', 'disable the filesystem cache').option('--no-source-maps', 'disable sourcemaps').option('--no-autoinstall', 'disable autoinstall').option('-t, --target [target]', 'set the runtime environment, either "node", "browser" or "electron". defaults to "browser"', /^(node|browser|electron)$/).option('--log-level <level>', 'set the log level, either "0" (no output), "1" (errors), "2" (warnings + errors) or "3" (all).', /^([0-3])$/).action(bundle);
50
51program.command('build [input]').description('bundles for production').option('-d, --out-dir <path>', 'set the output directory. defaults to "dist"').option('-o, --out-file <filename>', 'set the output filename for the application entry point.').option('--public-url <url>', 'set the public URL to serve on. defaults to the same as the --out-dir option').option('--no-minify', 'disable minification').option('--no-cache', 'disable the filesystem cache').option('--no-source-maps', 'disable sourcemaps').option('-t, --target <target>', 'set the runtime environment, either "node", "browser" or "electron". defaults to "browser"', /^(node|browser|electron)$/).option('--detailed-report', 'print a detailed build report after a completed build').option('--log-level <level>', 'set the log level, either "0" (no output), "1" (errors), "2" (warnings + errors) or "3" (all).', /^([0-3])$/).action(bundle);
52
53program.command('help [command]').description('display help information for a command').action(function (command) {
54 let cmd = program.commands.find(c => c.name() === command) || program;
55 cmd.help();
56});
57
58program.on('--help', function () {
59 console.log('');
60 console.log(' Run `' + chalk.bold('parcel help <command>') + '` for more information on specific commands');
61 console.log('');
62});
63
64// Make serve the default command except for --help
65var args = process.argv;
66if (args[2] === '--help' || args[2] === '-h') args[2] = 'help';
67if (!args[2] || !program.commands.some(c => c.name() === args[2])) {
68 args.splice(2, 0, 'serve');
69}
70
71program.parse(args);
\No newline at end of file