2017-02-26 19:45:16 +00:00
|
|
|
import DynamicEntryPlugin from 'webpack/lib/DynamicEntryPlugin'
|
|
|
|
import { EventEmitter } from 'events'
|
2018-02-14 15:20:41 +00:00
|
|
|
import { join } from 'path'
|
2017-02-26 19:45:16 +00:00
|
|
|
import { parse } from 'url'
|
|
|
|
import touch from 'touch'
|
2018-06-14 17:30:14 +00:00
|
|
|
import promisify from '../lib/promisify'
|
2018-03-30 14:59:42 +00:00
|
|
|
import globModule from 'glob'
|
2018-02-14 15:20:41 +00:00
|
|
|
import {normalizePagePath, pageNotFoundError} from './require'
|
2018-06-14 17:30:14 +00:00
|
|
|
import {createEntry} from '../build/webpack/utils'
|
|
|
|
import { ROUTE_NAME_REGEX, IS_BUNDLED_PAGE_REGEX } from '../lib/constants'
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2017-03-03 00:17:41 +00:00
|
|
|
const ADDED = Symbol('added')
|
|
|
|
const BUILDING = Symbol('building')
|
|
|
|
const BUILT = Symbol('built')
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2018-03-30 14:59:42 +00:00
|
|
|
const glob = promisify(globModule)
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
export default function onDemandEntryHandler (devMiddleware, compilers, {
|
2017-02-26 19:45:16 +00:00
|
|
|
dir,
|
|
|
|
dev,
|
2017-06-06 22:32:02 +00:00
|
|
|
reload,
|
2018-02-14 15:20:41 +00:00
|
|
|
pageExtensions,
|
2018-01-30 15:40:52 +00:00
|
|
|
maxInactiveAge = 1000 * 60,
|
2017-09-28 12:51:03 +00:00
|
|
|
pagesBufferLength = 2
|
2017-02-26 19:45:16 +00:00
|
|
|
}) {
|
2017-06-06 22:32:02 +00:00
|
|
|
let entries = {}
|
|
|
|
let lastAccessPages = ['']
|
|
|
|
let doneCallbacks = new EventEmitter()
|
2017-02-27 20:05:10 +00:00
|
|
|
const invalidator = new Invalidator(devMiddleware)
|
2017-02-26 19:45:16 +00:00
|
|
|
let touchedAPage = false
|
2017-06-06 22:32:02 +00:00
|
|
|
let reloading = false
|
|
|
|
let stopped = false
|
|
|
|
let reloadCallbacks = new EventEmitter()
|
2018-01-30 15:40:52 +00:00
|
|
|
// Keep the names of compilers which are building pages at a given moment.
|
|
|
|
const currentBuilders = new Set()
|
|
|
|
|
|
|
|
compilers.forEach(compiler => {
|
|
|
|
compiler.plugin('make', function (compilation, done) {
|
|
|
|
invalidator.startBuilding()
|
|
|
|
currentBuilders.add(compiler.name)
|
|
|
|
|
|
|
|
const allEntries = Object.keys(entries).map((page) => {
|
|
|
|
const { name, entry } = entries[page]
|
|
|
|
entries[page].status = BUILDING
|
2018-02-19 07:49:59 +00:00
|
|
|
return addEntry(compilation, compiler.context, name, entry)
|
2018-01-30 15:40:52 +00:00
|
|
|
})
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
Promise.all(allEntries)
|
|
|
|
.then(() => done())
|
|
|
|
.catch(done)
|
2017-02-26 19:45:16 +00:00
|
|
|
})
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
compiler.plugin('done', function (stats) {
|
|
|
|
// Wait until all the compilers mark the build as done.
|
|
|
|
currentBuilders.delete(compiler.name)
|
|
|
|
if (currentBuilders.size !== 0) return
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
const { compilation } = stats
|
|
|
|
const hardFailedPages = compilation.errors
|
|
|
|
.filter(e => {
|
|
|
|
// Make sure to only pick errors which marked with missing modules
|
|
|
|
const hasNoModuleFoundError = /ENOENT/.test(e.message) || /Module not found/.test(e.message)
|
|
|
|
if (!hasNoModuleFoundError) return false
|
2017-06-06 22:32:02 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
// The page itself is missing. So this is a failed page.
|
2018-06-14 17:30:14 +00:00
|
|
|
if (IS_BUNDLED_PAGE_REGEX.test(e.module.name)) return true
|
2017-06-06 22:32:02 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
// No dependencies means this is a top level page.
|
|
|
|
// So this is a failed page.
|
|
|
|
return e.module.dependencies.length === 0
|
|
|
|
})
|
|
|
|
.map(e => e.module.chunks)
|
|
|
|
.reduce((a, b) => [...a, ...b], [])
|
|
|
|
.map(c => {
|
2018-06-14 17:30:14 +00:00
|
|
|
const pageName = ROUTE_NAME_REGEX.exec(c.name)[1]
|
2018-01-30 15:40:52 +00:00
|
|
|
return normalizePage(`/${pageName}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Call all the doneCallbacks
|
|
|
|
Object.keys(entries).forEach((page) => {
|
|
|
|
const entryInfo = entries[page]
|
|
|
|
if (entryInfo.status !== BUILDING) return
|
|
|
|
|
|
|
|
// With this, we are triggering a filesystem based watch trigger
|
|
|
|
// It'll memorize some timestamp related info related to common files used
|
|
|
|
// in the page
|
|
|
|
// That'll reduce the page building time significantly.
|
|
|
|
if (!touchedAPage) {
|
|
|
|
setTimeout(() => {
|
|
|
|
touch.sync(entryInfo.pathname)
|
|
|
|
}, 1000)
|
|
|
|
touchedAPage = true
|
|
|
|
}
|
|
|
|
|
|
|
|
entryInfo.status = BUILT
|
|
|
|
entries[page].lastActiveTime = Date.now()
|
|
|
|
doneCallbacks.emit(page)
|
2017-06-06 22:32:02 +00:00
|
|
|
})
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
invalidator.doneBuilding(compiler.name)
|
|
|
|
|
|
|
|
if (hardFailedPages.length > 0 && !reloading) {
|
|
|
|
console.log(`> Reloading webpack due to inconsistant state of pages(s): ${hardFailedPages.join(', ')}`)
|
|
|
|
reloading = true
|
|
|
|
reload()
|
|
|
|
.then(() => {
|
|
|
|
console.log('> Webpack reloaded.')
|
|
|
|
reloadCallbacks.emit('done')
|
|
|
|
stop()
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(`> Webpack reloading failed: ${err.message}`)
|
|
|
|
console.error(err.stack)
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2017-02-26 19:45:16 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-06-06 22:32:02 +00:00
|
|
|
const disposeHandler = setInterval(function () {
|
|
|
|
if (stopped) return
|
2017-02-26 19:45:16 +00:00
|
|
|
disposeInactiveEntries(devMiddleware, entries, lastAccessPages, maxInactiveAge)
|
|
|
|
}, 5000)
|
|
|
|
|
2018-01-31 11:37:41 +00:00
|
|
|
disposeHandler.unref()
|
|
|
|
|
2017-06-06 22:32:02 +00:00
|
|
|
function stop () {
|
|
|
|
clearInterval(disposeHandler)
|
|
|
|
stopped = true
|
|
|
|
doneCallbacks = null
|
|
|
|
reloadCallbacks = null
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:45:16 +00:00
|
|
|
return {
|
2017-06-06 22:32:02 +00:00
|
|
|
waitUntilReloaded () {
|
|
|
|
if (!reloading) return Promise.resolve(true)
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
reloadCallbacks.once('done', function () {
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2017-02-26 19:45:16 +00:00
|
|
|
async ensurePage (page) {
|
2017-06-06 22:32:02 +00:00
|
|
|
await this.waitUntilReloaded()
|
2017-02-26 19:45:16 +00:00
|
|
|
page = normalizePage(page)
|
2018-02-14 15:20:41 +00:00
|
|
|
let normalizedPagePath
|
|
|
|
try {
|
|
|
|
normalizedPagePath = normalizePagePath(page)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
throw pageNotFoundError(normalizedPagePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
const extensions = pageExtensions.join('|')
|
|
|
|
const paths = await glob(`pages/{${normalizedPagePath}/index,${normalizedPagePath}}.+(${extensions})`, {cwd: dir})
|
|
|
|
|
|
|
|
if (paths.length === 0) {
|
|
|
|
throw pageNotFoundError(normalizedPagePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
const relativePathToPage = paths[0]
|
|
|
|
|
|
|
|
const pathname = join(dir, relativePathToPage)
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2018-02-14 15:20:41 +00:00
|
|
|
const {name, files} = createEntry(relativePathToPage, {pageExtensions: extensions})
|
2017-02-26 19:45:16 +00:00
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
const entryInfo = entries[page]
|
|
|
|
|
|
|
|
if (entryInfo) {
|
|
|
|
if (entryInfo.status === BUILT) {
|
|
|
|
resolve()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entryInfo.status === BUILDING) {
|
2018-01-30 15:40:52 +00:00
|
|
|
doneCallbacks.once(page, handleCallback)
|
2017-02-26 19:45:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`> Building page: ${page}`)
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
entries[page] = { name, entry: files, pathname, status: ADDED }
|
|
|
|
doneCallbacks.once(page, handleCallback)
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2017-02-27 20:05:10 +00:00
|
|
|
invalidator.invalidate()
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
function handleCallback (err) {
|
2017-02-26 19:45:16 +00:00
|
|
|
if (err) return reject(err)
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
middleware () {
|
2017-06-06 22:32:02 +00:00
|
|
|
return (req, res, next) => {
|
|
|
|
if (stopped) {
|
|
|
|
// If this handler is stopped, we need to reload the user's browser.
|
|
|
|
// So the user could connect to the actually running handler.
|
|
|
|
res.statusCode = 302
|
|
|
|
res.setHeader('Location', req.url)
|
|
|
|
res.end('302')
|
|
|
|
} else if (reloading) {
|
|
|
|
// Webpack config is reloading. So, we need to wait until it's done and
|
|
|
|
// reload user's browser.
|
|
|
|
// So the user could connect to the new handler and webpack setup.
|
|
|
|
this.waitUntilReloaded()
|
|
|
|
.then(() => {
|
|
|
|
res.statusCode = 302
|
|
|
|
res.setHeader('Location', req.url)
|
|
|
|
res.end('302')
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if (!/^\/_next\/on-demand-entries-ping/.test(req.url)) return next()
|
|
|
|
|
|
|
|
const { query } = parse(req.url, true)
|
|
|
|
const page = normalizePage(query.page)
|
|
|
|
const entryInfo = entries[page]
|
|
|
|
|
|
|
|
// If there's no entry.
|
|
|
|
// Then it seems like an weird issue.
|
|
|
|
if (!entryInfo) {
|
|
|
|
const message = `Client pings, but there's no entry for page: ${page}`
|
|
|
|
console.error(message)
|
|
|
|
sendJson(res, { invalid: true })
|
|
|
|
return
|
|
|
|
}
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2017-06-06 22:32:02 +00:00
|
|
|
sendJson(res, { success: true })
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2017-06-06 22:32:02 +00:00
|
|
|
// We don't need to maintain active state of anything other than BUILT entries
|
|
|
|
if (entryInfo.status !== BUILT) return
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2017-06-06 22:32:02 +00:00
|
|
|
// If there's an entryInfo
|
2017-09-28 12:51:03 +00:00
|
|
|
if (!lastAccessPages.includes(page)) {
|
|
|
|
lastAccessPages.unshift(page)
|
|
|
|
|
|
|
|
// Maintain the buffer max length
|
|
|
|
if (lastAccessPages.length > pagesBufferLength) lastAccessPages.pop()
|
|
|
|
}
|
2017-06-06 22:32:02 +00:00
|
|
|
entryInfo.lastActiveTime = Date.now()
|
|
|
|
}
|
2017-02-26 19:45:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
// Based on https://github.com/webpack/webpack/blob/master/lib/DynamicEntryPlugin.js#L29-L37
|
2017-02-26 19:45:16 +00:00
|
|
|
function addEntry (compilation, context, name, entry) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const dep = DynamicEntryPlugin.createDependency(entry, name)
|
|
|
|
compilation.addEntry(context, dep, name, (err) => {
|
|
|
|
if (err) return reject(err)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function disposeInactiveEntries (devMiddleware, entries, lastAccessPages, maxInactiveAge) {
|
|
|
|
const disposingPages = []
|
|
|
|
|
|
|
|
Object.keys(entries).forEach((page) => {
|
|
|
|
const { lastActiveTime, status } = entries[page]
|
|
|
|
|
|
|
|
// This means this entry is currently building or just added
|
|
|
|
// We don't need to dispose those entries.
|
|
|
|
if (status !== BUILT) return
|
|
|
|
|
|
|
|
// We should not build the last accessed page even we didn't get any pings
|
|
|
|
// Sometimes, it's possible our XHR ping to wait before completing other requests.
|
|
|
|
// In that case, we should not dispose the current viewing page
|
2017-09-28 12:51:03 +00:00
|
|
|
if (lastAccessPages.includes(page)) return
|
2017-02-26 19:45:16 +00:00
|
|
|
|
|
|
|
if (Date.now() - lastActiveTime > maxInactiveAge) {
|
|
|
|
disposingPages.push(page)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (disposingPages.length > 0) {
|
|
|
|
disposingPages.forEach((page) => {
|
|
|
|
delete entries[page]
|
|
|
|
})
|
|
|
|
console.log(`> Disposing inactive page(s): ${disposingPages.join(', ')}`)
|
|
|
|
devMiddleware.invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// /index and / is the same. So, we need to identify both pages as the same.
|
|
|
|
// This also applies to sub pages as well.
|
|
|
|
function normalizePage (page) {
|
|
|
|
return page.replace(/\/index$/, '/')
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendJson (res, payload) {
|
|
|
|
res.setHeader('Content-Type', 'application/json')
|
|
|
|
res.status = 200
|
|
|
|
res.end(JSON.stringify(payload))
|
|
|
|
}
|
2017-02-27 20:05:10 +00:00
|
|
|
|
|
|
|
// Make sure only one invalidation happens at a time
|
|
|
|
// Otherwise, webpack hash gets changed and it'll force the client to reload.
|
|
|
|
class Invalidator {
|
|
|
|
constructor (devMiddleware) {
|
|
|
|
this.devMiddleware = devMiddleware
|
2018-01-30 15:40:52 +00:00
|
|
|
// contains an array of types of compilers currently building
|
2017-02-27 20:05:10 +00:00
|
|
|
this.building = false
|
|
|
|
this.rebuildAgain = false
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidate () {
|
|
|
|
// If there's a current build is processing, we won't abort it by invalidating.
|
|
|
|
// (If aborted, it'll cause a client side hard reload)
|
|
|
|
// But let it to invalidate just after the completion.
|
|
|
|
// So, it can re-build the queued pages at once.
|
|
|
|
if (this.building) {
|
|
|
|
this.rebuildAgain = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.building = true
|
|
|
|
this.devMiddleware.invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
startBuilding () {
|
|
|
|
this.building = true
|
|
|
|
}
|
|
|
|
|
|
|
|
doneBuilding () {
|
|
|
|
this.building = false
|
2018-01-30 15:40:52 +00:00
|
|
|
|
2017-02-27 20:05:10 +00:00
|
|
|
if (this.rebuildAgain) {
|
|
|
|
this.rebuildAgain = false
|
|
|
|
this.invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|