Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: detect ESM by ast type #35627

Merged
merged 2 commits into from Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/build/entries.ts
Expand Up @@ -135,7 +135,7 @@ export async function getPageRuntime(
try {
const { body } = await parse(pageContent, {
filename: pageFilePath,
isModule: true,
isModule: 'unknown',
})

for (const node of body) {
Expand Down
Expand Up @@ -47,7 +47,7 @@ async function parseModuleInfo(
): Promise<void> {
const { body } = await parse(transformedSource, {
filename: resourcePath,
isModule: true,
isModule: 'unknown',
})
for (let i = 0; i < body.length; i++) {
const node = body[i]
Expand Down
@@ -1,6 +1,6 @@
import { parse } from '../../swc'
import { getRawPageExtensions } from '../../utils'
import { buildExports, isEsmNodeType } from './utils'
import { buildExports } from './utils'

const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif']

Expand Down Expand Up @@ -42,16 +42,18 @@ async function parseModuleInfo({
imports: string
isEsm: boolean
}> {
const ast = await parse(source, { filename: resourcePath, isModule: true })
const { body } = ast
const ast = await parse(source, {
filename: resourcePath,
isModule: 'unknown',
})
const { type, body } = ast
let transformedSource = ''
let lastIndex = 0
let imports = ''
let isEsm = false
const isEsm = type === 'Module'

for (let i = 0; i < body.length; i++) {
const node = body[i]
isEsm = isEsm || isEsmNodeType(node.type)
switch (node.type) {
case 'ImportDeclaration': {
const importSource = node.source.value
Expand Down
9 changes: 0 additions & 9 deletions packages/next/build/webpack/loaders/utils.ts
Expand Up @@ -11,12 +11,3 @@ export function buildExports(moduleExports: any, isESM: boolean) {
})
return ret
}

const esmNodeTypes = [
'ImportDeclaration',
'ExportDeclaration',
'ExportNamedDeclaration',
'ExportDefaultExpression',
'ExportDefaultDeclaration',
]
export const isEsmNodeType = (type: string) => esmNodeTypes.includes(type)