forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·210 lines (197 loc) · 6.5 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
require('v8-compile-cache');
const chalk = require('chalk');
const program = require('commander');
const version = require('../package.json').version;
program.version(version);
program
.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('--global <variable>', 'expose your module through a global variable')
.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])$/
)
.option('--cache-dir <path>', 'set the cache directory. defaults to ".cache"')
.action(bundle);
program
.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('--global <variable>', 'expose your module through a global variable')
.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', 'listen on HTTPS for HMR connections')
.option('--cert <path>', 'path to certificate to use with HTTPS')
.option('--key <path>', 'path to private key to use with HTTPS')
.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])$/
)
.option('--cache-dir <path>', 'set the cache directory. defaults to ".cache"')
.action(bundle);
program
.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('--global <variable>', 'expose your module through a global variable')
.option('--no-minify', 'disable minification')
.option('--no-cache', 'disable the filesystem cache')
.option('--no-source-maps', 'disable sourcemaps')
.option(
'--experimental-scope-hoisting',
'enable experimental scope hoisting/tree shaking support'
)
.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])$/
)
.option('--cache-dir <path>', 'set the cache directory. defaults to ".cache"')
.action(bundle);
program
.command('help [command]')
.description('display help information for a command')
.action(function(command) {
let cmd = program.commands.find(c => c.name() === command) || program;
cmd.help();
});
program.on('--help', function() {
console.log('');
console.log(
' Run `' +
chalk.bold('parcel help <command>') +
'` for more information on specific commands'
);
console.log('');
});
// Make serve the default command except for --help
var args = process.argv;
if (args[2] === '--help' || args[2] === '-h') args[2] = 'help';
if (!args[2] || !program.commands.some(c => c.name() === args[2])) {
args.splice(2, 0, 'serve');
}
program.parse(args);
async function bundle(main, command) {
// Require bundler here so the help command is fast
const Bundler = require('../');
if (command.name() === 'build') {
command.production = true;
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
} else {
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
}
if (command.cert && command.key) {
command.https = {
cert: command.cert,
key: command.key
};
}
command.scopeHoist = command.experimentalScopeHoisting || false;
const bundler = new Bundler(main, command);
command.target = command.target || 'browser';
if (command.name() === 'serve' && command.target === 'browser') {
const server = await bundler.serve(command.port || 1234, command.https);
if (server && command.open) {
await require('./utils/openInBrowser')(
`${command.https ? 'https' : 'http'}://localhost:${
server.address().port
}`,
command.open
);
}
} else {
bundler.bundle();
}
}