Unverified Commit 23bfce62 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

refactor: next-flight-client-module-loader return conditions (#64348)

* Early return if the `this._module` doesn't exist, aligning with the
type
* If the source is changed, we should return the changed source
otherwise the indexes in source map will be wrong unless the modified
code is appended to the source code, but in this loader it's a different
case

Closes NEXT-3075
parent 774563f2
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import { getRSCModuleInformation } from '../../analysis/get-page-static-info' import { getRSCModuleInformation } from '../../analysis/get-page-static-info'
import { getModuleBuildInfo } from './get-module-build-info' import { getModuleBuildInfo } from './get-module-build-info'
export default function transformSource( const flightClientModuleLoader: webpack.LoaderDefinitionFunction =
this: any, function transformSource(this, source: string, sourceMap: any) {
source: string, // Avoid buffer to be consumed
sourceMap: any if (typeof source !== 'string') {
) { throw new Error('Expected source to have been transformed to a string.')
// Avoid buffer to be consumed }
if (typeof source !== 'string') {
throw new Error('Expected source to have been transformed to a string.')
}
// Assign the RSC meta information to buildInfo. if (!this._module) {
const buildInfo = getModuleBuildInfo(this._module) return source
buildInfo.rsc = getRSCModuleInformation(source, false) }
// Assign the RSC meta information to buildInfo.
const buildInfo = getModuleBuildInfo(this._module)
buildInfo.rsc = getRSCModuleInformation(source, false)
// This is a server action entry module in the client layer. We need to attach // This is a server action entry module in the client layer. We need to attach
// noop exports of `callServer` wrappers for each action. // noop exports of `callServer` wrappers for each action.
if (buildInfo.rsc.actions) { if (buildInfo.rsc.actions) {
source = ` return `
import { callServer } from 'next/dist/client/app-call-server' import { callServer } from 'next/dist/client/app-call-server'
function __build_action__(action, args) { function __build_action__(action, args) {
...@@ -27,7 +28,9 @@ function __build_action__(action, args) { ...@@ -27,7 +28,9 @@ function __build_action__(action, args) {
${source} ${source}
` `
}
return this.callback(null, source, sourceMap)
} }
return this.callback(null, source, sourceMap) export default flightClientModuleLoader
}
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