2019-06-24 15:48:26 +02:00
|
|
|
import { Compiler, Plugin } from 'webpack'
|
2018-03-30 15:08:09 +02:00
|
|
|
import { RawSource } from 'webpack-sources'
|
2020-06-04 19:32:45 +02:00
|
|
|
import { PAGES_MANIFEST } from '../../../next-server/lib/constants'
|
|
|
|
import getRouteFromEntrypoint from '../../../next-server/server/get-route-from-entrypoint'
|
2018-03-30 15:08:09 +02:00
|
|
|
|
2020-05-25 23:15:56 +02:00
|
|
|
export type PagesManifest = { [page: string]: string }
|
|
|
|
|
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 {
|
2019-05-22 18:36:53 +02:00
|
|
|
serverless: boolean
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
constructor(serverless: boolean) {
|
2019-05-22 18:36:53 +02:00
|
|
|
this.serverless = serverless
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
apply(compiler: Compiler): void {
|
2020-05-18 21:24:37 +02:00
|
|
|
compiler.hooks.emit.tap('NextJsPagesManifest', (compilation) => {
|
2020-06-11 10:57:24 +02:00
|
|
|
const entrypoints = compilation.entrypoints
|
2020-05-25 23:15:56 +02:00
|
|
|
const pages: PagesManifest = {}
|
2018-03-30 15:08:09 +02:00
|
|
|
|
2020-06-11 10:57:24 +02:00
|
|
|
for (const entrypoint of entrypoints.values()) {
|
|
|
|
const pagePath = getRouteFromEntrypoint(
|
|
|
|
entrypoint.name,
|
|
|
|
this.serverless
|
|
|
|
)
|
2018-03-30 15:08:09 +02:00
|
|
|
|
|
|
|
if (!pagePath) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-06-11 10:57:24 +02:00
|
|
|
const files = entrypoint
|
|
|
|
.getFiles()
|
2020-07-01 17:34:00 +02:00
|
|
|
.filter(
|
|
|
|
(file: string) =>
|
|
|
|
!file.includes('webpack-runtime') && file.endsWith('.js')
|
|
|
|
)
|
2020-06-11 10:57:24 +02:00
|
|
|
|
|
|
|
if (files.length > 1) {
|
|
|
|
console.log(
|
|
|
|
`Found more than one file in server entrypoint ${entrypoint.name}`,
|
|
|
|
files
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:14:29 +01:00
|
|
|
// Write filename, replace any backslashes in path (on windows) with forwardslashes for cross-platform consistency.
|
2020-06-11 10:57:24 +02:00
|
|
|
pages[pagePath] = files[0].replace(/\\/g, '/')
|
2018-05-02 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 19:32:45 +02:00
|
|
|
compilation.assets[PAGES_MANIFEST] = new RawSource(
|
|
|
|
JSON.stringify(pages, null, 2)
|
|
|
|
)
|
2018-03-30 15:08:09 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|