-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
builder.ts
193 lines (169 loc) · 6.07 KB
/
builder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { pathToFileURL } from 'node:url'
import type { EventType } from '@parcel/watcher'
import type { FSWatcher } from 'chokidar'
import chokidar from 'chokidar'
import { isIgnored, tryResolveModule, useNuxt } from '@nuxt/kit'
import { interopDefault } from 'mlly'
import { debounce } from 'perfect-debounce'
import { normalize } from 'pathe'
import type { Nuxt } from 'nuxt/schema'
import { generateApp as _generateApp, createApp } from './app'
export async function build (nuxt: Nuxt) {
const app = createApp(nuxt)
nuxt.apps.default = app
const generateApp = debounce(() => _generateApp(nuxt, app), undefined, { leading: true })
await generateApp()
if (nuxt.options.dev) {
watch(nuxt)
nuxt.hook('builder:watch', async (event, path) => {
if (event !== 'change' && /^(app\.|error\.|plugins\/|middleware\/|layouts\/)/i.test(path)) {
if (path.startsWith('app')) {
app.mainComponent = undefined
}
if (path.startsWith('error')) {
app.errorComponent = undefined
}
await generateApp()
}
})
nuxt.hook('builder:generateApp', (options) => {
// Bypass debounce if we are selectively invalidating templates
if (options) { return _generateApp(nuxt, app, options) }
return generateApp()
})
}
await nuxt.callHook('build:before')
if (!nuxt.options._prepare) {
await bundle(nuxt)
await nuxt.callHook('build:done')
}
if (!nuxt.options.dev) {
await nuxt.callHook('close', nuxt)
}
}
const watchEvents: Record<EventType, 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'> = {
create: 'add',
delete: 'unlink',
update: 'change'
}
async function watch (nuxt: Nuxt) {
if (nuxt.options.experimental.watcher === 'parcel') {
const success = await createParcelWatcher()
if (success) { return }
}
if (nuxt.options.experimental.watcher === 'granular') {
return createGranularWatcher()
}
return createWatcher()
}
function createWatcher () {
const nuxt = useNuxt()
const watcher = chokidar.watch(nuxt.options._layers.map(i => i.config.srcDir as string).filter(Boolean), {
...nuxt.options.watchers.chokidar,
cwd: nuxt.options.srcDir,
ignoreInitial: true,
ignored: [
isIgnored,
'.nuxt',
'node_modules'
]
})
watcher.on('all', (event, path) => nuxt.callHook('builder:watch', event, normalize(path)))
nuxt.hook('close', () => watcher.close())
}
function createGranularWatcher () {
const nuxt = useNuxt()
if (nuxt.options.debug) {
console.time('[nuxt] builder:chokidar:watch')
}
let pending = 0
const ignoredDirs = new Set([...nuxt.options.modulesDir, nuxt.options.buildDir])
const pathsToWatch = nuxt.options._layers.map(layer => layer.config.srcDir).filter(d => d && !isIgnored(d))
for (const path of nuxt.options.watch) {
if (typeof path !== 'string') { continue }
if (pathsToWatch.some(w => path.startsWith(w.replace(/[^/]$/, '$&/')))) { continue }
pathsToWatch.push(path)
}
for (const dir of pathsToWatch) {
pending++
const watcher = chokidar.watch(dir, { ...nuxt.options.watchers.chokidar, ignoreInitial: false, depth: 0, ignored: [isIgnored] })
const watchers: Record<string, FSWatcher> = {}
watcher.on('all', (event, path) => {
if (!pending) {
nuxt.callHook('builder:watch', event, normalize(path))
}
if (event === 'unlinkDir' && path in watchers) {
watchers[path].close()
delete watchers[path]
}
if (event === 'addDir' && path !== dir && !ignoredDirs.has(path) && !(path in watchers) && !isIgnored(path)) {
watchers[path] = chokidar.watch(path, { ...nuxt.options.watchers.chokidar, ignored: [isIgnored] })
watchers[path].on('all', (event, path) => nuxt.callHook('builder:watch', event, normalize(path)))
nuxt.hook('close', () => watchers[path].close())
}
})
watcher.on('ready', () => {
pending--
if (nuxt.options.debug && !pending) {
console.timeEnd('[nuxt] builder:chokidar:watch')
}
})
}
}
async function createParcelWatcher () {
const nuxt = useNuxt()
if (nuxt.options.debug) {
console.time('[nuxt] builder:parcel:watch')
}
const watcherPath = await tryResolveModule('@parcel/watcher', [nuxt.options.rootDir, ...nuxt.options.modulesDir])
if (watcherPath) {
const { subscribe } = await import(pathToFileURL(watcherPath).href).then(interopDefault) as typeof import('@parcel/watcher')
for (const layer of nuxt.options._layers) {
if (!layer.config.srcDir) { continue }
const watcher = subscribe(layer.config.srcDir, (err, events) => {
if (err) { return }
for (const event of events) {
if (isIgnored(event.path)) { continue }
nuxt.callHook('builder:watch', watchEvents[event.type], normalize(event.path))
}
}, {
ignore: [
...nuxt.options.ignore,
'.nuxt',
'node_modules'
]
})
watcher.then((subscription) => {
if (nuxt.options.debug) {
console.timeEnd('[nuxt] builder:parcel:watch')
}
nuxt.hook('close', () => subscription.unsubscribe())
})
}
return true
}
console.warn('[nuxt] falling back to `chokidar` as `@parcel/watcher` cannot be resolved in your project.')
return false
}
async function bundle (nuxt: Nuxt) {
try {
const { bundle } = typeof nuxt.options.builder === 'string'
? await loadBuilder(nuxt, nuxt.options.builder)
: nuxt.options.builder
return bundle(nuxt)
} catch (error: any) {
await nuxt.callHook('build:error', error)
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'))
}
throw error
}
}
async function loadBuilder (nuxt: Nuxt, builder: string) {
const builderPath = await tryResolveModule(builder, [nuxt.options.rootDir, import.meta.url])
if (builderPath) {
return import(pathToFileURL(builderPath).href)
}
}