2016-11-09 22:59:41 +00:00
|
|
|
#!/usr/bin/env node
|
2017-10-31 13:16:40 +00:00
|
|
|
/* eslint-disable no-console */
|
2016-11-09 22:59:41 +00:00
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
const parseArgs = require('minimist')
|
|
|
|
const debug = require('debug')('nuxt:build')
|
|
|
|
debug.color = 2 // Force green color
|
2016-11-09 22:59:41 +00:00
|
|
|
|
2018-02-07 00:26:40 +00:00
|
|
|
const { Nuxt, Builder, Generator, Utils } = 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-03-15 14:01:44 +00:00
|
|
|
|
2017-06-11 13:48:20 +00:00
|
|
|
if (argv.help) {
|
|
|
|
console.log(`
|
|
|
|
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)
|
|
|
|
--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 || {}
|
2017-06-11 13:48:20 +00:00
|
|
|
if (argv.analyze) {
|
|
|
|
options.build.analyze = true
|
2017-01-24 19:25:56 +00:00
|
|
|
}
|
2017-01-23 16:55:39 +00:00
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
debug('Building...')
|
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
|
|
|
|
nuxt.hook('error', (_err, from) => Utils.fatalError(_err, from))
|
|
|
|
|
2018-01-14 10:34:45 +00:00
|
|
|
// Close function
|
|
|
|
const close = () => {
|
|
|
|
if (options.build.analyze === true) {
|
|
|
|
return // Wait for user exit
|
|
|
|
}
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
2017-08-18 13:44:16 +00:00
|
|
|
if (options.mode !== 'spa') {
|
2018-01-12 21:21:49 +00:00
|
|
|
// -- Build for SSR app --
|
2018-01-11 16:11:50 +00:00
|
|
|
builder
|
|
|
|
.build()
|
2018-03-11 23:37:38 +00:00
|
|
|
.then(() => console.error('Building done'))
|
2018-01-14 10:34:45 +00:00
|
|
|
.then(() => close())
|
2018-02-07 00:26:40 +00:00
|
|
|
.catch(Utils.fatalError)
|
2017-08-18 13:44:16 +00:00
|
|
|
} else {
|
2018-01-12 21:21:49 +00:00
|
|
|
// -- Build for SPA app --
|
2017-11-23 22:31:54 +00:00
|
|
|
const s = Date.now()
|
|
|
|
|
2018-01-12 21:21:49 +00:00
|
|
|
nuxt.hook('generate:distRemoved', () => debug('Destination folder cleaned'))
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2018-01-12 21:21:49 +00:00
|
|
|
nuxt.hook('generate:distCopied', () => debug('Static & build files copied'))
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2018-01-12 21:21:49 +00:00
|
|
|
nuxt.hook('generate:page', page => debug('Generate file: ' + page.path))
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2018-01-12 21:21:49 +00:00
|
|
|
nuxt.hook('generate:done', (generator, errors) => {
|
2017-11-23 22:31:54 +00:00
|
|
|
const duration = Math.round((Date.now() - s) / 100) / 10
|
|
|
|
|
|
|
|
debug(`HTML Files generated in ${duration}s`)
|
|
|
|
|
|
|
|
if (errors.length) {
|
2018-01-11 18:20:35 +00:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.log('\n' + errors.toString())
|
2017-11-23 22:31:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-08-18 13:44:16 +00:00
|
|
|
// Disable minify to get exact results of nuxt start
|
2017-08-18 13:46:00 +00:00
|
|
|
nuxt.options.generate.minify = false
|
2018-01-12 21:21:49 +00:00
|
|
|
|
|
|
|
// Generate dist for SPA static deployment
|
2017-08-18 13:44:16 +00:00
|
|
|
new Generator(nuxt, builder).generate({ build: true }).then(() => {
|
2018-01-14 10:34:45 +00:00
|
|
|
close()
|
2017-06-16 12:42:45 +00:00
|
|
|
})
|
2017-08-18 13:44:16 +00:00
|
|
|
}
|