mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-18 02:07:14 +00:00
124 lines
3.3 KiB
JavaScript
Executable File
124 lines
3.3 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
/* eslint-disable no-console */
|
|
|
|
// Show logs
|
|
process.env.DEBUG = process.env.DEBUG || 'nuxt:*'
|
|
|
|
const fs = require('fs')
|
|
const parseArgs = require('minimist')
|
|
const { Nuxt, Builder, Generator } = require('../')
|
|
const resolve = require('path').resolve
|
|
const debug = require('debug')('nuxt:build')
|
|
debug.color = 2 // Force green color
|
|
|
|
const argv = parseArgs(process.argv.slice(2), {
|
|
alias: {
|
|
h: 'help',
|
|
c: 'config-file',
|
|
a: 'analyze',
|
|
s: 'spa',
|
|
u: 'universal'
|
|
},
|
|
boolean: ['h', 'a', 's', 'u'],
|
|
string: ['c'],
|
|
default: {
|
|
c: 'nuxt.config.js'
|
|
}
|
|
})
|
|
|
|
if (argv.help) {
|
|
console.log(`
|
|
Description
|
|
Compiles the application for production deployment
|
|
Usage
|
|
$ nuxt build <dir>
|
|
Options
|
|
--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
|
|
`)
|
|
process.exit(0)
|
|
}
|
|
|
|
const rootDir = resolve(argv._[0] || '.')
|
|
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
|
|
|
var options = {}
|
|
if (fs.existsSync(nuxtConfigFile)) {
|
|
options = require(nuxtConfigFile)
|
|
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
|
console.error(`> Could not load config file ${argv['config-file']}`)
|
|
process.exit(1)
|
|
}
|
|
if (typeof options.rootDir !== 'string') {
|
|
options.rootDir = rootDir
|
|
}
|
|
// Create production build when calling `nuxt build`
|
|
options.dev = false
|
|
|
|
// Nuxt Mode
|
|
options.mode = (argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
|
|
|
|
// Analyze option
|
|
options.build = options.build || {}
|
|
if (argv.analyze) {
|
|
options.build.analyze = true
|
|
}
|
|
|
|
debug('Building...')
|
|
const nuxt = new Nuxt(options)
|
|
const builder = new Builder(nuxt)
|
|
|
|
if (options.mode !== 'spa') {
|
|
// Build for SSR app
|
|
builder.build()
|
|
.then(() => debug('Building done'))
|
|
.catch((err) => {
|
|
console.error(err)
|
|
process.exit(1)
|
|
})
|
|
} else {
|
|
const s = Date.now()
|
|
|
|
nuxt.hook('generate:distRemoved', function () {
|
|
debug('Destination folder cleaned')
|
|
})
|
|
|
|
nuxt.hook('generate:distCopied', function () {
|
|
debug('Static & build files copied')
|
|
})
|
|
|
|
nuxt.hook('generate:page', function (page) {
|
|
debug('Generate file: ' + page.path)
|
|
})
|
|
|
|
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)
|
|
}
|
|
})
|
|
console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
|
|
}
|
|
})
|
|
|
|
// Disable minify to get exact results of nuxt start
|
|
nuxt.options.generate.minify = false
|
|
// Generate on spa mode
|
|
new Generator(nuxt, builder).generate({ build: true }).then(() => {
|
|
if (!nuxt.options.dev) {
|
|
console.log(`✓ You can now directly upload ${nuxt.options.generate.dir}/ or start server using "nuxt start"`)
|
|
}
|
|
})
|
|
}
|