From 6e998e3c70209bd2c6b04d2a1de063ffb064fde9 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sun, 27 Mar 2022 03:13:19 +0200 Subject: [PATCH] enhance: detect ESM by ast type --- packages/next/build/entries.ts | 2 +- .../webpack/loaders/next-flight-client-loader.ts | 2 +- .../webpack/loaders/next-flight-server-loader.ts | 12 +++++++----- packages/next/build/webpack/loaders/utils.ts | 9 --------- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/packages/next/build/entries.ts b/packages/next/build/entries.ts index 85e39d25cfc0..9cfbb276b8d4 100644 --- a/packages/next/build/entries.ts +++ b/packages/next/build/entries.ts @@ -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) { 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 ee60abb516d1..d4343ad5048d 100644 --- a/packages/next/build/webpack/loaders/next-flight-client-loader.ts +++ b/packages/next/build/webpack/loaders/next-flight-client-loader.ts @@ -47,7 +47,7 @@ async function parseModuleInfo( ): Promise { const { body } = await parse(transformedSource, { filename: resourcePath, - isModule: true, + isModule: 'unknown', }) for (let i = 0; i < body.length; i++) { const node = body[i] 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 b87f234690b1..112f3c740a89 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, isEsmNodeType } from './utils' +import { buildExports } from './utils' const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif'] @@ -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 diff --git a/packages/next/build/webpack/loaders/utils.ts b/packages/next/build/webpack/loaders/utils.ts index e28e02eaea04..d771a714c277 100644 --- a/packages/next/build/webpack/loaders/utils.ts +++ b/packages/next/build/webpack/loaders/utils.ts @@ -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)