Nuxt/bin/nuxt-dev
2018-03-31 20:52:14 +04:30

144 lines
3.5 KiB
JavaScript
Executable File

#!/usr/bin/env node
const defaultsDeep = require('lodash/defaultsDeep')
const parseArgs = require('minimist')
const chokidar = require('chokidar')
const consola = require('consola')
const { version } = require('../package.json')
const { Nuxt, Builder } = require('..')
const { loadNuxtConfig, getLatestHost, nuxtConfigFile } = require('./common/utils')
const logger = consola.withScope('nuxt:dev')
const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
H: 'hostname',
p: 'port',
c: 'config-file',
s: 'spa',
u: 'universal',
v: 'version'
},
boolean: ['h', 's', 'u', 'v'],
string: ['H', 'c'],
default: {
c: 'nuxt.config.js'
}
})
if (argv.version) {
process.stderr.write(version + '\n')
process.exit(0)
}
if (argv.hostname === '') {
logger.fatal('Provided hostname argument has no value')
}
if (argv.help) {
process.stderr.write(`
Description
Starts the application in development mode (hot-code reloading, error
reporting, etc)
Usage
$ nuxt dev <dir> -p <port number> -H <hostname>
Options
--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
`)
process.exit(0)
}
// Load config once for chokidar
const nuxtConfig = loadNuxtConfig(argv)
defaultsDeep(nuxtConfig, { watchers: { chokidar: { ignoreInitial: true } } })
// Start dev
let dev = startDev()
let needToRestart = false
// Start watching for nuxt.config.js changes
chokidar
.watch(nuxtConfigFile(argv), nuxtConfig.watchers.chokidar)
.on('all', () => {
logger.debug('[nuxt.config.js] changed')
needToRestart = true
dev = dev.then(instance => {
if (needToRestart === false) return instance
needToRestart = false
logger.debug('Rebuilding the app...')
return startDev(instance)
})
})
function startDev(oldInstance) {
// Get latest environment variables
const { port, host } = getLatestHost(argv)
// Error handler
const onError = (err, instance) => {
logger.error(err)
return Promise.resolve(instance) // Wait for next reload
}
// Load options
let options = {}
try {
options = loadAndAugmentNuxtConfig()
} catch (err) {
return onError(err, oldInstance)
}
// Create nuxt and builder instance
let nuxt
let builder
let instance
try {
nuxt = new Nuxt(options)
builder = new Builder(nuxt)
instance = { nuxt, builder }
} catch (err) {
return onError(err, oldInstance)
}
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))
)
}
function loadAndAugmentNuxtConfig() {
const options = loadNuxtConfig(argv)
// Force development mode for add hot reloading and watching changes
options.dev = true
return options
}