2021-05-20 11:42:41 +00:00
|
|
|
import chokidar from 'chokidar'
|
2021-11-21 16:14:46 +00:00
|
|
|
import type { Nuxt } from '@nuxt/schema'
|
2022-02-25 19:11:01 +00:00
|
|
|
import { tryImportModule } from '@nuxt/kit'
|
2021-05-20 11:42:41 +00:00
|
|
|
import { createApp, generateApp } from './app'
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-05-20 11:42:41 +00:00
|
|
|
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) {
|
2021-05-20 11:42:41 +00:00
|
|
|
watch(nuxt)
|
|
|
|
nuxt.hook('builder:watch', async (event, path) => {
|
|
|
|
if (event !== 'change' && /app|plugins/i.test(path)) {
|
2021-07-23 14:42:49 +00:00
|
|
|
if (path.match(/app/i)) {
|
2021-10-12 12:51:41 +00:00
|
|
|
app.mainComponent = null
|
2021-07-23 14:42:49 +00:00
|
|
|
}
|
2021-05-20 11:42:41 +00:00
|
|
|
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
|
|
|
|
2021-10-18 08:50:46 +00:00
|
|
|
await nuxt.callHook('build:before', { nuxt }, nuxt.options.build)
|
2022-01-24 13:28:47 +00:00
|
|
|
if (!nuxt.options._prepare) {
|
|
|
|
await bundle(nuxt)
|
|
|
|
await nuxt.callHook('build:done', { nuxt })
|
|
|
|
}
|
2021-07-23 14:45:06 +00:00
|
|
|
|
|
|
|
if (!nuxt.options.dev) {
|
|
|
|
await nuxt.callHook('close', nuxt)
|
|
|
|
}
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 11:42:41 +00:00
|
|
|
function watch (nuxt: Nuxt) {
|
|
|
|
const watcher = chokidar.watch(nuxt.options.srcDir, {
|
|
|
|
...nuxt.options.watchers.chokidar,
|
|
|
|
cwd: nuxt.options.srcDir,
|
|
|
|
ignoreInitial: true,
|
|
|
|
ignored: [
|
|
|
|
'.nuxt',
|
|
|
|
'.output',
|
|
|
|
'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
|
|
|
}
|
|
|
|
|
2021-05-20 11:42:41 +00:00
|
|
|
async function bundle (nuxt: Nuxt) {
|
2022-02-25 19:11:01 +00:00
|
|
|
const { bundle } = typeof nuxt.options.builder === 'string'
|
|
|
|
? await tryImportModule(nuxt.options.builder, { paths: nuxt.options.rootDir })
|
|
|
|
: nuxt.options.builder
|
2021-10-20 18:50:01 +00:00
|
|
|
try {
|
|
|
|
return bundle(nuxt)
|
|
|
|
} catch (error) {
|
|
|
|
await nuxt.callHook('build:error', error)
|
|
|
|
throw error
|
|
|
|
}
|
2020-08-17 15:25:06 +00:00
|
|
|
}
|