diff --git a/packages/compiler/babel-utils.d.ts b/packages/compiler/babel-utils.d.ts index 9f0d0a91e5..c3ccef7f23 100644 --- a/packages/compiler/babel-utils.d.ts +++ b/packages/compiler/babel-utils.d.ts @@ -288,10 +288,6 @@ export function parseTemplateLiteral( ): t.TemplateLiteral; export function resolveRelativePath(file: t.BabelFile, request: string): string; -export function importEffect( - file: t.BabelFile, - request: string, -): t.NodePath; export function importDefault( file: t.BabelFile, request: string, diff --git a/packages/compiler/src/babel-utils/imports.js b/packages/compiler/src/babel-utils/imports.js index 35fc7e8197..e7bbfd4c88 100644 --- a/packages/compiler/src/babel-utils/imports.js +++ b/packages/compiler/src/babel-utils/imports.js @@ -20,23 +20,6 @@ export function resolveRelativePath(file, request) { return request; } -export function importEffect(file, request) { - const imports = getImports(file); - request = resolveRelativePath(file, request); - let importDeclaration = imports.get(request); - - if (!importDeclaration) { - imports.set( - request, - (importDeclaration = file.path.unshiftContainer( - "body", - t.importDeclaration([], t.stringLiteral(request)), - )[0]), - ); - } - return importDeclaration; -} - export function importDefault(file, request, nameHint) { const imports = getImports(file); request = resolveRelativePath(file, request); diff --git a/packages/compiler/src/babel-utils/index.js b/packages/compiler/src/babel-utils/index.js index cb3300d9f0..7cd3d006e6 100644 --- a/packages/compiler/src/babel-utils/index.js +++ b/packages/compiler/src/babel-utils/index.js @@ -18,7 +18,6 @@ export { } from "./diagnostics"; export { importDefault, - importEffect, importNamed, importStar, resolveRelativePath, diff --git a/packages/runtime-tags/src/translator/visitors/program/index.ts b/packages/runtime-tags/src/translator/visitors/program/index.ts index a30a19868c..e5a2516570 100644 --- a/packages/runtime-tags/src/translator/visitors/program/index.ts +++ b/packages/runtime-tags/src/translator/visitors/program/index.ts @@ -1,6 +1,5 @@ import { types as t } from "@marko/compiler"; import { - importEffect, loadFileForImport, resolveRelativePath, } from "@marko/compiler/babel-utils"; @@ -144,10 +143,22 @@ export default { } if (program.node.extra?.needsCompat) { - const compatRuntimeFile = getCompatRuntimeFile(); - const compatImport = importEffect(program.hub.file, compatRuntimeFile); - program.unshiftContainer("body", t.clone(compatImport.node)); - compatImport.remove(); + const compatFile = getCompatRuntimeFile(); + const body: [undefined | t.Statement, ...t.Statement[]] = [undefined]; + + for (const child of program.node.body) { + if ( + child.type === "ImportDeclaration" && + child.source.value === compatFile + ) { + body[0] = child; + } else { + body.push(child); + } + } + + body[0] ??= t.importDeclaration([], t.stringLiteral(compatFile)); + program.node.body = body as t.Statement[]; } currentProgramPath = previousProgramPath.get(currentProgramPath)!; }, diff --git a/packages/runtime-tags/src/translator/visitors/tag/dynamic-tag.ts b/packages/runtime-tags/src/translator/visitors/tag/dynamic-tag.ts index 61f1773f47..19410bc6f0 100644 --- a/packages/runtime-tags/src/translator/visitors/tag/dynamic-tag.ts +++ b/packages/runtime-tags/src/translator/visitors/tag/dynamic-tag.ts @@ -2,7 +2,6 @@ import { types as t } from "@marko/compiler"; import { assertAttributesOrArgs, importDefault, - importEffect, importNamed, loadFileForTag, } from "@marko/compiler/babel-utils"; @@ -115,15 +114,13 @@ export default { if (isClassAPI) { // This is the interop layer leaking into the translator // We use the dynamic tag when a custom tag from the class runtime is used - const compatRuntimeFile = getCompatRuntimeFile(); - importEffect(tag.hub.file, compatRuntimeFile); if (isOutputHTML()) { currentProgramPath.pushContainer( "body", t.expressionStatement( t.callExpression( - importNamed(tag.hub.file, compatRuntimeFile, "s"), + importNamed(tag.hub.file, getCompatRuntimeFile(), "s"), [ t.identifier((tagExpression as t.Identifier).name), t.stringLiteral(loadFileForTag(tag)!.metadata.marko.id),