Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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/extract-fluent-system-icons.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fluentui-react-native/extract-icons": minor
---

Add a CLI that generates React Native TSX modules for selected Fluent System Icons using command-line arguments or a shared configuration file. Select icons by their component export names or SVG catalog names.
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ nodeLinker: pnpm
npmMinimalAgeGate: 7d

npmPreapprovedPackages:
- "@fluentui/react-native-icons"
- "@microsoft/esrp-npm-release"
- "@office-iss/react-native-win32"
- "@rnx-kit/*"
Expand Down
2 changes: 1 addition & 1 deletion apps/tester-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@fluentui-react-native/vibrancy-view": "workspace:*",
"@fluentui-react-native/win32-theme": "workspace:*",
"@fluentui/react-native": "workspace:*",
"@fluentui/react-native-icons": "^2.0.322",
"@fluentui/react-native-icons": "^2.0.347",
"@fortawesome/fontawesome-svg-core": "^6.2.0",
"@fortawesome/free-solid-svg-icons": "^6.2.0",
"@fortawesome/react-native-fontawesome": "^0.3.0",
Expand Down
52 changes: 52 additions & 0 deletions packages/utils/extract-icons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Extract Fluent System Icons for React Native

Install `@fluentui-react-native/extract-icons` as a development dependency. It includes the SVG catalog and SVGR
dependencies needed by `@fluentui/react-native-icons/codegen`.

Generate one `.tsx` module by passing an output path and one or more icon names:

```sh
yarn extract-icons --output src/generated/icons.tsx AccessTime20Regular add_24_filled
```

Names can be case-sensitive component exports (`AccessTime20Regular` or `AccessTimeRegular`) or SVG catalog names
without the `.svg` extension (`access_time_20_regular`). You can mix formats in the same list. All names for the same
SVG select it once, and codegen emits its sized component and any resizable alias.

Relative `--output` paths are resolved from the working directory. The CLI creates parent directories and replaces
existing output files. The generated module imports from `react-native-svg` and `@fluentui/react-native-icons/utils`;
declare `react-native-svg` and `@fluentui/react-native-icons` as runtime dependencies of the app using the generated TSX.

## Shared configuration

```sh
yarn extract-icons --config config/icons.json
```

Under the `extractIcons` key, specify either one `{ "output": "...", "icons": [...] }` object or an array of those
objects. Other top-level keys are ignored.

```json
{
"extractIcons": [
{
"output": "../src/generated/common.tsx",
"icons": ["AccessTime20Regular"]
},
{
"output": "../src/generated/actions.tsx",
"icons": ["add_24_filled"]
}
]
}
```

The config path is relative to the working directory; each relative `output` is relative to the config file.
Use any local file Node can load with `require()`, including JSON, CommonJS, and synchronous ES modules, with
either a named `extractIcons` export or a default object containing that key. TypeScript configs use Node's
native type stripping, not a TypeScript compiler or `tsconfig.json`. Only load trusted configs: JavaScript and
TypeScript configs execute when loaded.

SVGR core and its JSX plugin use matching 6.5.1 versions because this SVGR core version declares its own JSX
plugin dependency, letting the upstream generator find it under strict package linkers. The SVG catalog is pinned
to `@fluentui/svg-icons@1.1.345` so generation uses the same source artwork across installs.
66 changes: 66 additions & 0 deletions packages/utils/extract-icons/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@fluentui-react-native/extract-icons",
"version": "0.0.0",
"description": "Generate React Native TSX modules for selected Fluent System Icons",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/microsoft/fluentui-react-native.git",
"directory": "packages/utils/extract-icons"
},
"bin": "./lib/cli.js",
"files": [
"lib/cli.js"
],
"type": "module",
"scripts": {
"build": "tsc -b",
"clean": "fluentui-scripts clean",
"format": "fluentui-scripts format",
"lint": "fluentui-scripts lint",
"test": "node --test lib/cli.test.js"
},
"dependencies": {
"@fluentui/react-native-icons": "2.0.347",
"@fluentui/svg-icons": "1.1.345",
"@svgr/core": "6.5.1",
"@svgr/plugin-jsx": "6.5.1"
},
"devDependencies": {
"@fluentui-react-native/scripts": "workspace:*",
"react": "19.1.4",
"react-native": "^0.81.6",
"react-native-svg": "^15.12.1"
},
"peerDependencies": {
"react": ">=18.2.0 <20.0.0",
"react-native": ">=0.73.0",
"react-native-svg": ">=12.5.0"
},
"peerDependenciesMeta": {
"react": {
"optional": true
},
"react-native": {
"optional": true
},
"react-native-svg": {
"optional": true
}
},
"engines": {
"node": "^22.19.0 || ^24.0.0 || >=26.0.0"
},
"furn": {
"knip": {
"ignoreDependencies": [
"@svgr/core",
"@svgr/plugin-jsx"
]
}
},
"rnx-kit": {
"kitType": "library",
"extends": "@fluentui-react-native/scripts/kit-config"
}
}
114 changes: 114 additions & 0 deletions packages/utils/extract-icons/src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { test, type TestContext } from 'node:test';
import { fileURLToPath } from 'node:url';

