2019-01-08 22:10:32 +00:00
|
|
|
|
import { join, 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-12-14 11:25:59 +00:00
|
|
|
|
import WebSocket from 'ws'
|
2018-12-03 13:18:52 +00:00
|
|
|
|
import getBaseWebpackConfig from '../build/webpack-config'
|
2019-01-08 22:10:32 +00:00
|
|
|
|
import {IS_BUNDLED_PAGE_REGEX, ROUTE_NAME_REGEX, BLOCKED_PAGES} from 'next-server/constants'
|
2018-10-01 22:55:31 +00:00
|
|
|
|
import {route} from 'next-server/dist/server/router'
|
2019-01-08 22:10:32 +00:00
|
|
|
|
import globModule from 'glob'
|
|
|
|
|
import {promisify} from 'util'
|
|
|
|
|
import {createPagesMapping, createEntrypoints} from '../build/entries'
|
|
|
|
|
|
|
|
|
|
const glob = promisify(globModule)
|
2018-12-06 15:47:10 +00: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-01 22:55:31 +00: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 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
|
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
|
2019-01-08 22:10:32 +00:00
|
|
|
|
const handlePageBundleRequest = async (res, parsedUrl) => {
|
2018-07-25 11:45:42 +00:00
|
|
|
|
const {pathname} = parsedUrl
|
|
|
|
|
const params = matchNextPageBundleRequest(pathname)
|
|
|
|
|
if (!params) {
|
|
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (params.buildId !== this.buildId) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const page = `/${params.path.join('/')}`
|
2019-02-03 14:34:28 +00:00
|
|
|
|
if (page === '/_error' || BLOCKED_PAGES.indexOf(page) === -1) {
|
2018-07-25 11:45:42 +00:00
|
|
|
|
try {
|
|
|
|
|
await this.ensurePage(page)
|
|
|
|
|
} catch (error) {
|
2018-12-06 15:47:10 +00:00
|
|
|
|
await renderScriptError(res, error)
|
2018-07-25 11:45:42 +00:00
|
|
|
|
return {finished: true}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const errors = await this.getCompilationErrors(page)
|
|
|
|
|
if (errors.length > 0) {
|
2018-12-06 15:47:10 +00:00
|
|
|
|
await renderScriptError(res, errors[0])
|
2018-07-25 11:45:42 +00:00
|
|
|
|
return {finished: true}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:10:32 +00:00
|
|
|
|
const {finished} = await handlePageBundleRequest(res, parsedUrl)
|
2018-07-25 11:45:42 +00:00
|
|
|
|
|
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 })
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-19 12:39:09 +00:00
|
|
|
|
addWsConfig (configs) {
|
|
|
|
|
const { websocketProxyPath, websocketProxyPort } = this.config.onDemandEntries
|
|
|
|
|
const opts = {
|
|
|
|
|
'process.env.NEXT_WS_PORT': websocketProxyPort || this.wsPort,
|
|
|
|
|
'process.env.NEXT_WS_PROXY_PATH': JSON.stringify(websocketProxyPath)
|
|
|
|
|
}
|
|
|
|
|
configs[0].plugins.push(new webpack.DefinePlugin(opts))
|
2018-12-14 11:25:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:10:32 +00:00
|
|
|
|
async getWebpackConfig () {
|
2019-02-03 14:34:28 +00:00
|
|
|
|
const pagePaths = await glob(`+(_app|_document).+(${this.config.pageExtensions.join('|')})`, {cwd: join(this.dir, 'pages')})
|
2019-01-08 22:10:32 +00:00
|
|
|
|
const pages = createPagesMapping(pagePaths, this.config.pageExtensions)
|
|
|
|
|
const entrypoints = createEntrypoints(pages, 'server', this.buildId, this.config)
|
|
|
|
|
return Promise.all([
|
|
|
|
|
getBaseWebpackConfig(this.dir, { dev: true, isServer: false, config: this.config, buildId: this.buildId, entrypoints: entrypoints.client }),
|
|
|
|
|
getBaseWebpackConfig(this.dir, { dev: true, isServer: true, config: this.config, buildId: this.buildId, entrypoints: entrypoints.server })
|
|
|
|
|
])
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2018-12-16 00:56:27 +00:00
|
|
|
|
this.wsPort = await new Promise((resolve, reject) => {
|
2019-01-01 00:07:10 +00:00
|
|
|
|
const { websocketPort } = this.config.onDemandEntries
|
|
|
|
|
// create on-demand-entries WebSocket
|
|
|
|
|
this.wss = new WebSocket.Server({ port: websocketPort }, function (err) {
|
2018-12-16 00:56:27 +00:00
|
|
|
|
if (err) {
|
|
|
|
|
return reject(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {port} = this.address()
|
|
|
|
|
if (!port) {
|
|
|
|
|
return reject(new Error('No websocket port could be detected'))
|
|
|
|
|
}
|
|
|
|
|
resolve(port)
|
2018-12-14 11:25:59 +00:00
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2019-01-08 22:10:32 +00:00
|
|
|
|
const configs = await this.getWebpackConfig()
|
2019-01-19 12:39:09 +00:00
|
|
|
|
this.addWsConfig(configs)
|
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) {
|
2018-12-14 11:25:59 +00:00
|
|
|
|
this.wss.close()
|
2017-06-06 22:32:02 +00:00
|
|
|
|
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
|
|
|
|
|
2019-01-08 22:10:32 +00:00
|
|
|
|
const configs = await this.getWebpackConfig()
|
2019-01-19 12:39:09 +00:00
|
|
|
|
this.addWsConfig(configs)
|
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
|
2018-12-14 11:25:59 +00:00
|
|
|
|
this.wss.on('connection', this.onDemandEntries.wsConnection)
|
2017-06-06 22:32:02 +00:00
|
|
|
|
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)
|
2019-01-08 22:10:32 +00:00
|
|
|
|
this.send('reloadPage')
|
2018-07-24 09:24:40 +00:00
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
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
|
2019-01-08 22:10:32 +00: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 07:22:15 +00:00
|
|
|
|
}
|
2016-11-24 14:03:16 +00:00
|
|
|
|
|
2019-01-08 22:10:32 +00: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 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.prevChunkNames = chunkNames
|
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',
|
2018-11-02 18:47:56 +00:00
|
|
|
|
watchOptions: { ignored },
|
|
|
|
|
writeToDisk: true
|
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-06-06 22:32:02 +00:00
|
|
|
|
reload: this.reload.bind(this),
|
2018-02-14 15:20:41 +00:00
|
|
|
|
pageExtensions: this.config.pageExtensions,
|
2018-12-14 11:25:59 +00:00
|
|
|
|
wsPort: this.wsPort,
|
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
|
2019-02-03 14:34:28 +00:00
|
|
|
|
if (page !== '/_error' && BLOCKED_PAGES.indexOf(page) !== -1) {
|
2018-05-15 22:36:24 +00:00
|
|
|
|
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)))
|
|
|
|
|
}
|