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-03-18 12:57:05 +00:00
import { importModule , isIgnored } from '@nuxt/kit'
2022-03-16 11:11:30 +00:00
import { debounce } from 'perfect-debounce'
2022-04-06 16:02:56 +00:00
import { normalize } from 'pathe'
2022-03-15 10:56:16 +00:00
import { createApp , generateApp as _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 )
2022-03-16 11:11:30 +00:00
const generateApp = debounce ( ( ) = > _generateApp ( nuxt , app ) , undefined , { leading : true } )
2022-03-15 10:56:16 +00:00
await generateApp ( )
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 ) = > {
2022-06-10 14:50:47 +00:00
if ( event !== 'change' && /^(app\.|error\.|plugins\/|layouts\/)/i . test ( path ) ) {
if ( path . startsWith ( 'app' ) ) {
2021-10-12 12:51:41 +00:00
app . mainComponent = null
2021-07-23 14:42:49 +00:00
}
2022-06-10 14:50:47 +00:00
if ( path . startsWith ( 'error' ) ) {
2022-03-11 08:22:16 +00:00
app . errorComponent = null
}
2022-03-15 10:56:16 +00:00
await generateApp ( )
2021-05-20 11:42:41 +00:00
}
} )
2022-03-15 10:56:16 +00:00
nuxt . hook ( 'builder:generateApp' , generateApp )
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 ) {
2022-06-10 14:50:47 +00:00
const watcher = chokidar . watch ( nuxt . options . _layers . map ( i = > i . config . srcDir ) , {
2021-05-20 11:42:41 +00:00
. . . nuxt . options . watchers . chokidar ,
cwd : nuxt.options.srcDir ,
ignoreInitial : true ,
ignored : [
2022-02-28 16:11:46 +00:00
isIgnored ,
2021-05-20 11:42:41 +00:00
'.nuxt' ,
'node_modules'
]
} )
2022-03-15 10:56:16 +00:00
2022-06-10 14:50:47 +00:00
watcher . on ( 'all' , ( event , path ) = > nuxt . callHook ( 'builder:watch' , event , normalize ( path ) ) )
2021-05-20 11:42:41 +00:00
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-20 18:50:01 +00:00
try {
2022-03-18 12:57:05 +00:00
const { bundle } = typeof nuxt . options . builder === 'string'
? await importModule ( nuxt . options . builder , { paths : nuxt.options.rootDir } )
: nuxt . options . builder
2021-10-20 18:50:01 +00:00
return bundle ( nuxt )
} catch ( error ) {
await nuxt . callHook ( 'build:error' , error )
2022-03-18 12:57:05 +00:00
if ( error . toString ( ) . includes ( 'Cannot find module \'@nuxt/webpack-builder\'' ) ) {
throw new Error ( [
'Could not load `@nuxt/webpack-builder`. You may need to add it to your project dependencies, following the steps in `https://github.com/nuxt/framework/pull/2812`.'
] . join ( '\n' ) )
}
2021-10-20 18:50:01 +00:00
throw error
}
2020-08-17 15:25:06 +00:00
}