diff --git a/packages/next/build/webpack/loaders/next-flight-client-loader.ts b/packages/next/build/webpack/loaders/next-flight-client-loader.ts index b1b9fdf4ff2b..ee60abb516d1 100644 --- a/packages/next/build/webpack/loaders/next-flight-client-loader.ts +++ b/packages/next/build/webpack/loaders/next-flight-client-loader.ts @@ -6,7 +6,7 @@ */ import { parse } from '../../swc' -import { buildExports, isEsmNode } from './utils' +import { buildExports } from './utils' function addExportNames(names: string[], node: any) { switch (node.type) { @@ -44,15 +44,13 @@ async function parseModuleInfo( resourcePath: string, transformedSource: string, names: Array -): Promise<{ isEsm: boolean }> { +): Promise { const { body } = await parse(transformedSource, { filename: resourcePath, isModule: true, }) - let isEsm = false for (let i = 0; i < body.length; i++) { const node = body[i] - isEsm = isEsm || isEsmNode(node) switch (node.type) { // TODO: support export * from module path // case 'ExportAllDeclaration': @@ -101,7 +99,6 @@ async function parseModuleInfo( break } } - return { isEsm } } export default async function transformSource( @@ -116,11 +113,7 @@ export default async function transformSource( } const names: string[] = [] - const { isEsm } = await parseModuleInfo( - resourcePath, - transformedSource, - names - ) + await parseModuleInfo(resourcePath, transformedSource, names) // next.js/packages/next/.js if (/[\\/]next[\\/](link|image)\.js$/.test(resourcePath)) { @@ -136,11 +129,12 @@ export default async function transformSource( JSON.stringify(resourcePath) + ', name: ' + JSON.stringify(name) + - '};\n' + ' };\n' res[name] = moduleRef return res }, {}) - const output = moduleRefDef + buildExports(clientRefsExports, isEsm) + // still generate module references in ESM + const output = moduleRefDef + buildExports(clientRefsExports, true) return output } diff --git a/packages/next/build/webpack/loaders/next-flight-server-loader.ts b/packages/next/build/webpack/loaders/next-flight-server-loader.ts index afda6ae7c9b7..b87f234690b1 100644 --- a/packages/next/build/webpack/loaders/next-flight-server-loader.ts +++ b/packages/next/build/webpack/loaders/next-flight-server-loader.ts @@ -1,6 +1,6 @@ import { parse } from '../../swc' import { getRawPageExtensions } from '../../utils' -import { buildExports, isEsmNode } from './utils' +import { buildExports, isEsmNodeType } from './utils' const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif'] @@ -51,7 +51,7 @@ async function parseModuleInfo({ for (let i = 0; i < body.length; i++) { const node = body[i] - isEsm = isEsm || isEsmNode(node) + isEsm = isEsm || isEsmNodeType(node.type) switch (node.type) { case 'ImportDeclaration': { const importSource = node.source.value @@ -182,7 +182,7 @@ export default async function transformSource( const rscExports: any = { __next_rsc__: `{ __webpack_require__, - _: () => {${imports}} + _: () => {\n${imports}\n} }`, } diff --git a/packages/next/build/webpack/loaders/utils.ts b/packages/next/build/webpack/loaders/utils.ts index f3e83553ad7a..c0628c568154 100644 --- a/packages/next/build/webpack/loaders/utils.ts +++ b/packages/next/build/webpack/loaders/utils.ts @@ -18,4 +18,4 @@ const esmNodeTypes = [ 'ExportDefaultExpression', 'ExportDefaultDeclaration', ] -export const isEsmNode = (node: any) => esmNodeTypes.includes(node.type) +export const isEsmNodeType = (type: string) => esmNodeTypes.includes(type)