2021-09-27 12:49:36 +00:00
|
|
|
import { resolve, relative } from 'pathe'
|
2021-04-15 19:17:44 +00:00
|
|
|
import chokidar from 'chokidar'
|
|
|
|
import debounce from 'debounce-promise'
|
2021-07-26 14:04:35 +00:00
|
|
|
import type { Nuxt } from '@nuxt/kit'
|
2021-10-07 10:15:15 +00:00
|
|
|
import consola from 'consola'
|
2021-04-15 19:17:44 +00:00
|
|
|
import { createServer, createLoadingHandler } from '../utils/server'
|
|
|
|
import { showBanner } from '../utils/banner'
|
2021-10-02 16:01:17 +00:00
|
|
|
import { importModule } from '../utils/cjs'
|
2021-10-07 13:53:31 +00:00
|
|
|
import { writeTypes } from '../utils/prepare'
|
2021-07-26 14:04:35 +00:00
|
|
|
import { defineNuxtCommand } from './index'
|
2021-04-09 15:52:45 +00:00
|
|
|
|
2021-07-26 14:04:35 +00:00
|
|
|
export default defineNuxtCommand({
|
|
|
|
meta: {
|
|
|
|
name: 'dev',
|
2021-08-10 17:37:03 +00:00
|
|
|
usage: 'npx nuxi dev [rootDir] [--clipboard] [--open, -o]',
|
2021-07-26 14:04:35 +00:00
|
|
|
description: 'Run nuxt development server'
|
|
|
|
},
|
|
|
|
async invoke (args) {
|
|
|
|
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
|
|
|
|
const server = createServer()
|
|
|
|
const listener = await server.listen({
|
|
|
|
clipboard: args.clipboard,
|
2021-10-13 10:33:21 +00:00
|
|
|
open: args.open || args.o,
|
|
|
|
port: args.port || args.p
|
2021-07-26 14:04:35 +00:00
|
|
|
})
|
2021-04-09 15:52:45 +00:00
|
|
|
|
2021-07-26 14:04:35 +00:00
|
|
|
const rootDir = resolve(args._[0] || '.')
|
2021-04-15 19:17:44 +00:00
|
|
|
|
2021-10-02 16:01:17 +00:00
|
|
|
const { loadNuxt, buildNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit')
|
2021-04-15 19:17:44 +00:00
|
|
|
|
2021-10-07 13:53:31 +00:00
|
|
|
const prepare = debounce(nuxt => writeTypes(nuxt), 1000)
|
|
|
|
|
2021-07-26 14:04:35 +00:00
|
|
|
let currentNuxt: Nuxt
|
2021-08-10 16:53:46 +00:00
|
|
|
const load = async (isRestart: boolean, reason?: string) => {
|
2021-07-26 14:04:35 +00:00
|
|
|
try {
|
2021-08-10 16:53:46 +00:00
|
|
|
const message = `${reason ? reason + '. ' : ''}${isRestart ? 'Restarting' : 'Starting'} nuxt...`
|
2021-07-26 14:04:35 +00:00
|
|
|
server.setApp(createLoadingHandler(message))
|
|
|
|
if (isRestart) {
|
2021-10-12 12:32:49 +00:00
|
|
|
consola.info(message)
|
2021-07-26 14:04:35 +00:00
|
|
|
}
|
|
|
|
if (currentNuxt) {
|
|
|
|
await currentNuxt.close()
|
|
|
|
}
|
|
|
|
const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false })
|
2021-10-07 13:53:31 +00:00
|
|
|
prepare(newNuxt)
|
2021-07-26 14:04:35 +00:00
|
|
|
currentNuxt = newNuxt
|
|
|
|
await currentNuxt.ready()
|
|
|
|
await buildNuxt(currentNuxt)
|
|
|
|
server.setApp(currentNuxt.server.app)
|
|
|
|
if (isRestart && args.clear !== false) {
|
|
|
|
showBanner()
|
|
|
|
listener.showURL()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-10-07 10:15:15 +00:00
|
|
|
consola.error(`Cannot ${isRestart ? 'restart' : 'start'} nuxt: `, err)
|
2021-07-26 14:04:35 +00:00
|
|
|
server.setApp(createLoadingHandler(
|
|
|
|
'Error while loading nuxt. Please check console and fix errors.'
|
|
|
|
))
|
2021-04-15 19:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 14:04:35 +00:00
|
|
|
// Watch for config changes
|
|
|
|
// TODO: Watcher service, modules, and requireTree
|
|
|
|
const dLoad = debounce(load, 250)
|
|
|
|
const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 1 })
|
|
|
|
watcher.on('all', (_event, file) => {
|
2021-10-02 11:44:51 +00:00
|
|
|
if (file.startsWith(currentNuxt.options.buildDir)) { return }
|
|
|
|
if (file.match(/nuxt\.config\.(js|ts|mjs|cjs)$/)) {
|
2021-08-10 16:53:46 +00:00
|
|
|
dLoad(true, `${relative(rootDir, file)} updated`)
|
2021-07-26 14:04:35 +00:00
|
|
|
}
|
2021-09-29 18:09:43 +00:00
|
|
|
if (['addDir', 'unlinkDir'].includes(_event) && file.match(/pages$/)) {
|
2021-10-12 12:32:49 +00:00
|
|
|
dLoad(true, `Directory \`pages/\` ${_event === 'addDir' ? 'created' : 'removed'}`)
|
|
|
|
}
|
|
|
|
if (['addDir', 'unlinkDir'].includes(_event) && file.match(/components$/)) {
|
|
|
|
dLoad(true, `Directory \`components/\` ${_event === 'addDir' ? 'created' : 'removed'}`)
|
2021-09-29 18:09:43 +00:00
|
|
|
}
|
|
|
|
if (['add', 'unlink'].includes(_event) && file.match(/app\.(js|ts|mjs|jsx|tsx|vue)$/)) {
|
2021-10-12 12:32:49 +00:00
|
|
|
dLoad(true, `\`${relative(rootDir, file)}\` ${_event === 'add' ? 'created' : 'removed'}`)
|
2021-09-29 18:09:43 +00:00
|
|
|
}
|
2021-07-26 14:04:35 +00:00
|
|
|
})
|
2021-04-09 15:52:45 +00:00
|
|
|
|
2021-07-26 14:04:35 +00:00
|
|
|
await load(false)
|
|
|
|
}
|
|
|
|
})
|