For projects not using Webpack, you can use babel-plugin-transform-imports. This requires a helper function to resolve the icon name to its atomic module path.
- Create a helper file (e.g.,
fluent-icons-transform.js) containing the resolveFluentIconImport logic. - Configure
.babelrc.js to use this helper within the transform-imports plugin.
// @filename fluent-icons-transform.js
/**
* Resolves a @fluentui/react-icons import name to its atomic module path.
* @param {string} importName - The named export being imported.
* @param {string} [target='svg'] - The target subpath (e.g. 'svg', 'svg-sprite', 'fonts', 'headless/svg', 'headless/fonts').
* @returns {string} The resolved module path.
*/
function resolveFluentIconImport(importName, target = 'svg') {
if (importName === 'useIconContext' || importName === 'IconDirectionContextProvider') {
return '@fluentui/react-icons/providers';
}
const match = importName.match(/^(.+?)(\d+)?(Regular|Filled|Light|Color)$/);
if (!match) {
return '@fluentui/react-icons/utils';
}
return `@fluentui/react-icons/${target}/${kebabCase(match[1])}`;
}
function kebabCase(str) {
return str.replace(/[a-z\d](?=[A-Z])|[a-zA-Z](?=\d)|[A-Z](?=[A-Z][a-z])/g, '$&-').toLowerCase();
}
module.exports = { resolveFluentIconImport };
// @filename .babelrc.js
const { resolveFluentIconImport } = require('./fluent-icons-transform');
module.exports = {
presets: [
// ... your preset configuration
],
plugins: [
[
'transform-imports',
{
'@fluentui/react-icons': {
// Change the second argument to match your target:
// 'svg' | 'svg-sprite' | 'fonts' | 'headless/svg' | 'headless/fonts'
transform: (importName) => resolveFluentIconImport(importName, 'svg'),
preventFullImport: false,
skipDefaultConversion: true,
},
},
],
],
};