Nuxt/bin/nuxt-build

80 lines
2.0 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'
2017-06-11 13:48:20 +00:00
},
2017-08-19 13:22:53 +00:00
boolean: ['h', 'a', 's', 'u'],
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 Launch in SPA mode
--universal 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)
--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
const nuxt = new Nuxt(options)
const builder = new Builder(nuxt)
2018-01-11 16:11:50 +00:00
// Setup hooks
2018-04-01 20:20:46 +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-04-01 20:20:46 +00:00
.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))
}