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'
|
2019-05-29 13:57:26 +02:00
|
|
|
import {
|
|
|
|
PAGES_MANIFEST,
|
|
|
|
ROUTE_NAME_REGEX,
|
|
|
|
SERVERLESS_ROUTE_NAME_REGEX,
|
2019-09-04 16:00:54 +02:00
|
|
|
} from '../../../next-server/lib/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 {
|
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 {
|
|
|
|
compiler.hooks.emit.tap('NextJsPagesManifest', compilation => {
|
2019-03-23 15:33:13 +01:00
|
|
|
const { chunks } = compilation
|
2019-05-29 13:57:26 +02:00
|
|
|
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) {
|
2019-05-22 18:36:53 +02:00
|
|
|
const result = (this.serverless
|
2019-05-29 13:57:26 +02:00
|
|
|
? SERVERLESS_ROUTE_NAME_REGEX
|
|
|
|
: ROUTE_NAME_REGEX
|
|
|
|
).exec(chunk.name)
|
2019-05-22 18:36:53 +02:00
|
|
|
|
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-05-29 13:57:26 +02: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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|