#!/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 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 === '') { consola.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 -p -H 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', () => { consola.debug('[nuxt.config.js] changed') needToRestart = true dev = dev.then(instance => { if (needToRestart === false) return instance needToRestart = false consola.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) => { consola.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(() => { if (oldInstance && oldInstance.builder) { return oldInstance.builder.unwatch() } else { return nuxt.listen(port, host) } }) // Start build .then(builder.build) // Close old nuxt after successful build .then( () => oldInstance && oldInstance.nuxt ? oldInstance.nuxt.close() : Promise.resolve() ) // Start listening .then(() => { if (oldInstance) { return nuxt.listen(port, host) } else { return Promise.resolve() } }) // 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 }