Skip to content

Commit 7cbfade

Browse files
authored
Merge pull request alibaba#212 from alibaba/hotfix/code-gen-import-issue
hotfix: 出码模块 - 解决 componentName 和 exportName 不一致时生成的 import 语句的问题
2 parents d086267 + 7fcf11b commit 7cbfade

File tree

6 files changed

+332
-22
lines changed

6 files changed

+332
-22
lines changed

modules/code-generator/CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
### [1.0.3](https://github.com/alibaba/lowcode-engine/compare/@alilc/lowcode-code-generator@1.0.2...@alilc/lowcode-code-generator@1.0.3) (2022-03-29)
6+
7+
8+
### Features
9+
10+
* add getConvertedExtraKey / getOriginalExtraKey to utils ([8e7bb9d](https://github.com/alibaba/lowcode-engine/commit/8e7bb9d4b86454dd77c6928eb769cd764cad8630))
11+
12+
13+
### Bug Fixes
14+
15+
* 🐛 出码: 解决 componentName 和 exportName 不一致时生成的 import 语句的问题 ([eefc091](https://github.com/alibaba/lowcode-engine/commit/eefc091ee7e86d6214d20d486212cb5aff237946))
16+
* component cannot be redisplayed by configuration after rendering is closed ([c54f369](https://github.com/alibaba/lowcode-engine/commit/c54f369e1860d818479dda9d6429f851c0b08fa6))
17+
* fix loop configuration auto fill empty array issue ([d087092](https://github.com/alibaba/lowcode-engine/commit/d087092fd712eff0556adacda692d3ff6f2f9f22))
18+
* make important true by default ([c63b6e1](https://github.com/alibaba/lowcode-engine/commit/c63b6e1bfadc3fc87ed41840952e02ffbff24fab))
19+
* make insertAfter & insertBefore work ([70fd372](https://github.com/alibaba/lowcode-engine/commit/70fd3720d098d6e227acb9281ee22feee66b9c0b))
20+
* npm源 ([437adcc](https://github.com/alibaba/lowcode-engine/commit/437adccf5f2dbb400de6e2bef10cfc4b65286f2b))
21+
* prop should return undefined when all items are undefined ([5bb9ec7](https://github.com/alibaba/lowcode-engine/commit/5bb9ec7a1dfaabfdb5369226b54d5f63a7999e59))
22+
* should not create new prop while querying fileName ([19c207d](https://github.com/alibaba/lowcode-engine/commit/19c207d29de045f473ba73baaf34e7294d40261a))
23+
* variable binding lost after modify the mock value ([ef95b56](https://github.com/alibaba/lowcode-engine/commit/ef95b5683273d8302bde1582de8afe3d87a808d8))
24+
* Workbench should receive the original skeleton other than shell skeleton ([d5c3ca1](https://github.com/alibaba/lowcode-engine/commit/d5c3ca1068ce2c2140980bd059d0da333574dc34))
25+
526
### [1.0.2](https://github.com/alibaba/lowcode-engine/compare/@alilc/lowcode-code-generator@1.0.2-beta.1...@alilc/lowcode-code-generator@1.0.2) (2022-03-08)
627

728
### [1.0.2-beta.1](https://github.com/alibaba/lowcode-engine/compare/@alilc/lowcode-code-generator@1.0.2-beta.0...@alilc/lowcode-code-generator@1.0.2-beta.1) (2022-03-08)

modules/code-generator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alilc/lowcode-code-generator",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "出码引擎 for LowCode Engine",
55
"license": "MIT",
66
"main": "lib/index.js",

modules/code-generator/src/plugins/common/esmodule.ts

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { flatMap } from 'lodash';
1+
import { flatMap, camelCase, get } from 'lodash';
22
import { COMMON_CHUNK_NAME } from '../../const/generator';
33

44
import {
@@ -71,6 +71,37 @@ function getDependencyIdentifier(info: IDependencyItem): string {
7171
return info.aliasName || info.exportName;
7272
}
7373

74+
function getExportNameOfDep(dep: IDependency): string {
75+
if (dep.destructuring) {
76+
return (
77+
dep.exportName ||
78+
dep.componentName ||
79+
throwNewError('destructuring dependency must have exportName or componentName')
80+
);
81+
}
82+
83+
if (!dep.subName) {
84+
return (
85+
dep.componentName ||
86+
dep.exportName ||
87+
throwNewError('dependency item must have componentName or exportName')
88+
);
89+
}
90+
91+
return (
92+
dep.exportName ||
93+
`__$${camelCase(
94+
get(dep, 'moduleName') ||
95+
get(dep, 'package') ||
96+
throwNewError('dep.moduleName or dep.package is undefined'),
97+
)}_default`
98+
);
99+
}
100+
101+
function throwNewError(msg: string): never {
102+
throw new Error(msg);
103+
}
104+
74105
function buildPackageImport(
75106
pkg: string,
76107
deps: IDependency[],
@@ -90,7 +121,7 @@ function buildPackageImport(
90121

91122
const depsInfo: IDependencyItem[] = deps.map((dep) => {
92123
const info: IDependencyItem = {
93-
exportName: dep.exportName,
124+
exportName: getExportNameOfDep(dep),
94125
isDefault: !dep.destructuring,
95126
subName: dep.subName || undefined,
96127
nodeIdentifier: dep.componentName || undefined,
@@ -171,24 +202,21 @@ function buildPackageImport(
171202

172203
// 发现 nodeIdentifier 与 exportName 或者 aliasName 冲突的场景
173204
const nodeIdentifiers = depsInfo.map((info) => info.nodeIdentifier).filter(Boolean);
174-
const conflictInfos = flatMap(
175-
Object.keys(exportItems),
176-
(exportName) => {
177-
const exportItem = exportItems[exportName];
178-
const usedNames = [
179-
...exportItem.aliasNames,
180-
...(exportItem.needOriginExport || exportItem.aliasNames.length <= 0 ? [exportName] : []),
205+
const conflictInfos = flatMap(Object.keys(exportItems), (exportName) => {
206+
const exportItem = exportItems[exportName];
207+
const usedNames = [
208+
...exportItem.aliasNames,
209+
...(exportItem.needOriginExport || exportItem.aliasNames.length <= 0 ? [exportName] : []),
210+
];
211+
const conflictNames = usedNames.filter((n) => nodeIdentifiers.indexOf(n) >= 0);
212+
if (conflictNames.length > 0) {
213+
return [
214+
...(conflictNames.indexOf(exportName) >= 0 ? [[exportName, true, exportItem]] : []),
215+
...conflictNames.filter((n) => n !== exportName).map((n) => [n, false, exportItem]),
181216
];
182-
const conflictNames = usedNames.filter((n) => nodeIdentifiers.indexOf(n) >= 0);
183-
if (conflictNames.length > 0) {
184-
return [
185-
...(conflictNames.indexOf(exportName) >= 0 ? [[exportName, true, exportItem]] : []),
186-
...conflictNames.filter((n) => n !== exportName).map((n) => [n, false, exportItem]),
187-
];
188-
}
189-
return [];
190-
},
191-
);
217+
}
218+
return [];
219+
});
192220

193221
const conflictExports = conflictInfos.filter((c) => c[1]).map((c) => c[0] as string);
194222
const conflictAlias = conflictInfos.filter((c) => !c[1]).map((c) => c[0] as string);
@@ -282,6 +310,12 @@ function buildPackageImport(
282310
},
283311
});
284312
} else if (info.aliasName) {
313+
// default 方式的导入会生成单独de import 语句,无需生成赋值语句
314+
if (info.isDefault && defaultExportNames.find((n) => n === info.aliasName)) {
315+
delete aliasDefineStatements[info.aliasName];
316+
return;
317+
}
318+
285319
let contentStatement = '';
286320
if (aliasDefineStatements[info.aliasName]) {
287321
contentStatement = aliasDefineStatements[info.aliasName];

modules/code-generator/test-cases/react-app/demo3/expected/demo-project/src/pages/Test/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Super, {
99
SearchTable as SearchTableExport,
1010
} from "@alifd/next";
1111

12+
import SuperOther from "@alifd/next";
13+
1214
import utils from "../../utils";
1315

1416
import { i18n as _$$i18n } from "../../i18n";
@@ -17,8 +19,6 @@ import "./index.css";
1719

1820
const SuperSub = Super.Sub;
1921

20-
const SuperOther = Super;
21-
2222
const SelectOption = Select.Option;
2323

2424
const SearchTable = SearchTableExport.default;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"version": "1.0.0",
3+
"componentsMap": [
4+
{
5+
"package": "example-package",
6+
"version": "1.2.3",
7+
"exportName": "Bar",
8+
"main": "lib/index.js",
9+
"destructuring": false,
10+
"subName": "",
11+
"componentName": "Foo"
12+
}
13+
],
14+
"componentsTree": [
15+
{
16+
"componentName": "Page",
17+
"id": "node_ocl137q7oc1",
18+
"fileName": "test",
19+
"props": { "style": {} },
20+
"lifeCycles": {},
21+
"dataSource": { "list": [] },
22+
"state": {},
23+
"methods": {},
24+
"children": [
25+
{
26+
"componentName": "Foo",
27+
"id": "node_ocl137q7oc4",
28+
"props": {}
29+
}
30+
]
31+
}
32+
],
33+
"i18n": {}
34+
}

0 commit comments

Comments
 (0)