cli-dev.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*!
  2. * @nuxt/cli v2.15.8 (c) 2016-2021
  3. * Released under the MIT License
  4. * Repository: https://github.com/nuxt/nuxt.js
  5. * Website: https://nuxtjs.org
  6. */
  7. 'use strict';
  8. const consola = require('consola');
  9. const chalk = require('chalk');
  10. const opener = require('opener');
  11. const index = require('./cli-index.js');
  12. const banner = require('./cli-banner.js');
  13. require('@nuxt/utils');
  14. require('@nuxt/config');
  15. require('path');
  16. require('exit');
  17. require('std-env');
  18. require('wrap-ansi');
  19. require('boxen');
  20. require('minimist');
  21. require('hable');
  22. require('defu');
  23. require('semver');
  24. require('fs');
  25. require('execa');
  26. require('pretty-bytes');
  27. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  28. const consola__default = /*#__PURE__*/_interopDefaultLegacy(consola);
  29. const chalk__default = /*#__PURE__*/_interopDefaultLegacy(chalk);
  30. const opener__default = /*#__PURE__*/_interopDefaultLegacy(opener);
  31. const dev = {
  32. name: 'dev',
  33. description: 'Start the application in development mode (e.g. hot-code reloading, error reporting)',
  34. usage: 'dev <dir>',
  35. options: {
  36. ...index.common,
  37. ...index.server$1,
  38. open: {
  39. alias: 'o',
  40. type: 'boolean',
  41. description: 'Opens the server listeners url in the default browser'
  42. }
  43. },
  44. async run (cmd) {
  45. const { argv } = cmd;
  46. await this.startDev(cmd, argv, argv.open);
  47. },
  48. async startDev (cmd, argv) {
  49. let nuxt;
  50. try {
  51. nuxt = await this._listenDev(cmd, argv);
  52. } catch (error) {
  53. consola__default['default'].fatal(error);
  54. return
  55. }
  56. try {
  57. await this._buildDev(cmd, argv, nuxt);
  58. } catch (error) {
  59. await nuxt.callHook('cli:buildError', error);
  60. consola__default['default'].error(error);
  61. }
  62. return nuxt
  63. },
  64. async _listenDev (cmd, argv) {
  65. const config = await cmd.getNuxtConfig({ dev: true, _build: true });
  66. const nuxt = await cmd.getNuxt(config);
  67. // Setup hooks
  68. nuxt.hook('watch:restart', payload => this.onWatchRestart(payload, { nuxt, cmd, argv }));
  69. nuxt.hook('bundler:change', changedFileName => this.onBundlerChange(changedFileName));
  70. // Wait for nuxt to be ready
  71. await nuxt.ready();
  72. // Start listening
  73. await nuxt.server.listen();
  74. // Show banner when listening
  75. banner.showBanner(nuxt, false);
  76. // Opens the server listeners url in the default browser (only once)
  77. if (argv.open) {
  78. argv.open = false;
  79. const openerPromises = nuxt.server.listeners.map(listener => opener__default['default'](listener.url));
  80. await Promise.all(openerPromises);
  81. }
  82. // Return instance
  83. return nuxt
  84. },
  85. async _buildDev (cmd, argv, nuxt) {
  86. // Create builder instance
  87. const builder = await cmd.getBuilder(nuxt);
  88. // Start Build
  89. await builder.build();
  90. // Print memory usage
  91. banner.showMemoryUsage();
  92. // Display server urls after the build
  93. for (const listener of nuxt.server.listeners) {
  94. consola__default['default'].info(chalk__default['default'].bold('Listening on: ') + listener.url);
  95. }
  96. // Return instance
  97. return nuxt
  98. },
  99. logChanged ({ event, path }) {
  100. const { icon, color, action } = index.eventsMapping[event] || index.eventsMapping.change;
  101. consola__default['default'].log({
  102. type: event,
  103. icon: chalk__default['default'][color].bold(icon),
  104. message: `${action} ${chalk__default['default'].cyan(index.formatPath(path))}`
  105. });
  106. },
  107. async onWatchRestart ({ event, path }, { nuxt, cmd, argv }) {
  108. this.logChanged({ event, path });
  109. await nuxt.close();
  110. await this.startDev(cmd, argv);
  111. },
  112. onBundlerChange (path) {
  113. this.logChanged({ event: 'change', path });
  114. }
  115. };
  116. exports.default = dev;