Unverified Commit 4bfcf834 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

enhance: detect ESM by ast type (#35627)

Simplify the esmodule detection. If there's any import/exprt usage, it will return `Module` in ast.type otherwise `Script` when `isModule` is set to `"unknown"`
parent 0d401ddc
...@@ -135,7 +135,7 @@ export async function getPageRuntime( ...@@ -135,7 +135,7 @@ export async function getPageRuntime(
try { try {
const { body } = await parse(pageContent, { const { body } = await parse(pageContent, {
filename: pageFilePath, filename: pageFilePath,
isModule: true, isModule: 'unknown',
}) })
for (const node of body) { for (const node of body) {
......
...@@ -47,7 +47,7 @@ async function parseModuleInfo( ...@@ -47,7 +47,7 @@ async function parseModuleInfo(
): Promise<void> { ): Promise<void> {
const { body } = await parse(transformedSource, { const { body } = await parse(transformedSource, {
filename: resourcePath, filename: resourcePath,
isModule: true, isModule: 'unknown',
}) })
for (let i = 0; i < body.length; i++) { for (let i = 0; i < body.length; i++) {
const node = body[i] const node = body[i]
......
import { parse } from '../../swc' import { parse } from '../../swc'
import { getRawPageExtensions } from '../../utils' import { getRawPageExtensions } from '../../utils'
import { buildExports, isEsmNodeType } from './utils' import { buildExports } from './utils'
const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif'] const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif']
...@@ -42,16 +42,18 @@ async function parseModuleInfo({ ...@@ -42,16 +42,18 @@ async function parseModuleInfo({
imports: string imports: string
isEsm: boolean isEsm: boolean
}> { }> {
const ast = await parse(source, { filename: resourcePath, isModule: true }) const ast = await parse(source, {
const { body } = ast filename: resourcePath,
isModule: 'unknown',
})
const { type, body } = ast
let transformedSource = '' let transformedSource = ''
let lastIndex = 0 let lastIndex = 0
let imports = '' let imports = ''
let isEsm = false const isEsm = type === 'Module'
for (let i = 0; i < body.length; i++) { for (let i = 0; i < body.length; i++) {
const node = body[i] const node = body[i]
isEsm = isEsm || isEsmNodeType(node.type)
switch (node.type) { switch (node.type) {
case 'ImportDeclaration': { case 'ImportDeclaration': {
const importSource = node.source.value const importSource = node.source.value
......
...@@ -11,12 +11,3 @@ export function buildExports(moduleExports: any, isESM: boolean) { ...@@ -11,12 +11,3 @@ export function buildExports(moduleExports: any, isESM: boolean) {
}) })
return ret return ret
} }
const esmNodeTypes = [
'ImportDeclaration',
'ExportDeclaration',
'ExportNamedDeclaration',
'ExportDefaultExpression',
'ExportDefaultDeclaration',
]
export const isEsmNodeType = (type: string) => esmNodeTypes.includes(type)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment