2020-08-03 14:26:23 +02:00
|
|
|
import webpack, { Compiler, Plugin } from 'webpack'
|
2020-08-22 14:16:13 +02:00
|
|
|
import sources 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 }
|
|
|
|
|
2020-08-22 14:16:13 +02:00
|
|
|
// @ts-ignore: TODO: remove ignore when webpack 5 is stable
|
|
|
|
const { RawSource } = webpack.sources || sources
|
|
|
|
|
2020-08-03 14:26:23 +02:00
|
|
|
const isWebpack5 = parseInt(webpack.version!) === 5
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:26:23 +02:00
|
|
|
createAssets(compilation: any, assets: any) {
|
|
|
|
const entrypoints = compilation.entrypoints
|
|
|
|
const pages: PagesManifest = {}
|
|
|
|
|
|
|
|
for (const entrypoint of entrypoints.values()) {
|
|
|
|
const pagePath = getRouteFromEntrypoint(entrypoint.name, this.serverless)
|
|
|
|
|
|
|
|
if (!pagePath) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const files = entrypoint
|
|
|
|
.getFiles()
|
|
|
|
.filter(
|
|
|
|
(file: string) =>
|
|
|
|
!file.includes('webpack-runtime') && file.endsWith('.js')
|
2020-06-11 10:57:24 +02:00
|
|
|
)
|
2018-03-30 15:08:09 +02:00
|
|
|
|
2020-08-03 14:26:23 +02:00
|
|
|
if (files.length > 1) {
|
|
|
|
console.log(
|
|
|
|
`Found more than one file in server entrypoint ${entrypoint.name}`,
|
|
|
|
files
|
|
|
|
)
|
|
|
|
continue
|
2018-05-02 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 14:26:23 +02:00
|
|
|
// Write filename, replace any backslashes in path (on windows) with forwardslashes for cross-platform consistency.
|
|
|
|
pages[pagePath] = files[0].replace(/\\/g, '/')
|
|
|
|
}
|
|
|
|
|
|
|
|
assets[PAGES_MANIFEST] = new RawSource(JSON.stringify(pages, null, 2))
|
|
|
|
}
|
|
|
|
|
|
|
|
apply(compiler: Compiler): void {
|
|
|
|
if (isWebpack5) {
|
|
|
|
compiler.hooks.make.tap('NextJsPagesManifest', (compilation) => {
|
|
|
|
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
|
|
|
|
compilation.hooks.processAssets.tap(
|
|
|
|
{
|
|
|
|
name: 'NextJsPagesManifest',
|
|
|
|
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
|
|
|
|
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
|
|
|
|
},
|
|
|
|
(assets: any) => {
|
|
|
|
this.createAssets(compilation, assets)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
compiler.hooks.emit.tap('NextJsPagesManifest', (compilation: any) => {
|
|
|
|
this.createAssets(compilation, compilation.assets)
|
2018-03-30 15:08:09 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|