Nuxt/bin/nuxt-build
2017-08-18 17:36:46 +04:30

92 lines
2.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
// 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',
m: 'mode'
},
boolean: ['h', 'a'],
string: ['c', 'm'],
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.
--mode, -m [spa|universal|ssr-only] Nuxt Mode (default: universal)
--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
if (argv['mode']) {
options.mode = argv['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)
builder.build()
.then(() => {
debug('Building done')
if (options.mode === 'spa') {
// Disable minify to get exact results of nuxt start
nuxt.options.generate.minify = false
// Generate on SPA mode
return new Generator(nuxt, builder).generate({ build: false, minify: false }).then(() => {
if (!nuxt.options.dev) {
// eslint-disable-next-line no-console
console.log(`✓ You can now directly upload ${nuxt.options.generate.dir}/ or start server using "nuxt start"`)
}
})
}
})
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})