Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-friends-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/icon': minor
---

Bumps svgr dependency from v5 to v8
2 changes: 1 addition & 1 deletion packages/icon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@lg-tools/build": "workspace:^",
"@lg-tools/lint": "workspace:^",
"@lg-tools/storybook-utils": "workspace:^",
"@svgr/core": "^5.3.1",
"@svgr/core": "^8.1.0",
"@types/xml2json": "^0.11.0",
"commander": "^11.0.0",
"rollup": "4.16.1",
Expand Down
94 changes: 81 additions & 13 deletions packages/icon/scripts/prebuild/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/* eslint-disable no-console */
import { formatLG } from '@lg-tools/lint';
// @ts-ignore - no types in svgr v5.5 // TODO: update to v8 LG-5484
import { default as svgr } from '@svgr/core';
import { transform } from '@svgr/core';
import { Command } from 'commander';
import fs from 'fs';
import path from 'path';

import { getChecksum } from './checksum';
import { indexTemplate } from './indexTemplate';
import { FileObject, PrebuildOptions } from './prebuild.types';
import { svgrTemplate } from './svgrTemplate';

const SRC_PATH = path.resolve(__dirname, '..', '..', 'src');

Expand Down Expand Up @@ -117,32 +115,102 @@ function makeFileProcessor(outputDir: string, options?: PrebuildOptions) {
encoding: 'utf8',
});

// Note: must use `require` since svgrrc is a CommonJS module
// @ts-ignore - svgrrc is not typed
const svgrrc = await require('../../.svgrrc.js');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we rm svgrrc.js as well?


const processedSVGR = await svgr(
// Generate basic React component first
const processedSVGR = await transform(
svgContent,
{
...svgrrc,
template: svgrTemplate,
plugins: ['@svgr/plugin-jsx'],
typescript: true,
jsx: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we extract this config into either a separate file, or at least a constant?

babelConfig: {
plugins: [
[
'@svgr/babel-plugin-replace-jsx-attribute-value',
{
values: [
{ value: '#000', newValue: 'currentColor' },
{ value: '#000000', newValue: 'currentColor' },
{ value: 'black', newValue: 'currentColor' },
],
},
],
],
},
},
},
{ componentName: file.name },
);

// Post-process to add custom LeafyGreen wrapper
const customizedSVGR = processedSVGR.replace(
/import \* as React from "react";\nimport type \{ SVGProps \} from "react";\nconst (\w+) = \(props: SVGProps<SVGSVGElement>\) => (.*?);\nexport default \1;/s,
Copy link
Preview

Copilot AI Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This complex regex pattern is fragile and depends on exact formatting from SVGR output. Consider using an AST-based approach or at minimum, break this into smaller, more readable regex patterns with comments explaining each part.

Copilot uses AI. Check for mistakes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing svgrTemplate.ts and hard-coding it inline here?

Copy link
Collaborator

@TheSonOfThomp TheSonOfThomp Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SVGR 8 still takes a template prop. (https://react-svgr.com/docs/options/#template) IMO we should be using this instead of hard-coding a complex regex replacer

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

`import * as React from 'react';
import { css, cx } from '@leafygreen-ui/emotion';
import { generateAccessibleProps, sizeMap } from '../glyphCommon';
import { LGGlyph } from '../types';

export interface $1Props extends LGGlyph.ComponentProps {}

const $1 = ({
className,
size = 16,
title,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby,
fill,
role = 'img',
...props
}: $1Props) => {
const fillStyle = css\`
color: \${fill};
\`;

const noFlexShrink = css\`
flex-shrink: 0;
\`;

const accessibleProps = generateAccessibleProps(role, '$1', {
title,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby
});

const svgElement = $2;

return React.cloneElement(svgElement, {
className: cx(
{
componentName: file.name,
[fillStyle]: fill != null,
},
noFlexShrink,
className,
),
height: typeof size === 'number' ? size : sizeMap[size],
width: typeof size === 'number' ? size : sizeMap[size],
role,
...accessibleProps,
...props,
});
};

$1.displayName = '$1';
$1.isGlyph = true;

export default $1;`,
);

const scriptPath =
'packages/icon/' +
path.relative(path.resolve(__dirname, process.cwd()), __filename);

const checksum = getChecksum(svgContent, processedSVGR);
const finalContent = customizedSVGR || processedSVGR;
const checksum = getChecksum(svgContent, finalContent);

const outfilePath = path.resolve(outputDir, `${file.name}.tsx`);
const annotatedFileContent = annotateFileContent(
scriptPath,
checksum,
processedSVGR,
finalContent,
);

if (options?.verbose) {
Expand Down
92 changes: 0 additions & 92 deletions packages/icon/scripts/prebuild/svgrTemplate.ts

This file was deleted.

50 changes: 35 additions & 15 deletions packages/icon/src/generated/AIModel.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 35 additions & 15 deletions packages/icon/src/generated/ActivityFeed.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading