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,
sourceMap: any
) {
// Avoid buffer to be consumed // Avoid buffer to be consumed
if (typeof source !== 'string') { if (typeof source !== 'string') {
throw new Error('Expected source to have been transformed to a string.') throw new Error('Expected source to have been transformed to a string.')
} }
if (!this._module) {
return source
}
// Assign the RSC meta information to buildInfo. // Assign the RSC meta information to buildInfo.
const buildInfo = getModuleBuildInfo(this._module) const buildInfo = getModuleBuildInfo(this._module)
buildInfo.rsc = getRSCModuleInformation(source, false) buildInfo.rsc = getRSCModuleInformation(source, false)
...@@ -18,7 +19,7 @@ export default function transformSource( ...@@ -18,7 +19,7 @@ export default function transformSource(
// 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) {
...@@ -30,4 +31,6 @@ ${source} ...@@ -30,4 +31,6 @@ ${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