rsnext/packages/next/build/webpack/plugins/app-build-manifest-plugin.ts
zoomdong 78cb07a4ca
chore: remove unsless @ts-ignore (#40992)
## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [x] Errors have a 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`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-09-28 17:53:08 +00:00

96 lines
2.6 KiB
TypeScript

import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import {
APP_BUILD_MANIFEST,
CLIENT_STATIC_FILES_RUNTIME_AMP,
CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
} from '../../../shared/lib/constants'
import { getEntrypointFiles } from './build-manifest-plugin'
import getAppRouteFromEntrypoint from '../../../server/get-app-route-from-entrypoint'
type Options = {
dev: boolean
}
export type AppBuildManifest = {
pages: Record<string, string[]>
}
const PLUGIN_NAME = 'AppBuildManifestPlugin'
export class AppBuildManifestPlugin {
private readonly dev: boolean
constructor(options: Options) {
this.dev = options.dev
}
public apply(compiler: any) {
compiler.hooks.compilation.tap(
PLUGIN_NAME,
(compilation: any, { normalModuleFactory }: any) => {
compilation.dependencyFactories.set(
(webpack as any).dependencies.ModuleDependency,
normalModuleFactory
)
compilation.dependencyTemplates.set(
(webpack as any).dependencies.ModuleDependency,
new (webpack as any).dependencies.NullDependency.Template()
)
}
)
compiler.hooks.make.tap(PLUGIN_NAME, (compilation: any) => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
(assets: any) => this.createAsset(assets, compilation)
)
})
}
private createAsset(assets: any, compilation: webpack.Compilation) {
const manifest: AppBuildManifest = {
pages: {},
}
const systemEntrypoints = new Set<string>([
CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
CLIENT_STATIC_FILES_RUNTIME_AMP,
CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
])
const mainFiles = new Set(
getEntrypointFiles(
compilation.entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_MAIN_APP)
)
)
for (const entrypoint of compilation.entrypoints.values()) {
if (!entrypoint.name) {
continue
}
if (systemEntrypoints.has(entrypoint.name)) {
continue
}
const pagePath = getAppRouteFromEntrypoint(entrypoint.name)
if (!pagePath) {
continue
}
const filesForPage = getEntrypointFiles(entrypoint)
manifest.pages[pagePath] = [...new Set([...mainFiles, ...filesForPage])]
}
const json = JSON.stringify(manifest, null, 2)
assets[APP_BUILD_MANIFEST] = new sources.RawSource(json)
}
}