forked from nuxt/nuxt
/
builder.js
250 lines (201 loc) · 6.81 KB
/
builder.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import path from 'path'
import pify from 'pify'
import webpack from 'webpack'
import Glob from 'glob'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
import consola from 'consola'
import { TARGETS, parallel, sequence, wrapArray, isModernRequest } from '@nuxt/utils'
import AsyncMFS from './utils/async-mfs'
import * as WebpackConfigs from './config'
import PerfLoader from './utils/perf-loader'
const glob = pify(Glob)
export class WebpackBundler {
constructor (buildContext) {
this.buildContext = buildContext
// Class fields
this.compilers = []
this.compilersWatching = []
this.devMiddleware = {}
this.hotMiddleware = {}
// Bind middleware to self
this.middleware = this.middleware.bind(this)
// Initialize shared MFS for dev
if (this.buildContext.options.dev) {
this.mfs = new AsyncMFS()
}
}
getWebpackConfig (name) {
const Config = WebpackConfigs[name.toLowerCase()] // eslint-disable-line import/namespace
if (!Config) {
throw new Error(`Unsupported webpack config ${name}`)
}
const config = new Config(this)
return config.config()
}
async build () {
const { options } = this.buildContext
const webpackConfigs = [
this.getWebpackConfig('Client')
]
if (options.modern) {
webpackConfigs.push(this.getWebpackConfig('Modern'))
}
if (options.build.ssr) {
webpackConfigs.push(this.getWebpackConfig('Server'))
}
await this.buildContext.nuxt.callHook('webpack:config', webpackConfigs)
// Check styleResource existence
const { styleResources } = this.buildContext.options.build
if (styleResources && Object.keys(styleResources).length) {
consola.warn(
'Using styleResources without the @nuxtjs/style-resources is not suggested and can lead to severe performance issues.',
'Please use https://github.com/nuxt-community/style-resources-module'
)
for (const ext of Object.keys(styleResources)) {
await Promise.all(wrapArray(styleResources[ext]).map(async (p) => {
const styleResourceFiles = await glob(path.resolve(this.buildContext.options.rootDir, p))
if (!styleResourceFiles || styleResourceFiles.length === 0) {
throw new Error(`Style Resource not found: ${p}`)
}
}))
}
}
// Configure compilers
this.compilers = webpackConfigs.map((config) => {
const compiler = webpack(config)
// In dev, write files in memory FS
if (options.dev) {
compiler.outputFileSystem = this.mfs
}
return compiler
})
// Warm up perfLoader before build
if (options.build.parallel) {
consola.info('Warming up worker pools')
PerfLoader.warmupAll({ dev: options.dev })
consola.success('Worker pools ready')
}
// Start Builds
const runner = options.dev ? parallel : sequence
await runner(this.compilers, compiler => this.webpackCompile(compiler))
}
async webpackCompile (compiler) {
const { name } = compiler.options
const { nuxt, options } = this.buildContext
await nuxt.callHook('build:compile', { name, compiler })
// Load renderer resources after build
compiler.hooks.done.tap('load-resources', async (stats) => {
await nuxt.callHook('build:compiled', {
name,
compiler,
stats
})
// Reload renderer
await nuxt.callHook('build:resources', this.mfs)
})
// --- Dev Build ---
if (options.dev) {
// Client Build, watch is started by dev-middleware
if (['client', 'modern'].includes(name)) {
return new Promise((resolve, reject) => {
compiler.hooks.done.tap('nuxt-dev', () => resolve())
return this.webpackDev(compiler)
})
}
// Server, build and watch for changes
return new Promise((resolve, reject) => {
const watching = compiler.watch(options.watchers.webpack, (err) => {
if (err) {
return reject(err)
}
resolve()
})
watching.close = pify(watching.close)
this.compilersWatching.push(watching)
})
}
// --- Production Build ---
compiler.run = pify(compiler.run)
const stats = await compiler.run()
if (stats.hasErrors()) {
// non-quiet mode: errors will be printed by webpack itself
const error = new Error('Nuxt build error')
if (options.build.quiet === true) {
error.stack = stats.toString('errors-only')
}
throw error
}
// Await for renderer to load resources (programmatic, tests and generate)
await nuxt.callHook('build:resources')
}
async webpackDev (compiler) {
consola.debug('Creating webpack middleware...')
const { name } = compiler.options
const buildOptions = this.buildContext.options.build
const { client, ...hotMiddlewareOptions } = buildOptions.hotMiddleware || {}
// Create webpack dev middleware
this.devMiddleware[name] = pify(
webpackDevMiddleware(
compiler, {
publicPath: buildOptions.publicPath,
stats: false,
logLevel: 'silent',
watchOptions: this.buildContext.options.watchers.webpack,
fs: compiler.outputFileSystem,
...buildOptions.devMiddleware
})
)
this.devMiddleware[name].close = pify(this.devMiddleware[name].close)
this.compilersWatching.push(this.devMiddleware[name].context.watching)
this.hotMiddleware[name] = pify(
webpackHotMiddleware(
compiler, {
log: false,
heartbeat: 10000,
path: `/__webpack_hmr/${name}`,
...hotMiddlewareOptions
})
)
// Register devMiddleware on server
await this.buildContext.nuxt.callHook('server:devMiddleware', this.middleware)
}
async middleware (req, res, next) {
const name = isModernRequest(req, this.buildContext.options.modern) ? 'modern' : 'client'
if (this.devMiddleware && this.devMiddleware[name]) {
await this.devMiddleware[name](req, res)
}
if (this.hotMiddleware && this.hotMiddleware[name]) {
await this.hotMiddleware[name](req, res)
}
next()
}
async unwatch () {
await Promise.all(this.compilersWatching.map(watching => watching.close()))
}
async close () {
if (this.__closed) {
return
}
this.__closed = true
// Unwatch
await this.unwatch()
// Stop webpack middleware
for (const devMiddleware of Object.values(this.devMiddleware)) {
await devMiddleware.close()
}
// Cleanup MFS
if (this.mfs) {
delete this.mfs.data
delete this.mfs
}
// Cleanup more resources
delete this.compilers
delete this.compilersWatching
delete this.devMiddleware
delete this.hotMiddleware
}
forGenerate () {
this.buildContext.target = TARGETS.static
}
}