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
|
|
|
|
2018-02-07 00:26:40 +00:00
|
|
|
const defaultsDeep = require('lodash/defaultsDeep')
|
2017-06-16 12:42:45 +00:00
|
|
|
const debug = require('debug')('nuxt:build')
|
|
|
|
const parseArgs = require('minimist')
|
|
|
|
const chokidar = require('chokidar')
|
2018-02-07 00:26:40 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
const { version } = require('../package.json')
|
2018-02-07 00:26:40 +00:00
|
|
|
const { Nuxt, Builder, Utils } = require('..')
|
2018-03-16 16:12:06 +00:00
|
|
|
|
|
|
|
const { loadNuxtConfig, getLatestHost, nuxtConfigFile } = require('./common/utils')
|
|
|
|
|
|
|
|
debug.color = 2 // force green color
|
2017-04-19 20:01:26 +00:00
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
const argv = parseArgs(process.argv.slice(2), {
|
2017-06-11 13:48:20 +00:00
|
|
|
alias: {
|
|
|
|
h: 'help',
|
|
|
|
H: 'hostname',
|
|
|
|
p: 'port',
|
2017-08-18 10:54:35 +00:00
|
|
|
c: 'config-file',
|
2017-08-19 13:22:53 +00:00
|
|
|
s: 'spa',
|
2017-10-13 03:48:20 +00:00
|
|
|
u: 'universal',
|
|
|
|
v: 'version'
|
2017-06-11 13:48:20 +00:00
|
|
|
},
|
2017-10-13 03:48:20 +00:00
|
|
|
boolean: ['h', 's', 'u', 'v'],
|
2017-08-19 13:22:53 +00:00
|
|
|
string: ['H', 'c'],
|
2017-06-11 13:48:20 +00:00
|
|
|
default: {
|
|
|
|
c: 'nuxt.config.js'
|
|
|
|
}
|
|
|
|
})
|
2017-04-19 20:01:26 +00:00
|
|
|
|
2017-10-30 14:48:19 +00:00
|
|
|
if (argv.version) {
|
2018-02-07 00:26:40 +00:00
|
|
|
console.log(version)
|
2017-10-13 03:48:20 +00:00
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
2017-06-11 13:48:20 +00:00
|
|
|
if (argv.hostname === '') {
|
2018-01-11 16:11:50 +00:00
|
|
|
Utils.fatalError('Provided hostname argument has no value')
|
2017-04-19 20:01:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 13:48:20 +00:00
|
|
|
if (argv.help) {
|
|
|
|
console.log(`
|
|
|
|
Description
|
|
|
|
Starts the application in development mode (hot-code reloading, error
|
|
|
|
reporting, etc)
|
|
|
|
Usage
|
|
|
|
$ nuxt dev <dir> -p <port number> -H <hostname>
|
|
|
|
Options
|
2017-08-19 13:22:53 +00:00
|
|
|
--port, -p A port number on which to start the application
|
|
|
|
--hostname, -H Hostname on which to start the application
|
|
|
|
--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-04-19 20:01:26 +00:00
|
|
|
}
|
2016-11-09 22:59:41 +00:00
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
// Load config once for chokidar
|
2018-02-07 00:26:40 +00:00
|
|
|
const nuxtConfig = loadNuxtConfig(argv)
|
|
|
|
defaultsDeep(nuxtConfig, { watchers: { chokidar: { ignoreInitial: true } } })
|
2016-11-10 01:19:47 +00:00
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
// Start dev
|
|
|
|
let dev = startDev()
|
2017-11-21 21:33:37 +00:00
|
|
|
let needToRestart = false
|
2017-06-18 15:44:01 +00:00
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
// Start watching for nuxt.config.js changes
|
2018-02-07 00:26:40 +00:00
|
|
|
chokidar
|
|
|
|
.watch(nuxtConfigFile(argv), nuxtConfig.watchers.chokidar)
|
|
|
|
.on('all', () => {
|
|
|
|
debug('[nuxt.config.js] changed')
|
|
|
|
needToRestart = true
|
|
|
|
|
|
|
|
dev = dev.then(instance => {
|
|
|
|
if (needToRestart === false) return instance
|
|
|
|
needToRestart = false
|
|
|
|
|
|
|
|
debug('Rebuilding the app...')
|
|
|
|
return startDev(instance)
|
|
|
|
})
|
2017-11-21 21:33:37 +00:00
|
|
|
})
|
2017-02-08 13:09:59 +00:00
|
|
|
|
2017-11-22 12:57:39 +00:00
|
|
|
function startDev(oldInstance) {
|
2017-06-20 11:44:47 +00:00
|
|
|
// Get latest environment variables
|
2018-02-07 00:26:40 +00:00
|
|
|
const { port, host } = getLatestHost(argv)
|
2016-11-16 17:41:09 +00:00
|
|
|
|
2017-11-21 13:03:44 +00:00
|
|
|
// Error handler
|
2017-11-22 12:57:39 +00:00
|
|
|
const onError = (err, instance) => {
|
2018-01-11 16:11:50 +00:00
|
|
|
Utils.printError(err)
|
2017-11-22 12:57:39 +00:00
|
|
|
return Promise.resolve(instance) // Wait for next reload
|
2017-11-21 13:03:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
// Load options
|
|
|
|
let options = {}
|
|
|
|
try {
|
2018-02-07 00:26:40 +00:00
|
|
|
options = loadAndAugmentNuxtConfig()
|
2017-06-20 11:44:47 +00:00
|
|
|
} catch (err) {
|
2017-11-22 12:57:39 +00:00
|
|
|
return onError(err, oldInstance)
|
2017-06-20 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create nuxt and builder instance
|
2017-11-21 13:03:44 +00:00
|
|
|
let nuxt
|
|
|
|
let builder
|
2017-11-22 12:57:39 +00:00
|
|
|
let instance
|
2017-11-21 13:03:44 +00:00
|
|
|
try {
|
|
|
|
nuxt = new Nuxt(options)
|
|
|
|
builder = new Builder(nuxt)
|
2018-02-07 00:26:40 +00:00
|
|
|
instance = { nuxt, builder }
|
2017-11-21 13:03:44 +00:00
|
|
|
} catch (err) {
|
2018-02-07 00:26:40 +00:00
|
|
|
return onError(err, oldInstance)
|
2017-11-21 13:03:44 +00:00
|
|
|
}
|
2017-06-20 11:44:47 +00:00
|
|
|
|
2018-01-11 16:11:50 +00:00
|
|
|
return (
|
|
|
|
Promise.resolve()
|
|
|
|
.then(
|
|
|
|
() =>
|
|
|
|
oldInstance && oldInstance.builder
|
|
|
|
? oldInstance.builder.unwatch()
|
|
|
|
: Promise.resolve()
|
|
|
|
)
|
|
|
|
// Start build
|
|
|
|
.then(() => builder.build())
|
|
|
|
// Close old nuxt after successful build
|
|
|
|
.then(
|
|
|
|
() =>
|
|
|
|
oldInstance && oldInstance.nuxt
|
|
|
|
? oldInstance.nuxt.close()
|
|
|
|
: Promise.resolve()
|
|
|
|
)
|
|
|
|
// Start listening
|
|
|
|
.then(() => nuxt.listen(port, host))
|
|
|
|
// Pass new nuxt to watch chain
|
|
|
|
.then(() => instance)
|
|
|
|
// Handle errors
|
|
|
|
.catch(err => onError(err, instance))
|
|
|
|
)
|
2016-11-16 17:41:09 +00:00
|
|
|
}
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2018-02-07 00:26:40 +00:00
|
|
|
function loadAndAugmentNuxtConfig() {
|
|
|
|
const options = loadNuxtConfig(argv)
|
2017-06-20 11:44:47 +00:00
|
|
|
|
|
|
|
// Force development mode for add hot reloading and watching changes
|
|
|
|
options.dev = true
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|