diff --git a/src/utils/transform/transform-dynamic-import.ts b/src/utils/transform/transform-dynamic-import.ts index 76affadba..52ff4c34a 100644 --- a/src/utils/transform/transform-dynamic-import.ts +++ b/src/utils/transform/transform-dynamic-import.ts @@ -2,10 +2,7 @@ import MagicString from 'magic-string'; import type { RawSourceMap } from '../../source-map'; import { parseEsm } from '../es-module-lexer'; -const handlerName = '___tsxInteropDynamicImport'; - -// Prefixed with a newline since there could be a comment on the last line (e.g. sourcemap comment) -const handleEsModuleFunction = `\nfunction ${handlerName}${(function (imported: Record) { +const toEsmFunctionString = ((imported: Record) => { const d = 'default'; const exports = Object.keys(imported); if ( @@ -19,9 +16,9 @@ const handleEsModuleFunction = `\nfunction ${handlerName}${(function (imported: } return imported; -}).toString().slice('function'.length)}`; +}).toString(); -const handleDynamicImport = `.then(${handlerName})`; +const handleDynamicImport = `.then(${toEsmFunctionString})`; const esmImportPattern = /\bimport\b/; @@ -46,8 +43,6 @@ export const transformDynamicImport = ( magicString.appendRight(dynamicImport.se, handleDynamicImport); } - magicString.append(handleEsModuleFunction); - const newCode = magicString.toString(); const newMap = magicString.generateMap({ source: filePath, diff --git a/tests/specs/smoke.ts b/tests/specs/smoke.ts index 1b9d3cf48..bb658d4a2 100644 --- a/tests/specs/smoke.ts +++ b/tests/specs/smoke.ts @@ -355,6 +355,9 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => { } }); + const importWorksInEval = async () => await import('./js/index.js'); + (0, eval)(importWorksInEval.toString())(); + // .json import * as json from './json/index.json'; import './json/index';