Nuxt/packages/nuxt3/src/core/builder.ts

65 lines
1.7 KiB
TypeScript
Raw Normal View History

import chokidar from 'chokidar'
import type { Nuxt } from '@nuxt/schema'
import { isIgnored, tryImportModule } from '@nuxt/kit'
import { createApp, generateApp } from './app'
export async function build (nuxt: Nuxt) {
const app = createApp(nuxt)
await generateApp(nuxt, app)
2020-08-17 18:02:10 +00:00
if (nuxt.options.dev) {
watch(nuxt)
nuxt.hook('builder:watch', async (event, path) => {
if (event !== 'change' && /app|error|plugins/i.test(path)) {
if (path.match(/app/i)) {
app.mainComponent = null
}
if (path.match(/error/i)) {
app.errorComponent = null
}
await generateApp(nuxt, app)
}
})
nuxt.hook('builder:generateApp', () => generateApp(nuxt, app))
2020-08-17 18:02:10 +00:00
}
2020-07-02 13:02:35 +00:00
await nuxt.callHook('build:before', { nuxt }, nuxt.options.build)
if (!nuxt.options._prepare) {
await bundle(nuxt)
await nuxt.callHook('build:done', { nuxt })
}
if (!nuxt.options.dev) {
await nuxt.callHook('close', nuxt)
}
2020-07-02 13:02:35 +00:00
}
function watch (nuxt: Nuxt) {
const watcher = chokidar.watch(nuxt.options.srcDir, {
...nuxt.options.watchers.chokidar,
cwd: nuxt.options.srcDir,
ignoreInitial: true,
ignored: [
isIgnored,
'.nuxt',
'node_modules'
]
})
const watchHook = (event, path) => nuxt.callHook('builder:watch', event, path)
watcher.on('all', watchHook)
nuxt.hook('close', () => watcher.close())
return watcher
2020-08-17 18:02:10 +00:00
}
async function bundle (nuxt: Nuxt) {
const { bundle } = typeof nuxt.options.builder === 'string'
? await tryImportModule(nuxt.options.builder, { paths: nuxt.options.rootDir })
: nuxt.options.builder
try {
return bundle(nuxt)
} catch (error) {
await nuxt.callHook('build:error', error)
throw error
}
}