2016-11-09 22:59:41 +00:00
|
|
|
#!/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('..')
|
2018-02-07 00:26:40 +00:00
|
|
|
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-03-15 14:01:44 +00:00
|
|
|
|
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)
|
2018-04-15 06:41:31 +00:00
|
|
|
--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)
|
2017-03-15 14:01:44 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 00:26:40 +00:00
|
|
|
const options = loadNuxtConfig(argv)
|
2018-01-12 21:21:49 +00:00
|
|
|
|
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 || {}
|
2018-06-06 12:30:22 +00:00
|
|
|
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
|
|
|
|
2017-06-18 15:44:01 +00:00
|
|
|
const nuxt = new Nuxt(options)
|
|
|
|
const builder = new Builder(nuxt)
|
|
|
|
|
2018-01-11 16:11:50 +00:00
|
|
|
// Setup hooks
|
2018-07-20 15:42:31 +00:00
|
|
|
nuxt.hook('error', consola.fatal)
|
2018-01-11 16:11:50 +00:00
|
|
|
|
2018-01-14 10:34:45 +00:00
|
|
|
// Close function
|
|
|
|
const close = () => {
|
2018-03-27 07:13:29 +00:00
|
|
|
// In analyze mode wait for plugin
|
|
|
|
// emitting assets and opening browser
|
2018-06-06 12:30:22 +00:00
|
|
|
if (options.build.analyze === true || typeof options.build.analyze === 'object') {
|
2018-03-27 06:52:22 +00:00
|
|
|
return
|
2018-01-14 10:34:45 +00:00
|
|
|
}
|
2018-03-27 06:52:22 +00:00
|
|
|
|
2018-01-14 10:34:45 +00:00
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
2018-04-15 06:41:31 +00:00
|
|
|
if (options.mode !== 'spa' || argv.generate === false) {
|
|
|
|
// Build only
|
2018-01-11 16:11:50 +00:00
|
|
|
builder
|
|
|
|
.build()
|
2018-07-20 15:42:31 +00:00
|
|
|
.then(close)
|
|
|
|
.catch(consola.fatal)
|
2017-08-18 13:44:16 +00:00
|
|
|
} else {
|
2018-04-15 06:41:31 +00:00
|
|
|
// Build + Generate for static deployment
|
|
|
|
new Generator(nuxt, builder)
|
|
|
|
.generate({ build: true })
|
2018-07-20 15:42:31 +00:00
|
|
|
.then(close)
|
|
|
|
.catch(consola.fatal)
|
2017-08-18 13:44:16 +00:00
|
|
|
}
|