mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-11 14:57:15 +00:00
80 lines
2.0 KiB
JavaScript
Executable File
80 lines
2.0 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
const parseArgs = require('minimist')
|
|
const consola = require('consola')
|
|
const { Nuxt, Builder, Generator } = require('..')
|
|
const { loadNuxtConfig } = require('./common/utils')
|
|
|
|
const argv = parseArgs(process.argv.slice(2), {
|
|
alias: {
|
|
h: 'help',
|
|
c: 'config-file',
|
|
a: 'analyze',
|
|
s: 'spa',
|
|
u: 'universal'
|
|
},
|
|
boolean: ['h', 'a', 's', 'u'],
|
|
string: ['c'],
|
|
default: {
|
|
c: 'nuxt.config.js'
|
|
}
|
|
})
|
|
|
|
if (argv.help) {
|
|
process.stderr.write(`
|
|
Description
|
|
Compiles the application for production deployment
|
|
Usage
|
|
$ nuxt build <dir>
|
|
Options
|
|
--analyze, -a Launch webpack-bundle-analyzer to optimize your bundles.
|
|
--spa Launch in SPA mode
|
|
--universal Launch in Universal mode (default)
|
|
--no-generate Don't generate static version for SPA mode (useful for nuxt start)
|
|
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
|
|
--help, -h Displays this message
|
|
`)
|
|
process.exit(0)
|
|
}
|
|
|
|
const options = loadNuxtConfig(argv)
|
|
|
|
// Create production build when calling `nuxt build`
|
|
options.dev = false
|
|
|
|
// Analyze option
|
|
options.build = options.build || {}
|
|
if (argv.analyze && typeof options.build.analyze !== 'object') {
|
|
options.build.analyze = true
|
|
}
|
|
|
|
const nuxt = new Nuxt(options)
|
|
const builder = new Builder(nuxt)
|
|
|
|
// Setup hooks
|
|
nuxt.hook('error', (_err) => consola.fatal(_err))
|
|
|
|
// Close function
|
|
const close = () => {
|
|
// In analyze mode wait for plugin
|
|
// emitting assets and opening browser
|
|
if (options.build.analyze === true || typeof options.build.analyze === 'object') {
|
|
return
|
|
}
|
|
|
|
process.exit(0)
|
|
}
|
|
|
|
if (options.mode !== 'spa' || argv.generate === false) {
|
|
// Build only
|
|
builder
|
|
.build()
|
|
.then(() => close())
|
|
.catch(error => consola.fatal(error))
|
|
} else {
|
|
// Build + Generate for static deployment
|
|
new Generator(nuxt, builder)
|
|
.generate({ build: true })
|
|
.then(() => close())
|
|
.catch(error => consola.fatal(error))
|
|
}
|