797dabe351
Currently `new URL()` for server assets is completely broken because of the `publicPath` that is used for them too. `new URL()` for SSR is broken on windows as it's using absolute urls on the windows filesystem. And `new URL()` is using an incorrect filename * Place all `asset`s correctly in `/_next/static/media` with `[name].[hash:8][ext]` * Added a separate runtime chunk for api entries, without `publicPath` * Introduce separate layer for api entries, which uses server-side URLs. * Otherwise new URL() will return a faked relative URL, that is identical in SSR and CSR * Disables react-refresh for api entries Fixes #27413 ## Bug - [ ] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes
86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
import {
|
|
webpack,
|
|
isWebpack5,
|
|
sources,
|
|
} from 'next/dist/compiled/webpack/webpack'
|
|
import { PAGES_MANIFEST } from '../../../shared/lib/constants'
|
|
import getRouteFromEntrypoint from '../../../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.includes('webpack-api-runtime') &&
|
|
file.endsWith('.js')
|
|
)
|
|
|
|
if (!isWebpack5 && 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[files.length - 1]
|
|
|
|
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)
|
|
})
|
|
}
|
|
}
|