b80fdfb828
This updates to output server chunks to a nested folder to prevent bundling the entire folder when tracing. This also fixes the webpack 5 tests not actually using webpack 5 since https://github.com/vercel/next.js/pull/22583 since the webpack 5 enabling check didn't account for the test environment variable used to enable webpack 5. This also clears up some deprecation warnings from webpack 5 in the mini-css-extract-plugin. Fixes: https://github.com/vercel/next.js/issues/21297
85 lines
2.6 KiB
TypeScript
85 lines
2.6 KiB
TypeScript
import {
|
|
webpack,
|
|
isWebpack5,
|
|
sources,
|
|
} from 'next/dist/compiled/webpack/webpack'
|
|
import { PAGES_MANIFEST } from '../../../next-server/lib/constants'
|
|
import getRouteFromEntrypoint from '../../../next-server/server/get-route-from-entrypoint'
|
|
|
|
export type PagesManifest = { [page: string]: string }
|
|
|
|
// This plugin creates a pages-manifest.json from page entrypoints.
|
|
// This is used for mapping paths like `/` to `.next/server/static/<buildid>/pages/index.js` when doing SSR
|
|
// It's also used by next export to provide defaultPathMap
|
|
export default class PagesManifestPlugin implements webpack.Plugin {
|
|
serverless: boolean
|
|
dev: boolean
|
|
|
|
constructor({ serverless, dev }: { serverless: boolean; dev: boolean }) {
|
|
this.serverless = serverless
|
|
this.dev = dev
|
|
}
|
|
|
|
createAssets(compilation: any, assets: any) {
|
|
const entrypoints = compilation.entrypoints
|
|
const pages: PagesManifest = {}
|
|
|
|
for (const entrypoint of entrypoints.values()) {
|
|
const pagePath = getRouteFromEntrypoint(entrypoint.name)
|
|
|
|
if (!pagePath) {
|
|
continue
|
|
}
|
|
|
|
const files = entrypoint
|
|
.getFiles()
|
|
.filter(
|
|
(file: string) =>
|
|
!file.includes('webpack-runtime') && file.endsWith('.js')
|
|
)
|
|
|
|
if (files.length > 1) {
|
|
console.log(
|
|
`Found more than one file in server entrypoint ${entrypoint.name}`,
|
|
files
|
|
)
|
|
continue
|
|
}
|
|
|
|
// Write filename, replace any backslashes in path (on windows) with forwardslashes for cross-platform consistency.
|
|
pages[pagePath] = files[0]
|
|
|
|
if (isWebpack5 && !this.dev) {
|
|
pages[pagePath] = pages[pagePath].slice(3)
|
|
}
|
|
pages[pagePath] = pages[pagePath].replace(/\\/g, '/')
|
|
}
|
|
|
|
assets[
|
|
`${isWebpack5 && !this.dev ? '../' : ''}` + PAGES_MANIFEST
|
|
] = new sources.RawSource(JSON.stringify(pages, null, 2))
|
|
}
|
|
|
|
apply(compiler: webpack.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)
|
|
})
|
|
}
|
|
}
|