From 88067c4d9d04916f77d90a784b92d3b0f73f6631 Mon Sep 17 00:00:00 2001 From: Jason Morse <35609178+JasonVMo@users.noreply.github.com> Date: Thu, 19 Nov 2026 18:29:25 -0800 Subject: [PATCH 1/2] Add Fluent React Native icon extraction CLI Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .changeset/extract-fluent-system-icons.md | 5 + .yarnrc.yml | 1 + apps/tester-core/package.json | 2 +- packages/utils/extract-icons/README.md | 48 +++ packages/utils/extract-icons/package.json | 66 ++++ packages/utils/extract-icons/src/cli.test.ts | 113 +++++++ packages/utils/extract-icons/src/cli.ts | 120 +++++++ packages/utils/extract-icons/tsconfig.json | 15 + tsconfig.json | 3 + yarn.lock | 326 ++++++++++++++++++- 10 files changed, 687 insertions(+), 12 deletions(-) create mode 100644 .changeset/extract-fluent-system-icons.md create mode 100644 packages/utils/extract-icons/README.md create mode 100644 packages/utils/extract-icons/package.json create mode 100644 packages/utils/extract-icons/src/cli.test.ts create mode 100644 packages/utils/extract-icons/src/cli.ts create mode 100644 packages/utils/extract-icons/tsconfig.json diff --git a/.changeset/extract-fluent-system-icons.md b/.changeset/extract-fluent-system-icons.md new file mode 100644 index 0000000000..f81198ee0f --- /dev/null +++ b/.changeset/extract-fluent-system-icons.md @@ -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. diff --git a/.yarnrc.yml b/.yarnrc.yml index cff0251a5b..b890293a8e 100644 --- a/.yarnrc.yml +++ b/.yarnrc.yml @@ -70,6 +70,7 @@ nodeLinker: pnpm npmMinimalAgeGate: 7d npmPreapprovedPackages: + - "@fluentui/react-native-icons" - "@microsoft/esrp-npm-release" - "@office-iss/react-native-win32" - "@rnx-kit/*" diff --git a/apps/tester-core/package.json b/apps/tester-core/package.json index 747b5aafa3..3287b5005b 100644 --- a/apps/tester-core/package.json +++ b/apps/tester-core/package.json @@ -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", diff --git a/packages/utils/extract-icons/README.md b/packages/utils/extract-icons/README.md new file mode 100644 index 0000000000..b9dd05d1b6 --- /dev/null +++ b/packages/utils/extract-icons/README.md @@ -0,0 +1,48 @@ +# 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 SVG catalog names, without `.svg` extensions: + +```sh +yarn extract-icons --output src/generated/icons.tsx access_time_20_regular add_24_filled +``` + +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": ["access_time_20_regular"] + }, + { + "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. diff --git a/packages/utils/extract-icons/package.json b/packages/utils/extract-icons/package.json new file mode 100644 index 0000000000..3e13086feb --- /dev/null +++ b/packages/utils/extract-icons/package.json @@ -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" + } +} diff --git a/packages/utils/extract-icons/src/cli.test.ts b/packages/utils/extract-icons/src/cli.test.ts new file mode 100644 index 0000000000..c1d8413e2e --- /dev/null +++ b/packages/utils/extract-icons/src/cli.test.ts @@ -0,0 +1,113 @@ +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('writes the selected icons from the CLI outside the package root', (t) => { + const { run, read } = fixture(t); + const result = run('-o', 'generated/icons.tsx', 'access_time_20_regular', 'access_time_20_regular'); + 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); +}); + +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: ['access_time_20_regular'] }, + { 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: ['nonexistent_icon_24_regular'] }, + ], + }), + ); + const result = run('--config', 'shared.json'); + assert.equal(result.status, 1); + assert.match(result.stderr, /Cannot generate .*invalid\.tsx: .*nonexistent_icon_24_regular/); + 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); +}); diff --git a/packages/utils/extract-icons/src/cli.ts b/packages/utils/extract-icons/src/cli.ts new file mode 100644 index 0000000000..f34e900c22 --- /dev/null +++ b/packages/utils/extract-icons/src/cli.ts @@ -0,0 +1,120 @@ +#!/usr/bin/env node +import { mkdirSync, writeFileSync } from 'node:fs'; +import { createRequire } from 'node:module'; +import { dirname, join, resolve } from 'node:path'; +import { parseArgs } from 'node:util'; + +const require = createRequire(import.meta.url); +// The published import target is typeless ESM; the require target also works under Yarn PnP. +const { + generateIconModule, + ICON_STYLES, +}: typeof import('@fluentui/react-native-icons/codegen') = require('@fluentui/react-native-icons/codegen'); +const configKey = 'extractIcons'; +const usage = `Usage: + extract-icons --output [ ...] + extract-icons --config + +Icon names use __