@fluentui/react-icons-font-subsetting-webpack-plugin 2.0.316 → 2.0.318

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,12 +1,34 @@
1
- @fluentui/react-icons-font-subsetting-webpack-plugin
2
- ===
1
+ # @fluentui/react-icons-font-subsetting-webpack-plugin
3
2
 
4
- This package includes a plugin for `webpack@>=5.0.0` to subset the icon font files used by `@fluentui/react-icons` when using the `"fluentIconFont"` condition in `resolve.conditionNames`.
3
+ This package includes a plugin for `webpack@>=5.0.0` to subset the icon font files used by `@fluentui/react-icons` when using font-based icon implementations.
5
4
 
6
- If `optimization.usedExports` is enabled (as it is by default), this plugin will subset the font files to only include the glyphs actually used by your build.
5
+ If `optimization.usedExports` is enabled (as it is by default in webpack `production` mode), this plugin will subset the font files to only include the glyphs actually used by your build.
6
+
7
+ ## Supported Import Patterns
8
+
9
+ The plugin supports the following import patterns from `@fluentui/react-icons`:
10
+
11
+ ### 1. Using the `fluentIconFont` condition
12
+
13
+ ```js
14
+ // Uses font implementation via resolve.conditionNames
15
+ import { AddRegular, DeleteFilled } from '@fluentui/react-icons';
16
+ ```
17
+
18
+ ### 2. Using atomic imports from `@fluentui/react-icons/fonts/*`
19
+
20
+ ```js
21
+ // Direct atomic imports - no conditionNames required
22
+ import { AddRegular, AddFilled } from '@fluentui/react-icons/fonts/add';
23
+ import { DeleteRegular } from '@fluentui/react-icons/fonts/delete';
24
+ ```
25
+
26
+ Atomic imports provide better tree-shaking and faster build times for applications using a small number of icons.
27
+
28
+ ## Usage
29
+
30
+ ### With `fluentIconFont` condition
7
31
 
8
- Usage
9
- ---
10
32
  ```js
11
33
  // webpack.config.js
12
34
  const {default: FluentUIReactIconsFontSubsettingPlugin} = require('@fluentui/react-icons-font-subsetting-webpack-plugin');
@@ -30,4 +52,27 @@ module.exports = {
30
52
  new FluentUIReactIconsFontSubsettingPlugin(),
31
53
  ],
32
54
  };
55
+ ```
56
+
57
+ ### With atomic imports (no conditionNames required)
58
+
59
+ ```js
60
+ // webpack.config.js
61
+ const {default: FluentUIReactIconsFontSubsettingPlugin} = require('@fluentui/react-icons-font-subsetting-webpack-plugin');
62
+
63
+ module.exports = {
64
+ module: {
65
+ rules: [
66
+ // Treat the font files as webpack assets
67
+ {
68
+ test: /\.(ttf|woff2?)$/,
69
+ type: 'asset',
70
+ }
71
+ ]
72
+ },
73
+ plugins: [
74
+ // Include this plugin
75
+ new FluentUIReactIconsFontSubsettingPlugin(),
76
+ ],
77
+ };
33
78
  ```
package/lib/index.d.ts CHANGED
@@ -1,4 +1,17 @@
1
1
  import * as webpack from "webpack";
2
2
  export default class FluentUIReactIconsFontSubsettingPlugin implements webpack.WebpackPluginInstance {
3
+ /**
4
+ * Entry point for the Webpack plugin that registers hooks to perform font subsetting for `@fluentui/react-icons`.
5
+ *
6
+ * This method is executed **once** by Webpack when the plugin is initialized during the compiler's
7
+ * bootstrap phase. The internal logic hooked into `optimizeAssets` is executed **once per compilation**
8
+ * (whenever Webpack processes the module graph and prepares to output assets) during the
9
+ * asset optimization stage.
10
+ *
11
+ * It analyzes the module graph to determine which specific icons are used from Fluent UI icon packages
12
+ * and triggers font subsetting to remove unused glyphs from the final output assets.
13
+ *
14
+ * @param compiler - The Webpack compiler instance.
15
+ */
3
16
  apply(compiler: webpack.Compiler): void;
4
17
  }
package/lib/index.js CHANGED
@@ -42,7 +42,26 @@ const FONT_EXTENSIONS = [
42
42
  '.woff',
43
43
  '.woff2'
44
44
  ];
