webpack-dev-server.js 14 KB
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
#!/usr/bin/env node

'use strict';

/* eslint global-require: off, import/order: off, no-console: off */
require('../lib/polyfills');

const debug = require('debug')('webpack-dev-server');
const fs = require('fs');
const net = require('net');
const path = require('path');
const importLocal = require('import-local');
const open = require('opn');
const portfinder = require('portfinder');
const addDevServerEntrypoints = require('../lib/util/addDevServerEntrypoints');
const createDomain = require('../lib/util/createDomain'); // eslint-disable-line

// Prefer the local installation of webpack-dev-server
if (importLocal(__filename)) {
  debug('Using local install of webpack-dev-server');
  return;
}

const Server = require('../lib/Server');
const webpack = require('webpack'); // eslint-disable-line

function versionInfo() {
  return `webpack-dev-server ${require('../package.json').version}\n` +
  `webpack ${require('webpack/package.json').version}`;
}

function colorInfo(useColor, msg) {
  if (useColor) {
    // Make text blue and bold, so it *pops*
    return `\u001b[1m\u001b[34m${msg}\u001b[39m\u001b[22m`;
  }
  return msg;
}

function colorError(useColor, msg) {
  if (useColor) {
    // Make text red and bold, so it *pops*
    return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
  }
  return msg;
}

// eslint-disable-next-line
const defaultTo = (value, def) => value == null ? def : value;

const yargs = require('yargs')
  .usage(`${versionInfo()}\nUsage: https://webpack.js.org/configuration/dev-server/`);

require('webpack/bin/config-yargs')(yargs);

// It is important that this is done after the webpack yargs config,
// so it overrides webpack's version info.
yargs
  .version(versionInfo());

const ADVANCED_GROUP = 'Advanced options:';
const DISPLAY_GROUP = 'Stats options:';
const SSL_GROUP = 'SSL options:';
const CONNECTION_GROUP = 'Connection options:';
const RESPONSE_GROUP = 'Response options:';
const BASIC_GROUP = 'Basic options:';

// Taken out of yargs because we must know if
// it wasn't given by the user, in which case
// we should use portfinder.
const DEFAULT_PORT = 8080;

yargs.options({
  bonjour: {
    type: 'boolean',
    describe: 'Broadcasts the server via ZeroConf networking on start'
  },
  lazy: {
    type: 'boolean',
    describe: 'Lazy'
  },
  inline: {
    type: 'boolean',
    default: true,
    describe: 'Inline mode (set to false to disable including client scripts like livereload)'
  },
  progress: {
    type: 'boolean',
    describe: 'Print compilation progress in percentage',
    group: BASIC_GROUP
  },
  'hot-only': {
    type: 'boolean',
    describe: 'Do not refresh page if HMR fails',
    group: ADVANCED_GROUP
  },
  stdin: {
    type: 'boolean',
    describe: 'close when stdin ends'
  },
  open: {
    type: 'string',
    describe: 'Open the default browser, or optionally specify a browser name'
  },
  useLocalIp: {
    type: 'boolean',
    describe: 'Open default browser with local IP'
  },
  'open-page': {
    type: 'string',
    describe: 'Open default browser with the specified page',
    requiresArg: true
  },
  color: {
    type: 'boolean',
    alias: 'colors',
    default: function supportsColor() {
      return require('supports-color');
    },
    group: DISPLAY_GROUP,
    describe: 'Enables/Disables colors on the console'
  },
  info: {
    type: 'boolean',
    group: DISPLAY_GROUP,
    default: true,
    describe: 'Info'
  },
  quiet: {
    type: 'boolean',
    group: DISPLAY_GROUP,
    describe: 'Quiet'
  },
  'client-log-level': {
    type: 'string',
    group: DISPLAY_GROUP,
    default: 'info',
    describe: 'Log level in the browser (info, warning, error or none)'
  },
  https: {
    type: 'boolean',
    group: SSL_GROUP,
    describe: 'HTTPS'
  },
  key: {
    type: 'string',
    describe: 'Path to a SSL key.',
    group: SSL_GROUP
  },
  cert: {
    type: 'string',
    describe: 'Path to a SSL certificate.',
    group: SSL_GROUP
  },
  cacert: {
    type: 'string',
    describe: 'Path to a SSL CA certificate.',
    group: SSL_GROUP
  },
  pfx: {
    type: 'string',
    describe: 'Path to a SSL pfx file.',
    group: SSL_GROUP
  },
  'pfx-passphrase': {
    type: 'string',
    describe: 'Passphrase for pfx file.',
    group: SSL_GROUP
  },
  'content-base': {
    type: 'string',
    describe: 'A directory or URL to serve HTML content from.',
    group: RESPONSE_GROUP
  },
  'watch-content-base': {
    type: 'boolean',
    describe: 'Enable live-reloading of the content-base.',
    group: RESPONSE_GROUP
  },
  'history-api-fallback': {
    type: 'boolean',
    describe: 'Fallback to /index.html for Single Page Applications.',
    group: RESPONSE_GROUP
  },
  compress: {
    type: 'boolean',
    describe: 'Enable gzip compression',
    group: RESPONSE_GROUP
  },
  port: {
    describe: 'The port',
    group: CONNECTION_GROUP
  },
  'disable-host-check': {
    type: 'boolean',
    describe: 'Will not check the host',
    group: CONNECTION_GROUP
  },
  socket: {
    type: 'String',
    describe: 'Socket to listen',
    group: CONNECTION_GROUP
  },
  public: {
    type: 'string',
    describe: 'The public hostname/ip address of the server',
    group: CONNECTION_GROUP
  },
  host: {
    type: 'string',
    default: 'localhost',
    describe: 'The hostname/ip address the server will bind to',
    group: CONNECTION_GROUP
  },
  'allowed-hosts': {
    type: 'string',
    describe: 'A comma-delimited string of hosts that are allowed to access the dev server',
    group: CONNECTION_GROUP
  }
});

