2019-03-23 15:33:13 +01:00
|
|
|
import {Compiler, Plugin} from 'webpack'
|
2018-03-30 15:08:09 +02:00
|
|
|
import { RawSource } from 'webpack-sources'
|
2019-02-19 22:45:07 +01:00
|
|
|
import { PAGES_MANIFEST, ROUTE_NAME_REGEX } from 'next-server/constants'
|
2018-03-30 15:08:09 +02:00
|
|
|
|
|
|
|
// This plugin creates a pages-manifest.json from page entrypoints.
|
2018-07-25 13:45:42 +02:00
|
|
|
// This is used for mapping paths like `/` to `.next/server/static/<buildid>/pages/index.js` when doing SSR
|
2018-03-30 15:08:09 +02:00
|
|
|
// It's also used by next export to provide defaultPathMap
|
2019-03-23 15:33:13 +01:00
|
|
|
export default class PagesManifestPlugin implements Plugin {
|
|
|
|
apply (compiler: Compiler): void {
|
2018-09-16 16:06:02 +02:00
|
|
|
compiler.hooks.emit.tap('NextJsPagesManifest', (compilation) => {
|
2019-03-23 15:33:13 +01:00
|
|
|
const { chunks } = compilation
|
|
|
|
const pages: {[page: string]: string} = {}
|
2018-03-30 15:08:09 +02:00
|
|
|
|
2019-03-23 15:33:13 +01:00
|
|
|
for (const chunk of chunks) {
|
|
|
|
const result = ROUTE_NAME_REGEX.exec(chunk.name)
|
2018-04-02 21:18:33 +02:00
|
|
|
if (!result) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const pagePath = result[1]
|
2018-03-30 15:08:09 +02:00
|
|
|
|
|
|
|
if (!pagePath) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:14:29 +01:00
|
|
|
// Write filename, replace any backslashes in path (on windows) with forwardslashes for cross-platform consistency.
|
2019-03-23 15:33:13 +01:00
|
|
|
pages[`/${pagePath.replace(/\\/g, '/')}`] = chunk.name.replace(/\\/g, '/')
|
2018-03-30 15:08:09 +02:00
|
|
|
}
|
|
|
|
|
2018-05-02 19:37:52 +02:00
|
|
|
if (typeof pages['/index'] !== 'undefined') {
|
|
|
|
pages['/'] = pages['/index']
|
|
|
|
}
|
|
|
|
|
2018-03-30 15:08:09 +02:00
|
|
|
compilation.assets[PAGES_MANIFEST] = new RawSource(JSON.stringify(pages))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|