@dune2/babel 0.4.1 → 1.0.0

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/dist/index.cjs CHANGED
@@ -1,176 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/i18n/tFunction.ts
2
- var tFunctionPlugin = (api) => {
3
- const { types: t } = api;
4
- const tFunction = t.identifier("t");
5
- return {
6
- name: "tFunctionPlugin",
7
- visitor: {
8
- TaggedTemplateExpression(path) {
9
- const { tag, quasi } = path.node;
10
- if (tag.type !== "Identifier" || tag.name !== "t") {
11
- return;
12
- }
13
- const { quasis, expressions } = quasi;
14
- let id = "";
15
- let slots = {};
16
- let hasSlots = false;
17
- quasis.forEach((v, i) => {
18
- id += v.value.raw;
19
- const exp = expressions[i];
20
- if (t.isExpression(exp)) {
21
- const varName = t.isIdentifier(exp) ? exp.name : i;
22
- id += `{${varName}}`;
23
- slots[varName] = exp;
24
- hasSlots = true;
25
- }
26
- });
27
- const args = [t.stringLiteral(id)];
28
- if (hasSlots) {
29
- const slotsObj = t.objectExpression(
30
- Object.entries(slots).map(([key, value]) => {
31
- return t.objectProperty(t.identifier(key), value);
32
- })
33
- );
34
- args.push(slotsObj);
35
- }
36
- path.replaceWith(t.callExpression(tFunction, args));
37
- }
38
- }
39
- };
40
- };
41
-
42
- // src/i18n/trans.ts
43
- var transPlugin = (api) => {
44
- const { types: t } = api;
45
- const TransJSXIdent = t.jsxIdentifier("Trans");
46
- const idJSXIdent = t.jsxIdentifier("id");
47
- const messageJSXIdent = t.jsxIdentifier("message");
48
- const valuesJSXIdent = t.jsxIdentifier("values");
49
- const componentsJSXIdent = t.jsxIdentifier("components");
50
- return {
51
- name: "transPlugin",
52
- visitor: {
53
- JSXElement(path) {
54
- const { openingElement } = path.node;
55
- if (!t.isJSXIdentifier(openingElement.name, TransJSXIdent)) {
56
- return;
57
- }
58
- let id = "";
59
- let userDefinedId = false;
60
- openingElement.attributes.forEach((attr) => {
61
- if (userDefinedId) {
62
- return;
63
- }
64
- if (!t.isJSXAttribute(attr) || !t.isJSXIdentifier(attr.name, idJSXIdent)) {
65
- return;
66
- }
67
- let idExpr;
68
- if (t.isJSXExpressionContainer(attr.value) && t.isStringLiteral(attr.value.expression)) {
69
- idExpr = attr.value.expression;
70
- } else if (t.isStringLiteral(attr.value)) {
71
- idExpr = attr.value;
72
- }
73
- if (!idExpr) {
74
- throw path.buildCodeFrameError(
75
- `Trans id attribute must be static string`,
76
- Error
77
- );
78
- }
79
- id = idExpr.value;
80
- userDefinedId = true;
81
- });
82
- const { msg, vars, components } = workChildren(path.node.children, t);
83
- if (!userDefinedId) {
84
- id = msg;
85
- }
86
- path.node.children = [];
87
- if (!userDefinedId) {
88
- openingElement.attributes.push(
89
- t.jsxAttribute(idJSXIdent, t.stringLiteral(id))
90
- );
91
- }
92
- if (id !== msg) {
93
- openingElement.attributes.push(
94
- t.jsxAttribute(messageJSXIdent, t.stringLiteral(msg))
95
- );
96
- }
97
- const valuesAttr = exprMapToJSXAttribute(valuesJSXIdent, vars, t);
98
- if (valuesAttr) {
99
- openingElement.attributes.push(valuesAttr);
100
- }
101
- const componentsAttr = exprMapToJSXAttribute(
102
- componentsJSXIdent,
103
- components,
104
- t
105
- );
106
- if (componentsAttr) {
107
- openingElement.attributes.push(componentsAttr);
108
- }
109
- }
110
- }
111
- };
112
- };
113
- function workChildren(children, t, vars = {}, components = {}, i = 0) {
114
- let msg = "";
115
- children.forEach((child, index) => {
116
- if (t.isJSXText(child)) {
117
- const text = child.value.replace(/^\n\s*/, "").replace(/\s*\n$/, "").replace(/\n\s*$/, " ");
118
- msg += text;
119
- return;
120
- }
121
- if (t.isJSXExpressionContainer(child)) {
122
- if (t.isJSXEmptyExpression(child.expression)) {
123
- return;
124
- }
125
- let varName = "";
126
- if (t.isIdentifier(child.expression)) {
127
- varName = child.expression.name;
128
- } else {
129
- varName = i + "";
130
- i++;
131
- }
132
- vars[varName] = child.expression;
133
- msg += `{${varName}}`;
134
- return;
135
- }
136
- if (t.isJSXElement(child)) {
137
- if (child.children.length) {
138
- msg += `<${i}>`;
139
- const { msg: childMsg, i: childI } = workChildren(
140
- child.children,
141
- t,
142
- vars,
143
- components,
144
- i + 1
145
- );
146
- components[i] = child;
147
- msg += childMsg;
148
- msg += `</${i}>`;
149
- i = childI;
150
- } else {
151
- msg += `<${i}/>`;
152
- components[i] = child;
153
- i++;
154
- }
155
- return;
156
- }
157
- });
158
- return { msg, i, vars, components };
159
- }
160
- function exprMapToJSXAttribute(keyIdentifier, exprMap, t) {
161
- const props = [];
162
- Object.entries(exprMap).forEach(([key, value]) => {
163
- props.push(t.objectProperty(t.identifier(key), value));
164
- });
165
- if (props.length) {
166
- return t.jsxAttribute(
167
- keyIdentifier,
168
- t.jsxExpressionContainer(t.objectExpression(props))
169
- );
170
- }
171
- }
172
-
173
-
174
-
175
- exports.tFunctionPlugin = tFunctionPlugin; exports.transPlugin = transPlugin;
176
- //# sourceMappingURL=index.cjs.map
1
+ "use strict";//# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/i18n/tFunction.ts","../src/i18n/trans.ts"],"names":[],"mappings":";AAIO,IAAM,kBAAkB,CAAC,QAAiC;AAC/D,QAAM,EAAE,OAAO,EAAE,IAAI;AAErB,QAAM,YAAY,EAAE,WAAW,GAAG;AAElC,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,MACP,yBAAyB,MAAM;AAC7B,cAAM,EAAE,KAAK,MAAM,IAAI,KAAK;AAC5B,YAAI,IAAI,SAAS,gBAAgB,IAAI,SAAS,KAAK;AAEjD;AAAA,QACF;AACA,cAAM,EAAE,QAAQ,YAAY,IAAI;AAChC,YAAI,KAAK;AACT,YAAI,QAAoC,CAAC;AACzC,YAAI,WAAW;AAEf,eAAO,QAAQ,CAAC,GAAG,MAAM;AACvB,gBAAM,EAAE,MAAM;AACd,gBAAM,MAAM,YAAY,CAAC;AACzB,cAA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,UAAU,GAAG;AACxB,aAAO;AACP;AAAA,IACF;AACA,QAAI,EAAE,yBAAyB,KAAK,GAAG;AACrC,UAAI,EAAE,qBAAqB,MAAM,UAAU,GAAG;AAG5C;AAAA,MACF;AACA,UAAI,UAAU;AACd,UAAI,EAAE,aAAa,MAAM,UAAU,GAAG;AAGpC,kBAAU,MAAM,WAAW;AAAA,MAC7B,OAAO;AAGL,kBAAU,IAAI;AACd;AAAA,MACF;AACA,WAAK,OAAO,IAAI,MAAM;AACtB,aAAO,IAAI,OAAO;AAClB;AAAA,IACF;AACA,QAAI,EAAE,aAAa,KAAK,GAAG;AACzB,UAAI,MAAM,SAAS,QAAQ;AAGzB,eAAO,IAAI,CAAC;AACZ,cAAM,EAAE,KAAK,UAAU,GAAG,OAAO,IAAI;AAAA,UACnC,MAAM;AAAA,UACN;AAAA,UACA;AAAA,UACA;AAAA,UACA,IAAI;AAAA,QACN;AACA,mBAAW,CAAC,IAAI;AAChB,eAAO;AACP,eAAO,KAAK,CAAC;AACb,YAAI;AAAA,MACN,OAAO;AAEL,eAAO,IAAI,CAAC;AACZ,mBAAW,CAAC,IAAI;AAChB;AAAA,MACF;AACA;AAAA,IACF;AAAA,EACF,CAAC;AAED,SAAO,EAAE,KAAK,GAAG,MAAM,WAAW;AACpC;AAEA,SAAS,sBACP,eACA,SAEA,GAC0B;AAC1B,QAAM,QAA0B,CAAC;AACjC,SAAO,QAAQ,OAAO,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AAChD,UAAM,KAAK,EAAE,eAAe,EAAE,WAAW,GAAG,GAAG,KAAK,CAAC;AAAA,EACvD,CAAC;AACD,MAAI,MAAM,QAAQ;AAChB,WAAO,EAAE;AAAA,MACP;AAAA,MACA,EAAE,uBAAuB,EAAE,iBAAiB,KAAK,CAAC;AAAA,IACpD;AAAA,EACF;AACF","sourcesContent":["import type babel from \"@babel/core\";\nimport type { PluginObj } from \"@babel/core\";\nimport type { Expression } from \"@babel/types\";\n\nexport const tFunctionPlugin = (api: typeof babel): PluginObj => {\n const { types: t } = api;\n\n const tFunction = t.identifier(\"t\");\n\n return {\n name: \"tFunctionPlugin\",\n visitor: {\n TaggedTemplateExpression(path) {\n const { tag, quasi } = path.node;\n if (tag.type !== \"Identifier\" || tag.name !== \"t\") {\n // not t function\n return;\n }\n const { quasis, expressions } = quasi;\n let id = \"\";\n let slots: Record<string, Expression> = {};\n let hasSlots = false;\n\n quasis.forEach((v, i) => {\n id += v.value.raw;\n const exp = expressions[i];\n if (t.isExpression(exp)) {\n // 能获取到变量名的情况,用变量名,否则用索引\n // t`Refresh inbox ${name}` => name\n // t`Refresh inbox ${obj.name}` => 0\n const varName = t.isIdentifier(exp) ? exp.name : i;\n id += `{${varName}}`;\n slots[varName] = exp;\n hasSlots = true;\n }\n });\n\n // 组装新\n // t(id,{ [key]: value })\n const args: Expression[] = [t.stringLiteral(id)];\n if (hasSlots) {\n const slotsObj = t.objectExpression(\n Object.entries(slots).map(([key, value]) => {\n return t.objectProperty(t.identifier(key), value);\n }),\n );\n args.push(slotsObj);\n }\n path.replaceWith(t.callExpression(tFunction, args));\n },\n },\n };\n};\n","import type babel from \"@babel/core\";\nimport type { PluginObj } from \"@babel/core\";\nimport type {\n Expression,\n JSXAttribute,\n JSXElement,\n JSXIdentifier,\n ObjectProperty,\n StringLiteral,\n} from \"@babel/types\";\n\nexport const transPlugin = (api: typeof babel): PluginObj => {\n const { types: t } = api;\n\n const TransJSXIdent = t.jsxIdentifier(\"Trans\");\n const idJSXIdent = t.jsxIdentifier(\"id\");\n const messageJSXIdent = t.jsxIdentifier(\"message\");\n const valuesJSXIdent = t.jsxIdentifier(\"values\");\n const componentsJSXIdent = t.jsxIdentifier(\"components\");\n\n return {\n name: \"transPlugin\",\n visitor: {\n JSXElement(path) {\n const { openingElement } = path.node;\n if (!t.isJSXIdentifier(openingElement.name, TransJSXIdent)) {\n // 不是 Trans 组件\n return;\n }\n let id = \"\";\n let userDefinedId = false;\n\n openingElement.attributes.forEach((attr) => {\n if (userDefinedId) {\n return;\n }\n if (\n !t.isJSXAttribute(attr) ||\n !t.isJSXIdentifier(attr.name, idJSXIdent)\n ) {\n // 不是 <Trans id=\"xxx\" ></Trans>\n return;\n }\n let idExpr: StringLiteral | undefined;\n if (\n t.isJSXExpressionContainer(attr.value) &&\n t.isStringLiteral(attr.value.expression)\n ) {\n idExpr = attr.value.expression;\n } else if (t.isStringLiteral(attr.value)) {\n idExpr = attr.value;\n }\n\n if (!idExpr) {\n // 不支持 id 属性为变量 或者 模版字符串\n throw path.buildCodeFrameError(\n `Trans id attribute must be static string`,\n Error,\n );\n }\n id = idExpr.value;\n userDefinedId = true;\n });\n\n const { msg, vars, components } = workChildren(path.node.children, t);\n if (!userDefinedId) {\n // 外部没有指定 id,则使用从 children 中提取的文本\n id = msg;\n }\n\n // 清空 children,Trans 组件编译后没有 children\n path.node.children = [];\n // 添加 id props\n if (!userDefinedId) {\n openingElement.attributes.push(\n t.jsxAttribute(idJSXIdent, t.stringLiteral(id)),\n );\n }\n // 添加 message props\n if (id !== msg) {\n openingElement.attributes.push(\n t.jsxAttribute(messageJSXIdent, t.stringLiteral(msg)),\n );\n }\n // 添加 values props\n const valuesAttr = exprMapToJSXAttribute(valuesJSXIdent, vars, t);\n if (valuesAttr) {\n openingElement.attributes.push(valuesAttr);\n }\n // 添加 components props\n const componentsAttr = exprMapToJSXAttribute(\n componentsJSXIdent,\n components,\n t,\n );\n if (componentsAttr) {\n openingElement.attributes.push(componentsAttr);\n }\n },\n },\n };\n};\n\nfunction workChildren(\n children: JSXElement[\"children\"],\n t: typeof babel.types,\n vars: Record<string, Expression> = {},\n components: Record<string, Expression> = {},\n i: number = 0,\n) {\n let msg = \"\";\n children.forEach((child, index) => {\n if (t.isJSXText(child)) {\n // 开头 和 结尾 babel 都会包括回车,如果有的话\n const text = child.value\n // 换行 + 空格 开头\n .replace(/^\\n\\s*/, \"\")\n // 空格 + 换行 结尾\n .replace(/\\s*\\n$/, \"\")\n // 换行 + 空格 结尾\n .replace(/\\n\\s*$/, \" \");\n msg += text;\n return;\n }\n if (t.isJSXExpressionContainer(child)) {\n if (t.isJSXEmptyExpression(child.expression)) {\n // 空表达式,直接忽略\n // case: <Trans>hello {}</Trans>\n return;\n }\n let varName = \"\";\n if (t.isIdentifier(child.expression)) {\n // 能获取到变量名的情况,用变量名,否则用索引\n // case: <Trans>hello {name}</Trans>\n varName = child.expression.name;\n } else {\n // 直接用 索引\n // case: <Trans>hello {user.name}</Trans>\n varName = i + \"\";\n i++;\n }\n vars[varName] = child.expression;\n msg += `{${varName}}`;\n return;\n }\n if (t.isJSXElement(child)) {\n if (child.children.length) {\n // case: <Trans>hello <a>world</a></Trans>\n // 中的 <a>world</a>\n msg += `<${i}>`;\n const { msg: childMsg, i: childI } = workChildren(\n child.children,\n t,\n vars,\n components,\n i + 1,\n );\n components[i] = child;\n msg += childMsg;\n msg += `</${i}>`;\n i = childI;\n } else {\n // case: <Trans>hello <br/> world</Trans>\n msg += `<${i}/>`;\n components[i] = child;\n i++;\n }\n return;\n }\n });\n\n return { msg, i, vars, components };\n}\n\nfunction exprMapToJSXAttribute(\n keyIdentifier: JSXIdentifier,\n exprMap: Record<string, Expression>,\n\n t: typeof babel.types,\n): JSXAttribute | undefined {\n const props: ObjectProperty[] = [];\n Object.entries(exprMap).forEach(([key, value]) => {\n props.push(t.objectProperty(t.identifier(key), value));\n });\n if (props.length) {\n return t.jsxAttribute(\n keyIdentifier,\n t.jsxExpressionContainer(t.objectExpression(props)),\n );\n }\n}\n"]}
1
+ {"version":3,"sources":["/Users/Leon.Liao-zd/work/github/next-tools/packages/babel/dist/index.cjs"],"names":[],"mappings":"AAAA","file":"/Users/Leon.Liao-zd/work/github/next-tools/packages/babel/dist/index.cjs"}
package/dist/index.d.cts CHANGED
@@ -1,7 +1,2 @@
1
- import babel, { PluginObj } from '@babel/core';
2
1
 
