Nuxt/bin/nuxt-build

103 lines
2.5 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
2017-10-31 13:16:40 +00:00
/* eslint-disable no-console */
2017-06-16 12:42:45 +00:00
const parseArgs = require('minimist')
const debug = require('debug')('nuxt:build')
debug.color = 2 // Force green color
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-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)
--generate Generate static version after build
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 || {}
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...')
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))
// Close function
const close = () => {
// In analyze mode wait for plugin opening the browser
2018-03-27 07:10:50 +00:00
if (options.build.analyze === true && !(options.ci || options.test)) {
return
}
process.exit(0)
}
if (!argv.generate) {
// -- Build only --
2018-01-11 16:11:50 +00:00
builder
.build()
.then(() => close())
.catch(Utils.fatalError)
} else {
// -- Build and generate --
const s = Date.now()
nuxt.hook('generate:distRemoved', () => debug('Destination folder cleaned'))
nuxt.hook('generate:distCopied', () => debug('Static & build files copied'))
nuxt.hook('generate:page', page => debug('Generate file: ' + page.path))
nuxt.hook('generate:done', (generator, errors) => {
const duration = Math.round((Date.now() - s) / 100) / 10
debug(`HTML Files generated in ${duration}s`)
if (errors.length) {
/* eslint-disable no-console */
console.log('\n' + errors.toString())
}
})
// Generate dist for SPA static deployment
new Generator(nuxt, builder).generate({ build: true }).then(() => {
close()
2017-06-16 12:42:45 +00:00
})
}