2018-10-25 10:55:05 +00:00
|
|
|
import path from 'path'
|
2018-10-24 16:55:18 +00:00
|
|
|
import pify from 'pify'
|
|
|
|
import webpack from 'webpack'
|
2018-10-25 10:55:05 +00:00
|
|
|
import Glob from 'glob'
|
2018-10-24 16:55:18 +00:00
|
|
|
import webpackDevMiddleware from 'webpack-dev-middleware'
|
|
|
|
import webpackHotMiddleware from 'webpack-hot-middleware'
|
|
|
|
import consola from 'consola'
|
|
|
|
|
2019-04-23 09:16:56 +00:00
|
|
|
import { parallel, sequence, wrapArray, isModernRequest } from '@nuxt/utils'
|
2019-03-08 12:20:03 +00:00
|
|
|
import AsyncMFS from './utils/async-mfs'
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2019-03-31 21:15:46 +00:00
|
|
|
import * as WebpackConfigs from './config'
|
2018-12-01 10:13:28 +00:00
|
|
|
import PerfLoader from './utils/perf-loader'
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2018-10-25 10:55:05 +00:00
|
|
|
const glob = pify(Glob)
|
|
|
|
|
2018-11-08 09:15:56 +00:00
|
|
|
export class WebpackBundler {
|
2019-07-10 10:45:49 +00:00
|
|
|
constructor (buildContext) {
|
2019-02-18 17:00:51 +00:00
|
|
|
this.buildContext = buildContext
|
2019-03-08 20:43:23 +00:00
|
|
|
|
|
|
|
// Class fields
|
2018-10-24 16:55:18 +00:00
|
|
|
this.compilers = []
|
|
|
|
this.compilersWatching = []
|
2018-11-07 23:37:06 +00:00
|
|
|
this.devMiddleware = {}
|
|
|
|
this.hotMiddleware = {}
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
// Bind middleware to self
|
|
|
|
this.middleware = this.middleware.bind(this)
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
// Initialize shared MFS for dev
|
2019-02-18 17:00:51 +00:00
|
|
|
if (this.buildContext.options.dev) {
|
2019-03-08 12:20:03 +00:00
|
|
|
this.mfs = new AsyncMFS()
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
getWebpackConfig (name) {
|
2019-03-31 21:15:46 +00:00
|
|
|
const Config = WebpackConfigs[name] // eslint-disable-line import/namespace
|
|
|
|
if (!Config) {
|
|
|
|
throw new Error(`Unsupported webpack config ${name}`)
|
|
|
|
}
|
|
|
|
const config = new Config(this)
|
|
|
|
return config.config()
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async build () {
|
2019-02-18 17:00:51 +00:00
|
|
|
const { options } = this.buildContext
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2019-03-31 21:15:46 +00:00
|
|
|
const webpackConfigs = [
|
|
|
|
this.getWebpackConfig('Client')
|
|
|
|
]
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2018-11-07 23:37:06 +00:00
|
|
|
if (options.modern) {
|
2019-03-31 21:15:46 +00:00
|
|
|
webpackConfigs.push(this.getWebpackConfig('Modern'))
|
2018-10-31 15:52:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 16:55:18 +00:00
|
|
|
if (options.build.ssr) {
|
2019-03-31 21:15:46 +00:00
|
|
|
webpackConfigs.push(this.getWebpackConfig('Server'))
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:32:49 +00:00
|
|
|
await this.buildContext.nuxt.callHook('webpack:config', webpackConfigs)
|
|
|
|
|
2018-10-25 10:55:05 +00:00
|
|
|
// Check styleResource existence
|
2019-02-18 17:00:51 +00:00
|
|
|
const { styleResources } = this.buildContext.options.build
|
2018-11-09 23:59:40 +00:00
|
|
|
if (styleResources && Object.keys(styleResources).length) {
|
|
|
|
consola.warn(
|
2019-04-11 10:04:52 +00:00
|
|
|
'Using styleResources without the @nuxtjs/style-resources is not suggested and can lead to severe performance issues.',
|
2018-11-09 23:59:40 +00:00
|
|
|
'Please use https://github.com/nuxt-community/style-resources-module'
|
|
|
|
)
|
2018-12-12 10:47:54 +00:00
|
|
|
for (const ext of Object.keys(styleResources)) {
|
|
|
|
await Promise.all(wrapArray(styleResources[ext]).map(async (p) => {
|
2019-02-18 17:00:51 +00:00
|
|
|
const styleResourceFiles = await glob(path.resolve(this.buildContext.options.rootDir, p))
|
2018-12-12 10:47:54 +00:00
|
|
|
|
|
|
|
if (!styleResourceFiles || styleResourceFiles.length === 0) {
|
|
|
|
throw new Error(`Style Resource not found: ${p}`)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
}
|
2018-10-25 10:55:05 +00:00
|
|
|
|
2018-10-24 16:55:18 +00:00
|
|
|
// Configure compilers
|
2019-03-31 21:15:46 +00:00
|
|
|
this.compilers = webpackConfigs.map((config) => {
|
|
|
|
const compiler = webpack(config)
|
2018-10-24 16:55:18 +00:00
|
|
|
|
|
|
|
// In dev, write files in memory FS
|
|
|
|
if (options.dev) {
|
|
|
|
compiler.outputFileSystem = this.mfs
|
|
|
|
}
|
|
|
|
|
|
|
|
return compiler
|
|
|
|
})
|
|
|
|
|
2018-12-12 06:31:49 +00:00
|
|
|
// Warm up perfLoader before build
|
2018-10-24 16:55:18 +00:00
|
|
|
if (options.build.parallel) {
|
|
|
|
consola.info('Warming up worker pools')
|
2018-11-08 22:26:52 +00:00
|
|
|
PerfLoader.warmupAll({ dev: options.dev })
|
2018-10-24 16:55:18 +00:00
|
|
|
consola.success('Worker pools ready')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start Builds
|
|
|
|
const runner = options.dev ? parallel : sequence
|
|
|
|
|
2019-02-08 16:25:11 +00:00
|
|
|
await runner(this.compilers, compiler => this.webpackCompile(compiler))
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async webpackCompile (compiler) {
|
2019-01-17 21:18:29 +00:00
|
|
|
const { name } = compiler.options
|
2019-02-18 17:00:51 +00:00
|
|
|
const { nuxt, options } = this.buildContext
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
await nuxt.callHook('build:compile', { name, compiler })
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
// Load renderer resources after build
|
|
|
|
compiler.hooks.done.tap('load-resources', async (stats) => {
|
|
|
|
await nuxt.callHook('build:compiled', {
|
|
|
|
name,
|
|
|
|
compiler,
|
|
|
|
stats
|
|
|
|
})
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
// Reload renderer
|
|
|
|
await nuxt.callHook('build:resources', this.mfs)
|
|
|
|
})
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
// --- 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())
|
2019-03-08 20:43:23 +00:00
|
|
|
return this.webpackDev(compiler)
|
2018-12-09 10:42:22 +00:00
|
|
|
})
|
|
|
|
}
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2018-12-09 10:42:22 +00:00
|
|
|
// Server, build and watch for changes
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const watching = compiler.watch(options.watchers.webpack, (err) => {
|
2018-10-24 16:55:18 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
watching.close = pify(watching.close)
|
|
|
|
this.compilersWatching.push(watching)
|
2018-10-24 16:55:18 +00:00
|
|
|
resolve()
|
|
|
|
})
|
2018-12-09 10:42:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Production Build ---
|
|
|
|
compiler.run = pify(compiler.run)
|
|
|
|
const stats = await compiler.run()
|
|
|
|
|
|
|
|
if (stats.hasErrors()) {
|
2019-07-17 19:04:40 +00:00
|
|
|
// non-quiet mode: errors will be printed by webpack itself
|
|
|
|
const error = new Error('Nuxt build error')
|
2018-12-09 10:42:22 +00:00
|
|
|
if (options.build.quiet === true) {
|
2019-07-17 19:04:40 +00:00
|
|
|
error.stack = stats.toString('errors-only')
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
2019-07-17 19:04:40 +00:00
|
|
|
throw error
|
2018-12-09 10:42:22 +00:00
|
|
|
}
|
2019-03-23 07:02:55 +00:00
|
|
|
|
|
|
|
// Await for renderer to load resources (programmatic, tests and generate)
|
|
|
|
await nuxt.callHook('build:resources')
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async webpackDev (compiler) {
|
2019-03-08 20:43:23 +00:00
|
|
|
consola.debug('Creating webpack middleware...')
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2019-01-17 21:18:29 +00:00
|
|
|
const { name } = compiler.options
|
2019-03-08 20:43:23 +00:00
|
|
|
const buildOptions = this.buildContext.options.build
|
|
|
|
const { client, ...hotMiddlewareOptions } = buildOptions.hotMiddleware || {}
|
2018-10-24 16:55:18 +00:00
|
|
|
|
|
|
|
// Create webpack dev middleware
|
2018-11-07 23:37:06 +00:00
|
|
|
this.devMiddleware[name] = pify(
|
2018-10-24 16:55:18 +00:00
|
|
|
webpackDevMiddleware(
|
2019-08-21 19:04:04 +00:00
|
|
|
compiler, {
|
|
|
|
publicPath: buildOptions.publicPath,
|
|
|
|
stats: false,
|
|
|
|
logLevel: 'silent',
|
|
|
|
watchOptions: this.buildContext.options.watchers.webpack,
|
|
|
|
...buildOptions.devMiddleware
|
|
|
|
})
|
2018-10-24 16:55:18 +00:00
|
|
|
)
|
|
|
|
|
2018-11-07 23:37:06 +00:00
|
|
|
this.devMiddleware[name].close = pify(this.devMiddleware[name].close)
|
2018-10-24 16:55:18 +00:00
|
|
|
|
2019-08-21 19:04:04 +00:00
|
|
|
this.compilersWatching.push(this.devMiddleware[name].context.watching)
|
|
|
|
|
2018-11-07 23:37:06 +00:00
|
|
|
this.hotMiddleware[name] = pify(
|
2018-10-24 16:55:18 +00:00
|
|
|
webpackHotMiddleware(
|
2019-08-21 19:04:04 +00:00
|
|
|
compiler, {
|
|
|
|
log: false,
|
|
|
|
heartbeat: 10000,
|
|
|
|
path: `/__webpack_hmr/${name}`,
|
|
|
|
...hotMiddlewareOptions
|
|
|
|
})
|
2018-10-24 16:55:18 +00:00
|
|
|
)
|
|
|
|
|
2019-03-08 20:43:23 +00:00
|
|
|
// Register devMiddleware on server
|
|
|
|
await this.buildContext.nuxt.callHook('server:devMiddleware', this.middleware)
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async middleware (req, res, next) {
|
2019-04-23 09:16:56 +00:00
|
|
|
const name = isModernRequest(req, this.buildContext.options.modern) ? 'modern' : 'client'
|
2019-03-08 20:43:23 +00:00
|
|
|
|
|
|
|
if (this.devMiddleware && this.devMiddleware[name]) {
|
|
|
|
await this.devMiddleware[name](req, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hotMiddleware && this.hotMiddleware[name]) {
|
|
|
|
await this.hotMiddleware[name](req, res)
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
2019-03-08 20:43:23 +00:00
|
|
|
|
|
|
|
next()
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async unwatch () {
|
2019-02-08 16:25:11 +00:00
|
|
|
await Promise.all(this.compilersWatching.map(watching => watching.close()))
|
2018-12-09 10:42:22 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 19:04:04 +00:00
|
|
|
pauseWatch () {
|
|
|
|
this.compilersWatching.forEach(watching => watching.suspend())
|
|
|
|
}
|
|
|
|
|
|
|
|
resumeWatch () {
|
|
|
|
this.compilersWatching.forEach(watching => watching.resume())
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async close () {
|
2018-12-09 22:00:48 +00:00
|
|
|
if (this.__closed) {
|
|
|
|
return
|
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
this.__closed = true
|
|
|
|
|
|
|
|
// Unwatch
|
|
|
|
await this.unwatch()
|
|
|
|
|
2018-10-24 16:55:18 +00:00
|
|
|
// Stop webpack middleware
|
2018-11-07 23:37:06 +00:00
|
|
|
for (const devMiddleware of Object.values(this.devMiddleware)) {
|
|
|
|
await devMiddleware.close()
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
|
|
|
|
// 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
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
forGenerate () {
|
2019-02-18 17:00:51 +00:00
|
|
|
this.buildContext.isStatic = true
|
2018-10-24 16:55:18 +00:00
|
|
|
}
|
|
|
|
}
|