Skip to content

Commit ccbaef6

Browse files
committed
charts components
1 parent 2a2e22f commit ccbaef6

File tree

1 file changed

+75
-30
lines changed
  • packages/cli/src/scripts/codemod/transforms/export-maps

1 file changed

+75
-30
lines changed

packages/cli/src/scripts/codemod/transforms/export-maps/main.cts

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import fs from 'fs';
44
import path from 'path';
55
import type { API, FileInfo, JSCodeshift, Collection } from 'jscodeshift';
66

7-
const packageName = '@ui5/webcomponents-react';
7+
const mainPackageName = '@ui5/webcomponents-react';
8+
const basePackageName = '@ui5/webcomponents-react-base';
9+
const chartsPackageName = '@ui5/webcomponents-react-charts';
10+
11+
const componentPackageNames = [mainPackageName, chartsPackageName];
12+
13+
// main enums
814
const libraryPath = require.resolve('@ui5/webcomponents-react/package.json');
915
const enumsDir = path.join(path.dirname(libraryPath), 'dist', 'enums');
10-
11-
// build enum names dynamically from dist/enums filenames
1216
let enumNames: Set<string> = new Set();
13-
1417
try {
1518
enumNames = new Set(
1619
fs
@@ -21,43 +24,88 @@ try {
2124
)
2225
.map((file) => path.basename(file, path.extname(file))),
2326
);
24-
// console.log('Loaded enums:', Array.from(enumNames));
2527
} catch (e) {
2628
console.warn(`⚠️ Could not read enums directory at ${enumsDir}. Skipping enum detection.`, e);
2729
}
2830

31+
// exports-map base
32+
function resolveBaseExport(importedName: string): string | undefined {
33+
const directMap: Record<string, string> = {
34+
Device: `${basePackageName}/Device`,
35+
hooks: `${basePackageName}/hooks`,
36+
VersionInfo: `${basePackageName}/VersionInfo`,
37+
I18nStore: `${basePackageName}/I18nStore`,
38+
StyleStore: `${basePackageName}/StyleStore`,
39+
CssSizeVariables: `${basePackageName}/CssSizeVariables`,
40+
ThemingParameters: `${basePackageName}/ThemingParameters`,
41+
withWebComponent: `${basePackageName}/withWebComponent`,
42+
utils: `${basePackageName}/utils`,
43+
addCustomCSSWithScoping: `${basePackageName}/internal/addCustomCSSWithScoping.js`,
44+
};
45+
if (directMap[importedName]) {
46+
return directMap[importedName];
47+
}
48+
// fallback
49+
if (importedName === 'default' || importedName === 'index') {
50+
return basePackageName;
51+
}
52+
return undefined;
53+
}
54+
2955
export default function transform(file: FileInfo, api: API): string | undefined {
3056
const j: JSCodeshift = api.jscodeshift;
3157
const root: Collection = j(file.source);
3258

3359
let isDirty = false;
3460

35-
root.find(j.ImportDeclaration, { source: { value: packageName } }).forEach((importPath) => {
36-
const specifiers = importPath.node.specifiers || [];
61+
// main & charts pkg
62+
componentPackageNames.forEach((packageName) => {
63+
root.find(j.ImportDeclaration, { source: { value: packageName } }).forEach((importPath) => {
64+
const specifiers = importPath.node.specifiers || [];
65+
specifiers.forEach((spec) => {
66+
if (spec.type !== 'ImportSpecifier') return;
67+
const importedName = spec.imported.name as string;
68+
let componentName = importedName;
69+
if (importPath.node.importKind === 'type') {
70+
if (importedName.endsWith('PropTypes')) {
71+
componentName = importedName.replace(/PropTypes$/, '');
72+
// charts props
73+
} else if (importedName.endsWith('Props')) {
74+
componentName = importedName.replace(/Props$/, '');
75+
} else if (importedName.endsWith('DomRef')) {
76+
componentName = importedName.replace(/DomRef$/, '');
77+
}
78+
}
79+
const newSource =
80+
importPath.node.importKind === 'type'
81+
? `${packageName}/${componentName}`
82+
: enumNames.has(importedName)
83+
? `${packageName}/enums/${importedName}`
84+
: `${packageName}/${importedName}`;
85+
const newImport = j.importDeclaration(
86+
[
87+
j.importSpecifier(
88+
j.identifier(importedName),
89+
j.identifier(spec.local && typeof spec.local.name === 'string' ? spec.local.name : importedName),
90+
),
91+
],
92+
j.literal(newSource),
93+
);
94+
newImport.importKind = importPath.node.importKind;
95+
j(importPath).insertBefore(newImport);
96+
isDirty = true;
97+
});
98+
j(importPath).remove();
99+
});
100+
});
37101

102+
// base pkg
103+
root.find(j.ImportDeclaration, { source: { value: basePackageName } }).forEach((importPath) => {
104+
const specifiers = importPath.node.specifiers || [];
38105
specifiers.forEach((spec) => {
39106
if (spec.type !== 'ImportSpecifier') return;
40-
41107
const importedName = spec.imported.name as string;
42-
43-
// target component
44-
let componentName = importedName;
45-
if (importPath.node.importKind === 'type') {
46-
if (importedName.endsWith('PropTypes')) {
47-
componentName = importedName.replace(/PropTypes$/, '');
48-
} else if (importedName.endsWith('DomRef')) {
49-
componentName = importedName.replace(/DomRef$/, '');
50-
}
51-
}
52-
53-
// import path
54-
const newSource =
55-
importPath.node.importKind === 'type'
56-
? `${packageName}/${componentName}` // type imports
57-
: enumNames.has(importedName)
58-
? `${packageName}/enums/${importedName}` // enums
59-
: `${packageName}/${importedName}`; // regular component
60-
108+
const newSource = resolveBaseExport(importedName) || basePackageName;
61109
const newImport = j.importDeclaration(
62110
[
63111
j.importSpecifier(
@@ -67,13 +115,10 @@ export default function transform(file: FileInfo, api: API): string | undefined
67115
],
68116
j.literal(newSource),
69117
);
70-
71118
newImport.importKind = importPath.node.importKind;
72-
73119
j(importPath).insertBefore(newImport);
74120
isDirty = true;
75121
});
76-
77122
j(importPath).remove();
78123
});
79124

0 commit comments

Comments
 (0)