|
| 1 | +import DynamicEntryPlugin from 'webpack/lib/DynamicEntryPlugin' |
| 2 | +import { EventEmitter } from 'events' |
| 3 | +import { join } from 'path' |
| 4 | +import { parse } from 'url' |
| 5 | +import resolvePath from './resolve' |
| 6 | +import touch from 'touch' |
| 7 | + |
| 8 | +const ADDED = Symbol() |
| 9 | +const BUILDING = Symbol() |
| 10 | +const BUILT = Symbol() |
| 11 | + |
| 12 | +export default function onDemandEntryHandler (devMiddleware, compiler, { |
| 13 | + dir, |
| 14 | + dev, |
| 15 | + maxInactiveAge = 1000 * 25 |
| 16 | +}) { |
| 17 | + const entries = {} |
| 18 | + const lastAccessPages = [''] |
| 19 | + const doneCallbacks = new EventEmitter() |
| 20 | + let touchedAPage = false |
| 21 | + |
| 22 | + compiler.plugin('make', function (compilation, done) { |
| 23 | + const allEntries = Object.keys(entries).map((page) => { |
| 24 | + const { name, entry } = entries[page] |
| 25 | + entries[page].status = BUILDING |
| 26 | + return addEntry(compilation, this.context, name, entry) |
| 27 | + }) |
| 28 | + |
| 29 | + Promise.all(allEntries) |
| 30 | + .then(() => done()) |
| 31 | + .catch(done) |
| 32 | + }) |
| 33 | + |
| 34 | + compiler.plugin('done', function (stats) { |
| 35 | + // Call all the doneCallbacks |
| 36 | + Object.keys(entries).forEach((page) => { |
| 37 | + const entryInfo = entries[page] |
| 38 | + if (entryInfo.status !== BUILDING) return |
| 39 | + |
| 40 | + // With this, we are triggering a filesystem based watch trigger |
| 41 | + // It'll memorize some timestamp related info related to common files used |
| 42 | + // in the page |
| 43 | + // That'll reduce the page building time significantly. |
| 44 | + if (!touchedAPage) { |
| 45 | + setTimeout(() => { |
| 46 | + touch.sync(entryInfo.pathname) |
| 47 | + }, 0) |
| 48 | + touchedAPage = true |
| 49 | + } |
| 50 | + |
| 51 | + entryInfo.status = BUILT |
| 52 | + entries[page].lastActiveTime = Date.now() |
| 53 | + doneCallbacks.emit(page) |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + setInterval(function () { |
| 58 | + disposeInactiveEntries(devMiddleware, entries, lastAccessPages, maxInactiveAge) |
| 59 | + }, 5000) |
| 60 | + |
| 61 | + return { |
| 62 | + async ensurePage (page) { |
| 63 | + page = normalizePage(page) |
| 64 | + |
| 65 | + const pagePath = join(dir, 'pages', page) |
| 66 | + const pathname = await resolvePath(pagePath) |
| 67 | + const name = join('bundles', pathname.substring(dir.length)) |
| 68 | + |
| 69 | + const entry = [ |
| 70 | + join(__dirname, '..', 'client/webpack-hot-middleware-client'), |
| 71 | + join(__dirname, '..', 'client', 'on-demand-entries-client'), |
| 72 | + `${pathname}?entry` |
| 73 | + ] |
| 74 | + |
| 75 | + await new Promise((resolve, reject) => { |
| 76 | + const entryInfo = entries[page] |
| 77 | + |
| 78 | + if (entryInfo) { |
| 79 | + if (entryInfo.status === BUILT) { |
| 80 | + resolve() |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + if (entryInfo.status === BUILDING) { |
| 85 | + doneCallbacks.on(page, processCallback) |
| 86 | + return |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + console.log(`> Building page: ${page}`) |
| 91 | + |
| 92 | + entries[page] = { name, entry, pathname, status: ADDED } |
| 93 | + doneCallbacks.on(page, processCallback) |
| 94 | + |
| 95 | + devMiddleware.invalidate() |
| 96 | + |
| 97 | + function processCallback (err) { |
| 98 | + if (err) return reject(err) |
| 99 | + resolve() |
| 100 | + } |
| 101 | + }) |
| 102 | + }, |
| 103 | + |
| 104 | + middleware () { |
| 105 | + return function (req, res, next) { |
| 106 | + if (!/^\/on-demand-entries-ping/.test(req.url)) return next() |
| 107 | + |
| 108 | + const { query } = parse(req.url, true) |
| 109 | + const page = normalizePage(query.page) |
| 110 | + const entryInfo = entries[page] |
| 111 | + |
| 112 | + // If there's no entry. |
| 113 | + // Then it seems like an weird issue. |
| 114 | + if (!entryInfo) { |
| 115 | + const message = `Client pings, but there's no entry for page: ${page}` |
| 116 | + console.error(message) |
| 117 | + sendJson(res, { invalid: true }) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + sendJson(res, { success: true }) |
| 122 | + |
| 123 | + // We don't need to maintain active state of anything other than BUILT entries |
| 124 | + if (entryInfo.status !== BUILT) return |
| 125 | + |
| 126 | + // If there's an entryInfo |
| 127 | + lastAccessPages.pop() |
| 128 | + lastAccessPages.unshift(page) |
| 129 | + entryInfo.lastActiveTime = Date.now() |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +function addEntry (compilation, context, name, entry) { |
| 136 | + return new Promise((resolve, reject) => { |
| 137 | + const dep = DynamicEntryPlugin.createDependency(entry, name) |
| 138 | + compilation.addEntry(context, dep, name, (err) => { |
| 139 | + if (err) return reject(err) |
| 140 | + resolve() |
| 141 | + }) |
| 142 | + }) |
| 143 | +} |
| 144 | + |
| 145 | +function disposeInactiveEntries (devMiddleware, entries, lastAccessPages, maxInactiveAge) { |
| 146 | + const disposingPages = [] |
| 147 | + |
| 148 | + Object.keys(entries).forEach((page) => { |
| 149 | + const { lastActiveTime, status } = entries[page] |
| 150 | + |
| 151 | + // This means this entry is currently building or just added |
| 152 | + // We don't need to dispose those entries. |
| 153 | + if (status !== BUILT) return |
| 154 | + |
| 155 | + // We should not build the last accessed page even we didn't get any pings |
| 156 | + // Sometimes, it's possible our XHR ping to wait before completing other requests. |
| 157 | + // In that case, we should not dispose the current viewing page |
| 158 | + if (lastAccessPages[0] === page) return |
| 159 | + |
| 160 | + if (Date.now() - lastActiveTime > maxInactiveAge) { |
| 161 | + disposingPages.push(page) |
| 162 | + } |
| 163 | + }) |
| 164 | + |
| 165 | + if (disposingPages.length > 0) { |
| 166 | + disposingPages.forEach((page) => { |
| 167 | + delete entries[page] |
| 168 | + }) |
| 169 | + console.log(`> Disposing inactive page(s): ${disposingPages.join(', ')}`) |
| 170 | + devMiddleware.invalidate() |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// /index and / is the same. So, we need to identify both pages as the same. |
| 175 | +// This also applies to sub pages as well. |
| 176 | +function normalizePage (page) { |
| 177 | + return page.replace(/\/index$/, '/') |
| 178 | +} |
| 179 | + |
| 180 | +function sendJson (res, payload) { |
| 181 | + res.setHeader('Content-Type', 'application/json') |
| 182 | + res.status = 200 |
| 183 | + res.end(JSON.stringify(payload)) |
| 184 | +} |
0 commit comments