Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate functions manifest #33770

Merged
merged 5 commits into from Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/next/build/webpack-config.ts
Expand Up @@ -48,6 +48,7 @@ import { regexLikeCss } from './webpack/config/blocks/css'
import { CopyFilePlugin } from './webpack/plugins/copy-file-plugin'
import { FlightManifestPlugin } from './webpack/plugins/flight-manifest-plugin'
import { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'
import FunctionsManifestPlugin from './webpack/plugins/functions-manifest-plugin'
import type { Span } from '../trace'
import { getRawPageExtensions } from './utils'
import browserslist from 'next/dist/compiled/browserslist'
Expand Down Expand Up @@ -1460,6 +1461,9 @@ export default async function getBaseWebpackConfig(
// replacement is done before its process.env.* handling
(!isServer || webServerRuntime) &&
new MiddlewarePlugin({ dev, webServerRuntime }),
process.env.ENABLE_FILE_SYSTEM_API === '1' &&
webServerRuntime &&
new FunctionsManifestPlugin({ dev, webServerRuntime }),
isServer && new NextJsSsrImportPlugin(),
!isServer &&
new BuildManifestPlugin({
Expand Down
@@ -0,0 +1,68 @@
import { sources, webpack5 } from 'next/dist/compiled/webpack/webpack'
import { collectAssets, getEntrypointInfo } from './middleware-plugin'
import { FUNCTIONS_MANIFEST } from '../../../shared/lib/constants'

const PLUGIN_NAME = 'FunctionsManifestPlugin'
export interface FunctionsManifest {
version: 1
pages: {
[page: string]: {
runtime: string
env: string[]
files: string[]
name: string
page: string
regexp: string
}
}
}

export default class FunctionsManifestPlugin {
dev: boolean
webServerRuntime: boolean

constructor({
dev,
webServerRuntime,
}: {
dev: boolean
webServerRuntime: boolean
}) {
this.dev = dev
this.webServerRuntime = webServerRuntime
}

createAssets(
compilation: webpack5.Compilation,
assets: any,
envPerRoute: Map<string, string[]>,
webServerRuntime: boolean
) {
const functionsManifest: FunctionsManifest = {
version: 1,
pages: {},
}

const infos = getEntrypointInfo(compilation, envPerRoute, webServerRuntime)
infos.forEach((info) => {
functionsManifest.pages[info.page] = {
runtime: 'web',
...info,
}
})

const assetPath =
(this.webServerRuntime ? '' : 'server/') + FUNCTIONS_MANIFEST
assets[assetPath] = new sources.RawSource(
JSON.stringify(functionsManifest, null, 2)
)
}

apply(compiler: webpack5.Compiler) {
collectAssets(compiler, this.createAssets.bind(this), {
dev: this.dev,
pluginName: PLUGIN_NAME,
webServerRuntime: this.webServerRuntime,
})
}
}