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

Move FlightManifestPlugin to server compilers #36810

Merged
merged 5 commits into from May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 9 additions & 6 deletions packages/next/build/webpack-config.ts
Expand Up @@ -909,9 +909,8 @@ export default async function getBaseWebpackConfig(
},
}

const rscCodeCondition = {
const serverComponentCodeCondition = {
test: serverComponentsRegex,
// only apply to the pages as the begin process of rsc loaders
include: [dir, /next[\\/]dist[\\/]pages/],
}

Expand Down Expand Up @@ -1210,7 +1209,7 @@ export default async function getBaseWebpackConfig(
? [
// RSC server compilation loaders
{
...rscCodeCondition,
...serverComponentCodeCondition,
use: {
loader: 'next-flight-server-loader',
},
Expand All @@ -1219,7 +1218,7 @@ export default async function getBaseWebpackConfig(
: [
// RSC client compilation loaders
{
...rscCodeCondition,
...serverComponentCodeCondition,
use: {
loader: 'next-flight-server-loader',
options: {
Expand Down Expand Up @@ -1589,8 +1588,12 @@ export default async function getBaseWebpackConfig(
},
}),
hasServerComponents &&
isClient &&
new FlightManifestPlugin({ dev, pageExtensions: rawPageExtensions }),
!isClient &&
new FlightManifestPlugin({
dev,
pageExtensions: rawPageExtensions,
isEdgeServer,
}),
!dev &&
isClient &&
new TelemetryPlugin(
Expand Down
25 changes: 22 additions & 3 deletions packages/next/build/webpack/plugins/flight-manifest-plugin.ts
Expand Up @@ -19,20 +19,26 @@ import { createClientComponentFilter } from '../loaders/utils'
type Options = {
dev: boolean
pageExtensions: string[]
isEdgeServer: boolean
}

const PLUGIN_NAME = 'FlightManifestPlugin'

let edgeFlightManifest = {}
let nodeFlightManifest = {}

const isClientComponent = createClientComponentFilter()
export class FlightManifestPlugin {
dev: boolean = false
pageExtensions: string[]
isEdgeServer: boolean

constructor(options: Options) {
if (typeof options.dev === 'boolean') {
this.dev = options.dev
}
this.pageExtensions = options.pageExtensions
this.isEdgeServer = options.isEdgeServer
}

apply(compiler: any) {
Expand Down Expand Up @@ -107,10 +113,14 @@ export class FlightManifestPlugin {
for (const mod of chunkModules) {
let modId = compilation.chunkGraph.getModuleId(mod)

// remove resource query on production
// Clean up the module id.
if (typeof modId === 'string') {
// Remove resource queries.
modId = modId.split('?')[0]
// Remove the loader prefix.
modId = modId.split('next-flight-client-loader.js!')[1]
}

recordModule(modId, chunk, mod)
// If this is a concatenation, register each child to the parent ID.
if (mod.modules) {
Expand All @@ -124,8 +134,17 @@ export class FlightManifestPlugin {

// With switchable runtime, we need to emit the manifest files for both
// runtimes.
const file = `server/${MIDDLEWARE_FLIGHT_MANIFEST}`
const json = JSON.stringify(manifest)
if (this.isEdgeServer) {
edgeFlightManifest = manifest
} else {
nodeFlightManifest = manifest
}
const mergedManifest = {
...nodeFlightManifest,
...edgeFlightManifest,
}
const file = MIDDLEWARE_FLIGHT_MANIFEST
const json = JSON.stringify(mergedManifest)

assets[file + '.js'] = new sources.RawSource('self.__RSC_MANIFEST=' + json)
assets[file + '.json'] = new sources.RawSource(json)
Expand Down