2021-05-20 11:42:41 +00:00
|
|
|
import chokidar from 'chokidar'
|
2021-04-02 11:47:01 +00:00
|
|
|
import { Nuxt } from '@nuxt/kit'
|
2021-10-02 16:01:17 +00:00
|
|
|
import fse from 'fs-extra'
|
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) {
|
|
|
|
// Clear buildDir once
|
2021-10-02 16:01:17 +00:00
|
|
|
await fse.emptyDir(nuxt.options.buildDir)
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-05-20 11:42:41 +00:00
|
|
|
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)) {
|
|
|
|
app.main = null
|
|
|
|
}
|
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-05-20 11:42:41 +00:00
|
|
|
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) {
|
2021-10-05 14:18:03 +00:00
|
|
|
const useVite = nuxt.options.vite !== false
|
2021-04-03 13:38:07 +00:00
|
|
|
const { bundle } = await (useVite ? import('@nuxt/vite-builder') : import('@nuxt/webpack-builder'))
|
2021-01-22 22:02:33 +00:00
|
|
|
return bundle(nuxt)
|
2020-08-17 15:25:06 +00:00
|
|
|
}
|