const argv = yargs.argv;
const wpOpt = require('webpack/bin/convert-argv')(yargs, argv, {
  outputFilename: '/bundle.js'
});

function processOptions(webpackOptions) {
  // process Promise
  if (typeof webpackOptions.then === 'function') {
    webpackOptions.then(processOptions).catch((err) => {
      console.error(err.stack || err);
      process.exit(); // eslint-disable-line
    });
    return;
  }

  const firstWpOpt = Array.isArray(webpackOptions) ? webpackOptions[0] : webpackOptions;

  const options = webpackOptions.devServer || firstWpOpt.devServer || {};

  if (argv.bonjour) { options.bonjour = true; }

  if (argv.host !== 'localhost' || !options.host) { options.host = argv.host; }

  if (argv['allowed-hosts']) { options.allowedHosts = argv['allowed-hosts'].split(','); }

  if (argv.public) { options.public = argv.public; }

  if (argv.socket) { options.socket = argv.socket; }

  if (argv.progress) { options.progress = argv.progress; }

  if (!options.publicPath) {
    // eslint-disable-next-line
    options.publicPath = firstWpOpt.output && firstWpOpt.output.publicPath || '';
    if (!/^(https?:)?\/\//.test(options.publicPath) && options.publicPath[0] !== '/') {
      options.publicPath = `/${options.publicPath}`;
    }
  }

  if (!options.filename) { options.filename = firstWpOpt.output && firstWpOpt.output.filename; }

  if (!options.watchOptions) { options.watchOptions = firstWpOpt.watchOptions; }

  if (argv.stdin) {
    process.stdin.on('end', () => {
      process.exit(0); // eslint-disable-line no-process-exit
    });
    process.stdin.resume();
  }

  if (!options.hot) { options.hot = argv.hot; }

  if (!options.hotOnly) { options.hotOnly = argv['hot-only']; }

  if (!options.clientLogLevel) { options.clientLogLevel = argv['client-log-level']; }

  // eslint-disable-next-line
  if (options.contentBase === undefined) {
    if (argv['content-base']) {
      options.contentBase = argv['content-base'];
      if (Array.isArray(options.contentBase)) {
        options.contentBase = options.contentBase.map(val => path.resolve(val));
      } else if (/^[0-9]$/.test(options.contentBase)) { options.contentBase = +options.contentBase; } else if (!/^(https?:)?\/\//.test(options.contentBase)) { options.contentBase = path.resolve(options.contentBase); }
      // It is possible to disable the contentBase by using `--no-content-base`, which results in arg["content-base"] = false
    } else if (argv['content-base'] === false) {
      options.contentBase = false;
    }
  }

  if (argv['watch-content-base']) { options.watchContentBase = true; }

  if (!options.stats) {
    options.stats = {
      cached: false,
      cachedAssets: false
    };
  }

  if (typeof options.stats === 'object' && typeof options.stats.colors === 'undefined') {
    options.stats = Object.assign({}, options.stats, { colors: argv.color });
  }

  if (argv.lazy) { options.lazy = true; }

  if (!argv.info) { options.noInfo = true; }

  if (argv.quiet) { options.quiet = true; }

  if (argv.https) { options.https = true; }

  if (argv.cert) { options.cert = fs.readFileSync(path.resolve(argv.cert)); }

  if (argv.key) { options.key = fs.readFileSync(path.resolve(argv.key)); }

  if (argv.cacert) { options.ca = fs.readFileSync(path.resolve(argv.cacert)); }

  if (argv.pfx) { options.pfx = fs.readFileSync(path.resolve(argv.pfx)); }

  if (argv['pfx-passphrase']) { options.pfxPassphrase = argv['pfx-passphrase']; }

  if (argv.inline === false) { options.inline = false; }

  if (argv['history-api-fallback']) { options.historyApiFallback = true; }

  if (argv.compress) { options.compress = true; }

  if (argv['disable-host-check']) { options.disableHostCheck = true; }

  if (argv['open-page']) {
    options.open = true;
    options.openPage = argv['open-page'];
  }

  if (typeof argv.open !== 'undefined') {
    options.open = argv.open !== '' ? argv.open : true;
  }

  if (options.open && !options.openPage) { options.openPage = ''; }

  if (argv.useLocalIp) { options.useLocalIp = true; }

  // Kind of weird, but ensures prior behavior isn't broken in cases
  // that wouldn't throw errors. E.g. both argv.port and options.port
  // were specified, but since argv.port is 8080, options.port will be
  // tried first instead.
  options.port = argv.port === DEFAULT_PORT ? defaultTo(options.port, argv.port) : defaultTo(argv.port, options.port);

  if (options.port != null) {
    startDevServer(webpackOptions, options);
    return;
  }

  portfinder.basePort = DEFAULT_PORT;
  portfinder.getPort((err, port) => {
    if (err) throw err;
    options.port = port;
    startDevServer(webpackOptions, options);
  });
}

function startDevServer(webpackOptions, options) {
  addDevServerEntrypoints(webpackOptions, options);

  let compiler;
  try {
    compiler = webpack(webpackOptions);
  } catch (e) {
    if (e instanceof webpack.WebpackOptionsValidationError) {
      console.error(colorError(options.stats.colors, e.message));
      process.exit(1); // eslint-disable-line
    }
    throw e;
  }

  if (options.progress) {
    compiler.apply(new webpack.ProgressPlugin({
      profile: argv.profile
    }));
  }

  const suffix = (options.inline !== false || options.lazy === true ? '/' : '/webpack-dev-server/');

  let server;
  try {
    server = new Server(compiler, options);
  } catch (e) {
    const OptionsValidationError = require('../lib/OptionsValidationError');
    if (e instanceof OptionsValidationError) {
      console.error(colorError(options.stats.colors, e.message));
          process.exit(1); // eslint-disable-line
    }
    throw e;
  }

  ['SIGINT', 'SIGTERM'].forEach((sig) => {
    process.on(sig, () => {
      server.close(() => {
        process.exit(); // eslint-disable-line no-process-exit
      });
    });
  });

  if (options.socket) {
    server.listeningApp.on('error', (e) => {
      if (e.code === 'EADDRINUSE') {
        const clientSocket = new net.Socket();
        clientSocket.on('error', (clientError) => {
          if (clientError.code === 'ECONNREFUSED') {
            // No other server listening on this socket so it can be safely removed
            fs.unlinkSync(options.socket);
            server.listen(options.socket, options.host, (err) => {
              if (err) throw err;
            });
          }
        });
        clientSocket.connect({ path: options.socket }, () => {
          throw new Error('This socket is already used');
        });
      }
    });
    server.listen(options.socket, options.host, (err) => {
      if (err) throw err;
      // chmod 666 (rw rw rw)
      const READ_WRITE = 438;
      fs.chmod(options.socket, READ_WRITE, (fsError) => {
        if (fsError) throw fsError;

        const uri = createDomain(options, server.listeningApp) + suffix;
        reportReadiness(uri, options);
      });
    });
  } else {
    server.listen(options.port, options.host, (err) => {
      if (err) throw err;
      if (options.bonjour) broadcastZeroconf(options);

      const uri = createDomain(options, server.listeningApp) + suffix;
      reportReadiness(uri, options);
    });
  }
}

function reportReadiness(uri, options) {
  const useColor = argv.color;
  const contentBase = Array.isArray(options.contentBase) ? options.contentBase.join(', ') : options.contentBase;

  if (!options.quiet) {
    let startSentence = `Project is running at ${colorInfo(useColor, uri)}`;
    if (options.socket) {
      startSentence = `Listening to socket at ${colorInfo(useColor, options.socket)}`;
    }

    console.log((options.progress ? '\n' : '') + startSentence);

    console.log(`webpack output is served from ${colorInfo(useColor, options.publicPath)}`);

    if (contentBase) { console.log(`Content not from webpack is served from ${colorInfo(useColor, contentBase)}`); }

    if (options.historyApiFallback) { console.log(`404s will fallback to ${colorInfo(useColor, options.historyApiFallback.index || '/index.html')}`); }

    if (options.bonjour) { console.log('Broadcasting "http" with subtype of "webpack" via ZeroConf DNS (Bonjour)'); }
  }

  if (options.open) {
    let openOptions = {};
    let openMessage = 'Unable to open browser';

    if (typeof options.open === 'string') {
      openOptions = { app: options.open };
      openMessage += `: ${options.open}`;
    }

    open(uri + (options.openPage || ''), openOptions).catch(() => {
      console.log(`${openMessage}. If you are running in a headless environment, please do not use the open flag.`);
    });
  }
}

function broadcastZeroconf(options) {
  const bonjour = require('bonjour')();
  bonjour.publish({
    name: 'Webpack Dev Server',
    port: options.port,
    type: 'http',
    subtypes: ['webpack']
  });
  process.on('exit', () => {
    bonjour.unpublishAll(() => {
      bonjour.destroy();
    });
  });
}

processOptions(wpOpt);