File size: 8,012 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import type {
  javascript,
  LoaderContext,
  NormalModule,
  webpack,
} from 'next/dist/compiled/webpack/webpack'
import { RSC_MOD_REF_PROXY_ALIAS } from '../../../../lib/constants'
import {
  BARREL_OPTIMIZATION_PREFIX,
  RSC_MODULE_TYPES,
} from '../../../../shared/lib/constants'
import { warnOnce } from '../../../../shared/lib/utils/warn-once'
import { getRSCModuleInformation } from '../../../analysis/get-page-static-info'
import { formatBarrelOptimizedResource } from '../../utils'
import { getModuleBuildInfo } from '../get-module-build-info'
import { ModuleFilenameHelpers } from 'next/dist/compiled/webpack/webpack'

type SourceType = javascript.JavascriptParser['sourceType'] | 'commonjs'

const noopHeadPath = require.resolve('next/dist/client/components/noop-head')
// For edge runtime it will be aliased to esm version by webpack
const MODULE_PROXY_PATH =
  'next/dist/build/webpack/loaders/next-flight-loader/module-proxy'

export function getAssumedSourceType(
  mod: webpack.Module,
  sourceType: SourceType
): SourceType {
  const buildInfo = getModuleBuildInfo(mod)
  const detectedClientEntryType = buildInfo?.rsc?.clientEntryType
  const clientRefs = buildInfo?.rsc?.clientRefs || []

  // It's tricky to detect the type of a client boundary, but we should always
  // use the `module` type when we can, to support `export *` and `export from`
  // syntax in other modules that import this client boundary.

  if (sourceType === 'auto') {
    if (detectedClientEntryType === 'auto') {
      if (clientRefs.length === 0) {
        // If there's zero export detected in the client boundary, and it's the
        // `auto` type, we can safely assume it's a CJS module because it doesn't
        // have ESM exports.
        return 'commonjs'
      } else if (!clientRefs.includes('*')) {
        // Otherwise, we assume it's an ESM module.
        return 'module'
      }
    } else if (detectedClientEntryType === 'cjs') {
      return 'commonjs'
    }
  }

  return sourceType
}

