File size: 1,880 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import {
APP_BUILD_MANIFEST,
CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
SYSTEM_ENTRYPOINTS,
} 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.make.tap(PLUGIN_NAME, (compilation: any) => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
() => this.createAsset(compilation)
)
})
}
private createAsset(compilation: webpack.Compilation) {
const manifest: AppBuildManifest = {
pages: {},
}
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 (SYSTEM_ENTRYPOINTS.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)
compilation.emitAsset(
APP_BUILD_MANIFEST,
new sources.RawSource(json) as unknown as webpack.sources.RawSource
)
}
}
|