Skip to content

Commit 1a5c69f

Browse files
TheSonOfThomptsck
andauthored
[LG-5528] chore: Consistent UMD global naming (#3137)
* add missing storybook-utils dep * ensure lg packages have umd global name * use same umd global name to build icons * fix unsupported TS warning in lint * changesets * changeset * Update tools/build/config/utils/constructUMDGlobalName.mjs Co-authored-by: Terrence Keane <[email protected]> * Update tools/build/config/utils/getDirectGlyphImports.mjs * Apply suggestions from code review --------- Co-authored-by: Terrence Keane <[email protected]>
1 parent c568585 commit 1a5c69f

File tree

10 files changed

+287
-219
lines changed

10 files changed

+287
-219
lines changed

.changeset/build-umd-global.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lg-tools/build': patch
3+
---
4+
5+
Adds reusable `constructUMDGlobalName` function to generate consistent UMD global names when building

.changeset/icon-build-umd.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@leafygreen-ui/icon': patch
3+
---
4+
5+
Updates build script to use the new consistent UMD global names
6+
Adds missing `@lg-tools/storybook-utils` devDependency

.changeset/lint-ts-eslint.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lg-tools/lint': patch
3+
---
4+
5+
Updates to latest @typescript-eslint packages

packages/icon/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@leafygreen-ui/palette": "workspace:^",
4343
"@lg-tools/build": "workspace:^",
4444
"@lg-tools/lint": "workspace:^",
45+
"@lg-tools/storybook-utils": "workspace:^",
4546
"@svgr/core": "^5.3.1",
4647
"@types/xml2json": "^0.11.0",
4748
"commander": "^11.0.0",

packages/icon/scripts/build/build-batch.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { GENERATED_DIR } from './constants';
66
async function getBatchBuildOptions(
77
batch: Array<string>,
88
): Promise<Array<MergedRollupOptions>> {
9+
const { constructUMDGlobalName } = await import(
10+
'@lg-tools/build/config/utils/constructUMDGlobalName.mjs'
11+
);
12+
913
const { esmConfig, umdConfig } = await import(
1014
'@lg-tools/build/config/rollup.config.mjs'
1115
);
@@ -18,14 +22,14 @@ async function getBatchBuildOptions(
1822
output: [esmConfig.output],
1923
},
2024
// UMD builds need a single input file
21-
...batch.map(icon => {
25+
...batch.map(iconName => {
2226
return {
2327
...umdConfig,
24-
input: `${GENERATED_DIR}/${icon}.tsx`,
28+
input: `${GENERATED_DIR}/${iconName}.tsx`,
2529
output: [
2630
{
2731
...umdConfig.output,
28-
name: `LeafyGreen_${icon}Icon`,
32+
name: constructUMDGlobalName('icon', iconName),
2933
dir: `dist/umd`,
3034
},
3135
],

pnpm-lock.yaml

Lines changed: 189 additions & 182 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Constructs a UMD global name from a package name.
3+
* E.g., '@leafygreen-ui/button' becomes 'leafyGreen_Button'
4+
*
5+
* @param {string} packageName - The name of the package (e.g., '@leafygreen-ui/button').
6+
* @param {string} [suffix] - An optional suffix to append to the global name (e.g., 'Icon').
7+
* @returns {string} - The constructed UMD global name (e.g., 'leafyGreenIconBeaker').
8+
*/
9+
export const constructUMDGlobalName = (packageName, suffix = '') => {
10+
const name = packageName.split('/').pop();
11+
const prefix = 'leafyGreen';
12+
const sentenceCasePackageName = toPascalCase(name);
13+
const sentenceCaseSuffix = toPascalCase(suffix);
14+
return `${prefix}${sentenceCasePackageName}${sentenceCaseSuffix}`;
15+
};
16+
17+
const toPascalCase = str => {
18+
return str
19+
.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase())
20+
.replace(/^[a-z]/, letter => letter.toUpperCase());
21+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
/**
5+
* @returns An array of all glyph import paths
6+
*/
7+
export function getDirectGlyphImports(allDependencies) {
8+
const pkgHasIconDependency = allDependencies['@leafygreen-ui/icon'];
9+
const glyphsDir = path.resolve(process.cwd(), '../icon/src/glyphs');
10+
11+
if (pkgHasIconDependency && fs.existsSync(glyphsDir)) {
12+
return fs
13+
.readdirSync(glyphsDir)
14+
.filter(filePath => /.svg/.test(filePath))
15+
.map(
16+
fileName =>
17+
`@leafygreen-ui/icon/dist/${path.basename(fileName, '.svg')}`,
18+
);
19+
}
20+
21+
return [];
22+
}
Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
1+
import fs from 'fs';
12
import path from 'path';
23

4+
import { getDirectGlyphImports } from './getDirectGlyphImports.mjs';
5+
import { constructUMDGlobalName } from './constructUMDGlobalName.mjs';
6+
37
export function getUMDGlobals() {
48
// Read from the current package's package.json
59
// to get the package name and dependencies
6-
const { dependencies, devDependencies, peerDependencies } = import(
7-
path.resolve(process.cwd(), 'package.json'),
8-
{ with: { type: 'json' } }
9-
);
10+
const packageJsonPath = path.resolve(process.cwd(), 'package.json');
11+
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
12+
const {
13+
dependencies = {},
14+
devDependencies = {},
15+
peerDependencies = {},
16+
} = JSON.parse(packageJsonContent);
1017

1118
const allDependencies = {
1219
...dependencies,
1320
...devDependencies,
1421
...peerDependencies,
1522
};
1623

17-
const lgGlobals = Object.keys(allDependencies).reduce((acc, pkg) => {
18-
acc[pkg] = pkg;
19-
return acc;
20-
}, {});
24+
const lgGlobals = Object.entries(allDependencies).reduce(
25+
(acc, [pkg, version]) => {
26+
// Only include packages in this monorepo
27+
if (version.includes('workspace:')) {
28+
acc[pkg] = constructUMDGlobalName(pkg);
29+
}
30+
return acc;
31+
},
32+
{},
33+
);
2134

22-
const iconGlobals = getDirectGlyphImports().reduce((acc, glyph) => {
23-
acc[glyph] = /[^/]+$/.exec(glyph)[0];
24-
return acc;
25-
}, {});
35+
const iconGlobals = getDirectGlyphImports(allDependencies).reduce(
36+
(acc, glyph) => {
37+
const iconName = /[^/]+$/.exec(glyph)[0]; // Get the part after the last slash
38+
acc[glyph] = constructUMDGlobalName('icon', iconName);
39+
return acc;
40+
},
41+
{},
42+
);
2643

2744
// Mapping of packages to the `window` property they'd be
2845
// bound to if used in the browser without a module loader.
@@ -59,25 +76,5 @@ export function getUMDGlobals() {
5976
}
6077
}
6178

62-
/**
63-
* @returns An array of all glyph import paths
64-
*/
65-
function getDirectGlyphImports() {
66-
const pkgHasIconDependency = allDependencies['@leafygreen-ui/icon'];
67-
const glyphsDir = path.resolve(process.cwd(), '../icon/src/glyphs');
68-
69-
if (pkgHasIconDependency && fs.existsSync(glyphsDir)) {
70-
return fs
71-
.readdirSync(glyphsDir)
72-
.filter(path => /.svg/.test(path))
73-
.map(
74-
fileName =>
75-
`@leafygreen-ui/icon/dist/${path.basename(fileName, '.svg')}`,
76-
);
77-
}
78-
79-
return [];
80-
}
81-
8279
return globals;
8380
}

tools/lint/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"@types/cli-progress": "^3.11.6",
2626
"@types/cross-spawn": "6.0.2",
2727
"@types/prettier": "^2.7.0",
28-
"@typescript-eslint/eslint-plugin": "8.17.0",
29-
"@typescript-eslint/parser": "8.17.0",
28+
"@typescript-eslint/eslint-plugin": "8.44.0",
29+
"@typescript-eslint/parser": "8.44.0",
3030
"chalk": "4.1.2",
3131
"cross-spawn": "7.0.3",
3232
"eslint": "9.16.0",

0 commit comments

Comments
 (0)