UNPKG

3.74 kBPlain TextView Raw
1#!/usr/bin/env node
2
3/*
4 Copyright © 2018 Andrew Powell
5
6 This Source Code Form is subject to the terms of the Mozilla Public
7 License, v. 2.0. If a copy of the MPL was not distributed with this
8 file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10 The above copyright notice and this permission notice shall be
11 included in all copies or substantial portions of this Source Code Form.
12*/
13// require('v8-compile-cache');
14
15const chalk = require('chalk');
16const importLocal = require('import-local');
17const parse = require('yargs-parser');
18const webpack = require('webpack');
19
20const pkg = require('../package.json');
21const { check } = require('../lib/flags');
22const { run } = require('../lib/compiler');
23const { loadConfig } = require('../lib/config');
24
25const { error: stderr } = console;
26const end = () => process.exit(0);
27
28const help = chalk`
29 ${pkg.description}
30
31 {underline Usage}
32 $ webpack-serve [...options]
33
34 {underline Options}
35 --all Apply webpack-plugin-serve to all compilers in the config
36 --client.address Overrides the WebSocket address in the client
37 --client.retry Instructs the client to attempt to reconnect all WebSockets when interrupted
38 --client.silent Instructs the client not to log anything to the console
39 --compress Enables compression middleware which serves files with GZip compression
40 --config A path to a webpack config file
41 --config.\{name\} A path to a webpack config file, and the config name to run
42 --help Displays this message
43 --history-fallback Enables History API Fallback
44 --hmr Enables Hot Module Replacement. On by default
45 --host Sets the host the server should listen from
46 --http2 Instructs the server to enable HTTP2
47 --live-reload Instructs the client to perform a full page reload after each build
48 --no-watch Does not apply \`watch: true\` to the config, allowing for greater customization
49 --open Opens the default browser to the set host and port
50 --port Sets the port on which the server should listen
51 --progress Shows build progress in the client
52 --silent Instruct the CLI to produce no console output
53 --static Sets the directory from which static files will be served
54 --status Shows build status (errors, warnings) in the client
55 --version Displays webpack-nano and webpack versions
56 --wait-for-build Instructs the server to halt middleware processing until the current build is done
57
58 {underline Examples}
59 $ webpack-serve
60 $ webpack-serve --help
61 $ webpack-serve --config webpack.config.js
62 $ webpack-serve --config.serve webpack.config.js
63`;
64
65const doeet = async () => {
66 process.on('SIGINT', end);
67 process.on('SIGTERM', end);
68
69 const argv = parse(process.argv.slice(2));
70 const logPrefix = { ok: chalk.blue('⬡ webpack:'), whoops: chalk.red('⬢ webpack:') };
71 const log = {
72 error: (...args) => {
73 if (argv.silent) return;
74 args.unshift(logPrefix.whoops);
75 stderr(...args);
76 },
77 info: (...args) => {
78 if (argv.silent) return;
79 args.unshift(logPrefix.ok);
80 stderr(...args);
81 }
82 };
83
84 check(argv);
85
86 if (argv.help) {
87 stderr(help);
88 return;
89 }
90
91 if (argv.version || argv.v) {
92 stderr(`
93webpack-serve v${pkg.version}
94webpack v${webpack.version}
95`);
96 return;
97 }
98
99 const config = await loadConfig(argv);
100
101 run(config, log);
102};
103
104process.on('unhandledRejection', (err) => {
105 stderr(err.stack);
106 process.exitCode = 1;
107});
108
109// eslint-disable-next-line no-unused-expressions
110importLocal(__filename) || doeet();