export default function transformSource(
  this: LoaderContext<undefined>,
  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 module = this._module!

  // Assign the RSC meta information to buildInfo.
  // Exclude next internal files which are not marked as client files
  const buildInfo = getModuleBuildInfo(module)
  buildInfo.rsc = getRSCModuleInformation(source, true)
  let prefix = ''
  if (process.env.BUILTIN_FLIGHT_CLIENT_ENTRY_PLUGIN) {
    const rscModuleInformationJson = JSON.stringify(buildInfo.rsc)
    prefix = `/* __rspack_internal_rsc_module_information_do_not_use__ ${rscModuleInformationJson} */\n`
    source = prefix + source
  }
  prefix += `// This file is generated by the Webpack next-flight-loader.\n`

  // Resource key is the unique identifier for the resource. When RSC renders
  // a client module, that key is used to identify that module across all compiler
  // layers.
  //
  // Usually it's the module's file path + the export name (e.g. `foo.js#bar`).
  // But with Barrel Optimizations, one file can be splitted into multiple modules,
  // so when you import `foo.js#bar` and `foo.js#baz`, they are actually different
  // "foo.js" being created by the Barrel Loader (one only exports `bar`, the other
  // only exports `baz`).
  //
  // Because of that, we must add another query param to the resource key to
  // differentiate them.
  let resourceKey: string = this.resourcePath
  if (module.matchResource?.startsWith(BARREL_OPTIMIZATION_PREFIX)) {
    resourceKey = formatBarrelOptimizedResource(
      resourceKey,
      module.matchResource
    )
  }

  // A client boundary.
  if (buildInfo.rsc?.type === RSC_MODULE_TYPES.client) {
    const assumedSourceType = getAssumedSourceType(
      module,
      sourceTypeFromModule(module)
    )

    const clientRefs = buildInfo.rsc.clientRefs!
    const stringifiedResourceKey = JSON.stringify(resourceKey)

    if (assumedSourceType === 'module') {
      if (clientRefs.length === 0) {
        return this.callback(null, 'export {}')
      }

      if (clientRefs.includes('*')) {
        this.callback(
          new Error(
            `It's currently unsupported to use "export *" in a client boundary. Please use named exports instead.`
          )
        )
        return
      }

      let esmSource =
        prefix +
        `\
import { registerClientReference } from "react-server-dom-webpack/server";
`
      for (const ref of clientRefs) {
        if (ref === 'default') {
          esmSource += `export default registerClientReference(
function() { throw new Error(${JSON.stringify(`Attempted to call the default \
export of ${stringifiedResourceKey} from the server, but it's on the client. \
It's not possible to invoke a client function from the server, it can only be \
rendered as a Component or passed to props of a Client Component.`)}); },
${stringifiedResourceKey},
"default",
);\n`
        } else {
          esmSource += `export const ${ref} = registerClientReference(
function() { throw new Error(${JSON.stringify(`Attempted to call ${ref}() from \
the server but ${ref} is on the client. It's not possible to invoke a client \
function from the server, it can only be rendered as a Component or passed to \
props of a Client Component.`)}); },
${stringifiedResourceKey},
${JSON.stringify(ref)},
);`
        }
      }

      const compilation = this._compilation!
      const originalSourceURL = ModuleFilenameHelpers.createFilename(
        module,
        {
          moduleFilenameTemplate:
            'webpack://[namespace]/[resource-path]/__nextjs-internal-proxy.mjs',
          namespace: '_N_E',
        },
        {
          requestShortener: compilation.requestShortener,
          chunkGraph: compilation.chunkGraph,
          hashFunction: compilation.outputOptions.hashFunction,
        }
      )

      return this.callback(null, esmSource, {
        version: 3,
        sources: [originalSourceURL],
        // minimal, parseable mappings
        mappings: 'AAAA',
        sourcesContent: [esmSource],
      })
    } else if (assumedSourceType === 'commonjs') {
      let cjsSource =
        prefix +
        `\
const { createProxy } = require("${MODULE_PROXY_PATH}")

module.exports = createProxy(${stringifiedResourceKey})
`

      const compilation = this._compilation!
      const originalSourceURL = ModuleFilenameHelpers.createFilename(
        module,
        {
          moduleFilenameTemplate:
            'webpack://[namespace]/[resource-path]/__nextjs-internal-proxy.cjs',
          namespace: '_N_E',
        },
        {
          requestShortener: compilation.requestShortener,
          chunkGraph: compilation.chunkGraph,
          hashFunction: compilation.outputOptions.hashFunction,
        }
      )

      return this.callback(null, cjsSource, {
        version: 3,
        sources: [originalSourceURL],
        // minimal, parseable mappings
        mappings: 'AAAA',
        sourcesContent: [cjsSource],
      })
    }
  }

  if (buildInfo.rsc?.type !== RSC_MODULE_TYPES.client) {
    if (noopHeadPath === this.resourcePath) {
      warnOnce(
        `Warning: You're using \`next/head\` inside the \`app\` directory, please migrate to the Metadata API. See https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration#step-3-migrating-nexthead for more details.`
      )
    }
  }

  const replacedSource = source.replace(
    RSC_MOD_REF_PROXY_ALIAS,
    MODULE_PROXY_PATH
  )
  this.callback(null, replacedSource, sourceMap)
}

function sourceTypeFromModule(module: NormalModule): SourceType {
  const moduleType = module.type
  switch (moduleType) {
    case 'javascript/auto':
      return 'auto'
    case 'javascript/dynamic':
      return 'script'
    case 'javascript/esm':
      return 'module'
    default:
      throw new Error('Unexpected module type ' + moduleType)
  }
}