2021-10-26 18:50:56 +02:00
|
|
|
/**
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
|
2022-05-24 16:54:26 +02:00
|
|
|
import { MIDDLEWARE_FLIGHT_MANIFEST } from '../../../shared/lib/constants'
|
2022-05-10 21:20:13 +02:00
|
|
|
import { clientComponentRegex } from '../loaders/utils'
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
// This is the module that will be used to anchor all client references to.
|
|
|
|
// I.e. it will have all the client files as async deps from this point on.
|
|
|
|
// We use the Flight client implementation because you can't get to these
|
|
|
|
// without the client runtime so it's the first time in the loading sequence
|
|
|
|
// you might want them.
|
|
|
|
// const clientFileName = require.resolve('../');
|
|
|
|
|
|
|
|
type Options = {
|
|
|
|
dev: boolean
|
2022-05-24 16:54:26 +02:00
|
|
|
viewsDir: boolean
|
2022-04-05 21:46:17 +02:00
|
|
|
pageExtensions: string[]
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const PLUGIN_NAME = 'FlightManifestPlugin'
|
|
|
|
|
|
|
|
export class FlightManifestPlugin {
|
|
|
|
dev: boolean = false
|
2022-04-05 21:46:17 +02:00
|
|
|
pageExtensions: string[]
|
2022-05-24 16:54:26 +02:00
|
|
|
viewsDir: boolean = false
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
constructor(options: Options) {
|
|
|
|
if (typeof options.dev === 'boolean') {
|
|
|
|
this.dev = options.dev
|
|
|
|
}
|
2022-05-24 16:54:26 +02:00
|
|
|
this.viewsDir = options.viewsDir
|
2022-04-05 21:46:17 +02:00
|
|
|
this.pageExtensions = options.pageExtensions
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-05-18 13:18:28 +02:00
|
|
|
compiler.hooks.make.tap(PLUGIN_NAME, (compilation: any) => {
|
|
|
|
compilation.hooks.processAssets.tap(
|
|
|
|
{
|
|
|
|
name: PLUGIN_NAME,
|
|
|
|
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
|
|
|
|
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
|
|
|
|
},
|
|
|
|
(assets: any) => this.createAsset(assets, compilation)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2022-05-13 19:48:53 +02:00
|
|
|
|
2021-10-26 23:02:51 +02:00
|
|
|
createAsset(assets: any, compilation: any) {
|
2022-03-08 21:55:14 +01:00
|
|
|
const manifest: any = {}
|
2022-05-24 16:54:26 +02:00
|
|
|
const viewsDir = this.viewsDir
|
|
|
|
|
2021-10-26 18:50:56 +02:00
|
|
|
compilation.chunkGroups.forEach((chunkGroup: any) => {
|
2022-05-24 16:54:26 +02:00
|
|
|
function recordModule(chunk: any, id: string, mod: any) {
|
2022-04-09 03:26:16 +02:00
|
|
|
const resource = mod.resource
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
// TODO: Hook into deps instead of the target module.
|
|
|
|
// That way we know by the type of dep whether to include.
|
|
|
|
// It also resolves conflicts when the same module is in multiple chunks.
|
2022-05-10 21:20:13 +02:00
|
|
|
if (
|
|
|
|
!resource ||
|
|
|
|
!clientComponentRegex.test(resource) ||
|
|
|
|
!clientComponentRegex.test(id)
|
|
|
|
) {
|
2021-10-26 18:50:56 +02:00
|
|
|
return
|
|
|
|
}
|
2022-05-10 21:20:13 +02:00
|
|
|
|
2022-03-08 21:55:14 +01:00
|
|
|
const moduleExports: any = manifest[resource] || {}
|
2021-10-26 18:50:56 +02:00
|
|
|
|
|
|
|
const exportsInfo = compilation.moduleGraph.getExportsInfo(mod)
|
|
|
|
const moduleExportedKeys = ['', '*'].concat(
|
2022-03-02 21:57:50 +01:00
|
|
|
[...exportsInfo.exports]
|
|
|
|
.map((exportInfo) => {
|
|
|
|
if (exportInfo.provided) {
|
|
|
|
return exportInfo.name
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
.filter(Boolean)
|
2021-10-26 18:50:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
moduleExportedKeys.forEach((name) => {
|
|
|
|
if (!moduleExports[name]) {
|
|
|
|
moduleExports[name] = {
|
2022-05-13 19:48:53 +02:00
|
|
|
id: id.replace(/^\(sc_server\)\//, ''),
|
2021-10-26 18:50:56 +02:00
|
|
|
name,
|
2022-05-24 16:54:26 +02:00
|
|
|
chunks: viewsDir ? chunk.ids : [],
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-03-08 21:55:14 +01:00
|
|
|
manifest[resource] = moduleExports
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
chunkGroup.chunks.forEach((chunk: any) => {
|
|
|
|
const chunkModules =
|
|
|
|
compilation.chunkGraph.getChunkModulesIterable(chunk)
|
|
|
|
for (const mod of chunkModules) {
|
2021-10-26 22:24:28 +02:00
|
|
|
let modId = compilation.chunkGraph.getModuleId(mod)
|
2021-10-26 18:50:56 +02:00
|
|
|
|
2022-05-10 21:20:13 +02:00
|
|
|
if (typeof modId !== 'string') continue
|
|
|
|
|
|
|
|
// Remove resource queries.
|
|
|
|
modId = modId.split('?')[0]
|
|
|
|
// Remove the loader prefix.
|
|
|
|
modId = modId.split('next-flight-client-loader.js!')[1] || modId
|
2022-05-24 16:54:26 +02:00
|
|
|
modId = modId.replace(/^\(sc_server\)\//, '')
|
|
|
|
|
|
|
|
recordModule(chunk, modId, mod)
|
2022-05-10 21:20:13 +02:00
|
|
|
|
2021-10-26 18:50:56 +02:00
|
|
|
// If this is a concatenation, register each child to the parent ID.
|
|
|
|
if (mod.modules) {
|
|
|
|
mod.modules.forEach((concatenatedMod: any) => {
|
2022-05-24 16:54:26 +02:00
|
|
|
recordModule(chunk, modId, concatenatedMod)
|
2021-10-26 18:50:56 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-05-24 16:54:26 +02:00
|
|
|
const file = 'server/' + MIDDLEWARE_FLIGHT_MANIFEST
|
|
|
|
const json = JSON.stringify(manifest)
|
2022-03-08 21:55:14 +01:00
|
|
|
|
|
|
|
assets[file + '.js'] = new sources.RawSource('self.__RSC_MANIFEST=' + json)
|
|
|
|
assets[file + '.json'] = new sources.RawSource(json)
|
2021-10-26 18:50:56 +02:00
|
|
|
}
|
|
|
|
}
|