-
Notifications
You must be signed in to change notification settings - Fork 789
feat(atomic-loader): support @fluentui/react-brand-icons #1096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
micahgodbolt
wants to merge
1
commit into
microsoft:main
from
micahgodbolt:feat/brand-icons-atomic-loader
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,29 +2,31 @@ import { parseSync } from 'oxc-parser'; | |
| import type { StaticImport, StaticExport } from 'oxc-parser'; | ||
| import MagicString from 'magic-string'; | ||
|
|
||
| const MODULE_NAME = '@fluentui/react-icons'; | ||
| const DEFAULT_MODULES = ['@fluentui/react-icons', '@fluentui/react-brand-icons']; | ||
| const ICON_SUFFIX_REGEX = /(\d*)?(Regular|Filled|Light|Color)$/; | ||
|
|
||
| interface TransformOptions { | ||
| iconVariant: 'svg' | 'fonts' | 'svg-sprite'; | ||
| path: string; | ||
| /** Icon package names to transform. Defaults to react-icons and react-brand-icons. */ | ||
| modules?: string[]; | ||
| } | ||
|
|
||
| function getAtomicImportPath(importName: string, iconVariant: 'svg' | 'fonts' | 'svg-sprite'): string { | ||
| function getAtomicImportPath(importName: string, iconVariant: 'svg' | 'fonts' | 'svg-sprite', moduleName: string): string { | ||
| if (importName === 'useIconContext' || importName === 'IconDirectionContextProvider') { | ||
| return '@fluentui/react-icons/providers'; | ||
| return `${moduleName}/providers`; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| const isIcon = importName.match(ICON_SUFFIX_REGEX); | ||
|
|
||
| if (!isIcon) { | ||
| return '@fluentui/react-icons/utils'; | ||
| return `${moduleName}/utils`; | ||
| } | ||
|
|
||
| const withoutSuffix = importName.replace(ICON_SUFFIX_REGEX, ''); | ||
| const kebabCase = withoutSuffix.replace(/[a-z\d](?=[A-Z])|[a-zA-Z](?=\d)|[A-Z](?=[A-Z][a-z])/g, '$&-').toLowerCase(); | ||
|
|
||
| return `@fluentui/react-icons/${iconVariant}/${kebabCase}`; | ||
| return `${moduleName}/${iconVariant}/${kebabCase}`; | ||
| } | ||
|
|
||
| export interface TransformResult { | ||
|
|
@@ -33,7 +35,8 @@ export interface TransformResult { | |
| } | ||
|
|
||
| export function transformSource(source: string, options: TransformOptions): TransformResult { | ||
| const { iconVariant, path } = options; | ||
| const { iconVariant, path, modules = DEFAULT_MODULES } = options; | ||
| const activeModules = new Set(modules); | ||
|
|
||
| const result = parseSync(path, source, { | ||
| sourceType: 'module', | ||
|
|
@@ -47,7 +50,8 @@ export function transformSource(source: string, options: TransformOptions): Tran | |
| const src = new MagicString(source); | ||
|
|
||
| for (const imp of staticImports) { | ||
| if (imp.moduleRequest.value !== MODULE_NAME) continue; | ||
| const moduleName = imp.moduleRequest.value; | ||
| if (!activeModules.has(moduleName)) continue; | ||
|
|
||
| const namedEntries = imp.entries.filter((e) => e.importName.kind === 'Name'); | ||
| if (namedEntries.length === 0) continue; | ||
|
|
@@ -59,13 +63,13 @@ export function transformSource(source: string, options: TransformOptions): Tran | |
| const names = otherEntries | ||
| .map((e) => (e.importName.kind === 'Default' ? e.localName.value : `* as ${e.localName.value}`)) | ||
| .join(', '); | ||
| lines.push(`import ${names} from '${MODULE_NAME}';`); | ||
| lines.push(`import ${names} from '${moduleName}';`); | ||
| } | ||
|
|
||
| for (const entry of namedEntries) { | ||
| const importedName = entry.importName.name!; | ||
| const localName = entry.localName.value; | ||
| const newSource = getAtomicImportPath(importedName, iconVariant); | ||
| const newSource = getAtomicImportPath(importedName, iconVariant, moduleName); | ||
| const spec = importedName === localName ? importedName : `${importedName} as ${localName}`; | ||
| lines.push(`import { ${spec} } from '${newSource}';`); | ||
| } | ||
|
|
@@ -75,7 +79,7 @@ export function transformSource(source: string, options: TransformOptions): Tran | |
|
|
||
| for (const exp of staticExports) { | ||
| const relevantEntries = exp.entries.filter( | ||
| (e) => e.moduleRequest?.value === MODULE_NAME && e.exportName.kind === 'Name', | ||
| (e) => e.moduleRequest?.value != null && activeModules.has(e.moduleRequest.value) && e.exportName.kind === 'Name', | ||
| ); | ||
| if (relevantEntries.length === 0) continue; | ||
|
|
||
|
|
@@ -87,9 +91,10 @@ export function transformSource(source: string, options: TransformOptions): Tran | |
| const lines: string[] = []; | ||
|
|
||
| for (const entry of relevantEntries) { | ||
| const moduleName = entry.moduleRequest!.value; | ||
| const importedName = entry.importName.name!; | ||
| const exportedName = entry.exportName.name!; | ||
| const newSource = getAtomicImportPath(importedName, iconVariant); | ||
| const newSource = getAtomicImportPath(importedName, iconVariant, moduleName); | ||
| const spec = importedName === exportedName ? importedName : `${importedName} as ${exportedName}`; | ||
| lines.push(`export { ${spec} } from '${newSource}';`); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is bad design, product icons don't and wont support all
iconVariants, so this api needs either better more generic design or handle the logic properly internally