Nuxt/bin/nuxt-build

87 lines
2.1 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
2017-06-16 12:42:45 +00:00
const parseArgs = require('minimist')
2018-03-31 16:22:14 +00:00
const consola = require('consola')
const { Nuxt, Builder, Generator } = require('..')
const { loadNuxtConfig } = require('./common/utils')
2017-06-11 13:48:20 +00:00
const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
c: 'config-file',
2017-08-18 10:54:35 +00:00
a: 'analyze',
2017-08-19 13:22:53 +00:00
s: 'spa',
u: 'universal',
q: 'quiet'
2017-06-11 13:48:20 +00:00
},
boolean: ['h', 'a', 's', 'u', 'q'],
2017-08-19 13:22:53 +00:00
string: ['c'],
2017-06-11 13:48:20 +00:00
default: {
c: 'nuxt.config.js'
}
})
2017-06-11 13:48:20 +00:00
if (argv.help) {
2018-03-31 16:22:14 +00:00
process.stderr.write(`
2017-06-11 13:48:20 +00:00
Description
Compiles the application for production deployment
Usage
$ nuxt build <dir>
Options
2017-08-19 13:22:53 +00:00
--analyze, -a Launch webpack-bundle-analyzer to optimize your bundles.
--spa, -s Launch in SPA mode
--universal, -u Launch in Universal mode (default)
--no-generate Don't generate static version for SPA mode (useful for nuxt start)
2017-08-19 13:22:53 +00:00
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
--quiet, -q Disable output except for errors
2017-08-19 13:22:53 +00:00
--help, -h Displays this message
2017-06-11 13:48:20 +00:00
`)
process.exit(0)
}
const options = loadNuxtConfig(argv)
2017-06-11 13:48:20 +00:00
// Create production build when calling `nuxt build`
options.dev = false
2017-06-14 18:51:14 +00:00
2017-06-11 13:48:20 +00:00
// Analyze option
2017-01-23 16:55:39 +00:00
options.build = options.build || {}
if (argv.analyze && typeof options.build.analyze !== 'object') {
2017-06-11 13:48:20 +00:00
options.build.analyze = true
2017-01-24 19:25:56 +00:00
}
2017-01-23 16:55:39 +00:00
// Silence output when using --quiet
if (argv.quiet) {
2018-08-12 12:39:43 +00:00
options.build.quiet = !!argv.quiet
}
const nuxt = new Nuxt(options)
const builder = new Builder(nuxt)
2018-01-11 16:11:50 +00:00
// Setup hooks
2018-07-25 15:57:43 +00:00
nuxt.hook('error', err => consola.fatal(err))
2018-01-11 16:11:50 +00:00
// 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
2018-01-11 16:11:50 +00:00
builder
.build()
.then(close)
2018-07-25 15:57:43 +00:00
.catch(err => consola.fatal(err))
} else {
// Build + Generate for static deployment
new Generator(nuxt, builder)
.generate({ build: true })
.then(close)
2018-07-25 15:57:43 +00:00
.catch(err => consola.fatal(err))
}