MCPcopy
hub / github.com/nuxt/nuxt / build

Function build

packages/nuxt/src/core/builder.ts:14–110  ·  view source on GitHub ↗
(nuxt: Nuxt)

Source from the content-addressed store, hash-verified

12import type { Nuxt, NuxtBuilder, NuxtHooks } from 'nuxt/schema'
13
14export async function build (nuxt: Nuxt): Promise<void> {
15 nuxt._perf?.startPhase('app:generate')
16 const app = createApp(nuxt)
17 nuxt.apps.default = app
18
19 let closing = false
20 const writes = new Set<Promise<unknown>>()
21 const track = async <T> (run: () => Promise<T>) => {
22 if (closing) { return }
23 const p = run()
24 writes.add(p)
25 try { await p } finally { writes.delete(p) }
26 }
27 const generateApp = debounce(() => _generateApp(nuxt, app), undefined, { leading: true })
28 await generateApp()
29 nuxt._perf?.endPhase('app:generate')
30
31 if (nuxt.options.dev) {
32 watch(nuxt)
33 nuxt.hook('close', async () => {
34 closing = true
35 generateApp.cancel()
36 await Promise.allSettled(writes)
37 })
38 nuxt.hook('builder:watch', async (event, relativePath) => {
39 // Unset mainComponent and errorComponent if app or error component is changed
40 if (event === 'add' || event === 'unlink') {
41 const path = resolve(nuxt.options.srcDir, relativePath)
42 for (const dirs of getLayerDirectories(nuxt)) {
43 const relativePath = relative(dirs.app, path)
44 if (/^app\./i.test(relativePath)) {
45 app.mainComponent = undefined
46 break
47 }
48 if (/^error\./i.test(relativePath)) {
49 app.errorComponent = undefined
50 break
51 }
52 }
53 }
54
55 // Recompile app templates
56 await track(() => generateApp())
57 })
58 nuxt.hook('builder:generateApp', (options) => {
59 // Bypass debounce if we are selectively invalidating templates
60 if (options) { return track(() => _generateApp(nuxt, app, options)) }
61 return track(() => generateApp())
62 })
63 }
64
65 if (!nuxt.options._prepare && !nuxt.options.dev && nuxt.options.experimental.buildCache) {
66 const { restoreCache, collectCache } = await getVueHash(nuxt)
67 if (await restoreCache()) {
68 await nuxt.callHook('build:done')
69 await nuxt.callHook('close', nuxt)
70 return
71 }

Callers 6

builder.test.tsFile · 0.90
cache.test.tsFile · 0.90
render.bench.tsFile · 0.90
bundleFunction · 0.85
buildNuxtFunction · 0.85

Calls 14

createAppFunction · 0.90
generateAppFunction · 0.90
getLayerDirectoriesFunction · 0.90
getVueHashFunction · 0.90
cleanupCachesFunction · 0.90
watchFunction · 0.85
trackFunction · 0.85
restoreCacheFunction · 0.85
collectCacheFunction · 0.85
clearBuildHooksFunction · 0.85
startPhaseMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…