45
+ /**
46
+ * Match both chunk files and atomic font imports:
47
+ * - lib/fonts/sizedIcons/chunk-0.js (chunk-based)
48
+ * - lib/atoms/fonts/access-time.js (atomic imports)
49
+ */
50
+ const REACT_ICONS_FONT_MODULE_IMPORT_PATTERN = /react-icons[\/\\]lib(-cjs)?[\/\\](fonts[\/\\](sizedIcons|icons)[\/\\]chunk-\d+|atoms[\/\\]fonts[\/\\][\w-]+)\.js$/;
45
51
  class FluentUIReactIconsFontSubsettingPlugin {
52
+ /**
53
+ * Entry point for the Webpack plugin that registers hooks to perform font subsetting for `@fluentui/react-icons`.
54
+ *
55
+ * This method is executed **once** by Webpack when the plugin is initialized during the compiler's
56
+ * bootstrap phase. The internal logic hooked into `optimizeAssets` is executed **once per compilation**
57
+ * (whenever Webpack processes the module graph and prepares to output assets) during the
58
+ * asset optimization stage.
59
+ *
60
+ * It analyzes the module graph to determine which specific icons are used from Fluent UI icon packages
61
+ * and triggers font subsetting to remove unused glyphs from the final output assets.
62
+ *
63
+ * @param compiler - The Webpack compiler instance.
64
+ */
46
65
  apply(compiler) {
47
66
  compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
48
67
  compilation.hooks.optimizeAssets.tapPromise(PLUGIN_NAME, async () => {
@@ -51,8 +70,11 @@ class FluentUIReactIconsFontSubsettingPlugin {
51
70
  for (const m of compilation.modules) {
52
71
  if (isFluentUIReactFontChunk(m)) {
53
72
  const usedModuleExports = compilation.moduleGraph.getUsedExports(m, undefined);
73
+ // Either all exports are used or there's no info on used exports
54
74
  if (usedModuleExports === null || typeof usedModuleExports === 'boolean') {
55
- // Either all exports are used or there's no info on used exports
75
+ // In development mode (or when optimization.usedExports is disabled),
76
+ // getUsedExports() returns `true` (all exports used) or `null` (no info).
77
+ // Subsetting requires knowing exactly which exports are used.
56
78
  continue;
57
79
  }
58
80
  const pkgLibPath = (0, path_1.resolve)((0, path_1.dirname)(m.resource), '../..');
@@ -69,17 +91,22 @@ class FluentUIReactIconsFontSubsettingPlugin {
69
91
  optimizationPromises.push(optimizeFontAsset(codepointMap, usedExports, compilation, assetName));
70
92
  }
71
93
  }
72
- await optimizationPromises;
94
+ // IMPORTANT: actually await all subsetting work
95
+ await Promise.all(optimizationPromises);
73
96
  });
74
97
  });
75
98
  }
76
99
  }
77
100
  exports.default = FluentUIReactIconsFontSubsettingPlugin;
78
101
  async function optimizeFontAsset(codepointMap, usedExports, compilation, assetName) {
79
- const subsetText = Object.entries(codepointMap)
80
- .filter(([glyphName]) => usedExports.has(glyphName))
81
- .map(([, codepoint]) => String.fromCodePoint(codepoint))
82
- .join('');
102
+ // Build subset text from the used exports set (usually small) instead of scanning all glyphs
103
+ let subsetText = '';
104
+ for (const glyphName of usedExports) {
105
+ const codepoint = codepointMap[glyphName];
106
+ if (codepoint !== undefined) {
107
+ subsetText += String.fromCodePoint(codepoint);
108
+ }
109
+ }
83
110
  let source = compilation.assets[assetName].source();
84
111
  if (typeof source === "string") {
85
112
  source = Buffer.from(source);
@@ -102,10 +129,18 @@ function isNormalModule(m) {
102
129
  return m instanceof webpack.NormalModule;
103
130
  }
104
131
  function isFluentUIReactFontChunk(m) {
105
- if (isNormalModule(m)) {
106
- return /react-icons[\/\\]lib(-cjs)?[\/\\]fonts[\/\\](sizedIcons|icons)[\/\\]chunk-\d+\.js$/.test(m.resource);
132
+ if (!isNormalModule(m)) {
133
+ return false;
134
+ }
135
+ const resource = m.resource;
136
+ if (!resource) {
137
+ return false;
138
+ }
139
+ // Cheap pre-filter before regex
140
+ if (!resource.includes('react-icons')) {
141
+ return false;
107
142
  }
108
- return false;
143
+ return REACT_ICONS_FONT_MODULE_IMPORT_PATTERN.test(resource);
109
144
  }
110
145
  async function getFontAssetsAndCodepoints(pkgLibPath, compilation) {
111
146
  const utilsFontsFolder = (0, path_1.resolve)(pkgLibPath, 'utils/fonts');
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@fluentui/react-icons-font-subsetting-webpack-plugin",
3
- "version": "2.0.316",
3
+ "version": "2.0.318",
4
4
  "description": "Webpack plugin to subset the icon fonts used by @fluentui/react-icons based on which icons are used.",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
7
7
  "test": "webpack -c test/webpack.config.js",
8
+ "dev": "webpack serve -c test/webpack.config.js --open",
8
9
  "build": "tsc -p ."
9
10
  },
10
11
  "engines": {
@@ -36,4 +37,4 @@
36
37
  "files": [
37
38
  "lib/*"
38
39
  ]
39
- }
40
+ }