Unverified Commit 5b31c18e authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Generate functions manifest (#33770)

## Feature

Reuse most of the part from manifest plugin to generate similar assets

Resolves #33667

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
parent 030666b5
......@@ -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'
......@@ -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({
......
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,
})
}
}
......@@ -12,6 +12,7 @@ export const ROUTES_MANIFEST = 'routes-manifest.json'
export const IMAGES_MANIFEST = 'images-manifest.json'
export const SERVER_FILES_MANIFEST = 'required-server-files.json'
export const DEV_CLIENT_PAGES_MANIFEST = '_devPagesManifest.json'
export const FUNCTIONS_MANIFEST = 'functions-manifest.json'
export const MIDDLEWARE_MANIFEST = 'middleware-manifest.json'
export const DEV_MIDDLEWARE_MANIFEST = '_devMiddlewareManifest.json'
export const REACT_LOADABLE_MANIFEST = 'react-loadable-manifest.json'
......
......@@ -99,8 +99,9 @@ export default function Page404() {
}
`
async function nextBuild(dir) {
async function nextBuild(dir, options) {
return await _nextBuild(dir, [], {
...options,
stdout: true,
stderr: true,
nodeArgs,
......@@ -212,6 +213,38 @@ describe('concurrentFeatures - prod', () => {
runBasicTests(context, 'prod')
})
describe('Functions manifest', () => {
it('should not generate functions manifest when filesystem API is not enabled', async () => {
await nextBuild(appDir)
const functionsManifestPath = join(
distDir,
'server',
'functions-manifest.json'
)
expect(fs.existsSync(functionsManifestPath)).toBe(false)
})
it('should contain rsc paths in functions manifest', async () => {
await nextBuild(appDir, { env: { ENABLE_FILE_SYSTEM_API: '1' } })
const functionsManifestPath = join(
distDir,
'server',
'functions-manifest.json'
)
const content = JSON.parse(await fs.readFile(functionsManifestPath, 'utf8'))
const { pages } = content
const pageNames = Object.keys(pages)
const paths = ['/', '/next-api/link', '/routes/[dynamic]']
paths.forEach((path) => {
expect(pageNames).toContain(path)
expect(pages[path].runtime).toBe('web')
expect(pages[path].files.every((f) => f.startsWith('server/'))).toBe(true)
})
expect(content.version).toBe(1)
})
})
const customAppPageSuite = {
runTests: (context) => {
it('should render container in app', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment