mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-13 17:43:59 +00:00
a522aaf125
* misc: add user-defined chokidar files to watch * Improved user-defined chokidar files setup * Improved user-defined chokidar files setup (2) * Added custom file for CLI watch test * Added cli.dev.test.js * Added cli.dev.test.js (2) * Remove cli-start/cli.dev, just use cli.test * Refactored CLI test * Missing dot in fname * Improved user-defined chokidar files setup (3) * Fix killNuxt() * Remove genHandlers * Refactored CLI test (2) * Refactor exitCode * Remove debugging code * Remove debugging code (2) * Linting * Linting (2) * Disable nuxt-start test for now * Disable nuxt-start test for now (2) * Tweaking nuxt-start test * Tweaking nuxt-start test (2) * Tweaking nuxt-start test (3) * Fix ext * Tweaked wait params * Fix conflicts * hotfix * nuxt-dev tweak * Add blank line after variables declaration * Disable waitFor() test due to random failure in appveyor * Fixed error msg * Refactored into builder.js * Refactored into builder.js (2) * Remove unnecessary import from nuxt-dev * Fixed nuxt-dev test * Debugging nuxt-dev test * Debugging nuxt-dev test (2) * Skip in appveyor * Linting * Requested changes * Hook into nuxt-dev * Hook into nuxt-dev (2) * Get fname * Get fname (2) * Get fname (3) * Change hook name * Fixed conflict
142 lines
3.3 KiB
JavaScript
Executable File
142 lines
3.3 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
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 <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)
|
|
|
|
// Start dev
|
|
let hooked = false
|
|
let dev = startDev()
|
|
|
|
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)
|
|
}
|
|
|
|
if (!hooked) {
|
|
nuxt.hook('watch:fileChanged', (fname) => {
|
|
consola.debug(`[${fname}] changed`)
|
|
dev = dev.then((instance) => {
|
|
consola.debug('Rebuilding the app...')
|
|
return startDev(instance)
|
|
})
|
|
})
|
|
hooked = true
|
|
}
|
|
|
|
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
|
|
}
|