line_push/node_modules/@nuxt/cli/dist/cli-serve.js
2022-07-17 13:16:16 +08:00

109 lines
3.3 KiB
JavaScript

/*!
* @nuxt/cli v2.13.3 (c) 2016-2020
* - All the amazing contributors
* Released under the MIT License.
* Website: https://nuxtjs.org
*/
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
const index = require('./cli-index.js');
const path = require('path');
const path__default = _interopDefault(path);
const config = require('@nuxt/config');
require('exit');
const utils = require('@nuxt/utils');
require('chalk');
require('std-env');
require('wrap-ansi');
require('boxen');
require('consola');
require('minimist');
require('hable');
const fs = require('fs');
const fs__default = _interopDefault(fs);
require('execa');
require('pretty-bytes');
const banner = require('./cli-banner.js');
const connect = _interopDefault(require('connect'));
const serveStatic = _interopDefault(require('serve-static'));
const compression = _interopDefault(require('compression'));
const serve = {
name: 'serve',
description: 'Serve the exported static application (should be compiled with `nuxt build` and `nuxt export` first)',
usage: 'serve <dir>',
options: {
'config-file': index.common['config-file'],
version: index.common.version,
help: index.common.help,
...index.server
},
async run (cmd) {
let options = await cmd.getNuxtConfig({ dev: false });
// add default options
options = config.getNuxtConfig(options);
try {
// overwrites with build config
const buildConfig = require(path.join(options.buildDir, 'nuxt/config.json'));
options.target = buildConfig.target;
} catch (err) {}
if (options.target === utils.TARGETS.server) {
throw new Error('You cannot use `nuxt serve` with ' + utils.TARGETS.server + ' target, please use `nuxt start`')
}
const distStat = await fs.promises.stat(options.generate.dir).catch(err => null); // eslint-disable-line handle-callback-err
if (!distStat || !distStat.isDirectory()) {
throw new Error('Output directory `' + path.basename(options.generate.dir) + '/` does not exists, please run `nuxt export` before `nuxt serve`.')
}
const app = connect();
app.use(compression({ threshold: 0 }));
app.use(
options.router.base,
serveStatic(options.generate.dir, {
extensions: ['html']
})
);
if (options.generate.fallback) {
const fallbackFile = await fs.promises.readFile(path.join(options.generate.dir, options.generate.fallback), 'utf-8');
app.use((req, res, next) => {
const ext = path.extname(req.url) || '.html';
if (ext !== '.html') {
return next()
}
res.writeHeader(200, {
'Content-Type': 'text/html'
});
res.write(fallbackFile);
res.end();
});
}
const { port, host, socket, https } = options.server;
const { Listener } = await index.server$1();
const listener = new Listener({
port,
host,
socket,
https,
app,
dev: true, // try another port if taken
baseURL: options.router.base
});
await listener.listen();
const { Nuxt } = await index.core();
banner.showBanner({
constructor: Nuxt,
options,
server: {
listeners: [listener]
}
}, false);
}
};
exports.default = serve;