const cli = fileURLToPath(new URL('./cli.js', import.meta.url));

function fixture(t: TestContext) {
const cwd = mkdtempSync(join(tmpdir(), 'extract-icons-'));
t.after(() => rmSync(cwd, { force: true, recursive: true }));
return {
cwd,
run: (...args: string[]) => spawnSync(process.execPath, [cli, ...args], { cwd, encoding: 'utf8' }),
read: (path: string) => readFileSync(join(cwd, path), 'utf8'),
};
}

test('accepts and deduplicates component, alias, and catalog names for the same icon', (t) => {
const { run, read } = fixture(t);
const result = run('-o', 'generated/icons.tsx', 'AccessTime20Regular', 'access_time_20_regular', 'AccessTimeRegular', 'Fps12020Regular');
assert.equal(result.status, 0, result.stderr);
const source = read('generated/icons.tsx');
assert.match(source, /from "@fluentui\/react-native-icons\/utils"/);
assert.match(source, /export const AccessTimeRegular = /);
assert.equal([...source.matchAll(/export const AccessTime20Regular = /g)].length, 1);
assert.match(source, /export const Fps12020Regular = /);
});

test('accepts a single JSON job alongside unrelated settings', (t) => {
const { cwd, run, read } = fixture(t);
writeFileSync(join(cwd, 'shared.json'), JSON.stringify({ other: true, extractIcons: { output: 'icons.tsx', icons: ['add_24_filled'] } }));
const result = run('--config', 'shared.json');
assert.equal(result.status, 0, result.stderr);
assert.match(read('icons.tsx'), /export const Add24Filled = /);
});

test('loads a CommonJS config with multiple jobs relative to the config file', (t) => {
const { cwd, run, read } = fixture(t);
mkdirSync(join(cwd, 'config'));
writeFileSync(
join(cwd, 'config', 'shared.cjs'),
`module.exports = {
extractIcons: [
{ output: '../generated/common.tsx', icons: ['AccessTime20Regular'] },
{ output: 'actions.tsx', icons: ['add_24_filled'] },
],
};`,
);
const result = run('--config', 'config/shared.cjs');
assert.equal(result.status, 0, result.stderr);
assert.match(read('generated/common.tsx'), /export const AccessTime20Regular = /);
assert.doesNotMatch(read('generated/common.tsx'), /export const Add24Filled = /);
assert.match(read('config/actions.tsx'), /export const Add24Filled = /);
});

test('loads a default-exported TypeScript config with Node require', (t) => {
const { cwd, run, read } = fixture(t);
writeFileSync(
join(cwd, 'shared.ts'),
`const icons: string[] = ['access_time_20_regular'];
export default { extractIcons: { output: 'icons.tsx', icons } };`,
);
const result = run('-c', 'shared.ts');
assert.equal(result.status, 0, result.stderr);
assert.match(read('icons.tsx'), /export const AccessTime20Regular = /);
});

test('reports a missing icon before writing any file in the batch', (t) => {
const { cwd, run } = fixture(t);
writeFileSync(
join(cwd, 'shared.json'),
JSON.stringify({
extractIcons: [
{ output: 'valid.tsx', icons: ['access_time_20_regular'] },
{ output: 'invalid.tsx', icons: ['NonexistentIcon24Regular'] },
],
}),
);
const result = run('--config', 'shared.json');
assert.equal(result.status, 1);
assert.match(result.stderr, /Cannot generate .*invalid\.tsx: Unknown icon component "NonexistentIcon24Regular"/);
assert.equal(existsSync(join(cwd, 'valid.tsx')), false);
assert.equal(existsSync(join(cwd, 'invalid.tsx')), false);
});

test('rejects invalid arguments, unnamed settings, and output collisions', (t) => {
const { cwd, run } = fixture(t);
writeFileSync(join(cwd, 'missing.json'), '{"output":"icons.tsx","icons":["add_24_filled"]}');
writeFileSync(
join(cwd, 'duplicate.json'),
JSON.stringify({
extractIcons: [
{ output: 'icons.tsx', icons: ['add_24_filled'] },
{ output: './icons.tsx', icons: ['access_time_20_regular'] },
],
}),
);
for (const [args, diagnostic] of [
[['--output', 'icons.tsx', '../access_time_20_regular'], /without a path or extension/],
[['--output', 'icons.tsx'], /non-empty array/],
[['--output', 'icons.ts', 'access_time_20_regular'], /ending in .tsx/],
[['--config', 'missing.json'], /"extractIcons" must contain/],
[['--config', 'duplicate.json'], /Duplicate output path/],
[['--config', 'missing.json', '--output', 'icons.tsx'], /cannot be combined/],
] as const) {
const result = run(...args);
assert.equal(result.status, 1, result.stderr);
assert.match(result.stderr, diagnostic);
}
assert.equal(existsSync(join(cwd, 'icons.tsx')), false);
});
Loading
Loading