Skip to content

Commit fc2d86b

Browse files
committed
fix(compiler): remove cloneDeep for optimization
1 parent 5033410 commit fc2d86b

File tree

6 files changed

+41
-71
lines changed

6 files changed

+41
-71
lines changed

.changeset/curly-insects-dance.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@lingo.dev/_compiler": patch
3+
"lingo.dev": patch
4+
---
5+
6+
remove cloneDeep for optimization

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
"devDependencies": {
1313
"@babel/generator": "^7.27.1",
1414
"@babel/parser": "^7.27.1",
15-
"@babel/traverse": "^7.26.7",
15+
"@babel/traverse": "^7.27.4",
1616
"@babel/types": "^7.27.1",
1717
"@commitlint/cli": "^19.8.0",
1818
"@commitlint/config-conventional": "^19.8.0",
19-
"@types/babel__traverse": "^7.20.7",
2019
"commitlint": "^19.7.1",
2120
"husky": "^9.1.7",
2221
"turbo": "^2.5.0"

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
"@ai-sdk/openai": "^1.3.22",
102102
"@babel/generator": "^7.27.1",
103103
"@babel/parser": "^7.27.1",
104-
"@babel/traverse": "^7.26.7",
104+
"@babel/traverse": "^7.27.4",
105105
"@babel/types": "^7.27.1",
106106
"@datocms/cma-client-node": "^4.0.1",
107107
"@gitbeaker/rest": "^39.34.3",

packages/compiler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"@ai-sdk/groq": "^1.2.3",
4040
"@babel/generator": "^7.26.5",
4141
"@babel/parser": "^7.26.7",
42-
"@babel/traverse": "^7.26.7",
42+
"@babel/traverse": "^7.27.4",
4343
"@babel/types": "^7.26.7",
4444
"ai": "^4.2.10",
4545
"dedent": "^1.6.0",

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

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export const lingoJsxScopeInjectMutation = createCodeMutation((payload) => {
2222
if (skip) {
2323
continue;
2424
}
25-
2625
// Import LingoComponent based on the module execution mode
2726
const packagePath =
2827
mode === "client" ? ModuleId.ReactClient : ModuleId.ReactRSC;
@@ -31,101 +30,82 @@ export const lingoJsxScopeInjectMutation = createCodeMutation((payload) => {
3130
exportedName: "LingoComponent",
3231
});
3332

34-
// Preserve original scope before he JSX element is replaced with the lingo component
35-
const originalJsxScope = _.cloneDeep(jsxScope);
36-
3733
// Get the original JSX element name
3834
const originalJsxElementName = getJsxElementName(jsxScope);
3935
if (!originalJsxElementName) {
4036
continue;
4137
}
4238

43-
// Replace the name with the lingo component
44-
jsxScope.node.openingElement.name = t.jsxIdentifier(
45-
lingoComponentImport.importedName,
46-
);
47-
if (jsxScope.node.closingElement) {
48-
jsxScope.node.closingElement = null;
49-
jsxScope.node.children = [];
50-
jsxScope.node.selfClosing = true;
51-
jsxScope.node.openingElement.selfClosing = true;
52-
}
39+
// Build new attributes array, preserving all original attributes
40+
const originalAttributes = jsxScope.node.openingElement.attributes.slice();
5341

5442
// Add $as prop
5543
const as = /^[A-Z]/.test(originalJsxElementName)
5644
? t.jsxExpressionContainer(t.identifier(originalJsxElementName))
5745
: t.stringLiteral(originalJsxElementName);
58-
jsxScope.node.openingElement.attributes.push(
59-
t.jsxAttribute(t.jsxIdentifier("$as"), as),
60-
);
61-
46+
originalAttributes.push(t.jsxAttribute(t.jsxIdentifier("$as"), as));
6247
// Add $fileKey prop
63-
jsxScope.node.openingElement.attributes.push(
48+
originalAttributes.push(
6449
t.jsxAttribute(
6550
t.jsxIdentifier("$fileKey"),
6651
t.stringLiteral(payload.fileKey),
6752
),
6853
);
69-
7054
// Add $entryKey prop
71-
jsxScope.node.openingElement.attributes.push(
55+
originalAttributes.push(
7256
t.jsxAttribute(
7357
t.jsxIdentifier("$entryKey"),
7458
t.stringLiteral(getJsxScopeAttribute(jsxScope)!),
7559
),
7660
);
7761

7862
// Extract $variables from original JSX scope before lingo component was inserted
79-
const $variables = getJsxVariables(originalJsxScope);
63+
const $variables = getJsxVariables(jsxScope);
8064
if ($variables.properties.length > 0) {
81-
jsxScope.node.openingElement.attributes.push(
65+
originalAttributes.push(
8266
t.jsxAttribute(
8367
t.jsxIdentifier("$variables"),
8468
t.jsxExpressionContainer($variables),
8569
),
8670
);
8771
}
88-
8972
// Extract nested JSX elements
90-
const $elements = getNestedJsxElements(originalJsxScope);
73+
const $elements = getNestedJsxElements(jsxScope);
9174
if ($elements.elements.length > 0) {
92-
jsxScope.node.openingElement.attributes.push(
75+
originalAttributes.push(
9376
t.jsxAttribute(
9477
t.jsxIdentifier("$elements"),
9578
t.jsxExpressionContainer($elements),
9679
),
9780
);
9881
}
99-
10082
// Extract nested functions
101-
const $functions = getJsxFunctions(originalJsxScope);
83+
const $functions = getJsxFunctions(jsxScope);
10284
if ($functions.properties.length > 0) {
103-
jsxScope.node.openingElement.attributes.push(
85+
originalAttributes.push(
10486
t.jsxAttribute(
10587
t.jsxIdentifier("$functions"),
10688
t.jsxExpressionContainer($functions),
10789
),
10890
);
10991
}
110-
11192
// Extract expressions
112-
const $expressions = getJsxExpressions(originalJsxScope);
93+
const $expressions = getJsxExpressions(jsxScope);
11394
if ($expressions.elements.length > 0) {
114-
jsxScope.node.openingElement.attributes.push(
95+
originalAttributes.push(
11596
t.jsxAttribute(
11697
t.jsxIdentifier("$expressions"),
11798
t.jsxExpressionContainer($expressions),
11899
),
119100
);
120101
}
121-
122102
if (mode === "server") {
123103
// Add $loadDictionary prop
124104
const loadDictionaryImport = getOrCreateImport(payload.ast, {
125105
exportedName: "loadDictionary",
126106
moduleName: ModuleId.ReactRSC,
127107
});
128-
jsxScope.node.openingElement.attributes.push(
108+
originalAttributes.push(
129109
t.jsxAttribute(
130110
t.jsxIdentifier("$loadDictionary"),
131111
t.jsxExpressionContainer(
@@ -140,6 +120,20 @@ export const lingoJsxScopeInjectMutation = createCodeMutation((payload) => {
140120
),
141121
);
142122
}
123+
124+
// Create new JSXElement (self-closing)
125+
const newNode = t.jsxElement(
126+
t.jsxOpeningElement(
127+
t.jsxIdentifier(lingoComponentImport.importedName),
128+
originalAttributes,
129+
true, // selfClosing
130+
),
131+
null, // no closing element
132+
[], // no children
133+
true, // selfClosing
134+
);
135+
136+
jsxScope.replaceWith(newNode);
143137
}
144138

145139
return payload;

pnpm-lock.yaml

Lines changed: 4 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)