2018-07-24 09:24:40 +00:00
|
|
|
|
import { join, relative, sep, normalize } from 'path'
|
2017-06-06 22:32:02 +00:00
|
|
|
|
import WebpackDevMiddleware from 'webpack-dev-middleware'
|
|
|
|
|
import WebpackHotMiddleware from 'webpack-hot-middleware'
|
2018-08-24 14:30:41 +00:00
|
|
|
|
import errorOverlayMiddleware from './lib/error-overlay-middleware'
|
2018-02-14 15:17:41 +00:00
|
|
|
|
import del from 'del'
|
2018-07-24 09:24:40 +00:00
|
|
|
|
import onDemandEntryHandler, {normalizePage} from './on-demand-entry-handler'
|
2018-01-30 15:40:52 +00:00
|
|
|
|
import webpack from 'webpack'
|
2018-06-14 17:30:14 +00:00
|
|
|
|
import getBaseWebpackConfig from '../build/webpack'
|
2018-10-01 22:55:31 +00:00
|
|
|
|
import {IS_BUNDLED_PAGE_REGEX, ROUTE_NAME_REGEX, BLOCKED_PAGES, CLIENT_STATIC_FILES_PATH} from 'next-server/constants'
|
|
|
|
|
import {route} from 'next-server/dist/server/router'
|
|
|
|
|
import {renderScriptError} from 'next-server/dist/server/render'
|
|
|
|
|
|
|
|
|
|
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 11:45:42 +00:00
|
|
|
|
const matchNextPageBundleRequest = route('/_next/static/:buildId/pages/:path*.js(.map)?')
|
2018-07-24 09:24:40 +00: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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function erroredPages (compilation, options = {enhanceName: (name) => name}) {
|
|
|
|
|
const failedPages = {}
|
|
|
|
|
for (const error of compilation.errors) {
|
|
|
|
|
const entryModule = findEntryModule(error.origin)
|
|
|
|
|
const {name} = entryModule
|
|
|
|
|
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 15:07:20 +00:00
|
|
|
|
|
2016-10-14 15:05:08 +00:00
|
|
|
|
export default class HotReloader {
|
2018-07-25 11:45:42 +00:00
|
|
|
|
constructor (dir, { config, buildId } = {}) {
|
2018-06-25 21:06:46 +00:00
|
|
|
|
this.buildId = buildId
|
2016-10-17 07:05:46 +00:00
|
|
|
|
this.dir = dir
|
2016-11-23 18:32:49 +00:00
|
|
|
|
this.middlewares = []
|
|
|
|
|
this.webpackDevMiddleware = null
|
|
|
|
|
this.webpackHotMiddleware = null
|
2016-10-31 10:51:03 +00:00
|
|
|
|
this.initialized = false
|
2016-10-19 12:41:45 +00:00
|
|
|
|
this.stats = null
|
|
|
|
|
this.compilationErrors = null
|
2016-10-31 10:51:03 +00:00
|
|
|
|
this.prevChunkNames = null
|
|
|
|
|
this.prevFailedChunkNames = null
|
2016-11-24 14:03:16 +00:00
|
|
|
|
this.prevChunkHashes = null
|
2018-07-24 09:24:40 +00:00
|
|
|
|
this.serverPrevDocumentHash = null
|
2017-02-26 19:45:16 +00:00
|
|
|
|
|
2018-02-14 15:17:41 +00:00
|
|
|
|
this.config = config
|
2016-10-17 07:05:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 11:45:42 +00:00
|
|
|
|
async run (req, res, parsedUrl) {
|
2018-01-30 15:40:52 +00: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 11:45:42 +00: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
|
|
|
|
|
const handlePageBundleRequest = async (req, res, parsedUrl) => {
|
|
|
|
|
const {pathname} = parsedUrl
|
|
|
|
|
const params = matchNextPageBundleRequest(pathname)
|
|
|
|
|
if (!params) {
|
|
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (params.buildId !== this.buildId) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const page = `/${params.path.join('/')}`
|
|
|
|
|
if (BLOCKED_PAGES.indexOf(page) === -1) {
|
|
|
|
|
try {
|
|
|
|
|
await this.ensurePage(page)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
await renderScriptError(req, res, page, error)
|
|
|
|
|
return {finished: true}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const errors = await this.getCompilationErrors(page)
|
|
|
|
|
if (errors.length > 0) {
|
|
|
|
|
await renderScriptError(req, res, page, errors[0])
|
|
|
|
|
return {finished: true}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {finished} = await handlePageBundleRequest(req, res, parsedUrl)
|
|
|
|
|
|
2016-11-23 18:32:49 +00:00
|
|
|
|
for (const fn of this.middlewares) {
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
|
fn(req, res, (err) => {
|
2017-01-08 02:02:29 +00:00
|
|
|
|
if (err) return reject(err)
|
2016-11-23 18:32:49 +00:00
|
|
|
|
resolve()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-07-25 11:45:42 +00:00
|
|
|
|
|
|
|
|
|
return {finished}
|
2016-10-17 07:05:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 15:17:41 +00:00
|
|
|
|
async clean () {
|
|
|
|
|
return del(join(this.dir, this.config.distDir), { force: true })
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 07:05:46 +00:00
|
|
|
|
async start () {
|
2018-02-14 15:17:41 +00:00
|
|
|
|
await this.clean()
|
2018-01-30 15:40:52 +00:00
|
|
|
|
|
|
|
|
|
const configs = await Promise.all([
|
2018-06-25 21:06:46 +00:00
|
|
|
|
getBaseWebpackConfig(this.dir, { dev: true, isServer: false, config: this.config, buildId: this.buildId }),
|
|
|
|
|
getBaseWebpackConfig(this.dir, { dev: true, isServer: true, config: this.config, buildId: this.buildId })
|
2016-12-16 20:33:08 +00:00
|
|
|
|
])
|
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
const multiCompiler = webpack(configs)
|
2018-01-30 15:40:52 +00:00
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
const buildTools = await this.prepareBuildTools(multiCompiler)
|
2017-06-06 22:32:02 +00:00
|
|
|
|
this.assignBuildTools(buildTools)
|
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
|
this.stats = (await this.waitUntilValid()).stats[0]
|
2016-10-17 07:05:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 22:32:02 +00:00
|
|
|
|
async stop (webpackDevMiddleware) {
|
|
|
|
|
const middleware = webpackDevMiddleware || this.webpackDevMiddleware
|
|
|
|
|
if (middleware) {
|
2016-12-17 04:04:40 +00:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-06-06 22:32:02 +00:00
|
|
|
|
middleware.close((err) => {
|
2017-01-08 02:02:29 +00:00
|
|
|
|
if (err) return reject(err)
|
2016-12-17 04:04:40 +00:00
|
|
|
|
resolve()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 22:32:02 +00:00
|
|
|
|
async reload () {
|
|
|
|
|
this.stats = null
|
|
|
|
|
|
2018-02-14 15:17:41 +00:00
|
|
|
|
await this.clean()
|
2018-01-30 15:40:52 +00:00
|
|
|
|
|
|
|
|
|
const configs = await Promise.all([
|
2018-07-25 11:45:42 +00:00
|
|
|
|
getBaseWebpackConfig(this.dir, { dev: true, isServer: false, config: this.config, buildId: this.buildId }),
|
|
|
|
|
getBaseWebpackConfig(this.dir, { dev: true, isServer: true, config: this.config, buildId: this.buildId })
|
2017-06-06 22:32:02 +00:00
|
|
|
|
])
|
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
|
const compiler = webpack(configs)
|
|
|
|
|
|
2017-06-06 22:32:02 +00: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,
|
|
|
|
|
webpackHotMiddleware,
|
2018-08-24 14:30:41 +00:00
|
|
|
|
errorOverlayMiddleware,
|
2017-06-06 22:32:02 +00:00
|
|
|
|
onDemandEntries.middleware()
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
async prepareBuildTools (multiCompiler) {
|
|
|
|
|
// 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 07:22:15 +00:00
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
const {compilation} = stats
|
|
|
|
|
|
|
|
|
|
// 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 11:45:42 +00:00
|
|
|
|
const documentChunk = compilation.chunks.find(c => c.name === normalize(`static/${this.buildId}/pages/_document.js`))
|
2018-07-24 09:24:40 +00: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)
|
|
|
|
|
this.send('reload', '/_document')
|
|
|
|
|
|
|
|
|
|
this.serverPrevDocumentHash = documentChunk.hash
|
2016-10-15 19:49:42 +00:00
|
|
|
|
})
|
2016-10-14 15:05:08 +00:00
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
multiCompiler.compilers[0].hooks.done.tap('NextjsHotReloaderForClient', (stats) => {
|
2016-10-31 10:51:03 +00:00
|
|
|
|
const { compilation } = stats
|
2017-06-05 15:07:20 +00:00
|
|
|
|
const chunkNames = new Set(
|
|
|
|
|
compilation.chunks
|
|
|
|
|
.map((c) => c.name)
|
2018-06-14 17:30:14 +00:00
|
|
|
|
.filter(name => IS_BUNDLED_PAGE_REGEX.test(name))
|
2017-06-05 15:07:20 +00:00
|
|
|
|
)
|
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
const failedChunkNames = new Set(Object.keys(erroredPages(compilation)))
|
2016-10-31 10:51:03 +00:00
|
|
|
|
|
2017-06-05 15:07:20 +00:00
|
|
|
|
const chunkHashes = new Map(
|
|
|
|
|
compilation.chunks
|
2018-06-14 17:30:14 +00:00
|
|
|
|
.filter(c => IS_BUNDLED_PAGE_REGEX.test(c.name))
|
2017-06-05 15:07:20 +00:00
|
|
|
|
.map((c) => [c.name, c.hash])
|
|
|
|
|
)
|
2016-11-24 14:03:16 +00:00
|
|
|
|
|
2016-10-31 10:51:03 +00:00
|
|
|
|
if (this.initialized) {
|
|
|
|
|
// detect chunks which have to be replaced with a new template
|
|
|
|
|
// e.g, pages/index.js <-> pages/_error.js
|
|
|
|
|
const added = diff(chunkNames, this.prevChunkNames)
|
|
|
|
|
const removed = diff(this.prevChunkNames, chunkNames)
|
|
|
|
|
const succeeded = diff(this.prevFailedChunkNames, failedChunkNames)
|
|
|
|
|
|
|
|
|
|
// reload all failed chunks to replace the templace to the error ones,
|
|
|
|
|
// and to update error content
|
|
|
|
|
const failed = failedChunkNames
|
2016-10-24 15:20:50 +00:00
|
|
|
|
|
2018-07-25 11:45:42 +00:00
|
|
|
|
const rootDir = join(CLIENT_STATIC_FILES_PATH, this.buildId, 'pages')
|
2016-10-24 15:20:50 +00:00
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
for (const n of new Set([...added, ...succeeded, ...removed, ...failed])) {
|
2016-10-24 15:20:50 +00:00
|
|
|
|
const route = toRoute(relative(rootDir, n))
|
|
|
|
|
this.send('reload', route)
|
2016-10-24 07:22:15 +00:00
|
|
|
|
}
|
2016-11-24 14:03:16 +00:00
|
|
|
|
|
2018-05-15 22:36:24 +00:00
|
|
|
|
let changedPageRoutes = []
|
|
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
|
for (const [n, hash] of chunkHashes) {
|
|
|
|
|
if (!this.prevChunkHashes.has(n)) continue
|
|
|
|
|
if (this.prevChunkHashes.get(n) === hash) continue
|
|
|
|
|
|
|
|
|
|
const route = toRoute(relative(rootDir, n))
|
|
|
|
|
|
2018-05-15 22:36:24 +00:00
|
|
|
|
changedPageRoutes.push(route)
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 17:32:16 +00:00
|
|
|
|
// This means `/_app` is most likely included in the list, or a page was added/deleted in this compilation run.
|
|
|
|
|
// This means we should filter out `/_app` because `/_app` will be re-rendered with the page reload.
|
|
|
|
|
if (added.size !== 0 || removed.size !== 0 || changedPageRoutes.length > 1) {
|
2018-05-16 08:08:18 +00:00
|
|
|
|
changedPageRoutes = changedPageRoutes.filter((route) => route !== '/_app' && route !== '/_document')
|
2018-05-15 22:36:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const changedPageRoute of changedPageRoutes) {
|
2016-11-24 14:03:16 +00:00
|
|
|
|
// notify change to recover from runtime errors
|
2018-05-15 22:36:24 +00:00
|
|
|
|
this.send('change', changedPageRoute)
|
2016-11-24 14:03:16 +00:00
|
|
|
|
}
|
2016-10-24 07:22:15 +00:00
|
|
|
|
}
|
2016-10-24 15:20:50 +00:00
|
|
|
|
|
2016-10-31 10:51:03 +00:00
|
|
|
|
this.initialized = true
|
|
|
|
|
this.stats = stats
|
|
|
|
|
this.compilationErrors = null
|
|
|
|
|
this.prevChunkNames = chunkNames
|
|
|
|
|
this.prevFailedChunkNames = failedChunkNames
|
2016-11-24 14:03:16 +00:00
|
|
|
|
this.prevChunkHashes = chunkHashes
|
2016-10-19 12:41:45 +00:00
|
|
|
|
})
|
|
|
|
|
|
2018-10-28 22:01:45 +00:00
|
|
|
|
// We don’t watch .git/ .next/ and node_modules for changes
|
2017-01-19 07:09:40 +00:00
|
|
|
|
const ignored = [
|
2018-10-28 22:01:45 +00:00
|
|
|
|
/[\\/]\.git[\\/]/,
|
|
|
|
|
/[\\/]\.next[\\/]/,
|
|
|
|
|
/[\\/]node_modules[\\/]/
|
2017-01-19 07:09:40 +00:00
|
|
|
|
]
|
2016-11-28 14:04:59 +00:00
|
|
|
|
|
2017-05-13 23:44:21 +00:00
|
|
|
|
let webpackDevMiddlewareConfig = {
|
2018-07-24 09:24:40 +00:00
|
|
|
|
publicPath: `/_next/static/webpack`,
|
2016-10-17 07:05:46 +00:00
|
|
|
|
noInfo: true,
|
2018-07-24 09:24:40 +00:00
|
|
|
|
logLevel: 'silent',
|
2017-06-30 06:50:18 +00:00
|
|
|
|
watchOptions: { ignored }
|
2017-05-13 23:44:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.config.webpackDevMiddleware) {
|
2017-05-31 08:06:07 +00:00
|
|
|
|
console.log(`> Using "webpackDevMiddleware" config function defined in ${this.config.configOrigin}.`)
|
2017-05-13 23:44:21 +00:00
|
|
|
|
webpackDevMiddlewareConfig = this.config.webpackDevMiddleware(webpackDevMiddlewareConfig)
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
const webpackDevMiddleware = WebpackDevMiddleware(multiCompiler, webpackDevMiddlewareConfig)
|
2016-11-23 18:32:49 +00:00
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
const webpackHotMiddleware = WebpackHotMiddleware(multiCompiler.compilers[0], {
|
2017-04-07 17:58:35 +00:00
|
|
|
|
path: '/_next/webpack-hmr',
|
2017-06-05 09:02:13 +00:00
|
|
|
|
log: false,
|
|
|
|
|
heartbeat: 2500
|
2017-04-07 17:58:35 +00:00
|
|
|
|
})
|
2018-01-30 15:40:52 +00:00
|
|
|
|
|
2018-09-16 14:06:02 +00:00
|
|
|
|
const onDemandEntries = onDemandEntryHandler(webpackDevMiddleware, multiCompiler, {
|
2017-02-26 19:45:16 +00:00
|
|
|
|
dir: this.dir,
|
2018-07-25 11:45:42 +00:00
|
|
|
|
buildId: this.buildId,
|
2017-02-26 19:45:16 +00:00
|
|
|
|
dev: true,
|
2017-06-06 22:32:02 +00:00
|
|
|
|
reload: this.reload.bind(this),
|
2018-02-14 15:20:41 +00:00
|
|
|
|
pageExtensions: this.config.pageExtensions,
|
2017-02-26 19:45:16 +00:00
|
|
|
|
...this.config.onDemandEntries
|
|
|
|
|
})
|
2016-11-23 18:32:49 +00:00
|
|
|
|
|
2017-06-06 22:32:02 +00:00
|
|
|
|
return {
|
|
|
|
|
webpackDevMiddleware,
|
|
|
|
|
webpackHotMiddleware,
|
|
|
|
|
onDemandEntries
|
|
|
|
|
}
|
2016-10-14 15:05:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 22:32:02 +00:00
|
|
|
|
waitUntilValid (webpackDevMiddleware) {
|
|
|
|
|
const middleware = webpackDevMiddleware || this.webpackDevMiddleware
|
2016-10-19 12:41:45 +00:00
|
|
|
|
return new Promise((resolve) => {
|
2017-06-06 22:32:02 +00:00
|
|
|
|
middleware.waitUntilValid(resolve)
|
2016-10-14 15:05:08 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
async getCompilationErrors (page) {
|
|
|
|
|
const normalizedPage = normalizePage(page)
|
2017-06-06 22:32:02 +00:00
|
|
|
|
// When we are reloading, we need to wait until it's reloaded properly.
|
|
|
|
|
await this.onDemandEntries.waitUntilReloaded()
|
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
if (this.stats.hasErrors()) {
|
|
|
|
|
const {compilation} = this.stats
|
|
|
|
|
const failedPages = erroredPages(compilation, {
|
|
|
|
|
enhanceName (name) {
|
|
|
|
|
return '/' + ROUTE_NAME_REGEX.exec(name)[1]
|
2016-10-19 12:41:45 +00:00
|
|
|
|
}
|
2018-07-24 09:24:40 +00: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 12:41:45 +00:00
|
|
|
|
}
|
2018-07-24 09:24:40 +00:00
|
|
|
|
|
|
|
|
|
// If none were found we still have to show the other errors
|
|
|
|
|
return this.stats.compilation.errors
|
2016-10-19 12:41:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
|
return []
|
2016-10-19 12:41:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-23 18:32:49 +00:00
|
|
|
|
send (action, ...args) {
|
|
|
|
|
this.webpackHotMiddleware.publish({ action, data: args })
|
2016-10-24 07:22:15 +00:00
|
|
|
|
}
|
2017-02-26 19:45:16 +00:00
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
|
async ensurePage (page) {
|
2018-05-15 22:36:24 +00:00
|
|
|
|
// Make sure we don't re-build or dispose prebuilt pages
|
|
|
|
|
if (page === '/_error' || page === '/_document' || page === '/_app') {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-01-30 15:40:52 +00:00
|
|
|
|
await this.onDemandEntries.ensurePage(page)
|
2017-02-26 19:45:16 +00:00
|
|
|
|
}
|
2016-10-14 15:05:08 +00:00
|
|
|
|
}
|
2016-10-19 12:41:45 +00:00
|
|
|
|
|
2016-10-24 07:22:15 +00:00
|
|
|
|
function diff (a, b) {
|
|
|
|
|
return new Set([...a].filter((v) => !b.has(v)))
|
|
|
|
|
}
|
2016-10-24 15:20:50 +00:00
|
|
|
|
|
|
|
|
|
function toRoute (file) {
|
|
|
|
|
const f = sep === '\\' ? file.replace(/\\/g, '/') : file
|
|
|
|
|
return ('/' + f).replace(/(\/index)?\.js$/, '') || '/'
|
|
|
|
|
}
|