Nuxt/bin/nuxt-build

126 lines
3.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
2017-10-31 13:16:40 +00:00
/* eslint-disable no-console */
2016-11-10 14:24:22 +00:00
// Show logs
2017-07-06 13:02:48 +00:00
process.env.DEBUG = process.env.DEBUG || 'nuxt:*'
2016-11-10 14:24:22 +00:00
2017-06-16 12:42:45 +00:00
const fs = require('fs')
const parseArgs = require('minimist')
2018-01-11 16:11:50 +00:00
const { Nuxt, Builder, Generator, Utils } = require('../')
2017-06-16 12:42:45 +00:00
const resolve = require('path').resolve
const debug = require('debug')('nuxt:build')
debug.color = 2 // Force green color
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)
--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-06-16 12:42:45 +00:00
const rootDir = resolve(argv._[0] || '.')
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
2016-12-07 18:01:46 +00:00
2016-12-09 18:40:59 +00:00
var options = {}
2017-06-11 13:48:20 +00:00
if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile)
} else if (argv['config-file'] !== 'nuxt.config.js') {
2018-01-11 16:11:50 +00:00
Utils.fatalError(`Could not load config file`, argv['config-file'])
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}
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-08-18 10:54:35 +00:00
// Nuxt Mode
2018-01-11 16:11:50 +00:00
options.mode =
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
2017-08-18 10:54:35 +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))
if (options.mode !== 'spa') {
// Build for SSR app
2018-01-11 16:11:50 +00:00
builder
.build()
.then(() => debug('Building done'))
2018-01-11 16:11:50 +00:00
.catch(err => {
2017-10-31 13:16:40 +00:00
console.error(err)
process.exit(1)
})
} else {
const s = Date.now()
2017-11-24 02:35:03 +00:00
nuxt.hook('generate:distRemoved', function () {
debug('Destination folder cleaned')
})
2017-11-24 02:35:03 +00:00
nuxt.hook('generate:distCopied', function () {
debug('Static & build files copied')
})
2017-11-24 02:35:03 +00:00
nuxt.hook('generate:page', function (page) {
debug('Generate file: ' + page.path)
})
2017-11-24 02:35:03 +00:00
nuxt.hook('generate:done', function (generator, errors) {
const duration = Math.round((Date.now() - s) / 100) / 10
debug(`HTML Files generated in ${duration}s`)
if (errors.length) {
const report = errors.map(({ type, route, error }) => {
/* istanbul ignore if */
if (type === 'unhandled') {
return `Route: '${route}'\n${error.stack}`
} else {
return `Route: '${route}' thrown an error: \n` + JSON.stringify(error)
}
})
2018-01-11 16:33:30 +00:00
Utils.printError(report.join('\n\n'), 'Generate errors summary:')
}
})
// Disable minify to get exact results of nuxt start
2017-08-18 13:46:00 +00:00
nuxt.options.generate.minify = false
// Generate on spa mode
new Generator(nuxt, builder).generate({ build: true }).then(() => {
2018-01-11 16:11:50 +00:00
process.exit(0)
2017-06-16 12:42:45 +00:00
})
}