cli-start.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 utils = require('@nuxt/utils');
  9. const index = require('./cli-index.js');
  10. const banner = require('./cli-banner.js');
  11. const fs = require('fs');
  12. const path = require('path');
  13. const consola = require('consola');
  14. const connect = require('connect');
  15. const serveStatic = require('serve-static');
  16. const compression = require('compression');
  17. const config = require('@nuxt/config');
  18. require('exit');
  19. require('chalk');
  20. require('std-env');
  21. require('wrap-ansi');
  22. require('boxen');
  23. require('minimist');
  24. require('hable');
  25. require('defu');
  26. require('semver');
  27. require('execa');
  28. require('pretty-bytes');
  29. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  30. const consola__default = /*#__PURE__*/_interopDefaultLegacy(consola);
  31. const connect__default = /*#__PURE__*/_interopDefaultLegacy(connect);
  32. const serveStatic__default = /*#__PURE__*/_interopDefaultLegacy(serveStatic);
  33. const compression__default = /*#__PURE__*/_interopDefaultLegacy(compression);
  34. async function serve (cmd) {
  35. const _config = await cmd.getNuxtConfig({ dev: false });
  36. // add default options
  37. const options = config.getNuxtConfig(_config);
  38. try {
  39. // overwrites with build config
  40. const buildConfig = utils.requireModule(path.join(options.buildDir, 'nuxt/config.json'));
  41. options.target = buildConfig.target;
  42. } catch (err) { }
  43. const distStat = await fs.promises.stat(options.generate.dir).catch(err => null); // eslint-disable-line node/handle-callback-err
  44. const distPath = path.join(options.generate.dir.replace(process.cwd() + path.sep, ''), path.sep);
  45. if (!distStat || !distStat.isDirectory()) {
  46. throw new Error('Output directory `' + distPath + '` does not exist, please use `nuxt generate` before `nuxt start` for static target.')
  47. }
  48. const app = connect__default['default']();
  49. app.use(compression__default['default']({ threshold: 0 }));
  50. app.use(
  51. options.router.base,
  52. serveStatic__default['default'](options.generate.dir, {
  53. extensions: ['html']
  54. })
  55. );
  56. if (options.generate.fallback) {
  57. const fallbackFile = await fs.promises.readFile(path.join(options.generate.dir, options.generate.fallback), 'utf-8');
  58. app.use((req, res, next) => {
  59. const ext = path.extname(req.url) || '.html';
  60. if (ext !== '.html') {
  61. return next()
  62. }
  63. res.writeHeader(200, {
  64. 'Content-Type': 'text/html'
  65. });
  66. res.write(fallbackFile);
  67. res.end();
  68. });
  69. }
  70. const { port, host, socket, https } = options.server;
  71. const { Listener } = await index.server();
  72. const listener = new Listener({
  73. port,
  74. host,
  75. socket,
  76. https,
  77. app,
  78. dev: true, // try another port if taken
  79. baseURL: options.router.base
  80. });
  81. await listener.listen();
  82. const { Nuxt } = await index.core();
  83. banner.showBanner({
  84. constructor: Nuxt,
  85. options,
  86. server: {
  87. listeners: [listener]
  88. }
  89. }, false);
  90. consola__default['default'].info(`Serving static application from \`${distPath}\``);
  91. }
  92. const start = {
  93. name: 'start',
  94. description: 'Start the application in production mode (the application should be compiled with `nuxt build` first)',
  95. usage: 'start <dir>',
  96. options: {
  97. ...index.common,
  98. ...index.server$1
  99. },
  100. async run (cmd) {
  101. const config = await cmd.getNuxtConfig({ dev: false, _start: true });
  102. if (config.target === utils.TARGETS.static) {
  103. return serve(cmd)
  104. }
  105. const nuxt = await cmd.getNuxt(config);
  106. // Listen and show ready banner
  107. await nuxt.server.listen();
  108. banner.showBanner(nuxt);
  109. }
  110. };
  111. exports.default = start;