2021-04-15 19:17:44 +00:00
|
|
|
import { resolve } from 'path'
|
|
|
|
import chokidar from 'chokidar'
|
|
|
|
import debounce from 'debounce-promise'
|
|
|
|
import { createServer, createLoadingHandler } from '../utils/server'
|
|
|
|
import { showBanner } from '../utils/banner'
|
|
|
|
import { requireModule } from '../utils/cjs'
|
|
|
|
import { error, info } from '../utils/log'
|
|
|
|
import { diff, printDiff } from '../utils/diff'
|
2021-04-09 15:52:45 +00:00
|
|
|
|
|
|
|
export async function invoke (args) {
|
2021-04-19 20:29:39 +00:00
|
|
|
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
|
2021-04-09 15:52:45 +00:00
|
|
|
const server = createServer()
|
2021-04-15 19:17:44 +00:00
|
|
|
const listener = await server.listen({ clipboard: true, open: true })
|
2021-04-09 15:52:45 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
const rootDir = resolve(args._[0] || '.')
|
|
|
|
|
|
|
|
const { loadNuxt, buildNuxt } = requireModule('@nuxt/kit', rootDir)
|
|
|
|
|
|
|
|
let currentNuxt
|
|
|
|
const load = async () => {
|
|
|
|
try {
|
2021-04-18 16:39:07 +00:00
|
|
|
showBanner(true)
|
|
|
|
listener.showURL()
|
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false })
|
2021-04-09 15:52:45 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
let configChanges
|
|
|
|
if (currentNuxt) {
|
|
|
|
configChanges = diff(currentNuxt.options, newNuxt.options, [
|
|
|
|
'generate.staticAssets.version',
|
|
|
|
'env.NITRO_PRESET'
|
|
|
|
])
|
|
|
|
server.setApp(createLoadingHandler('Restarting...', 1))
|
|
|
|
await currentNuxt.close()
|
|
|
|
currentNuxt = newNuxt
|
|
|
|
} else {
|
|
|
|
currentNuxt = newNuxt
|
|
|
|
}
|
2021-04-09 15:52:45 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
if (configChanges) {
|
|
|
|
if (configChanges.length) {
|
|
|
|
info('Nuxt config updated:')
|
|
|
|
printDiff(configChanges)
|
|
|
|
} else {
|
|
|
|
info('Restarted nuxt due to config changes')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await currentNuxt.ready()
|
|
|
|
await buildNuxt(currentNuxt)
|
|
|
|
server.setApp(currentNuxt.server.app)
|
|
|
|
} catch (err) {
|
|
|
|
error('Cannot load nuxt.', err)
|
|
|
|
server.setApp(createLoadingHandler(
|
|
|
|
'Error while loading nuxt. Please check console and fix errors.'
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:07 +00:00
|
|
|
// Watch for config changes
|
|
|
|
// TODO: Watcher service, modules, and requireTree
|
2021-04-15 19:17:44 +00:00
|
|
|
const dLoad = debounce(load, 250)
|
2021-04-18 16:39:07 +00:00
|
|
|
const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 1 })
|
2021-04-15 19:17:44 +00:00
|
|
|
watcher.on('all', (_event, file) => {
|
2021-04-18 16:39:07 +00:00
|
|
|
if (file.includes('nuxt.config') || file.includes('modules')) {
|
2021-04-15 19:17:44 +00:00
|
|
|
dLoad()
|
|
|
|
}
|
|
|
|
})
|
2021-04-09 15:52:45 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
await load()
|
2021-04-09 15:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
usage: 'nu dev [rootDir]',
|
|
|
|
description: 'Run nuxt development server'
|
|
|
|
}
|