@@ -42,30 +42,30 @@ function isImageImport(importSource: string) {
4242async function parseImportsInfo ( {
4343 resourcePath,
4444 source,
45- imports,
4645 isClientCompilation,
4746 isServerComponent,
4847 isClientComponent,
4948} : {
5049 resourcePath : string
5150 source : string
52- imports : Array < string >
5351 isClientCompilation : boolean
5452 isServerComponent : ( name : string ) => boolean
5553 isClientComponent : ( name : string ) => boolean
5654} ) : Promise < {
5755 source : string
58- defaultExportName : string
56+ imports : string
5957} > {
6058 const opts = getBaseSWCOptions ( {
6159 filename : resourcePath ,
6260 globalWindow : isClientCompilation ,
6361 } )
6462 const ast = await parse ( source , { ...opts . jsc . parser , isModule : true } )
6563 const { body } = ast
64+
6665 let transformedSource = ''
6766 let lastIndex = 0
68- let defaultExportName
67+ let imports = ''
68+
6969 for ( let i = 0 ; i < body . length ; i ++ ) {
7070 const node = body [ i ]
7171 switch ( node . type ) {
@@ -86,7 +86,7 @@ async function parseImportsInfo({
8686 // A client component. It should be loaded as module reference.
8787 transformedSource += importDeclarations
8888 transformedSource += JSON . stringify ( `${ importSource } ?__sc_client__` )
89- imports . push ( `require(${ JSON . stringify ( importSource ) } )` )
89+ imports += `require(${ JSON . stringify ( importSource ) } );`
9090 } else {
9191 // This is a special case to avoid the Duplicate React error.
9292 // Since we already include React in the SSR runtime,
@@ -116,27 +116,12 @@ async function parseImportsInfo({
116116 continue
117117 }
118118
119- imports . push ( `require(${ JSON . stringify ( importSource ) } )` )
119+ imports += `require(${ JSON . stringify ( importSource ) } );`
120120 }
121121
122122 lastIndex = node . source . span . end
123123 break
124124 }
125- case 'ExportDefaultDeclaration' : {
126- const def = node . decl
127- if ( def . type === 'Identifier' ) {
128- defaultExportName = def . name
129- } else if ( def . type === 'FunctionExpression' ) {
130- defaultExportName = def . identifier . value
131- }
132- break
133- }
134- case 'ExportDefaultExpression' :
135- const exp = node . expression
136- if ( exp . type === 'Identifier' ) {
137- defaultExportName = exp . value
138- }
139- break
140125 default :
141126 break
142127 }
@@ -146,7 +131,7 @@ async function parseImportsInfo({
146131 transformedSource += source . substring ( lastIndex )
147132 }
148133
149- return { source : transformedSource , defaultExportName }
134+ return { source : transformedSource , imports }
150135}
151136
152137export default async function transformSource (
@@ -181,44 +166,34 @@ export default async function transformSource(
181166 }
182167 }
183168
184- const imports : string [ ] = [ ]
185- const { source : transformedSource , defaultExportName } =
186- await parseImportsInfo ( {
187- resourcePath,
188- source,
189- imports,
190- isClientCompilation,
191- isServerComponent,
192- isClientComponent,
193- } )
169+ const { source : transformedSource , imports } = await parseImportsInfo ( {
170+ resourcePath,
171+ source,
172+ isClientCompilation,
173+ isServerComponent,
174+ isClientComponent,
175+ } )
194176
195177 /**
196178 * For .server.js files, we handle this loader differently.
197179 *
198180 * Server compilation output:
199- * export default function ServerComponent() { ... }
200- * export const __rsc_noop__ = () => { ... }
201- * ServerComponent.__next_rsc__ = 1
202- * ServerComponent.__webpack_require__ = __webpack_require__
181+ * (The content of the Server Component module will be kept.)
182+ * export const __next_rsc__ = { __webpack_require__, _: () => { ... } }
203183 *
204184 * Client compilation output:
205- * The function body of Server Component will be removed
185+ * (The content of the Server Component module will be removed.)
186+ * export const __next_rsc__ = { __webpack_require__, _: () => { ... } }
206187 */
207188
208- const noop = `export const __rsc_noop__=()=>{${ imports . join ( ';' ) } }`
189+ let rscExports = `export const __next_rsc__={
190+ __webpack_require__,
191+ _: () => {${ imports } }
192+ }`
209193
210- let defaultExportNoop = ''
211194 if ( isClientCompilation ) {
212- defaultExportNoop = `export default function ${
213- defaultExportName || 'ServerComponent'
214- } (){}\n${ defaultExportName || 'ServerComponent' } .__next_rsc__=1;`
215- } else {
216- if ( defaultExportName ) {
217- // It's required to have the default export for pages. For other components, it's fine to leave it as is.
218- defaultExportNoop = `${ defaultExportName } .__next_rsc__=1;${ defaultExportName } .__webpack_require__=__webpack_require__;`
219- }
195+ rscExports += '\nexport default function RSC () {}'
220196 }
221197
222- const transformed = transformedSource + '\n' + noop + '\n' + defaultExportNoop
223- return transformed
198+ return transformedSource + '\n' + rscExports
224199}
0 commit comments