2019-03-15 05:09:00 +01:00
|
|
|
|
import { relative as relativePath, join, normalize, sep } from 'path'
|
2017-06-07 00:32:02 +02:00
|
|
|
|
import WebpackDevMiddleware from 'webpack-dev-middleware'
|
|
|
|
|
import WebpackHotMiddleware from 'webpack-hot-middleware'
|
2018-08-24 16:30:41 +02:00
|
|
|
|
import errorOverlayMiddleware from './lib/error-overlay-middleware'
|
2019-02-19 22:45:07 +01:00
|
|
|
|
import onDemandEntryHandler, { normalizePage } from './on-demand-entry-handler'
|
2018-01-30 16:40:52 +01:00
|
|
|
|
import webpack from 'webpack'
|
2018-12-03 14:18:52 +01:00
|
|
|
|
import getBaseWebpackConfig from '../build/webpack-config'
|
2019-02-25 23:00:14 +01:00
|
|
|
|
import { IS_BUNDLED_PAGE_REGEX, ROUTE_NAME_REGEX, BLOCKED_PAGES, CLIENT_STATIC_FILES_RUNTIME_AMP } from 'next-server/constants'
|
|
|
|
|
import { NEXT_PROJECT_ROOT_DIST_CLIENT } from '../lib/constants'
|
2019-02-19 22:45:07 +01:00
|
|
|
|
import { route } from 'next-server/dist/server/router'
|
|
|
|
|
import { createPagesMapping, createEntrypoints } from '../build/entries'
|
|
|
|
|
import { watchCompiler } from '../build/output'
|
2019-02-24 22:08:35 +01:00
|
|
|
|
import { findPageFile } from './lib/find-page-file'
|
2019-03-05 14:01:42 +01:00
|
|
|
|
import { recursiveDelete } from '../lib/recursive-delete'
|
2018-12-06 16:47:10 +01:00
|
|
|
|
|
|
|
|
|
export async function renderScriptError (res, error) {
|
|
|
|
|
// Asks CDNs and others to not to cache the errored page
|
|
|
|
|
res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
|
|
|
|
|
|
|
|
|
|
if (error.code === 'ENOENT' || error.message === 'INVALID_BUILD_ID') {
|
|
|
|
|
res.statusCode = 404
|
|
|
|
|
res.end('404 - Not Found')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.error(error.stack)
|
|
|
|
|
res.statusCode = 500
|
|
|
|
|
res.end('500 - Internal Error')
|
|
|
|
|
}
|
2018-10-02 00:55:31 +02:00
|
|
|
|
|
|
|
|
|
function addCorsSupport (req, res) {
|
|
|
|
|
if (!req.headers.origin) {
|
|
|
|
|
return { preflight: false }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', req.headers.origin)
|
|
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
|
|
|
|
|
// Based on https://github.com/primus/access-control/blob/4cf1bc0e54b086c91e6aa44fb14966fa5ef7549c/index.js#L158
|
|
|
|
|
if (req.headers['access-control-request-headers']) {
|
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers'])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
|
res.writeHead(200)
|
|
|
|
|
res.end()
|
|
|
|
|
return { preflight: true }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { preflight: false }
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 13:45:42 +02:00
|
|
|
|
const matchNextPageBundleRequest = route('/_next/static/:buildId/pages/:path*.js(.map)?')
|
2018-07-24 11:24:40 +02:00
|
|
|
|
|
|
|
|
|
// Recursively look up the issuer till it ends up at the root
|
|
|
|
|
function findEntryModule (issuer) {
|
|
|
|
|
if (issuer.issuer) {
|
|
|
|
|
return findEntryModule(issuer.issuer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return issuer
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 22:45:07 +01:00
|
|
|
|
function erroredPages (compilation, options = { enhanceName: (name) => name }) {
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const failedPages = {}
|
|
|
|
|
for (const error of compilation.errors) {
|
|
|
|
|
const entryModule = findEntryModule(error.origin)
|
2019-02-19 22:45:07 +01:00
|
|
|
|
const { name } = entryModule
|
2018-07-24 11:24:40 +02:00
|
|
|
|
if (!name) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only pages have to be reloaded
|
|
|
|
|
if (!IS_BUNDLED_PAGE_REGEX.test(name)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const enhancedName = options.enhanceName(name)
|
|
|
|
|
|
|
|
|
|
if (!failedPages[enhancedName]) {
|
|
|
|
|
failedPages[enhancedName] = []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
failedPages[enhancedName].push(error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return failedPages
|
|
|
|
|
}
|
2017-06-05 17:07:20 +02:00
|
|
|
|
|
2016-10-14 17:05:08 +02:00
|
|
|
|
export default class HotReloader {
|
2018-07-25 13:45:42 +02:00
|
|
|
|
constructor (dir, { config, buildId } = {}) {
|
2018-06-25 23:06:46 +02:00
|
|
|
|
this.buildId = buildId
|
2016-10-17 09:05:46 +02:00
|
|
|
|
this.dir = dir
|
2016-11-23 19:32:49 +01:00
|
|
|
|
this.middlewares = []
|
|
|
|
|
this.webpackDevMiddleware = null
|
|
|
|
|
this.webpackHotMiddleware = null
|
2016-10-31 11:51:03 +01:00
|
|
|
|
this.initialized = false
|
2016-10-19 14:41:45 +02:00
|
|
|
|
this.stats = null
|
2018-07-24 11:24:40 +02:00
|
|
|
|
this.serverPrevDocumentHash = null
|
2017-02-26 20:45:16 +01:00
|
|
|
|
|
2018-02-14 16:17:41 +01:00
|
|
|
|
this.config = config
|
2016-10-17 09:05:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 13:45:42 +02:00
|
|
|
|
async run (req, res, parsedUrl) {
|
2018-01-30 16:40:52 +01:00
|
|
|
|
// Usually CORS support is not needed for the hot-reloader (this is dev only feature)
|
|
|
|
|
// With when the app runs for multi-zones support behind a proxy,
|
|
|
|
|
// the current page is trying to access this URL via assetPrefix.
|
|
|
|
|
// That's when the CORS support is needed.
|
|
|
|
|
const { preflight } = addCorsSupport(req, res)
|
|
|
|
|
if (preflight) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 13:45:42 +02:00
|
|
|
|
// When a request comes in that is a page bundle, e.g. /_next/static/<buildid>/pages/index.js
|
|
|
|
|
// we have to compile the page using on-demand-entries, this middleware will handle doing that
|
|
|
|
|
// by adding the page to on-demand-entries, waiting till it's done
|
|
|
|
|
// and then the bundle will be served like usual by the actual route in server/index.js
|
2019-01-08 23:10:32 +01:00
|
|
|
|
const handlePageBundleRequest = async (res, parsedUrl) => {
|
2019-02-19 22:45:07 +01:00
|
|
|
|
const { pathname } = parsedUrl
|
2018-07-25 13:45:42 +02:00
|
|
|
|
const params = matchNextPageBundleRequest(pathname)
|
|
|
|
|
if (!params) {
|
|
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (params.buildId !== this.buildId) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const page = `/${params.path.join('/')}`
|
2019-02-03 15:34:28 +01:00
|
|
|
|
if (page === '/_error' || BLOCKED_PAGES.indexOf(page) === -1) {
|
2018-07-25 13:45:42 +02:00
|
|
|
|
try {
|
|
|
|
|
await this.ensurePage(page)
|
|
|
|
|
} catch (error) {
|
2018-12-06 16:47:10 +01:00
|
|
|
|
await renderScriptError(res, error)
|
2019-02-19 22:45:07 +01:00
|
|
|
|
return { finished: true }
|
2018-07-25 13:45:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const errors = await this.getCompilationErrors(page)
|
|
|
|
|
if (errors.length > 0) {
|
2018-12-06 16:47:10 +01:00
|
|
|
|
await renderScriptError(res, errors[0])
|
2019-02-19 22:45:07 +01:00
|
|
|
|
return { finished: true }
|
2018-07-25 13:45:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 22:45:07 +01:00
|
|
|
|
const { finished } = await handlePageBundleRequest(res, parsedUrl)
|
2018-07-25 13:45:42 +02:00
|
|
|
|
|
2016-11-23 19:32:49 +01:00
|
|
|
|
for (const fn of this.middlewares) {
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
|
fn(req, res, (err) => {
|
2017-01-08 03:02:29 +01:00
|
|
|
|
if (err) return reject(err)
|
2016-11-23 19:32:49 +01:00
|
|
|
|
resolve()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-07-25 13:45:42 +02:00
|
|
|
|
|
2019-02-19 22:45:07 +01:00
|
|
|
|
return { finished }
|
2016-10-17 09:05:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 16:17:41 +01:00
|
|
|
|
async clean () {
|
2019-03-05 14:01:42 +01:00
|
|
|
|
return recursiveDelete(join(this.dir, this.config.distDir))
|
2018-02-14 16:17:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 23:10:32 +01:00
|
|
|
|
async getWebpackConfig () {
|
2019-02-24 22:08:35 +01:00
|
|
|
|
const pagesDir = join(this.dir, 'pages')
|
|
|
|
|
const pagePaths = await Promise.all([
|
|
|
|
|
findPageFile(pagesDir, '/_app', this.config.pageExtensions),
|
|
|
|
|
findPageFile(pagesDir, '/_document', this.config.pageExtensions)
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const pages = createPagesMapping(pagePaths.filter(i => i !== null), this.config.pageExtensions)
|
2019-04-04 23:54:01 +02:00
|
|
|
|
const entrypoints = createEntrypoints(pages, 'server', this.buildId, false, this.config)
|
2019-02-25 23:00:14 +01:00
|
|
|
|
|
|
|
|
|
let additionalClientEntrypoints = {}
|
2019-04-13 03:54:27 +02:00
|
|
|
|
additionalClientEntrypoints[CLIENT_STATIC_FILES_RUNTIME_AMP] = `.${sep}` + relativePath(this.dir, join(NEXT_PROJECT_ROOT_DIST_CLIENT, 'amp-dev'))
|
2019-02-25 23:00:14 +01:00
|
|
|
|
|
2019-04-22 20:57:02 +02:00
|
|
|
|
return Promise.all([
|
2019-02-25 23:00:14 +01:00
|
|
|
|
getBaseWebpackConfig(this.dir, { dev: true, isServer: false, config: this.config, buildId: this.buildId, entrypoints: { ...entrypoints.client, ...additionalClientEntrypoints } }),
|
2019-01-08 23:10:32 +01:00
|
|
|
|
getBaseWebpackConfig(this.dir, { dev: true, isServer: true, config: this.config, buildId: this.buildId, entrypoints: entrypoints.server })
|
2019-04-22 20:57:02 +02:00
|
|
|
|
])
|
2019-01-08 23:10:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 09:05:46 +02:00
|
|
|
|
async start () {
|
2018-02-14 16:17:41 +01:00
|
|
|
|
await this.clean()
|
2018-01-30 16:40:52 +01:00
|
|
|
|
|
2019-01-08 23:10:32 +01:00
|
|
|
|
const configs = await this.getWebpackConfig()
|
2016-12-16 21:33:08 +01:00
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const multiCompiler = webpack(configs)
|
2018-01-30 16:40:52 +01:00
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const buildTools = await this.prepareBuildTools(multiCompiler)
|
2017-06-07 00:32:02 +02:00
|
|
|
|
this.assignBuildTools(buildTools)
|
|
|
|
|
|
2018-01-30 16:40:52 +01:00
|
|
|
|
this.stats = (await this.waitUntilValid()).stats[0]
|
2016-10-17 09:05:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 00:32:02 +02:00
|
|
|
|
async stop (webpackDevMiddleware) {
|
|
|
|
|
const middleware = webpackDevMiddleware || this.webpackDevMiddleware
|
|
|
|
|
if (middleware) {
|
2016-12-17 05:04:40 +01:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-06-07 00:32:02 +02:00
|
|
|
|
middleware.close((err) => {
|
2017-01-08 03:02:29 +01:00
|
|
|
|
if (err) return reject(err)
|
2016-12-17 05:04:40 +01:00
|
|
|
|
resolve()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 00:32:02 +02:00
|
|
|
|
async reload () {
|
|
|
|
|
this.stats = null
|
|
|
|
|
|
2018-02-14 16:17:41 +01:00
|
|
|
|
await this.clean()
|
2018-01-30 16:40:52 +01:00
|
|
|
|
|
2019-01-08 23:10:32 +01:00
|
|
|
|
const configs = await this.getWebpackConfig()
|
2018-01-30 16:40:52 +01:00
|
|
|
|
const compiler = webpack(configs)
|
|
|
|
|
|
2017-06-07 00:32:02 +02:00
|
|
|
|
const buildTools = await this.prepareBuildTools(compiler)
|
|
|
|
|
this.stats = await this.waitUntilValid(buildTools.webpackDevMiddleware)
|
|
|
|
|
|
|
|
|
|
const oldWebpackDevMiddleware = this.webpackDevMiddleware
|
|
|
|
|
|
|
|
|
|
this.assignBuildTools(buildTools)
|
|
|
|
|
await this.stop(oldWebpackDevMiddleware)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assignBuildTools ({ webpackDevMiddleware, webpackHotMiddleware, onDemandEntries }) {
|
|
|
|
|
this.webpackDevMiddleware = webpackDevMiddleware
|
|
|
|
|
this.webpackHotMiddleware = webpackHotMiddleware
|
|
|
|
|
this.onDemandEntries = onDemandEntries
|
|
|
|
|
this.middlewares = [
|
|
|
|
|
webpackDevMiddleware,
|
2019-04-21 22:51:09 +02:00
|
|
|
|
// must come before hotMiddleware
|
|
|
|
|
onDemandEntries.middleware(),
|
2017-06-07 00:32:02 +02:00
|
|
|
|
webpackHotMiddleware,
|
2019-04-21 22:51:09 +02:00
|
|
|
|
errorOverlayMiddleware({ dir: this.dir })
|
2017-06-07 00:32:02 +02:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
async prepareBuildTools (multiCompiler) {
|
2019-02-16 17:09:49 +01:00
|
|
|
|
watchCompiler(
|
|
|
|
|
multiCompiler.compilers[0],
|
|
|
|
|
multiCompiler.compilers[1]
|
|
|
|
|
)
|
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
// This plugin watches for changes to _document.js and notifies the client side that it should reload the page
|
|
|
|
|
multiCompiler.compilers[1].hooks.done.tap('NextjsHotReloaderForServer', (stats) => {
|
|
|
|
|
if (!this.initialized) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-10-24 09:22:15 +02:00
|
|
|
|
|
2019-02-19 22:45:07 +01:00
|
|
|
|
const { compilation } = stats
|
2018-07-24 11:24:40 +02:00
|
|
|
|
|
|
|
|
|
// We only watch `_document` for changes on the server compilation
|
|
|
|
|
// the rest of the files will be triggered by the client compilation
|
2018-07-25 13:45:42 +02:00
|
|
|
|
const documentChunk = compilation.chunks.find(c => c.name === normalize(`static/${this.buildId}/pages/_document.js`))
|
2018-07-24 11:24:40 +02:00
|
|
|
|
// If the document chunk can't be found we do nothing
|
|
|
|
|
if (!documentChunk) {
|
|
|
|
|
console.warn('_document.js chunk not found')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initial value
|
|
|
|
|
if (this.serverPrevDocumentHash === null) {
|
|
|
|
|
this.serverPrevDocumentHash = documentChunk.hash
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If _document.js didn't change we don't trigger a reload
|
|
|
|
|
if (documentChunk.hash === this.serverPrevDocumentHash) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Notify reload to reload the page, as _document.js was changed (different hash)
|
2019-01-08 23:10:32 +01:00
|
|
|
|
this.send('reloadPage')
|
2018-07-24 11:24:40 +02:00
|
|
|
|
this.serverPrevDocumentHash = documentChunk.hash
|
2016-10-15 21:49:42 +02:00
|
|
|
|
})
|
2016-10-14 17:05:08 +02:00
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
multiCompiler.compilers[0].hooks.done.tap('NextjsHotReloaderForClient', (stats) => {
|
2016-10-31 11:51:03 +01:00
|
|
|
|
const { compilation } = stats
|
2017-06-05 17:07:20 +02:00
|
|
|
|
const chunkNames = new Set(
|
|
|
|
|
compilation.chunks
|
|
|
|
|
.map((c) => c.name)
|
2018-06-14 19:30:14 +02:00
|
|
|
|
.filter(name => IS_BUNDLED_PAGE_REGEX.test(name))
|
2017-06-05 17:07:20 +02:00
|
|
|
|
)
|
|
|
|
|
|
2016-10-31 11:51:03 +01:00
|
|
|
|
if (this.initialized) {
|
|
|
|
|
// detect chunks which have to be replaced with a new template
|
|
|
|
|
// e.g, pages/index.js <-> pages/_error.js
|
2019-01-08 23:10:32 +01:00
|
|
|
|
const addedPages = diff(chunkNames, this.prevChunkNames)
|
|
|
|
|
const removedPages = diff(this.prevChunkNames, chunkNames)
|
|
|
|
|
|
|
|
|
|
if (addedPages.size > 0) {
|
|
|
|
|
for (const addedPage of addedPages) {
|
|
|
|
|
let page = '/' + ROUTE_NAME_REGEX.exec(addedPage)[1].replace(/\\/g, '/')
|
|
|
|
|
page = page === '/index' ? '/' : page
|
|
|
|
|
this.send('addedPage', page)
|
|
|
|
|
}
|
2016-10-24 09:22:15 +02:00
|
|
|
|
}
|
2016-11-24 15:03:16 +01:00
|
|
|
|
|
2019-01-08 23:10:32 +01:00
|
|
|
|
if (removedPages.size > 0) {
|
|
|
|
|
for (const removedPage of removedPages) {
|
|
|
|
|
let page = '/' + ROUTE_NAME_REGEX.exec(removedPage)[1].replace(/\\/g, '/')
|
|
|
|
|
page = page === '/index' ? '/' : page
|
|
|
|
|
this.send('removedPage', page)
|
|
|
|
|
}
|
2016-11-24 15:03:16 +01:00
|
|
|
|
}
|
2016-10-24 09:22:15 +02:00
|
|
|
|
}
|
2016-10-24 17:20:50 +02:00
|
|
|
|
|
2016-10-31 11:51:03 +01:00
|
|
|
|
this.initialized = true
|
|
|
|
|
this.stats = stats
|
|
|
|
|
this.prevChunkNames = chunkNames
|
2016-10-19 14:41:45 +02:00
|
|
|
|
})
|
|
|
|
|
|
2018-10-28 23:01:45 +01:00
|
|
|
|
// We don’t watch .git/ .next/ and node_modules for changes
|
2017-01-19 08:09:40 +01:00
|
|
|
|
const ignored = [
|
2018-10-28 23:01:45 +01:00
|
|
|
|
/[\\/]\.git[\\/]/,
|
|
|
|
|
/[\\/]\.next[\\/]/,
|
|
|
|
|
/[\\/]node_modules[\\/]/
|
2017-01-19 08:09:40 +01:00
|
|
|
|
]
|
2016-11-28 15:04:59 +01:00
|
|
|
|
|
2017-05-14 01:44:21 +02:00
|
|
|
|
let webpackDevMiddlewareConfig = {
|
2018-07-24 11:24:40 +02:00
|
|
|
|
publicPath: `/_next/static/webpack`,
|
2016-10-17 09:05:46 +02:00
|
|
|
|
noInfo: true,
|
2018-07-24 11:24:40 +02:00
|
|
|
|
logLevel: 'silent',
|
2018-11-02 19:47:56 +01:00
|
|
|
|
watchOptions: { ignored },
|
|
|
|
|
writeToDisk: true
|
2017-05-14 01:44:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.config.webpackDevMiddleware) {
|
2017-05-31 10:06:07 +02:00
|
|
|
|
console.log(`> Using "webpackDevMiddleware" config function defined in ${this.config.configOrigin}.`)
|
2017-05-14 01:44:21 +02:00
|
|
|
|
webpackDevMiddlewareConfig = this.config.webpackDevMiddleware(webpackDevMiddlewareConfig)
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const webpackDevMiddleware = WebpackDevMiddleware(multiCompiler, webpackDevMiddlewareConfig)
|
2016-11-23 19:32:49 +01:00
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const webpackHotMiddleware = WebpackHotMiddleware(multiCompiler.compilers[0], {
|
2017-04-07 19:58:35 +02:00
|
|
|
|
path: '/_next/webpack-hmr',
|
2017-06-05 11:02:13 +02:00
|
|
|
|
log: false,
|
|
|
|
|
heartbeat: 2500
|
2017-04-07 19:58:35 +02:00
|
|
|
|
})
|
2018-01-30 16:40:52 +01:00
|
|
|
|
|
2018-09-16 16:06:02 +02:00
|
|
|
|
const onDemandEntries = onDemandEntryHandler(webpackDevMiddleware, multiCompiler, {
|
2017-02-26 20:45:16 +01:00
|
|
|
|
dir: this.dir,
|
2018-07-25 13:45:42 +02:00
|
|
|
|
buildId: this.buildId,
|
2019-04-16 15:56:34 +02:00
|
|
|
|
distDir: this.config.distDir,
|
2017-06-07 00:32:02 +02:00
|
|
|
|
reload: this.reload.bind(this),
|
2018-02-14 16:20:41 +01:00
|
|
|
|
pageExtensions: this.config.pageExtensions,
|
2019-04-16 15:56:34 +02:00
|
|
|
|
publicRuntimeConfig: this.config.publicRuntimeConfig,
|
|
|
|
|
serverRuntimeConfig: this.config.serverRuntimeConfig,
|
2017-02-26 20:45:16 +01:00
|
|
|
|
...this.config.onDemandEntries
|
|
|
|
|
})
|
2016-11-23 19:32:49 +01:00
|
|
|
|
|
2017-06-07 00:32:02 +02:00
|
|
|
|
return {
|
|
|
|
|
webpackDevMiddleware,
|
|
|
|
|
webpackHotMiddleware,
|
|
|
|
|
onDemandEntries
|
|
|
|
|
}
|
2016-10-14 17:05:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 00:32:02 +02:00
|
|
|
|
waitUntilValid (webpackDevMiddleware) {
|
|
|
|
|
const middleware = webpackDevMiddleware || this.webpackDevMiddleware
|
2016-10-19 14:41:45 +02:00
|
|
|
|
return new Promise((resolve) => {
|
2017-06-07 00:32:02 +02:00
|
|
|
|
middleware.waitUntilValid(resolve)
|
2016-10-14 17:05:08 +02:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
async getCompilationErrors (page) {
|
|
|
|
|
const normalizedPage = normalizePage(page)
|
2017-06-07 00:32:02 +02:00
|
|
|
|
// When we are reloading, we need to wait until it's reloaded properly.
|
|
|
|
|
await this.onDemandEntries.waitUntilReloaded()
|
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
if (this.stats.hasErrors()) {
|
2019-02-19 22:45:07 +01:00
|
|
|
|
const { compilation } = this.stats
|
2018-07-24 11:24:40 +02:00
|
|
|
|
const failedPages = erroredPages(compilation, {
|
|
|
|
|
enhanceName (name) {
|
|
|
|
|
return '/' + ROUTE_NAME_REGEX.exec(name)[1]
|
2016-10-19 14:41:45 +02:00
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// If there is an error related to the requesting page we display it instead of the first error
|
|
|
|
|
if (failedPages[normalizedPage] && failedPages[normalizedPage].length > 0) {
|
|
|
|
|
return failedPages[normalizedPage]
|
2016-10-19 14:41:45 +02:00
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
|
|
|
|
|
// If none were found we still have to show the other errors
|
|
|
|
|
return this.stats.compilation.errors
|
2016-10-19 14:41:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
|
return []
|
2016-10-19 14:41:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-23 19:32:49 +01:00
|
|
|
|
send (action, ...args) {
|
|
|
|
|
this.webpackHotMiddleware.publish({ action, data: args })
|
2016-10-24 09:22:15 +02:00
|
|
|
|
}
|
2017-02-26 20:45:16 +01:00
|
|
|
|
|
2019-04-11 20:59:26 +02:00
|
|
|
|
async ensurePage (page) {
|
2018-05-16 00:36:24 +02:00
|
|
|
|
// Make sure we don't re-build or dispose prebuilt pages
|
2019-02-03 15:34:28 +01:00
|
|
|
|
if (page !== '/_error' && BLOCKED_PAGES.indexOf(page) !== -1) {
|
2018-05-16 00:36:24 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-04-11 20:59:26 +02:00
|
|
|
|
return this.onDemandEntries.ensurePage(page)
|
2017-02-26 20:45:16 +01:00
|
|
|
|
}
|
2016-10-14 17:05:08 +02:00
|
|
|
|
}
|
2016-10-19 14:41:45 +02:00
|
|
|
|
|
2016-10-24 09:22:15 +02:00
|
|
|
|
function diff (a, b) {
|
|
|
|
|
return new Set([...a].filter((v) => !b.has(v)))
|
|
|
|
|
}
|