#!/usr/bin/env node /* Copyright © 2018 Andrew Powell This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. The above copyright notice and this permission notice shall be included in all copies or substantial portions of this Source Code Form. */ // require('v8-compile-cache'); const chalk = require('chalk'); const importLocal = require('import-local'); const parse = require('yargs-parser'); const webpack = require('webpack'); const pkg = require('../package.json'); const { check } = require('../lib/flags'); const { run } = require('../lib/compiler'); const { loadConfig } = require('../lib/config'); const { error: stderr } = console; const end = () => process.exit(0); const help = chalk` ${pkg.description} {underline Usage} $ webpack-serve [...options] {underline Options} --all Apply webpack-plugin-serve to all compilers in the config --client.address Overrides the WebSocket address in the client --client.retry Instructs the client to attempt to reconnect all WebSockets when interrupted --client.silent Instructs the client not to log anything to the console --compress Enables compression middleware which serves files with GZip compression --config A path to a webpack config file --config.\{name\} A path to a webpack config file, and the config name to run --help Displays this message --history-fallback Enables History API Fallback --hmr Enables Hot Module Replacement. On by default --host Sets the host the server should listen from --http2 Instructs the server to enable HTTP2 --live-reload Instructs the client to perform a full page reload after each build --no-watch Does not apply \`watch: true\` to the config, allowing for greater customization --open Opens the default browser to the set host and port --port Sets the port on which the server should listen --progress Shows build progress in the client --silent Instruct the CLI to produce no console output --static Sets the directory from which static files will be served --status Shows build status (errors, warnings) in the client --version Displays webpack-nano and webpack versions --wait-for-build Instructs the server to halt middleware processing until the current build is done {underline Examples} $ webpack-serve $ webpack-serve --help $ webpack-serve --config webpack.config.js $ webpack-serve --config.serve webpack.config.js `; const doeet = async () => { process.on('SIGINT', end); process.on('SIGTERM', end); const argv = parse(process.argv.slice(2)); const logPrefix = { ok: chalk.blue('⬡ webpack:'), whoops: chalk.red('⬢ webpack:') }; const log = { error: (...args) => { if (argv.silent) return; args.unshift(logPrefix.whoops); stderr(...args); }, info: (...args) => { if (argv.silent) return; args.unshift(logPrefix.ok); stderr(...args); } }; check(argv); if (argv.help) { stderr(help); return; } if (argv.version || argv.v) { stderr(` webpack-serve v${pkg.version} webpack v${webpack.version} `); return; } const config = await loadConfig(argv); run(config, log); }; process.on('unhandledRejection', (err) => { stderr(err.stack); process.exitCode = 1; }); // eslint-disable-next-line no-unused-expressions importLocal(__filename) || doeet();