Skip to content

Commit

Permalink
refactor: next-flight-client-module-loader return conditions (#64348)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
huozhi authored Apr 11, 2024
1 parent 774563f commit 23bfce6
Showing 1 changed file with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import { getRSCModuleInformation } from '../../analysis/get-page-static-info'
import { getModuleBuildInfo } from './get-module-build-info'

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

return this.callback(null, source, sourceMap)
}

return this.callback(null, source, sourceMap)
}
export default flightClientModuleLoader

0 comments on commit 23bfce6

Please sign in to comment.