@lingo.dev/_compiler 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.cjs +150 -160
- package/build/index.mjs +161 -171
- package/package.json +1 -1
package/build/index.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import { createUnplugin } from "unplugin";
|
|
|
4
4
|
// package.json
|
|
5
5
|
var package_default = {
|
|
6
6
|
name: "@lingo.dev/_compiler",
|
|
7
|
-
version: "0.2.
|
|
7
|
+
version: "0.2.4",
|
|
8
8
|
description: "Lingo.dev Compiler",
|
|
9
9
|
private: false,
|
|
10
10
|
publishConfig: {
|
|
@@ -204,7 +204,7 @@ function findExistingImport(ast, exportedName, moduleName) {
|
|
|
204
204
|
let result = null;
|
|
205
205
|
traverse(ast, {
|
|
206
206
|
ImportDeclaration(path7) {
|
|
207
|
-
if (path7.node.source.value
|
|
207
|
+
if (!moduleName.includes(path7.node.source.value)) {
|
|
208
208
|
return;
|
|
209
209
|
}
|
|
210
210
|
for (const specifier of path7.node.specifiers) {
|
|
@@ -244,14 +244,14 @@ function createImportDeclaration(ast, localName, exportedName, moduleName) {
|
|
|
244
244
|
t2.identifier(exportedName)
|
|
245
245
|
);
|
|
246
246
|
const existingImport = path7.get("body").find(
|
|
247
|
-
(nodePath) => t2.isImportDeclaration(nodePath.node) && nodePath.node.source.value
|
|
247
|
+
(nodePath) => t2.isImportDeclaration(nodePath.node) && moduleName.includes(nodePath.node.source.value)
|
|
248
248
|
);
|
|
249
249
|
if (existingImport && t2.isImportDeclaration(existingImport.node)) {
|
|
250
250
|
existingImport.node.specifiers.push(importSpecifier2);
|
|
251
251
|
} else {
|
|
252
252
|
const importDeclaration2 = t2.importDeclaration(
|
|
253
253
|
[importSpecifier2],
|
|
254
|
-
t2.stringLiteral(moduleName)
|
|
254
|
+
t2.stringLiteral(moduleName[0])
|
|
255
255
|
);
|
|
256
256
|
const lastImportIndex = findLastImportIndex(path7);
|
|
257
257
|
path7.node.body.splice(lastImportIndex + 1, 0, importDeclaration2);
|
|
@@ -345,6 +345,11 @@ function getNestedJsxElements(nodePath) {
|
|
|
345
345
|
}
|
|
346
346
|
|
|
347
347
|
// src/_const.ts
|
|
348
|
+
var ModuleId = {
|
|
349
|
+
ReactClient: ["lingo.dev/react/client", "lingo.dev/react-client"],
|
|
350
|
+
ReactRSC: ["lingo.dev/react/rsc", "lingo.dev/react-rsc"],
|
|
351
|
+
ReactRouter: ["lingo.dev/react/react-router", "lingo.dev/react-router"]
|
|
352
|
+
};
|
|
348
353
|
var LCP_DICTIONARY_FILE_NAME = "dictionary.js";
|
|
349
354
|
|
|
350
355
|
// src/jsx-provider.ts
|
|
@@ -357,11 +362,11 @@ var jsxProviderMutation = createCodeMutation((payload) => {
|
|
|
357
362
|
return;
|
|
358
363
|
}
|
|
359
364
|
const lingoProviderImport = getOrCreateImport(payload.ast, {
|
|
360
|
-
moduleName:
|
|
365
|
+
moduleName: ModuleId.ReactRSC,
|
|
361
366
|
exportedName: "LingoProvider"
|
|
362
367
|
});
|
|
363
368
|
const loadDictionaryImport = getOrCreateImport(payload.ast, {
|
|
364
|
-
moduleName:
|
|
369
|
+
moduleName: ModuleId.ReactRSC,
|
|
365
370
|
exportedName: "loadDictionary"
|
|
366
371
|
});
|
|
367
372
|
const loadDictionaryArrow = t4.arrowFunctionExpression(
|
|
@@ -1731,7 +1736,7 @@ function findInvokations(ast, params) {
|
|
|
1731
1736
|
const result = [];
|
|
1732
1737
|
traverse6(ast, {
|
|
1733
1738
|
ImportDeclaration(path7) {
|
|
1734
|
-
if (path7.node.source.value
|
|
1739
|
+
if (!params.moduleName.includes(path7.node.source.value)) return;
|
|
1735
1740
|
const importNames = /* @__PURE__ */ new Map();
|
|
1736
1741
|
const specifiers = path7.node.specifiers;
|
|
1737
1742
|
specifiers.forEach((specifier) => {
|
|
@@ -1766,17 +1771,38 @@ function collectCallExpressions(path7, importNames, result, functionName) {
|
|
|
1766
1771
|
}
|
|
1767
1772
|
|
|
1768
1773
|
// src/rsc-dictionary-loader.ts
|
|
1769
|
-
import * as
|
|
1774
|
+
import * as t13 from "@babel/types";
|
|
1770
1775
|
|
|
1771
1776
|
// src/_utils.ts
|
|
1772
1777
|
import path5 from "path";
|
|
1773
1778
|
var getDictionaryPath = (params) => {
|
|
1774
|
-
|
|
1775
|
-
params.
|
|
1776
|
-
|
|
1779
|
+
const absolute = path5.resolve(
|
|
1780
|
+
params.sourceRoot,
|
|
1781
|
+
params.lingoDir,
|
|
1782
|
+
LCP_DICTIONARY_FILE_NAME
|
|
1777
1783
|
);
|
|
1784
|
+
const rel = path5.relative(params.relativeFilePath, absolute);
|
|
1785
|
+
return rel.split(path5.sep).join(path5.posix.sep);
|
|
1778
1786
|
};
|
|
1779
1787
|
|
|
1788
|
+
// src/utils/create-locale-import-map.ts
|
|
1789
|
+
import * as t12 from "@babel/types";
|
|
1790
|
+
function createLocaleImportMap(allLocales, dictionaryPath) {
|
|
1791
|
+
return t12.objectExpression(
|
|
1792
|
+
allLocales.map(
|
|
1793
|
+
(locale) => t12.objectProperty(
|
|
1794
|
+
t12.stringLiteral(locale),
|
|
1795
|
+
t12.arrowFunctionExpression(
|
|
1796
|
+
[],
|
|
1797
|
+
t12.callExpression(t12.identifier("import"), [
|
|
1798
|
+
t12.stringLiteral(`${dictionaryPath}?locale=${locale}`)
|
|
1799
|
+
])
|
|
1800
|
+
)
|
|
1801
|
+
)
|
|
1802
|
+
)
|
|
1803
|
+
);
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1780
1806
|
// src/rsc-dictionary-loader.ts
|
|
1781
1807
|
var rscDictionaryLoaderMutation = createCodeMutation((payload) => {
|
|
1782
1808
|
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
|
|
@@ -1784,7 +1810,7 @@ var rscDictionaryLoaderMutation = createCodeMutation((payload) => {
|
|
|
1784
1810
|
return payload;
|
|
1785
1811
|
}
|
|
1786
1812
|
const invokations = findInvokations(payload.ast, {
|
|
1787
|
-
moduleName:
|
|
1813
|
+
moduleName: ModuleId.ReactRSC,
|
|
1788
1814
|
functionName: "loadDictionary"
|
|
1789
1815
|
});
|
|
1790
1816
|
const allLocales = Array.from(
|
|
@@ -1792,10 +1818,10 @@ var rscDictionaryLoaderMutation = createCodeMutation((payload) => {
|
|
|
1792
1818
|
);
|
|
1793
1819
|
for (const invokation of invokations) {
|
|
1794
1820
|
const internalDictionaryLoader = getOrCreateImport(payload.ast, {
|
|
1795
|
-
moduleName:
|
|
1821
|
+
moduleName: ModuleId.ReactRSC,
|
|
1796
1822
|
exportedName: "loadDictionary_internal"
|
|
1797
1823
|
});
|
|
1798
|
-
if (
|
|
1824
|
+
if (t13.isIdentifier(invokation.callee)) {
|
|
1799
1825
|
invokation.callee.name = internalDictionaryLoader.importedName;
|
|
1800
1826
|
}
|
|
1801
1827
|
const dictionaryPath = getDictionaryPath({
|
|
@@ -1803,26 +1829,14 @@ var rscDictionaryLoaderMutation = createCodeMutation((payload) => {
|
|
|
1803
1829
|
lingoDir: payload.params.lingoDir,
|
|
1804
1830
|
relativeFilePath: payload.relativeFilePath
|
|
1805
1831
|
});
|
|
1806
|
-
const localeImportMap =
|
|
1807
|
-
allLocales.map(
|
|
1808
|
-
(locale) => t12.objectProperty(
|
|
1809
|
-
t12.identifier(locale),
|
|
1810
|
-
t12.arrowFunctionExpression(
|
|
1811
|
-
[],
|
|
1812
|
-
t12.callExpression(t12.identifier("import"), [
|
|
1813
|
-
t12.stringLiteral(`${dictionaryPath}?locale=${locale}`)
|
|
1814
|
-
])
|
|
1815
|
-
)
|
|
1816
|
-
)
|
|
1817
|
-
)
|
|
1818
|
-
);
|
|
1832
|
+
const localeImportMap = createLocaleImportMap(allLocales, dictionaryPath);
|
|
1819
1833
|
invokation.arguments.push(localeImportMap);
|
|
1820
1834
|
}
|
|
1821
1835
|
return payload;
|
|
1822
1836
|
});
|
|
1823
1837
|
|
|
1824
1838
|
// src/react-router-dictionary-loader.ts
|
|
1825
|
-
import * as
|
|
1839
|
+
import * as t14 from "@babel/types";
|
|
1826
1840
|
var reactRouterDictionaryLoaderMutation = createCodeMutation(
|
|
1827
1841
|
(payload) => {
|
|
1828
1842
|
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
|
|
@@ -1830,7 +1844,7 @@ var reactRouterDictionaryLoaderMutation = createCodeMutation(
|
|
|
1830
1844
|
return payload;
|
|
1831
1845
|
}
|
|
1832
1846
|
const invokations = findInvokations(payload.ast, {
|
|
1833
|
-
moduleName:
|
|
1847
|
+
moduleName: ModuleId.ReactRouter,
|
|
1834
1848
|
functionName: "loadDictionary"
|
|
1835
1849
|
});
|
|
1836
1850
|
const allLocales = Array.from(
|
|
@@ -1838,10 +1852,10 @@ var reactRouterDictionaryLoaderMutation = createCodeMutation(
|
|
|
1838
1852
|
);
|
|
1839
1853
|
for (const invokation of invokations) {
|
|
1840
1854
|
const internalDictionaryLoader = getOrCreateImport(payload.ast, {
|
|
1841
|
-
moduleName:
|
|
1855
|
+
moduleName: ModuleId.ReactRouter,
|
|
1842
1856
|
exportedName: "loadDictionary_internal"
|
|
1843
1857
|
});
|
|
1844
|
-
if (
|
|
1858
|
+
if (t14.isIdentifier(invokation.callee)) {
|
|
1845
1859
|
invokation.callee.name = internalDictionaryLoader.importedName;
|
|
1846
1860
|
}
|
|
1847
1861
|
const dictionaryPath = getDictionaryPath({
|
|
@@ -1849,19 +1863,7 @@ var reactRouterDictionaryLoaderMutation = createCodeMutation(
|
|
|
1849
1863
|
lingoDir: payload.params.lingoDir,
|
|
1850
1864
|
relativeFilePath: payload.relativeFilePath
|
|
1851
1865
|
});
|
|
1852
|
-
const localeImportMap =
|
|
1853
|
-
allLocales.map(
|
|
1854
|
-
(locale) => t13.objectProperty(
|
|
1855
|
-
t13.identifier(locale),
|
|
1856
|
-
t13.arrowFunctionExpression(
|
|
1857
|
-
[],
|
|
1858
|
-
t13.callExpression(t13.identifier("import"), [
|
|
1859
|
-
t13.stringLiteral(`${dictionaryPath}?locale=${locale}`)
|
|
1860
|
-
])
|
|
1861
|
-
)
|
|
1862
|
-
)
|
|
1863
|
-
)
|
|
1864
|
-
);
|
|
1866
|
+
const localeImportMap = createLocaleImportMap(allLocales, dictionaryPath);
|
|
1865
1867
|
invokation.arguments.push(localeImportMap);
|
|
1866
1868
|
}
|
|
1867
1869
|
return payload;
|
|
@@ -1870,7 +1872,7 @@ var reactRouterDictionaryLoaderMutation = createCodeMutation(
|
|
|
1870
1872
|
|
|
1871
1873
|
// src/jsx-fragment.ts
|
|
1872
1874
|
import traverse7 from "@babel/traverse";
|
|
1873
|
-
import * as
|
|
1875
|
+
import * as t15 from "@babel/types";
|
|
1874
1876
|
function jsxFragmentMutation(payload) {
|
|
1875
1877
|
const { ast } = payload;
|
|
1876
1878
|
let foundFragments = false;
|
|
@@ -1879,7 +1881,7 @@ function jsxFragmentMutation(payload) {
|
|
|
1879
1881
|
ImportDeclaration(path7) {
|
|
1880
1882
|
if (path7.node.source.value !== "react") return;
|
|
1881
1883
|
for (const specifier of path7.node.specifiers) {
|
|
1882
|
-
if (
|
|
1884
|
+
if (t15.isImportSpecifier(specifier) && t15.isIdentifier(specifier.imported) && specifier.imported.name === "Fragment") {
|
|
1883
1885
|
fragmentImportName = specifier.local.name;
|
|
1884
1886
|
path7.stop();
|
|
1885
1887
|
}
|
|
@@ -1892,13 +1894,13 @@ function jsxFragmentMutation(payload) {
|
|
|
1892
1894
|
if (!fragmentImportName) {
|
|
1893
1895
|
const result = getOrCreateImport(ast, {
|
|
1894
1896
|
exportedName: "Fragment",
|
|
1895
|
-
moduleName: "react"
|
|
1897
|
+
moduleName: ["react"]
|
|
1896
1898
|
});
|
|
1897
1899
|
fragmentImportName = result.importedName;
|
|
1898
1900
|
}
|
|
1899
|
-
const fragmentElement =
|
|
1900
|
-
|
|
1901
|
-
|
|
1901
|
+
const fragmentElement = t15.jsxElement(
|
|
1902
|
+
t15.jsxOpeningElement(t15.jsxIdentifier(fragmentImportName), [], false),
|
|
1903
|
+
t15.jsxClosingElement(t15.jsxIdentifier(fragmentImportName)),
|
|
1902
1904
|
path7.node.children,
|
|
1903
1905
|
false
|
|
1904
1906
|
);
|
|
@@ -1910,22 +1912,22 @@ function jsxFragmentMutation(payload) {
|
|
|
1910
1912
|
|
|
1911
1913
|
// src/jsx-html-lang.ts
|
|
1912
1914
|
import traverse8 from "@babel/traverse";
|
|
1913
|
-
import * as
|
|
1915
|
+
import * as t16 from "@babel/types";
|
|
1914
1916
|
var jsxHtmlLangMutation = createCodeMutation((payload) => {
|
|
1915
1917
|
traverse8(payload.ast, {
|
|
1916
1918
|
JSXElement: (path7) => {
|
|
1917
1919
|
if (getJsxElementName(path7)?.toLowerCase() === "html") {
|
|
1918
1920
|
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
|
|
1919
|
-
const packagePath = mode === "client" ?
|
|
1921
|
+
const packagePath = mode === "client" ? ModuleId.ReactClient : ModuleId.ReactRSC;
|
|
1920
1922
|
const lingoHtmlComponentImport = getOrCreateImport(payload.ast, {
|
|
1921
1923
|
moduleName: packagePath,
|
|
1922
1924
|
exportedName: "LingoHtmlComponent"
|
|
1923
1925
|
});
|
|
1924
|
-
path7.node.openingElement.name =
|
|
1926
|
+
path7.node.openingElement.name = t16.jsxIdentifier(
|
|
1925
1927
|
lingoHtmlComponentImport.importedName
|
|
1926
1928
|
);
|
|
1927
1929
|
if (path7.node.closingElement) {
|
|
1928
|
-
path7.node.closingElement.name =
|
|
1930
|
+
path7.node.closingElement.name = t16.jsxIdentifier(
|
|
1929
1931
|
lingoHtmlComponentImport.importedName
|
|
1930
1932
|
);
|
|
1931
1933
|
}
|
|
@@ -1994,7 +1996,7 @@ function jsxAttributeScopesExportMutation(payload) {
|
|
|
1994
1996
|
import _10 from "lodash";
|
|
1995
1997
|
|
|
1996
1998
|
// src/utils/jsx-content.ts
|
|
1997
|
-
import * as
|
|
1999
|
+
import * as t17 from "@babel/types";
|
|
1998
2000
|
import _9 from "lodash";
|
|
1999
2001
|
var WHITESPACE_PLACEHOLDER = "[lingo-whitespace-placeholder]";
|
|
2000
2002
|
function extractJsxContent(nodePath, replaceWhitespacePlaceholders = true) {
|
|
@@ -2016,39 +2018,39 @@ function extractJsxContent(nodePath, replaceWhitespacePlaceholders = true) {
|
|
|
2016
2018
|
return;
|
|
2017
2019
|
}
|
|
2018
2020
|
const expr = path7.node.expression;
|
|
2019
|
-
if (
|
|
2021
|
+
if (t17.isCallExpression(expr)) {
|
|
2020
2022
|
let key = "";
|
|
2021
|
-
if (
|
|
2023
|
+
if (t17.isIdentifier(expr.callee)) {
|
|
2022
2024
|
key = `${expr.callee.name}`;
|
|
2023
|
-
} else if (
|
|
2025
|
+
} else if (t17.isMemberExpression(expr.callee)) {
|
|
2024
2026
|
let firstCallee = expr.callee;
|
|
2025
|
-
while (
|
|
2027
|
+
while (t17.isMemberExpression(firstCallee) && t17.isCallExpression(firstCallee.object)) {
|
|
2026
2028
|
firstCallee = firstCallee.object.callee;
|
|
2027
2029
|
}
|
|
2028
2030
|
let current = firstCallee;
|
|
2029
2031
|
const parts = [];
|
|
2030
|
-
while (
|
|
2031
|
-
if (
|
|
2032
|
+
while (t17.isMemberExpression(current)) {
|
|
2033
|
+
if (t17.isIdentifier(current.property)) {
|
|
2032
2034
|
parts.unshift(current.property.name);
|
|
2033
2035
|
}
|
|
2034
2036
|
current = current.object;
|
|
2035
2037
|
}
|
|
2036
|
-
if (
|
|
2038
|
+
if (t17.isIdentifier(current)) {
|
|
2037
2039
|
parts.unshift(current.name);
|
|
2038
2040
|
}
|
|
2039
|
-
if (
|
|
2041
|
+
if (t17.isMemberExpression(firstCallee) && t17.isNewExpression(firstCallee.object) && t17.isIdentifier(firstCallee.object.callee)) {
|
|
2040
2042
|
parts.unshift(firstCallee.object.callee.name);
|
|
2041
2043
|
}
|
|
2042
2044
|
key = parts.join(".");
|
|
2043
2045
|
}
|
|
2044
2046
|
chunks.push(`<function:${key}/>`);
|
|
2045
|
-
} else if (
|
|
2047
|
+
} else if (t17.isIdentifier(expr)) {
|
|
2046
2048
|
chunks.push(`{${expr.name}}`);
|
|
2047
|
-
} else if (
|
|
2049
|
+
} else if (t17.isMemberExpression(expr)) {
|
|
2048
2050
|
let current = expr;
|
|
2049
2051
|
const parts = [];
|
|
2050
|
-
while (
|
|
2051
|
-
if (
|
|
2052
|
+
while (t17.isMemberExpression(current)) {
|
|
2053
|
+
if (t17.isIdentifier(current.property)) {
|
|
2052
2054
|
if (current.computed) {
|
|
2053
2055
|
parts.unshift(`[${current.property.name}]`);
|
|
2054
2056
|
} else {
|
|
@@ -2057,7 +2059,7 @@ function extractJsxContent(nodePath, replaceWhitespacePlaceholders = true) {
|
|
|
2057
2059
|
}
|
|
2058
2060
|
current = current.object;
|
|
2059
2061
|
}
|
|
2060
|
-
if (
|
|
2062
|
+
if (t17.isIdentifier(current)) {
|
|
2061
2063
|
parts.unshift(current.name);
|
|
2062
2064
|
chunks.push(`{${parts.join(".").replaceAll(".[", "[")}}`);
|
|
2063
2065
|
}
|
|
@@ -2078,12 +2080,12 @@ function extractJsxContent(nodePath, replaceWhitespacePlaceholders = true) {
|
|
|
2078
2080
|
}
|
|
2079
2081
|
var compilerProps = ["data-jsx-attribute-scope", "data-jsx-scope"];
|
|
2080
2082
|
function isExpression2(nodePath) {
|
|
2081
|
-
const isCompilerExpression = !_9.isArray(nodePath.container) &&
|
|
2082
|
-
return !isCompilerExpression && !
|
|
2083
|
+
const isCompilerExpression = !_9.isArray(nodePath.container) && t17.isJSXAttribute(nodePath.container) && t17.isJSXIdentifier(nodePath.container.name) && compilerProps.includes(nodePath.container.name.name);
|
|
2084
|
+
return !isCompilerExpression && !t17.isJSXEmptyExpression(nodePath.node.expression);
|
|
2083
2085
|
}
|
|
2084
2086
|
function isWhitespace(nodePath) {
|
|
2085
2087
|
const expr = nodePath.node.expression;
|
|
2086
|
-
return
|
|
2088
|
+
return t17.isStringLiteral(expr) && expr.value === " ";
|
|
2087
2089
|
}
|
|
2088
2090
|
function normalizeJsxWhitespace(input) {
|
|
2089
2091
|
const lines = input.split("\n");
|
|
@@ -2143,13 +2145,13 @@ function jsxScopesExportMutation(payload) {
|
|
|
2143
2145
|
}
|
|
2144
2146
|
|
|
2145
2147
|
// src/jsx-attribute-scope-inject.ts
|
|
2146
|
-
import * as
|
|
2148
|
+
import * as t18 from "@babel/types";
|
|
2147
2149
|
var lingoJsxAttributeScopeInjectMutation = createCodeMutation(
|
|
2148
2150
|
(payload) => {
|
|
2149
2151
|
const mode = getModuleExecutionMode(payload.ast, payload.params.rsc);
|
|
2150
2152
|
const jsxAttributeScopes = collectJsxAttributeScopes(payload.ast);
|
|
2151
2153
|
for (const [jsxScope, attributes] of jsxAttributeScopes) {
|
|
2152
|
-
const packagePath = mode === "client" ?
|
|
2154
|
+
const packagePath = mode === "client" ? ModuleId.ReactClient : ModuleId.ReactRSC;
|
|
2153
2155
|
const lingoComponentImport = getOrCreateImport(payload.ast, {
|
|
2154
2156
|
moduleName: packagePath,
|
|
2155
2157
|
exportedName: "LingoAttributeComponent"
|
|
@@ -2158,34 +2160,34 @@ var lingoJsxAttributeScopeInjectMutation = createCodeMutation(
|
|
|
2158
2160
|
if (!originalJsxElementName) {
|
|
2159
2161
|
continue;
|
|
2160
2162
|
}
|
|
2161
|
-
jsxScope.node.openingElement.name =
|
|
2163
|
+
jsxScope.node.openingElement.name = t18.jsxIdentifier(
|
|
2162
2164
|
lingoComponentImport.importedName
|
|
2163
2165
|
);
|
|
2164
2166
|
if (jsxScope.node.closingElement) {
|
|
2165
|
-
jsxScope.node.closingElement.name =
|
|
2167
|
+
jsxScope.node.closingElement.name = t18.jsxIdentifier(
|
|
2166
2168
|
lingoComponentImport.importedName
|
|
2167
2169
|
);
|
|
2168
2170
|
}
|
|
2169
|
-
const as = /^[A-Z]/.test(originalJsxElementName) ?
|
|
2171
|
+
const as = /^[A-Z]/.test(originalJsxElementName) ? t18.jsxExpressionContainer(t18.identifier(originalJsxElementName)) : t18.stringLiteral(originalJsxElementName);
|
|
2170
2172
|
jsxScope.node.openingElement.attributes.push(
|
|
2171
|
-
|
|
2173
|
+
t18.jsxAttribute(t18.jsxIdentifier("$attrAs"), as)
|
|
2172
2174
|
);
|
|
2173
2175
|
jsxScope.node.openingElement.attributes.push(
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2176
|
+
t18.jsxAttribute(
|
|
2177
|
+
t18.jsxIdentifier("$fileKey"),
|
|
2178
|
+
t18.stringLiteral(payload.relativeFilePath)
|
|
2177
2179
|
)
|
|
2178
2180
|
);
|
|
2179
2181
|
jsxScope.node.openingElement.attributes.push(
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2182
|
+
t18.jsxAttribute(
|
|
2183
|
+
t18.jsxIdentifier("$attributes"),
|
|
2184
|
+
t18.jsxExpressionContainer(
|
|
2185
|
+
t18.objectExpression(
|
|
2184
2186
|
attributes.map((attributeDefinition) => {
|
|
2185
2187
|
const [attribute, key = ""] = attributeDefinition.split(":");
|
|
2186
|
-
return
|
|
2187
|
-
|
|
2188
|
-
|
|
2188
|
+
return t18.objectProperty(
|
|
2189
|
+
t18.stringLiteral(attribute),
|
|
2190
|
+
t18.stringLiteral(key)
|
|
2189
2191
|
);
|
|
2190
2192
|
})
|
|
2191
2193
|
)
|
|
@@ -2195,17 +2197,17 @@ var lingoJsxAttributeScopeInjectMutation = createCodeMutation(
|
|
|
2195
2197
|
if (mode === "server") {
|
|
2196
2198
|
const loadDictionaryImport = getOrCreateImport(payload.ast, {
|
|
2197
2199
|
exportedName: "loadDictionary",
|
|
2198
|
-
moduleName:
|
|
2200
|
+
moduleName: ModuleId.ReactRSC
|
|
2199
2201
|
});
|
|
2200
2202
|
jsxScope.node.openingElement.attributes.push(
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
[
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
[
|
|
2203
|
+
t18.jsxAttribute(
|
|
2204
|
+
t18.jsxIdentifier("$loadDictionary"),
|
|
2205
|
+
t18.jsxExpressionContainer(
|
|
2206
|
+
t18.arrowFunctionExpression(
|
|
2207
|
+
[t18.identifier("locale")],
|
|
2208
|
+
t18.callExpression(
|
|
2209
|
+
t18.identifier(loadDictionaryImport.importedName),
|
|
2210
|
+
[t18.identifier("locale")]
|
|
2209
2211
|
)
|
|
2210
2212
|
)
|
|
2211
2213
|
)
|
|
@@ -2218,10 +2220,10 @@ var lingoJsxAttributeScopeInjectMutation = createCodeMutation(
|
|
|
2218
2220
|
);
|
|
2219
2221
|
|
|
2220
2222
|
// src/jsx-scope-inject.ts
|
|
2221
|
-
import * as
|
|
2223
|
+
import * as t22 from "@babel/types";
|
|
2222
2224
|
|
|
2223
2225
|
// src/utils/jsx-variables.ts
|
|
2224
|
-
import * as
|
|
2226
|
+
import * as t19 from "@babel/types";
|
|
2225
2227
|
var getJsxVariables = (nodePath) => {
|
|
2226
2228
|
const variables = /* @__PURE__ */ new Set();
|
|
2227
2229
|
nodePath.traverse({
|
|
@@ -2229,13 +2231,13 @@ var getJsxVariables = (nodePath) => {
|
|
|
2229
2231
|
path7.skip();
|
|
2230
2232
|
},
|
|
2231
2233
|
JSXExpressionContainer(path7) {
|
|
2232
|
-
if (
|
|
2234
|
+
if (t19.isIdentifier(path7.node.expression)) {
|
|
2233
2235
|
variables.add(path7.node.expression.name);
|
|
2234
|
-
} else if (
|
|
2236
|
+
} else if (t19.isMemberExpression(path7.node.expression)) {
|
|
2235
2237
|
let current = path7.node.expression;
|
|
2236
2238
|
const parts = [];
|
|
2237
|
-
while (
|
|
2238
|
-
if (
|
|
2239
|
+
while (t19.isMemberExpression(current)) {
|
|
2240
|
+
if (t19.isIdentifier(current.property)) {
|
|
2239
2241
|
if (current.computed) {
|
|
2240
2242
|
parts.unshift(`[${current.property.name}]`);
|
|
2241
2243
|
} else {
|
|
@@ -2244,7 +2246,7 @@ var getJsxVariables = (nodePath) => {
|
|
|
2244
2246
|
}
|
|
2245
2247
|
current = current.object;
|
|
2246
2248
|
}
|
|
2247
|
-
if (
|
|
2249
|
+
if (t19.isIdentifier(current)) {
|
|
2248
2250
|
parts.unshift(current.name);
|
|
2249
2251
|
variables.add(parts.join(".").replaceAll(".[", "["));
|
|
2250
2252
|
}
|
|
@@ -2253,14 +2255,14 @@ var getJsxVariables = (nodePath) => {
|
|
|
2253
2255
|
}
|
|
2254
2256
|
});
|
|
2255
2257
|
const properties = Array.from(variables).map(
|
|
2256
|
-
(name) =>
|
|
2258
|
+
(name) => t19.objectProperty(t19.stringLiteral(name), t19.identifier(name))
|
|
2257
2259
|
);
|
|
2258
|
-
const result =
|
|
2260
|
+
const result = t19.objectExpression(properties);
|
|
2259
2261
|
return result;
|
|
2260
2262
|
};
|
|
2261
2263
|
|
|
2262
2264
|
// src/utils/jsx-functions.ts
|
|
2263
|
-
import * as
|
|
2265
|
+
import * as t20 from "@babel/types";
|
|
2264
2266
|
var getJsxFunctions = (nodePath) => {
|
|
2265
2267
|
const functions = /* @__PURE__ */ new Map();
|
|
2266
2268
|
let fnCounter = 0;
|
|
@@ -2269,27 +2271,27 @@ var getJsxFunctions = (nodePath) => {
|
|
|
2269
2271
|
path7.skip();
|
|
2270
2272
|
},
|
|
2271
2273
|
JSXExpressionContainer(path7) {
|
|
2272
|
-
if (
|
|
2274
|
+
if (t20.isCallExpression(path7.node.expression)) {
|
|
2273
2275
|
let key = "";
|
|
2274
|
-
if (
|
|
2276
|
+
if (t20.isIdentifier(path7.node.expression.callee)) {
|
|
2275
2277
|
key = `${path7.node.expression.callee.name}`;
|
|
2276
|
-
} else if (
|
|
2278
|
+
} else if (t20.isMemberExpression(path7.node.expression.callee)) {
|
|
2277
2279
|
let firstCallee = path7.node.expression.callee;
|
|
2278
|
-
while (
|
|
2280
|
+
while (t20.isMemberExpression(firstCallee) && t20.isCallExpression(firstCallee.object)) {
|
|
2279
2281
|
firstCallee = firstCallee.object.callee;
|
|
2280
2282
|
}
|
|
2281
2283
|
let current = firstCallee;
|
|
2282
2284
|
const parts = [];
|
|
2283
|
-
while (
|
|
2284
|
-
if (
|
|
2285
|
+
while (t20.isMemberExpression(current)) {
|
|
2286
|
+
if (t20.isIdentifier(current.property)) {
|
|
2285
2287
|
parts.unshift(current.property.name);
|
|
2286
2288
|
}
|
|
2287
2289
|
current = current.object;
|
|
2288
2290
|
}
|
|
2289
|
-
if (
|
|
2291
|
+
if (t20.isIdentifier(current)) {
|
|
2290
2292
|
parts.unshift(current.name);
|
|
2291
2293
|
}
|
|
2292
|
-
if (
|
|
2294
|
+
if (t20.isMemberExpression(firstCallee) && t20.isNewExpression(firstCallee.object) && t20.isIdentifier(firstCallee.object.callee)) {
|
|
2293
2295
|
parts.unshift(firstCallee.object.callee.name);
|
|
2294
2296
|
}
|
|
2295
2297
|
key = parts.join(".");
|
|
@@ -2302,13 +2304,13 @@ var getJsxFunctions = (nodePath) => {
|
|
|
2302
2304
|
}
|
|
2303
2305
|
});
|
|
2304
2306
|
const properties = Array.from(functions.entries()).map(
|
|
2305
|
-
([name, callExpr]) =>
|
|
2307
|
+
([name, callExpr]) => t20.objectProperty(t20.stringLiteral(name), t20.arrayExpression(callExpr))
|
|
2306
2308
|
);
|
|
2307
|
-
return
|
|
2309
|
+
return t20.objectExpression(properties);
|
|
2308
2310
|
};
|
|
2309
2311
|
|
|
2310
2312
|
// src/utils/jsx-expressions.ts
|
|
2311
|
-
import * as
|
|
2313
|
+
import * as t21 from "@babel/types";
|
|
2312
2314
|
var getJsxExpressions = (nodePath) => {
|
|
2313
2315
|
const expressions = [];
|
|
2314
2316
|
nodePath.traverse({
|
|
@@ -2317,13 +2319,13 @@ var getJsxExpressions = (nodePath) => {
|
|
|
2317
2319
|
},
|
|
2318
2320
|
JSXExpressionContainer(path7) {
|
|
2319
2321
|
const expr = path7.node.expression;
|
|
2320
|
-
if (!
|
|
2322
|
+
if (!t21.isJSXEmptyExpression(expr) && !t21.isIdentifier(expr) && !t21.isMemberExpression(expr) && !t21.isCallExpression(expr) && !(t21.isStringLiteral(expr) && expr.value === " ")) {
|
|
2321
2323
|
expressions.push(expr);
|
|
2322
2324
|
}
|
|
2323
2325
|
path7.skip();
|
|
2324
2326
|
}
|
|
2325
2327
|
});
|
|
2326
|
-
return
|
|
2328
|
+
return t21.arrayExpression(expressions);
|
|
2327
2329
|
};
|
|
2328
2330
|
|
|
2329
2331
|
// src/jsx-scope-inject.ts
|
|
@@ -2335,7 +2337,7 @@ var lingoJsxScopeInjectMutation = createCodeMutation((payload) => {
|
|
|
2335
2337
|
if (skip) {
|
|
2336
2338
|
continue;
|
|
2337
2339
|
}
|
|
2338
|
-
const packagePath = mode === "client" ?
|
|
2340
|
+
const packagePath = mode === "client" ? ModuleId.ReactClient : ModuleId.ReactRSC;
|
|
2339
2341
|
const lingoComponentImport = getOrCreateImport(payload.ast, {
|
|
2340
2342
|
moduleName: packagePath,
|
|
2341
2343
|
exportedName: "LingoComponent"
|
|
@@ -2345,79 +2347,79 @@ var lingoJsxScopeInjectMutation = createCodeMutation((payload) => {
|
|
|
2345
2347
|
continue;
|
|
2346
2348
|
}
|
|
2347
2349
|
const originalAttributes = jsxScope.node.openingElement.attributes.slice();
|
|
2348
|
-
const as = /^[A-Z]/.test(originalJsxElementName) ?
|
|
2349
|
-
originalAttributes.push(
|
|
2350
|
+
const as = /^[A-Z]/.test(originalJsxElementName) ? t22.jsxExpressionContainer(t22.identifier(originalJsxElementName)) : t22.stringLiteral(originalJsxElementName);
|
|
2351
|
+
originalAttributes.push(t22.jsxAttribute(t22.jsxIdentifier("$as"), as));
|
|
2350
2352
|
originalAttributes.push(
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2353
|
+
t22.jsxAttribute(
|
|
2354
|
+
t22.jsxIdentifier("$fileKey"),
|
|
2355
|
+
t22.stringLiteral(payload.relativeFilePath)
|
|
2354
2356
|
)
|
|
2355
2357
|
);
|
|
2356
2358
|
originalAttributes.push(
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2359
|
+
t22.jsxAttribute(
|
|
2360
|
+
t22.jsxIdentifier("$entryKey"),
|
|
2361
|
+
t22.stringLiteral(getJsxScopeAttribute(jsxScope))
|
|
2360
2362
|
)
|
|
2361
2363
|
);
|
|
2362
2364
|
const $variables = getJsxVariables(jsxScope);
|
|
2363
2365
|
if ($variables.properties.length > 0) {
|
|
2364
2366
|
originalAttributes.push(
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2367
|
+
t22.jsxAttribute(
|
|
2368
|
+
t22.jsxIdentifier("$variables"),
|
|
2369
|
+
t22.jsxExpressionContainer($variables)
|
|
2368
2370
|
)
|
|
2369
2371
|
);
|
|
2370
2372
|
}
|
|
2371
2373
|
const $elements = getNestedJsxElements(jsxScope);
|
|
2372
2374
|
if ($elements.elements.length > 0) {
|
|
2373
2375
|
originalAttributes.push(
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2376
|
+
t22.jsxAttribute(
|
|
2377
|
+
t22.jsxIdentifier("$elements"),
|
|
2378
|
+
t22.jsxExpressionContainer($elements)
|
|
2377
2379
|
)
|
|
2378
2380
|
);
|
|
2379
2381
|
}
|
|
2380
2382
|
const $functions = getJsxFunctions(jsxScope);
|
|
2381
2383
|
if ($functions.properties.length > 0) {
|
|
2382
2384
|
originalAttributes.push(
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2385
|
+
t22.jsxAttribute(
|
|
2386
|
+
t22.jsxIdentifier("$functions"),
|
|
2387
|
+
t22.jsxExpressionContainer($functions)
|
|
2386
2388
|
)
|
|
2387
2389
|
);
|
|
2388
2390
|
}
|
|
2389
2391
|
const $expressions = getJsxExpressions(jsxScope);
|
|
2390
2392
|
if ($expressions.elements.length > 0) {
|
|
2391
2393
|
originalAttributes.push(
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2394
|
+
t22.jsxAttribute(
|
|
2395
|
+
t22.jsxIdentifier("$expressions"),
|
|
2396
|
+
t22.jsxExpressionContainer($expressions)
|
|
2395
2397
|
)
|
|
2396
2398
|
);
|
|
2397
2399
|
}
|
|
2398
2400
|
if (mode === "server") {
|
|
2399
2401
|
const loadDictionaryImport = getOrCreateImport(payload.ast, {
|
|
2400
2402
|
exportedName: "loadDictionary",
|
|
2401
|
-
moduleName:
|
|
2403
|
+
moduleName: ModuleId.ReactRSC
|
|
2402
2404
|
});
|
|
2403
2405
|
originalAttributes.push(
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
[
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
[
|
|
2406
|
+
t22.jsxAttribute(
|
|
2407
|
+
t22.jsxIdentifier("$loadDictionary"),
|
|
2408
|
+
t22.jsxExpressionContainer(
|
|
2409
|
+
t22.arrowFunctionExpression(
|
|
2410
|
+
[t22.identifier("locale")],
|
|
2411
|
+
t22.callExpression(
|
|
2412
|
+
t22.identifier(loadDictionaryImport.importedName),
|
|
2413
|
+
[t22.identifier("locale")]
|
|
2412
2414
|
)
|
|
2413
2415
|
)
|
|
2414
2416
|
)
|
|
2415
2417
|
)
|
|
2416
2418
|
);
|
|
2417
2419
|
}
|
|
2418
|
-
const newNode =
|
|
2419
|
-
|
|
2420
|
-
|
|
2420
|
+
const newNode = t22.jsxElement(
|
|
2421
|
+
t22.jsxOpeningElement(
|
|
2422
|
+
t22.jsxIdentifier(lingoComponentImport.importedName),
|
|
2421
2423
|
originalAttributes,
|
|
2422
2424
|
true
|
|
2423
2425
|
// selfClosing
|
|
@@ -2435,7 +2437,7 @@ var lingoJsxScopeInjectMutation = createCodeMutation((payload) => {
|
|
|
2435
2437
|
});
|
|
2436
2438
|
|
|
2437
2439
|
// src/jsx-remove-attributes.ts
|
|
2438
|
-
import * as
|
|
2440
|
+
import * as t23 from "@babel/types";
|
|
2439
2441
|
import traverse9 from "@babel/traverse";
|
|
2440
2442
|
var jsxRemoveAttributesMutation = createCodeMutation(
|
|
2441
2443
|
(payload) => {
|
|
@@ -2448,7 +2450,7 @@ var jsxRemoveAttributesMutation = createCodeMutation(
|
|
|
2448
2450
|
JSXElement(path7) {
|
|
2449
2451
|
const openingElement = path7.node.openingElement;
|
|
2450
2452
|
openingElement.attributes = openingElement.attributes.filter((attr) => {
|
|
2451
|
-
const removeAttr =
|
|
2453
|
+
const removeAttr = t23.isJSXAttribute(attr) && t23.isJSXIdentifier(attr.name) && ATTRIBUTES_TO_REMOVE.includes(attr.name.name);
|
|
2452
2454
|
return !removeAttr;
|
|
2453
2455
|
});
|
|
2454
2456
|
}
|
|
@@ -2460,10 +2462,10 @@ var jsxRemoveAttributesMutation = createCodeMutation(
|
|
|
2460
2462
|
);
|
|
2461
2463
|
|
|
2462
2464
|
// src/client-dictionary-loader.ts
|
|
2463
|
-
import * as
|
|
2465
|
+
import * as t24 from "@babel/types";
|
|
2464
2466
|
var clientDictionaryLoaderMutation = createCodeMutation((payload) => {
|
|
2465
2467
|
const invokations = findInvokations(payload.ast, {
|
|
2466
|
-
moduleName:
|
|
2468
|
+
moduleName: ModuleId.ReactClient,
|
|
2467
2469
|
functionName: "loadDictionary"
|
|
2468
2470
|
});
|
|
2469
2471
|
const allLocales = Array.from(
|
|
@@ -2471,10 +2473,10 @@ var clientDictionaryLoaderMutation = createCodeMutation((payload) => {
|
|
|
2471
2473
|
);
|
|
2472
2474
|
for (const invokation of invokations) {
|
|
2473
2475
|
const internalDictionaryLoader = getOrCreateImport(payload.ast, {
|
|
2474
|
-
moduleName:
|
|
2476
|
+
moduleName: ModuleId.ReactClient,
|
|
2475
2477
|
exportedName: "loadDictionary_internal"
|
|
2476
2478
|
});
|
|
2477
|
-
if (
|
|
2479
|
+
if (t24.isIdentifier(invokation.callee)) {
|
|
2478
2480
|
invokation.callee.name = internalDictionaryLoader.importedName;
|
|
2479
2481
|
}
|
|
2480
2482
|
const dictionaryPath = getDictionaryPath({
|
|
@@ -2482,19 +2484,7 @@ var clientDictionaryLoaderMutation = createCodeMutation((payload) => {
|
|
|
2482
2484
|
lingoDir: payload.params.lingoDir,
|
|
2483
2485
|
relativeFilePath: payload.relativeFilePath
|
|
2484
2486
|
});
|
|
2485
|
-
const localeImportMap =
|
|
2486
|
-
allLocales.map(
|
|
2487
|
-
(locale) => t23.objectProperty(
|
|
2488
|
-
t23.identifier(locale),
|
|
2489
|
-
t23.arrowFunctionExpression(
|
|
2490
|
-
[],
|
|
2491
|
-
t23.callExpression(t23.identifier("import"), [
|
|
2492
|
-
t23.stringLiteral(`${dictionaryPath}?locale=${locale}`)
|
|
2493
|
-
])
|
|
2494
|
-
)
|
|
2495
|
-
)
|
|
2496
|
-
)
|
|
2497
|
-
);
|
|
2487
|
+
const localeImportMap = createLocaleImportMap(allLocales, dictionaryPath);
|
|
2498
2488
|
invokation.arguments.push(localeImportMap);
|
|
2499
2489
|
}
|
|
2500
2490
|
return payload;
|