2018-03-30 13:08:09 +00:00
|
|
|
// @flow
|
|
|
|
import { RawSource } from 'webpack-sources'
|
2018-06-16 17:23:02 +00:00
|
|
|
import {PAGES_MANIFEST, ROUTE_NAME_REGEX} from '../../../lib/constants'
|
2018-03-30 13:08:09 +00:00
|
|
|
|
|
|
|
// This plugin creates a pages-manifest.json from page entrypoints.
|
|
|
|
// This is used for mapping paths like `/` to `.next/dist/bundles/pages/index.js` when doing SSR
|
|
|
|
// It's also used by next export to provide defaultPathMap
|
|
|
|
export default class PagesManifestPlugin {
|
|
|
|
apply (compiler: any) {
|
2018-07-24 09:24:40 +00:00
|
|
|
compiler.hooks.emit.tapAsync('NextJsPagesManifest', (compilation, callback) => {
|
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))
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|