|
| 1 | +# Build-Time Import Transforms |
| 2 | + |
| 3 | +You can keep root-level barrel imports and leverage build-time transforms to automatically rewrite them to optimized atomic imports — no source code changes required. |
| 4 | + |
| 5 | +These transforms rewrite imports from: |
| 6 | + |
| 7 | +```tsx |
| 8 | +import { AccessTime24Filled } from '@fluentui/react-icons'; |
| 9 | +``` |
| 10 | + |
| 11 | +To the optimized atomic import for your chosen target: |
| 12 | + |
| 13 | +```tsx |
| 14 | +// SVG (standard) |
| 15 | +import { AccessTime24Filled } from '@fluentui/react-icons/svg/access-time'; |
| 16 | + |
| 17 | +// SVG sprites |
| 18 | +import { AccessTime24Filled } from '@fluentui/react-icons/svg-sprite/access-time'; |
| 19 | + |
| 20 | +// Font icons |
| 21 | +import { AccessTime24Filled } from '@fluentui/react-icons/fonts/access-time'; |
| 22 | + |
| 23 | +// Base API (SVG) |
| 24 | +import { AccessTime24Filled } from '@fluentui/react-icons/base/svg/access-time'; |
| 25 | + |
| 26 | +// Base API (SVG sprites) |
| 27 | +import { AccessTime24Filled } from '@fluentui/react-icons/base/svg-sprite/access-time'; |
| 28 | + |
| 29 | +// Base API (fonts) |
| 30 | +import { AccessTime24Filled } from '@fluentui/react-icons/base/fonts/access-time'; |
| 31 | +``` |
| 32 | + |
| 33 | +The examples below use `svg` as the target path. Replace it with the appropriate path for your setup from the list above. |
| 34 | + |
| 35 | +## Babel |
| 36 | + |
| 37 | +Add [babel-plugin-transform-imports](https://www.npmjs.com/package/babel-plugin-transform-imports) with the following setup. |
| 38 | + |
| 39 | +Copy the following helper into your project (e.g. as `fluent-icons-transform.js`): |
| 40 | + |
| 41 | +```js |
| 42 | +// @filename fluent-icons-transform.js |
| 43 | + |
| 44 | +/** |
| 45 | + * Resolves a @fluentui/react-icons import name to its atomic module path. |
| 46 | + * @param {string} importName - The named export being imported. |
| 47 | + * @param {string} [target='svg'] - The target subpath (e.g. 'svg', 'svg-sprite', 'fonts', |
| 48 | + * 'base/svg', 'base/svg-sprite', 'base/fonts'). |
| 49 | + * @returns {string} The resolved module path. |
| 50 | + */ |
| 51 | +function resolveFluentIconImport(importName, target = 'svg') { |
| 52 | + if (importName === 'useIconContext' || importName === 'IconDirectionContextProvider') { |
| 53 | + return '@fluentui/react-icons/providers'; |
| 54 | + } |
| 55 | + |
| 56 | + const match = importName.match(/^(.+?)(\d+)?(Regular|Filled|Light|Color)$/); |
| 57 | + if (!match) { |
| 58 | + return '@fluentui/react-icons/utils'; |
| 59 | + } |
| 60 | + |
| 61 | + return `@fluentui/react-icons/${target}/${kebabCase(match[1])}`; |
| 62 | +} |
| 63 | + |
| 64 | +function kebabCase(str) { |
| 65 | + return str.replace(/[a-z\d](?=[A-Z])|[a-zA-Z](?=\d)|[A-Z](?=[A-Z][a-z])/g, '$&-').toLowerCase(); |
| 66 | +} |
| 67 | + |
| 68 | +module.exports = { resolveFluentIconImport }; |
| 69 | +``` |
| 70 | + |
| 71 | +Then use it in your Babel config: |
| 72 | + |
| 73 | +```js |
| 74 | +// @filename .babelrc.js |
| 75 | +const { resolveFluentIconImport } = require('./fluent-icons-transform'); |
| 76 | + |
| 77 | +module.exports = { |
| 78 | + presets: [ |
| 79 | + // ... your preset configuration |
| 80 | + ], |
| 81 | + plugins: [ |
| 82 | + [ |
| 83 | + 'transform-imports', |
| 84 | + { |
| 85 | + '@fluentui/react-icons': { |
| 86 | + // Change the second argument to match your target: |
| 87 | + // 'svg' | 'svg-sprite' | 'fonts' | 'base/svg' | 'base/svg-sprite' | 'base/fonts' |
| 88 | + transform: (importName) => resolveFluentIconImport(importName, 'svg'), |
| 89 | + preventFullImport: false, |
| 90 | + skipDefaultConversion: true, |
| 91 | + }, |
| 92 | + }, |
| 93 | + ], |
| 94 | + ], |
| 95 | +}; |
| 96 | +``` |
| 97 | + |
| 98 | +## SWC |
| 99 | + |
| 100 | +Add [@swc/plugin-transform-imports](https://www.npmjs.com/package/@swc/plugin-transform-imports) with the following setup. |
| 101 | + |
| 102 | +Replace every `svg` segment in the target paths below with your chosen target (`svg`, `svg-sprite`, `fonts`, `base/svg`, `base/svg-sprite`, or `base/fonts`): |
| 103 | + |
| 104 | +```jsonc |
| 105 | +// @filename .swcrc |
| 106 | +{ |
| 107 | + "jsc": { |
| 108 | + "experimental": { |
| 109 | + "plugins": [ |
| 110 | + [ |
| 111 | + "@swc/plugin-transform-imports", |
| 112 | + { |
| 113 | + "@fluentui/react-icons": { |
| 114 | + "transform": [ |
| 115 | + // Transform provider imports to /providers |
| 116 | + ["^(useIconContext|IconDirectionContextProvider)$", "@fluentui/react-icons/providers"], |
| 117 | + // Transform icon imports to /{target}/{icon-name} |
| 118 | + // (.+?) lazily captures the icon base name (may contain digits, |
| 119 | + // e.g. "Space3D", "Rotate315Right"), (\d+)? greedily strips any |
| 120 | + // trailing all-digit segment (size suffixes like 16/20/24, but |
| 121 | + // also level indicators like Battery0) — this mirrors the |
| 122 | + // normalizeBaseName logic used by the generation pipeline. |
| 123 | + // {{ kebabCase }} on group 1 mirrors lodash.kebabCase. |
| 124 | + [ |
| 125 | + "(.+?)(\\d+)?(Regular|Filled|Light|Color)$", |
| 126 | + "@fluentui/react-icons/svg/{{ kebabCase memberMatches.[1] }}", |
| 127 | + ], |
| 128 | + // Fallback: all other exports are utilities |
| 129 | + [".*", "@fluentui/react-icons/utils"], |
| 130 | + ], |
| 131 | + "preventFullImport": false, |
| 132 | + "skipDefaultConversion": true, |
| 133 | + "handleDefaultImport": false, |
| 134 | + "handleNamespaceImport": false, |
| 135 | + }, |
| 136 | + }, |
| 137 | + ], |
| 138 | + ], |
| 139 | + }, |
| 140 | + }, |
| 141 | +} |
| 142 | +``` |
0 commit comments