Skip to content

Commit ec2f00a

Browse files
AleksandrSlvrcprl
andauthored
fix: babel CJS/ESM in the compiler (#1637)
* WIP * chore: fix types * chore: add changeset --------- Co-authored-by: Veronica Prilutskaya <[email protected]>
1 parent 1bde5ed commit ec2f00a

33 files changed

+84
-67
lines changed

.changeset/puny-cows-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lingo.dev/_compiler": patch
3+
---
4+
5+
fix babel CJS/ESM

packages/compiler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@
7171
"zod": "3.25.76"
7272
},
7373
"packageManager": "[email protected]"
74-
}
74+
}

packages/compiler/src/_base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import generate, { GeneratorResult } from "@babel/generator";
1+
import { generate, GeneratorResult } from "./babel-interop";
22
import * as t from "@babel/types";
33
import * as parser from "@babel/parser";
44
import { LocaleCode } from "@lingo.dev/_spec";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import _traverse, { NodePath } from "@babel/traverse";
2+
import _generate, { GeneratorResult } from "@babel/generator";
3+
4+
// Handle ESM/CJS interop - these packages may export differently
5+
// @ts-expect-error - Handle both default and named exports
6+
const traverse = typeof _traverse == "function" ? _traverse : _traverse.default;
7+
// @ts-expect-error - Handle both default and named exports
8+
const generate = typeof _generate == "function" ? _generate : _generate.default;
9+
10+
export type { NodePath };
11+
export type { GeneratorResult };
12+
13+
export { traverse, generate };

packages/compiler/src/jsx-attribute-scope-inject.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect } from "vitest";
22
import { lingoJsxAttributeScopeInjectMutation } from "./jsx-attribute-scope-inject";
33
import { createPayload, createOutput, defaultParams } from "./_base";
44
import * as parser from "@babel/parser";
5-
import generate from "@babel/generator";
5+
import { generate } from "./babel-interop";
66

77
// Helper function to run mutation and get result
88
function runMutation(code: string, rsc = false) {

packages/compiler/src/jsx-fragment.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createCodeMutation } from "./_base";
2-
import traverse from "@babel/traverse";
2+
import { traverse, NodePath } from "./babel-interop";
33
import * as t from "@babel/types";
44
import { getOrCreateImport } from "./utils";
55
import { CompilerPayload } from "./_base";
@@ -14,7 +14,7 @@ export function jsxFragmentMutation(
1414
let fragmentImportName: string | null = null;
1515

1616
traverse(ast, {
17-
ImportDeclaration(path) {
17+
ImportDeclaration(path: NodePath<t.ImportDeclaration>) {
1818
if (path.node.source.value !== "react") return;
1919

2020
for (const specifier of path.node.specifiers) {
@@ -31,7 +31,7 @@ export function jsxFragmentMutation(
3131
});
3232

3333
traverse(ast, {
34-
JSXFragment(path) {
34+
JSXFragment(path: NodePath<t.JSXFragment>) {
3535
foundFragments = true;
3636

3737
if (!fragmentImportName) {
@@ -62,7 +62,7 @@ export function transformFragmentShorthand(ast: t.Node): boolean {
6262
let fragmentImportName: string | null = null;
6363

6464
traverse(ast, {
65-
ImportDeclaration(path) {
65+
ImportDeclaration(path: NodePath<t.ImportDeclaration>) {
6666
if (path.node.source.value !== "react") return;
6767

6868
for (const specifier of path.node.specifiers) {
@@ -79,7 +79,7 @@ export function transformFragmentShorthand(ast: t.Node): boolean {
7979
});
8080

8181
traverse(ast, {
82-
JSXFragment(path) {
82+
JSXFragment(path: NodePath<t.JSXFragment>) {
8383
transformed = true;
8484

8585
if (!fragmentImportName) {

packages/compiler/src/jsx-html-lang.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import traverse from "@babel/traverse";
1+
import { traverse, NodePath } from "./babel-interop";
22
import * as t from "@babel/types";
33
import { createCodeMutation } from "./_base";
44
import { getJsxElementName } from "./utils/jsx-element";
@@ -7,7 +7,7 @@ import { ModuleId } from "./_const";
77

88
export const jsxHtmlLangMutation = createCodeMutation((payload) => {
99
traverse(payload.ast, {
10-
JSXElement: (path) => {
10+
JSXElement: (path: NodePath<t.JSXElement>) => {
1111
if (getJsxElementName(path)?.toLowerCase() === "html") {
1212
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
1313
const packagePath =

packages/compiler/src/jsx-provider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import traverse, { NodePath } from "@babel/traverse";
1+
import { NodePath, traverse } from "./babel-interop";
22
import * as t from "@babel/types";
33
import { CompilerPayload, createCodeMutation } from "./_base";
44
import { getJsxElementName } from "./utils/jsx-element";
@@ -11,7 +11,7 @@ import { ModuleId } from "./_const";
1111
*/
1212
const jsxProviderMutation = createCodeMutation((payload) => {
1313
traverse(payload.ast, {
14-
JSXElement: (path) => {
14+
JSXElement: (path: NodePath<t.JSXElement>) => {
1515
if (getJsxElementName(path)?.toLowerCase() === "html") {
1616
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
1717
if (mode === "client") {
@@ -93,11 +93,11 @@ function replaceHtmlComponent(
9393
(attr) => attr.type === "JSXAttribute" && attr.name.name === "lang",
9494
);
9595
if (!t.isJSXAttribute(langAttribute)) {
96-
(langAttribute = t.jsxAttribute(
96+
((langAttribute = t.jsxAttribute(
9797
t.jsxIdentifier("lang"),
9898
t.stringLiteral(""),
9999
)),
100-
path.node.openingElement.attributes.push(langAttribute);
100+
path.node.openingElement.attributes.push(langAttribute));
101101
}
102102
langAttribute.value = t.jsxExpressionContainer(
103103
t.awaitExpression(

packages/compiler/src/jsx-remove-attributes.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { createCodeMutation, CompilerPayload } from "./_base";
22
import * as t from "@babel/types";
3-
import traverse from "@babel/traverse";
4-
import { NodePath } from "@babel/traverse";
3+
import { traverse, NodePath } from "./babel-interop";
54

65
/**
76
* This mutation identifies JSX elements with data-jsx-* attributes and removes them

packages/compiler/src/jsx-root-flag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import traverse from "@babel/traverse";
1+
import { traverse } from "./babel-interop";
22
import { createCodeMutation } from "./_base";
33
import { getJsxRoots } from "./utils";
44
import * as t from "@babel/types";

0 commit comments

Comments
 (0)