Skip to content

Commit

Permalink
generate esm for module references
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Mar 24, 2022
1 parent 974c8cf commit 38a51c1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
18 changes: 6 additions & 12 deletions packages/next/build/webpack/loaders/next-flight-client-loader.ts
Expand Up @@ -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) {
Expand Down Expand Up @@ -44,15 +44,13 @@ async function parseModuleInfo(
resourcePath: string,
transformedSource: string,
names: Array<string>
): Promise<{ isEsm: boolean }> {
): Promise<void> {
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':
Expand Down Expand Up @@ -101,7 +99,6 @@ async function parseModuleInfo(
break
}
}
return { isEsm }
}

export default async function transformSource(
Expand All @@ -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/<component>.js
if (/[\\/]next[\\/](link|image)\.js$/.test(resourcePath)) {
Expand All @@ -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
}
@@ -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']

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -182,7 +182,7 @@ export default async function transformSource(
const rscExports: any = {
__next_rsc__: `{
__webpack_require__,
_: () => {${imports}}
_: () => {\n${imports}\n}
}`,
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack/loaders/utils.ts
Expand Up @@ -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)

0 comments on commit 38a51c1

Please sign in to comment.