@fluentui/react-icons-font-subsetting-webpack-plugin 2.0.339 → 2.0.340
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 +35 -4
- package/lib/bundler-api.d.ts +8 -2
- package/lib/index.d.ts +11 -0
- package/lib/index.js +93 -21
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @fluentui/react-icons-font-subsetting-webpack-plugin
|
|
2
2
|
|
|
3
|
-
This package includes a plugin for `webpack@>=5.0.0` and `@rspack/core@>=2.
|
|
3
|
+
This package includes a plugin for `webpack@>=5.0.0` and `@rspack/core@>=2.0.0` to subset the icon font files used by `@fluentui/react-icons` when using font-based icon implementations.
|
|
4
4
|
|
|
5
5
|
If `optimization.usedExports` is enabled (as it is by default in `production` mode), this plugin will subset the font files to only include the glyphs actually used by your build.
|
|
6
6
|
|
|
@@ -123,6 +123,37 @@ Two rspack-specific notes:
|
|
|
123
123
|
- For the headless import pattern, use `rspack.CssExtractRspackPlugin` instead of `mini-css-extract-plugin`, which is not compatible with rspack.
|
|
124
124
|
- `webpack` and `@rspack/core` are both declared as **optional** peer dependencies, so you only need to install the bundler you actually use.
|
|
125
125
|
|
|
126
|
-
rspack `>=2.
|
|
127
|
-
was exposed to the JS API in rspack 2.0.0
|
|
128
|
-
|
|
126
|
+
rspack `>=2.0.0` is required because subsetting reads the module graph, and
|
|
127
|
+
`moduleGraph.getUsedExports()` was exposed to the JS API in rspack 2.0.0.
|
|
128
|
+
|
|
129
|
+
On rspack `2.0.x` there is one limitation: `moduleGraph.getProvidedExports()` only landed in 2.1.0,
|
|
130
|
+
and it is what lets the plugin subset icons reached through a **namespace import**
|
|
131
|
+
(`import * as Icons from '@fluentui/react-icons/fonts/…'`). Because those icons cannot be
|
|
132
|
+
identified, _every_ font in the affected package is left un-subset — not just the namespace import's
|
|
133
|
+
own module — since subsetting from the package's remaining imports would drop glyphs the namespace
|
|
134
|
+
import actually uses. A build warning names the package when this happens. Named imports — the
|
|
135
|
+
common case — are unaffected and subset fully. Upgrade to rspack `>=2.1.0` for complete coverage.
|
|
136
|
+
|
|
137
|
+
## Options
|
|
138
|
+
|
|
139
|
+
### `onDuplicateInstances`
|
|
140
|
+
|
|
141
|
+
`'warn' | 'error'`, default `'warn'`.
|
|
142
|
+
|
|
143
|
+
The plugin subsets against one resolved `@fluentui/react-icons` package instance. When font modules
|
|
144
|
+
resolve from more than one installed copy — peer-dependency variants in a virtual store will do this
|
|
145
|
+
without any version conflict — emitted font assets cannot be attributed safely to one coherent source.
|
|
146
|
+
|
|
147
|
+
The plugin detects multiple participating copies before inspecting font-asset ownership, leaves all
|
|
148
|
+
fonts un-subset so every glyph still renders, and warns with the paths involved. Set `'error'` to fail
|
|
149
|
+
the build instead:
|
|
150
|
+
|
|
151
|
+
```js
|
|
152
|
+
new FluentUIReactIconsFontSubsettingPlugin({ onDuplicateInstances: 'error' });
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
To get subsetting back, collapse the copies onto one instance with bundler `resolve.alias` entries.
|
|
156
|
+
Note that this also binds every copy to a single React and Griffel instance.
|
|
157
|
+
|
|
158
|
+
Using ESM `import` and CommonJS `require()` from the same installation does not count as two copies.
|
|
159
|
+
Their used exports are combined, and each emitted font asset is subset once.
|
package/lib/bundler-api.d.ts
CHANGED
|
@@ -26,8 +26,8 @@ export interface BundlerNormalModule extends BundlerModule {
|
|
|
26
26
|
export interface BundlerModuleGraph {
|
|
27
27
|
/** webpack passes a `RuntimeSpec`; rspack requires explicit runtime names. */
|
|
28
28
|
getUsedExports(module: BundlerModule, runtime: string | string[] | ReadonlySet<string> | undefined): ReadonlySet<string> | readonly string[] | boolean | null;
|
|
29
|
-
/**
|
|
30
|
-
getProvidedExports(module: BundlerModule): readonly string[] | boolean | null;
|
|
29
|
+
/** Optional: exposed to rspack's JS API only in 2.1.0, so absent on the supported 2.0.x floor. */
|
|
30
|
+
getProvidedExports?(module: BundlerModule): readonly string[] | boolean | null;
|
|
31
31
|
}
|
|
32
32
|
export interface BundlerAsset {
|
|
33
33
|
name: string;
|
|
@@ -36,6 +36,10 @@ export interface BundlerAsset {
|
|
|
36
36
|
sourceFilename?: string;
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
|
+
/** webpack's `RuntimeSpec` is `string | SortableSet<string> | undefined`; rspack's is a `Set<string>`. */
|
|
40
|
+
export interface BundlerChunk {
|
|
41
|
+
runtime?: string | ReadonlySet<string>;
|
|
42
|
+
}
|
|
39
43
|
export interface BundlerCompilation {
|
|
40
44
|
hooks: {
|
|
41
45
|
processAssets: {
|
|
@@ -46,9 +50,11 @@ export interface BundlerCompilation {
|
|
|
46
50
|
};
|
|
47
51
|
};
|
|
48
52
|
modules: Iterable<BundlerModule>;
|
|
53
|
+
chunks: Iterable<BundlerChunk>;
|
|
49
54
|
moduleGraph: BundlerModuleGraph;
|
|
50
55
|
entrypoints: ReadonlyMap<string, unknown>;
|
|
51
56
|
warnings: Error[];
|
|
57
|
+
errors: Error[];
|
|
52
58
|
getAsset(name: string): BundlerAsset | undefined | void;
|
|
53
59
|
getAssets(): readonly BundlerAsset[];
|
|
54
60
|
/**
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import type { BundlerCompiler, BundlerPlugin } from './bundler-api';
|
|
2
2
|
export type * from './bundler-api';
|
|
3
|
+
export interface FluentUIReactIconsFontSubsettingPluginOptions {
|
|
4
|
+
/**
|
|
5
|
+
* What to do when font modules resolve from more than one installed copy of `@fluentui/react-icons`.
|
|
6
|
+
*
|
|
7
|
+
* `'warn'` (default) leaves all fonts un-subset, so every glyph still renders.
|
|
8
|
+
* `'error'` fails the build instead.
|
|
9
|
+
*/
|
|
10
|
+
onDuplicateInstances?: 'warn' | 'error';
|
|
11
|
+
}
|
|
3
12
|
export default class FluentUIReactIconsFontSubsettingPlugin implements BundlerPlugin {
|
|
13
|
+
private readonly __onDuplicateInstances;
|
|
14
|
+
constructor(options?: FluentUIReactIconsFontSubsettingPluginOptions);
|
|
4
15
|
/**
|
|
5
16
|
* Entry point for the bundler plugin that registers hooks to perform font subsetting for `@fluentui/react-icons`.
|
|
6
17
|
*
|
package/lib/index.js
CHANGED
|
@@ -14,6 +14,8 @@ const FONT_FILES_BASE_NAMES = [
|
|
|
14
14
|
'FluentSystemIcons-Light',
|
|
15
15
|
];
|
|
16
16
|
const FONT_EXTENSIONS = ['.ttf', '.woff', '.woff2'];
|
|
17
|
+
/** Separates "this module's icons are unknowable" from the benign "this module contributes nothing". */
|
|
18
|
+
const UNRESOLVABLE_NAMESPACE_IMPORT = Symbol('unresolvable-namespace-import');
|
|
17
19
|
/**
|
|
18
20
|
* Match both chunk files and atomic font imports, for the standard (Griffel)
|
|
19
21
|
* and headless APIs:
|
|
@@ -24,6 +26,9 @@ const FONT_EXTENSIONS = ['.ttf', '.woff', '.woff2'];
|
|
|
24
26
|
*/
|
|
25
27
|
const REACT_ICONS_FONT_MODULE_IMPORT_PATTERN = /react-icons[\/\\]lib(-cjs)?[\/\\](fonts[\/\\](sizedIcons|icons)[\/\\]chunk-\d+|atoms[\/\\](headless-)?fonts[\/\\][\w-]+)\.c?js$/;
|
|
26
28
|
class FluentUIReactIconsFontSubsettingPlugin {
|
|
29
|
+
constructor(options = {}) {
|
|
30
|
+
this.__onDuplicateInstances = options.onDuplicateInstances ?? 'warn';
|
|
31
|
+
}
|
|
27
32
|
/**
|
|
28
33
|
* Entry point for the bundler plugin that registers hooks to perform font subsetting for `@fluentui/react-icons`.
|
|
29
34
|
*
|
|
@@ -45,34 +50,79 @@ class FluentUIReactIconsFontSubsettingPlugin {
|
|
|
45
50
|
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|
46
51
|
compilation.hooks.processAssets.tapPromise({ name: PLUGIN_NAME, stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE }, async () => {
|
|
47
52
|
const runtime = getRuntimeSpec(compiler, compilation);
|
|
48
|
-
|
|
49
|
-
const packageToUsedFontExports = new Map();
|
|
53
|
+
const packageUsages = new Map();
|
|
50
54
|
for (const m of compilation.modules) {
|
|
51
55
|
if (isFluentUIReactFontChunk(m)) {
|
|
52
56
|
const icons = resolveUsedIconExports(m, compilation.moduleGraph, runtime);
|
|
53
57
|
if (icons === null) {
|
|
54
58
|
continue;
|
|
55
59
|
}
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
const outputRoot = (0, path_1.resolve)((0, path_1.dirname)(m.resource), '../..');
|
|
61
|
+
const packageRoot = (0, path_1.dirname)(outputRoot);
|
|
62
|
+
const usage = packageUsages.get(packageRoot) ?? {
|
|
63
|
+
outputRoots: new Set(),
|
|
64
|
+
usedExports: new Set(),
|
|
65
|
+
hasUnresolvableNamespace: false,
|
|
66
|
+
};
|
|
67
|
+
usage.outputRoots.add(outputRoot);
|
|
68
|
+
if (icons === UNRESOLVABLE_NAMESPACE_IMPORT) {
|
|
69
|
+
usage.hasUnresolvableNamespace = true;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
for (const icon of icons) {
|
|
73
|
+
usage.usedExports.add(icon);
|
|
74
|
+
}
|
|
60
75
|
}
|
|
61
|
-
|
|
76
|
+
packageUsages.set(packageRoot, usage);
|
|
62
77
|
}
|
|
63
78
|
}
|
|
64
79
|
const optimizationPromises = [];
|
|
65
|
-
for (const [
|
|
66
|
-
|
|
67
|
-
|
|
80
|
+
for (const [packageRoot, usage] of packageUsages) {
|
|
81
|
+
if (!usage.hasUnresolvableNamespace) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
compilation.warnings.push(new Error(`${PLUGIN_NAME}: "${packageRoot}" is reached through a namespace import (\`import * as ...\`) ` +
|
|
85
|
+
`whose icons cannot be determined — either the bundler does not expose ` +
|
|
86
|
+
`\`moduleGraph.getProvidedExports()\` (rspack <2.1.0), or \`optimization.providedExports\` is ` +
|
|
87
|
+
`disabled. Every font in this package was left un-subset, because subsetting from the ` +
|
|
88
|
+
`remaining imports alone would drop glyphs that are actually used. Named imports are ` +
|
|
89
|
+
`unaffected. Upgrade to rspack >=2.1.0 for full coverage.`));
|
|
90
|
+
}
|
|
91
|
+
if (packageUsages.size > 1) {
|
|
92
|
+
const message = new Error(`${PLUGIN_NAME}: font modules from more than one installed copy of "@fluentui/react-icons" were resolved, ` +
|
|
93
|
+
`so font assets cannot be attributed safely. Fonts were left un-subset to avoid dropping ` +
|
|
94
|
+
`glyphs. Participating copies: ${[...packageUsages.keys()].map((p) => `"${p}"`).join(', ')}. ` +
|
|
95
|
+
`Collapse them onto one instance with ` +
|
|
96
|
+
`bundler \`resolve.alias\` entries — note that this also binds every copy to a single React ` +
|
|
97
|
+
`and Griffel instance.`);
|
|
98
|
+
if (this.__onDuplicateInstances === 'error') {
|
|
99
|
+
compilation.errors.push(message);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
compilation.warnings.push(message);
|
|
103
|
+
}
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
for (const [packageRoot, usage] of packageUsages) {
|
|
107
|
+
if (usage.hasUnresolvableNamespace) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const fontAssetsByName = new Map();
|
|
111
|
+
for (const outputRoot of usage.outputRoots) {
|
|
112
|
+
const fontAssets = await getFontAssetsAndCodepoints(outputRoot, compilation, compiler.context);
|
|
113
|
+
for (const fontAsset of fontAssets) {
|
|
114
|
+
fontAssetsByName.set(fontAsset.assetName, fontAsset);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (fontAssetsByName.size === 0) {
|
|
68
118
|
// Loud failure: silently shipping an un-subset font is worse than a broken build.
|
|
69
|
-
compilation.warnings.push(new Error(`${PLUGIN_NAME}: found used icon fonts in "${
|
|
70
|
-
`to an emitted asset. Fonts will NOT be subset. Ensure a \`type: 'asset'\`
|
|
71
|
-
`module rule matches /\\.(ttf|woff2?)$/.`));
|
|
119
|
+
compilation.warnings.push(new Error(`${PLUGIN_NAME}: found used icon fonts in "${packageRoot}" but could not map any font module ` +
|
|
120
|
+
`to an emitted asset. Fonts will NOT be subset. Ensure a \`type: 'asset'\` ` +
|
|
121
|
+
`(or 'asset/resource') module rule matches /\\.(ttf|woff2?)$/.`));
|
|
72
122
|
continue;
|
|
73
123
|
}
|
|
74
|
-
for (const { assetName, codepoints: codepointMap } of
|
|
75
|
-
optimizationPromises.push(optimizeFontAsset(codepointMap, usedExports, compilation, assetName, sources.RawSource));
|
|
124
|
+
for (const { assetName, codepoints: codepointMap } of fontAssetsByName.values()) {
|
|
125
|
+
optimizationPromises.push(optimizeFontAsset(codepointMap, usage.usedExports, compilation, assetName, sources.RawSource));
|
|
76
126
|
}
|
|
77
127
|
}
|
|
78
128
|
// IMPORTANT: actually await all subsetting work
|
|
@@ -85,14 +135,30 @@ exports.default = FluentUIReactIconsFontSubsettingPlugin;
|
|
|
85
135
|
/**
|
|
86
136
|
* Resolves the runtime to query used-exports information for.
|
|
87
137
|
*
|
|
88
|
-
* webpack accepts `undefined
|
|
89
|
-
*
|
|
138
|
+
* webpack accepts `undefined`, meaning "merged across all runtimes". rspack requires explicit
|
|
139
|
+
* runtime names, and those are the names of the *runtime chunks* — which are only the entrypoint
|
|
140
|
+
* names by coincidence. Any build that names its runtime chunk (`entry.runtime`,
|
|
141
|
+
* `optimization.runtimeChunk`) makes the two diverge, and querying the wrong runtime reports every
|
|
142
|
+
* module as unused, so nothing gets subset. The runtimes are therefore read off the chunks.
|
|
90
143
|
*/
|
|
91
144
|
function getRuntimeSpec(compiler, compilation) {
|
|
92
145
|
if (!isRspack(compiler)) {
|
|
93
146
|
return undefined;
|
|
94
147
|
}
|
|
95
|
-
|
|
148
|
+
const runtimes = new Set();
|
|
149
|
+
for (const chunk of compilation.chunks) {
|
|
150
|
+
const { runtime } = chunk;
|
|
151
|
+
if (typeof runtime === 'string') {
|
|
152
|
+
runtimes.add(runtime);
|
|
153
|
+
}
|
|
154
|
+
else if (runtime) {
|
|
155
|
+
for (const name of runtime) {
|
|
156
|
+
runtimes.add(name);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Entrypoint names remain a last resort for the (unexpected) case of chunks without runtimes.
|
|
161
|
+
return runtimes.size > 0 ? Array.from(runtimes) : Array.from(compilation.entrypoints.keys());
|
|
96
162
|
}
|
|
97
163
|
function isRspack(compiler) {
|
|
98
164
|
return 'rspack' in compiler;
|
|
@@ -151,7 +217,9 @@ function getTargetFormat(assetName) {
|
|
|
151
217
|
* with namespace imports. Without this, namespace imports would skip subsetting entirely and ship
|
|
152
218
|
* the full unsubsetted font.
|
|
153
219
|
*
|
|
154
|
-
* Returns `null` when
|
|
220
|
+
* Returns `null` when the module contributes no glyphs, and {@link UNRESOLVABLE_NAMESPACE_IMPORT}
|
|
221
|
+
* when its glyphs exist but cannot be identified — the caller must then leave the whole package's
|
|
222
|
+
* fonts alone, since dropping the module silently would subset those glyphs away.
|
|
155
223
|
*/
|
|
156
224
|
function resolveUsedIconExports(m, moduleGraph, runtime) {
|
|
157
225
|
const usedModuleExports = moduleGraph.getUsedExports(m, runtime);
|
|
@@ -167,10 +235,14 @@ function resolveUsedIconExports(m, moduleGraph, runtime) {
|
|
|
167
235
|
// All exports are used (e.g. `import * as ns from '...'` or similar namespace import).
|
|
168
236
|
// Retrieve statically-provided exports from the module graph so we can subset to exactly
|
|
169
237
|
// the glyphs this module declares (rather than the full font or nothing).
|
|
238
|
+
if (typeof moduleGraph.getProvidedExports !== 'function') {
|
|
239
|
+
// rspack <2.1 has no such API; the glyphs are real but unknowable, so the caller must bail.
|
|
240
|
+
return UNRESOLVABLE_NAMESPACE_IMPORT;
|
|
241
|
+
}
|
|
170
242
|
const providedExports = moduleGraph.getProvidedExports(m);
|
|
171
243
|
if (providedExports === null || typeof providedExports === 'boolean') {
|
|
172
|
-
// Provided exports not statically known (optimization.providedExports disabled)
|
|
173
|
-
return
|
|
244
|
+
// Provided exports not statically known (optimization.providedExports disabled).
|
|
245
|
+
return UNRESOLVABLE_NAMESPACE_IMPORT;
|
|
174
246
|
}
|
|
175
247
|
return [...providedExports];
|
|
176
248
|
}
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluentui/react-icons-font-subsetting-webpack-plugin",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.340",
|
|
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
|
-
"test": "yarn run test:types && node test/run.js --bundler all",
|
|
7
|
+
"test": "yarn run -T vitest run && yarn run test:types && node test/run.js --bundler all",
|
|
8
8
|
"test:types": "yarn run -T tsc -p test/tsconfig.conformance.json",
|
|
9
9
|
"test:webpack": "node test/run.js --bundler webpack",
|
|
10
10
|
"test:rspack": "node test/run.js --bundler rspack",
|
|
11
11
|
"dev": "yarn run -T webpack serve -c test/webpack.config.js --open",
|
|
12
12
|
"build": "yarn run -T tsc -p .",
|
|
13
|
-
"lint": "yarn run -T eslint package.json"
|
|
13
|
+
"lint": "yarn run -T eslint src package.json"
|
|
14
14
|
},
|
|
15
15
|
"engines": {
|
|
16
16
|
"node": ">=14.5.0"
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"subset-font": "^1.4.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@rspack/core": ">=2.
|
|
36
|
+
"@rspack/core": ">=2.0.0",
|
|
37
37
|
"webpack": ">=5.0.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependenciesMeta": {
|