Skip to content

Commit

Permalink
refactor: alternate compat runtime dedupe fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Dec 28, 2024
1 parent 8ebe566 commit 2b28ad9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 31 deletions.
4 changes: 0 additions & 4 deletions packages/compiler/babel-utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<t.ImportDeclaration>;
export function importDefault(
file: t.BabelFile,
request: string,
Expand Down
17 changes: 0 additions & 17 deletions packages/compiler/src/babel-utils/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion packages/compiler/src/babel-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export {
} from "./diagnostics";
export {
importDefault,
importEffect,
importNamed,
importStar,
resolveRelativePath,
Expand Down
21 changes: 16 additions & 5 deletions packages/runtime-tags/src/translator/visitors/program/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { types as t } from "@marko/compiler";
import {
importEffect,
loadFileForImport,
resolveRelativePath,
} from "@marko/compiler/babel-utils";
Expand Down Expand Up @@ -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)!;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { types as t } from "@marko/compiler";
import {
assertAttributesOrArgs,
importDefault,
importEffect,
importNamed,
loadFileForTag,
} from "@marko/compiler/babel-utils";
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 2b28ad9

Please sign in to comment.