3
- declare const tFunctionPlugin: (api: typeof babel) => PluginObj;
4
-
5
- declare const transPlugin: (api: typeof babel) => PluginObj;
6
-
7
- export { tFunctionPlugin, transPlugin };
2
+ export { }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,2 @@
1
- import babel, { PluginObj } from '@babel/core';
2
1
 
3
- declare const tFunctionPlugin: (api: typeof babel) => PluginObj;
4
-
5
- declare const transPlugin: (api: typeof babel) => PluginObj;
6
-
7
- export { tFunctionPlugin, transPlugin };
2
+ export { }
package/dist/index.js CHANGED
@@ -1,176 +1 @@
1
- // src/i18n/tFunction.ts
2
- var tFunctionPlugin = (api) => {
3
- const { types: t } = api;
4
- const tFunction = t.identifier("t");
5
- return {
6
- name: "tFunctionPlugin",
7
- visitor: {
8
- TaggedTemplateExpression(path) {
9
- const { tag, quasi } = path.node;
10
- if (tag.type !== "Identifier" || tag.name !== "t") {
11
- return;
12
- }
13
- const { quasis, expressions } = quasi;
14
- let id = "";
15
- let slots = {};
16
- let hasSlots = false;
17
- quasis.forEach((v, i) => {
18
- id += v.value.raw;
19
- const exp = expressions[i];
20
- if (t.isExpression(exp)) {
21
- const varName = t.isIdentifier(exp) ? exp.name : i;
22
- id += `{${varName}}`;
23
- slots[varName] = exp;
24
- hasSlots = true;
25
- }
26
- });
27
- const args = [t.stringLiteral(id)];
28
- if (hasSlots) {
29
- const slotsObj = t.objectExpression(
30
- Object.entries(slots).map(([key, value]) => {
31
- return t.objectProperty(t.identifier(key), value);
32
- })
33
- );
34
- args.push(slotsObj);
35
- }
36
- path.replaceWith(t.callExpression(tFunction, args));
37
- }
38
- }
39
- };
40
- };
41
-
42
- // src/i18n/trans.ts
43
- var transPlugin = (api) => {
44
- const { types: t } = api;
45
- const TransJSXIdent = t.jsxIdentifier("Trans");
46
- const idJSXIdent = t.jsxIdentifier("id");
47
- const messageJSXIdent = t.jsxIdentifier("message");
48
- const valuesJSXIdent = t.jsxIdentifier("values");
49
- const componentsJSXIdent = t.jsxIdentifier("components");
50
- return {
51
- name: "transPlugin",
52
- visitor: {
53
- JSXElement(path) {
54
- const { openingElement } = path.node;
55
- if (!t.isJSXIdentifier(openingElement.name, TransJSXIdent)) {
56
- return;
57
- }
58
- let id = "";
59
- let userDefinedId = false;
60
- openingElement.attributes.forEach((attr) => {
61
- if (userDefinedId) {
62
- return;
63
- }
64
- if (!t.isJSXAttribute(attr) || !t.isJSXIdentifier(attr.name, idJSXIdent)) {
65
- return;
66
- }
67
- let idExpr;
68
- if (t.isJSXExpressionContainer(attr.value) && t.isStringLiteral(attr.value.expression)) {
69
- idExpr = attr.value.expression;
70
- } else if (t.isStringLiteral(attr.value)) {
71
- idExpr = attr.value;
72
- }
73
- if (!idExpr) {
74
- throw path.buildCodeFrameError(
75
- `Trans id attribute must be static string`,
76
- Error
77
- );
78
- }
79
- id = idExpr.value;
80
- userDefinedId = true;
81
- });
82
- const { msg, vars, components } = workChildren(path.node.children, t);
83
- if (!userDefinedId) {
84
- id = msg;
85
- }
86
- path.node.children = [];
87
- if (!userDefinedId) {
88
- openingElement.attributes.push(
89
- t.jsxAttribute(idJSXIdent, t.stringLiteral(id))
90
- );
91
- }
92
- if (id !== msg) {
93
- openingElement.attributes.push(
94
- t.jsxAttribute(messageJSXIdent, t.stringLiteral(msg))
95
- );
96
- }
97
- const valuesAttr = exprMapToJSXAttribute(valuesJSXIdent, vars, t);
98
- if (valuesAttr) {
99
- openingElement.attributes.push(valuesAttr);
100
- }
101
- const componentsAttr = exprMapToJSXAttribute(
102
- componentsJSXIdent,
103
- components,
104
- t
105
- );
106
- if (componentsAttr) {
107
- openingElement.attributes.push(componentsAttr);
108
- }
109
- }
110
- }
111
- };
112
- };
113
- function workChildren(children, t, vars = {}, components = {}, i = 0) {
114
- let msg = "";
115
- children.forEach((child, index) => {
116
- if (t.isJSXText(child)) {
117
- const text = child.value.replace(/^\n\s*/, "").replace(/\s*\n$/, "").replace(/\n\s*$/, " ");
118
- msg += text;
119
- return;
120
- }
121
- if (t.isJSXExpressionContainer(child)) {
122
- if (t.isJSXEmptyExpression(child.expression)) {
123
- return;
124
- }
125
- let varName = "";
126
- if (t.isIdentifier(child.expression)) {
127
- varName = child.expression.name;
128
- } else {
129
- varName = i + "";
130
- i++;
131
- }
132
- vars[varName] = child.expression;
133
- msg += `{${varName}}`;
134
- return;
135
- }
136
- if (t.isJSXElement(child)) {
137
- if (child.children.length) {
138
- msg += `<${i}>`;
139
- const { msg: childMsg, i: childI } = workChildren(
140
- child.children,
141
- t,
142
- vars,
143
- components,
144
- i + 1
145
- );
146
- components[i] = child;
147
- msg += childMsg;
148
- msg += `</${i}>`;
149
- i = childI;
150
- } else {
151
- msg += `<${i}/>`;
152
- components[i] = child;
153
- i++;
154
- }
155
- return;
156
- }
157
- });
158
- return { msg, i, vars, components };
159
- }
160
- function exprMapToJSXAttribute(keyIdentifier, exprMap, t) {
161
- const props = [];
162
- Object.entries(exprMap).forEach(([key, value]) => {
163
- props.push(t.objectProperty(t.identifier(key), value));
164
- });
165
- if (props.length) {
166
- return t.jsxAttribute(
167
- keyIdentifier,
168
- t.jsxExpressionContainer(t.objectExpression(props))
169
- );
170
- }
171
- }
172
- export {
173
- tFunctionPlugin,
174
- transPlugin
175
- };
176
1
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/i18n/tFunction.ts","../src/i18n/trans.ts"],"sourcesContent":["import type babel from \"@babel/core\";\nimport type { PluginObj } from \"@babel/core\";\nimport type { Expression } from \"@babel/types\";\n\nexport const tFunctionPlugin = (api: typeof babel): PluginObj => {\n const { types: t } = api;\n\n const tFunction = t.identifier(\"t\");\n\n return {\n name: \"tFunctionPlugin\",\n visitor: {\n TaggedTemplateExpression(path) {\n const { tag, quasi } = path.node;\n if (tag.type !== \"Identifier\" || tag.name !== \"t\") {\n // not t function\n return;\n }\n const { quasis, expressions } = quasi;\n let id = \"\";\n let slots: Record<string, Expression> = {};\n let hasSlots = false;\n\n quasis.forEach((v, i) => {\n id += v.value.raw;\n const exp = expressions[i];\n if (t.isExpression(exp)) {\n // 能获取到变量名的情况,用变量名,否则用索引\n // t`Refresh inbox ${name}` => name\n // t`Refresh inbox ${obj.name}` => 0\n const varName = t.isIdentifier(exp) ? exp.name : i;\n id += `{${varName}}`;\n slots[varName] = exp;\n hasSlots = true;\n }\n });\n\n // 组装新\n // t(id,{ [key]: value })\n const args: Expression[] = [t.stringLiteral(id)];\n if (hasSlots) {\n const slotsObj = t.objectExpression(\n Object.entries(slots).map(([key, value]) => {\n return t.objectProperty(t.identifier(key), value);\n }),\n );\n args.push(slotsObj);\n }\n path.replaceWith(t.callExpression(tFunction, args));\n },\n },\n };\n};\n","import type babel from \"@babel/core\";\nimport type { PluginObj } from \"@babel/core\";\nimport type {\n Expression,\n JSXAttribute,\n JSXElement,\n JSXIdentifier,\n ObjectProperty,\n StringLiteral,\n} from \"@babel/types\";\n\nexport const transPlugin = (api: typeof babel): PluginObj => {\n const { types: t } = api;\n\n const TransJSXIdent = t.jsxIdentifier(\"Trans\");\n const idJSXIdent = t.jsxIdentifier(\"id\");\n const messageJSXIdent = t.jsxIdentifier(\"message\");\n const valuesJSXIdent = t.jsxIdentifier(\"values\");\n const componentsJSXIdent = t.jsxIdentifier(\"components\");\n\n return {\n name: \"transPlugin\",\n visitor: {\n JSXElement(path) {\n const { openingElement } = path.node;\n if (!t.isJSXIdentifier(openingElement.name, TransJSXIdent)) {\n // 不是 Trans 组件\n return;\n }\n let id = \"\";\n let userDefinedId = false;\n\n openingElement.attributes.forEach((attr) => {\n if (userDefinedId) {\n return;\n }\n if (\n !t.isJSXAttribute(attr) ||\n !t.isJSXIdentifier(attr.name, idJSXIdent)\n ) {\n // 不是 <Trans id=\"xxx\" ></Trans>\n return;\n }\n let idExpr: StringLiteral | undefined;\n if (\n t.isJSXExpressionContainer(attr.value) &&\n t.isStringLiteral(attr.value.expression)\n ) {\n idExpr = attr.value.expression;\n } else if (t.isStringLiteral(attr.value)) {\n idExpr = attr.value;\n }\n\n if (!idExpr) {\n // 不支持 id 属性为变量 或者 模版字符串\n throw path.buildCodeFrameError(\n `Trans id attribute must be static string`,\n Error,\n );\n }\n id = idExpr.value;\n userDefinedId = true;\n });\n\n const { msg, vars, components } = workChildren(path.node.children, t);\n if (!userDefinedId) {\n // 外部没有指定 id,则使用从 children 中提取的文本\n id = msg;\n }\n\n // 清空 children,Trans 组件编译后没有 children\n path.node.children = [];\n // 添加 id props\n if (!userDefinedId) {\n openingElement.attributes.push(\n t.jsxAttribute(idJSXIdent, t.stringLiteral(id)),\n );\n }\n // 添加 message props\n if (id !== msg) {\n openingElement.attributes.push(\n t.jsxAttribute(messageJSXIdent, t.stringLiteral(msg)),\n );\n }\n // 添加 values props\n const valuesAttr = exprMapToJSXAttribute(valuesJSXIdent, vars, t);\n if (valuesAttr) {\n openingElement.attributes.push(valuesAttr);\n }\n // 添加 components props\n const componentsAttr = exprMapToJSXAttribute(\n componentsJSXIdent,\n components,\n t,\n );\n if (componentsAttr) {\n openingElement.attributes.push(componentsAttr);\n }\n },\n },\n };\n};\n\nfunction workChildren(\n children: JSXElement[\"children\"],\n t: typeof babel.types,\n vars: Record<string, Expression> = {},\n components: Record<string, Expression> = {},\n i: number = 0,\n) {\n let msg = \"\";\n children.forEach((child, index) => {\n if (t.isJSXText(child)) {\n // 开头 和 结尾 babel 都会包括回车,如果有的话\n const text = child.value\n // 换行 + 空格 开头\n .replace(/^\\n\\s*/, \"\")\n // 空格 + 换行 结尾\n .replace(/\\s*\\n$/, \"\")\n // 换行 + 空格 结尾\n .replace(/\\n\\s*$/, \" \");\n msg += text;\n return;\n }\n if (t.isJSXExpressionContainer(child)) {\n if (t.isJSXEmptyExpression(child.expression)) {\n // 空表达式,直接忽略\n // case: <Trans>hello {}</Trans>\n return;\n }\n let varName = \"\";\n if (t.isIdentifier(child.expression)) {\n // 能获取到变量名的情况,用变量名,否则用索引\n // case: <Trans>hello {name}</Trans>\n varName = child.expression.name;\n } else {\n // 直接用 索引\n // case: <Trans>hello {user.name}</Trans>\n varName = i + \"\";\n i++;\n }\n vars[varName] = child.expression;\n msg += `{${varName}}`;\n return;\n }\n if (t.isJSXElement(child)) {\n if (child.children.length) {\n // case: <Trans>hello <a>world</a></Trans>\n // 中的 <a>world</a>\n msg += `<${i}>`;\n const { msg: childMsg, i: childI } = workChildren(\n child.children,\n t,\n vars,\n components,\n i + 1,\n );\n components[i] = child;\n msg += childMsg;\n msg += `</${i}>`;\n i = childI;\n } else {\n // case: <Trans>hello <br/> world</Trans>\n msg += `<${i}/>`;\n components[i] = child;\n i++;\n }\n return;\n }\n });\n\n return { msg, i, vars, components };\n}\n\nfunction exprMapToJSXAttribute(\n keyIdentifier: JSXIdentifier,\n exprMap: Record<string, Expression>,\n\n t: typeof babel.types,\n): JSXAttribute | undefined {\n const props: ObjectProperty[] = [];\n Object.entries(exprMap).forEach(([key, value]) => {\n props.push(t.objectProperty(t.identifier(key), value));\n });\n if (props.length) {\n return t.jsxAttribute(\n keyIdentifier,\n t.jsxExpressionContainer(t.objectExpression(props)),\n );\n }\n}\n"],"mappings":";AAIO,IAAM,kBAAkB,CAAC,QAAiC;AAC/D,QAAM,EAAE,OAAO,EAAE,IAAI;AAErB,QAAM,YAAY,EAAE,WAAW,GAAG;AAElC,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,MACP,yBAAyB,MAAM;AAC7B,cAAM,EAAE,KAAK,MAAM,IAAI,KAAK;AAC5B,YAAI,IAAI,SAAS,gBAAgB,IAAI,SAAS,KAAK;AAEjD;AAAA,QACF;AACA,cAAM,EAAE,QAAQ,YAAY,IAAI;AAChC,YAAI,KAAK;AACT,YAAI,QAAoC,CAAC;AACzC,YAAI,WAAW;AAEf,eAAO,QAAQ,CAAC,GAAG,MAAM;AACvB,gBAAM,EAAE,MAAM;AACd,gBAAM,MAAM,YAAY,CAAC;AACzB,cAAI,EAAE,aAAa,GAAG,GAAG;AAIvB,kBAAM,UAAU,EAAE,aAAa,GAAG,IAAI,IAAI,OAAO;AACjD,kBAAM,IAAI,OAAO;AACjB,kBAAM,OAAO,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5C;AAAA,MACF;AACA,UAAI,UAAU;AACd,UAAI,EAAE,aAAa,MAAM,UAAU,GAAG;AAGpC,kBAAU,MAAM,WAAW;AAAA,MAC7B,OAAO;AAGL,kBAAU,IAAI;AACd;AAAA,MACF;AACA,WAAK,OAAO,IAAI,MAAM;AACtB,aAAO,IAAI,OAAO;AAClB;AAAA,IACF;AACA,QAAI,EAAE,aAAa,KAAK,GAAG;AACzB,UAAI,MAAM,SAAS,QAAQ;AAGzB,eAAO,IAAI,CAAC;AACZ,cAAM,EAAE,KAAK,UAAU,GAAG,OAAO,IAAI;AAAA,UACnC,MAAM;AAAA,UACN;AAAA,UACA;AAAA,UACA;AAAA,UACA,IAAI;AAAA,QACN;AACA,mBAAW,CAAC,IAAI;AAChB,eAAO;AACP,eAAO,KAAK,CAAC;AACb,YAAI;AAAA,MACN,OAAO;AAEL,eAAO,IAAI,CAAC;AACZ,mBAAW,CAAC,IAAI;AAChB;AAAA,MACF;AACA;AAAA,IACF;AAAA,EACF,CAAC;AAED,SAAO,EAAE,KAAK,GAAG,MAAM,WAAW;AACpC;AAEA,SAAS,sBACP,eACA,SAEA,GAC0B;AAC1B,QAAM,QAA0B,CAAC;AACjC,SAAO,QAAQ,OAAO,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AAChD,UAAM,KAAK,EAAE,eAAe,EAAE,WAAW,GAAG,GAAG,KAAK,CAAC;AAAA,EACvD,CAAC;AACD,MAAI,MAAM,QAAQ;AAChB,WAAO,EAAE;AAAA,MACP;AAAA,MACA,EAAE,uBAAuB,EAAE,iBAAiB,KAAK,CAAC;AAAA,IACpD;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dune2/babel",
3
- "version": "0.4.1",
3
+ "version": "1.0.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/liaoyinglong/next-tools.git",