2018-03-30 13:08:09 +00:00
|
|
|
import { RawSource } from 'webpack-sources'
|
2018-10-01 22:55:31 +00:00
|
|
|
import {PAGES_MANIFEST, ROUTE_NAME_REGEX} from 'next-server/constants'
|
2018-03-30 13:08:09 +00:00
|
|
|
|
|
|
|
// This plugin creates a pages-manifest.json from page entrypoints.
|
2018-07-25 11:45:42 +00:00
|
|
|
// This is used for mapping paths like `/` to `.next/server/static/<buildid>/pages/index.js` when doing SSR
|
2018-03-30 13:08:09 +00:00
|
|
|
// It's also used by next export to provide defaultPathMap
|
|
|
|
export default class PagesManifestPlugin {
|
2018-11-21 15:04:37 +00:00
|
|
|
apply (compiler) {
|
2018-09-16 14:06:02 +00:00
|
|
|
compiler.hooks.emit.tap('NextJsPagesManifest', (compilation) => {
|
2018-03-30 13:08:09 +00:00
|
|
|
const {entries} = compilation
|
|
|
|
const pages = {}
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
2018-06-14 17:30:14 +00:00
|
|
|
const result = ROUTE_NAME_REGEX.exec(entry.name)
|
2018-04-02 19:18:33 +00:00
|
|
|
if (!result) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const pagePath = result[1]
|
2018-03-30 13:08:09 +00:00
|
|
|
|
|
|
|
if (!pagePath) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const {name} = entry
|
|
|
|
pages[`/${pagePath.replace(/\\/g, '/')}`] = name
|
|
|
|
}
|
|
|
|
|
2018-05-02 17:37:52 +00:00
|
|
|
if (typeof pages['/index'] !== 'undefined') {
|
|
|
|
pages['/'] = pages['/index']
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:08:09 +00:00
|
|
|
compilation.assets[PAGES_MANIFEST] = new RawSource(JSON.stringify(pages))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|