@fluentui/react-icons-atomic-webpack-loader 0.0.3 → 0.0.5
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 +96 -4
- package/lib/index.d.ts +23 -0
- package/lib/index.js +7 -2
- package/lib/modules.d.ts +52 -1
- package/lib/modules.js +85 -7
- package/lib/transform.d.ts +2 -0
- package/lib/transform.js +64 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,10 +63,12 @@ module.exports = {
|
|
|
63
63
|
|
|
64
64
|
## Supported modules
|
|
65
65
|
|
|
66
|
-
| Module | Variants | Notes |
|
|
67
|
-
| ----------------------------- | ---------------------------- | ----------------------------- |
|
|
68
|
-
| `@fluentui/react-icons` | `svg`, `fonts`, `svg-sprite` | Has `/providers` and `/utils` |
|
|
69
|
-
| `@fluentui/react-brand-icons` | `svg` | Has `/utils`; no `/providers` |
|
|
66
|
+
| Module | Variants | Headless | Notes |
|
|
67
|
+
| ----------------------------- | ---------------------------- | -------------- | ----------------------------- |
|
|
68
|
+
| `@fluentui/react-icons` | `svg`, `fonts`, `svg-sprite` | `svg`, `fonts` | Has `/providers` and `/utils` |
|
|
69
|
+
| `@fluentui/react-brand-icons` | `svg` | `svg` | Has `/utils`; no `/providers` |
|
|
70
|
+
|
|
71
|
+
> **Color icons are SVG-only.** Color variants (`*Color`) rely on gradients that cannot be represented in an icon font, so they ship only in the `svg` and `svg-sprite` builds — never `fonts`. The loader reroutes color imports off font variants automatically (see [Color icons](#color-icons) below).
|
|
70
72
|
|
|
71
73
|
## Options
|
|
72
74
|
|
|
@@ -74,6 +76,7 @@ module.exports = {
|
|
|
74
76
|
| ----------------- | -------------------------------------- | ----------- | -------------------------------------------------------------------------- |
|
|
75
77
|
| `iconVariant` | `'svg'` \| `'fonts'` \| `'svg-sprite'` | `'svg'` | Variant icons resolve to. Applied to every supported module. |
|
|
76
78
|
| `fallbackVariant` | `'svg'` \| `'fonts'` \| `'svg-sprite'` | `undefined` | Variant used for a module that does not support `iconVariant` (see below). |
|
|
79
|
+
| `headless` | `boolean` | `false` | Resolve to the headless (Griffel-free) build where the module ships one. |
|
|
77
80
|
|
|
78
81
|
### Variant resolution & `fallbackVariant`
|
|
79
82
|
|
|
@@ -97,6 +100,26 @@ Resolution is lazy and per file: only modules actually imported in a given file
|
|
|
97
100
|
}
|
|
98
101
|
```
|
|
99
102
|
|
|
103
|
+
### Color icons
|
|
104
|
+
|
|
105
|
+
Color variants (`*Color`, e.g. `AddCircleColor`) are **SVG-only** — their gradients cannot be represented in an icon font, so the `fonts` build ships no color glyphs. When a color icon is imported under a color-less variant (`iconVariant: 'fonts'`), the loader reroutes just that import to a color-capable variant, following the same precedence as above (constrained to `svg` / `svg-sprite`), and emits a warning:
|
|
106
|
+
|
|
107
|
+
1. If `iconVariant` is already color-capable (`svg` / `svg-sprite`), the color import is left on it — no reroute, no warning.
|
|
108
|
+
2. Otherwise, if `fallbackVariant` is set and color-capable, it is used (e.g. `svg-sprite`).
|
|
109
|
+
3. Otherwise the loader falls back to `svg`.
|
|
110
|
+
|
|
111
|
+
Rerouting is per specifier, so color and non-color icons in the same statement resolve independently:
|
|
112
|
+
|
|
113
|
+
```js
|
|
114
|
+
// iconVariant: 'fonts'
|
|
115
|
+
import { AddFilled, AddCircleColor } from '@fluentui/react-icons';
|
|
116
|
+
// →
|
|
117
|
+
import { AddFilled } from '@fluentui/react-icons/fonts/add';
|
|
118
|
+
import { AddCircleColor } from '@fluentui/react-icons/svg/add-circle';
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
> Color icons are deprecated. See the [user guidance](https://microsoft.github.io/fluentui-system-icons/?path=/docs/icons-user-guidance--docs#color-variants-deprecated).
|
|
122
|
+
|
|
100
123
|
### Using font icons
|
|
101
124
|
|
|
102
125
|
```js
|
|
@@ -116,6 +139,40 @@ Resolution is lazy and per file: only modules actually imported in a given file
|
|
|
116
139
|
|
|
117
140
|
This changes icon resolution from `@fluentui/react-icons/svg/*` to `@fluentui/react-icons/fonts/*`. Non-icon exports (`utils`, `providers`) are unaffected.
|
|
118
141
|
|
|
142
|
+
> Color icons have no font build and are rerouted to `svg` (or `svg-sprite`) automatically — see [Color icons](#color-icons).
|
|
143
|
+
|
|
144
|
+
### Using the headless API
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
{
|
|
148
|
+
loader: '@fluentui/react-icons-atomic-webpack-loader',
|
|
149
|
+
options: {
|
|
150
|
+
iconVariant: 'fonts',
|
|
151
|
+
headless: true,
|
|
152
|
+
},
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
With the example above:
|
|
157
|
+
|
|
158
|
+
| Import | Resolves to |
|
|
159
|
+
| ------------------- | ------------------------------------------ |
|
|
160
|
+
| `AddFilled` | `@fluentui/react-icons/headless/fonts/add` |
|
|
161
|
+
| `bundleIcon` (util) | `@fluentui/react-icons/headless/utils` |
|
|
162
|
+
| `useIconContext` | `@fluentui/react-icons/providers` (shared) |
|
|
163
|
+
|
|
164
|
+
Notes:
|
|
165
|
+
|
|
166
|
+
- **Best-effort per module:** a module without a headless build for the resolved variant degrades to its standard (Griffel) implementation with a warning rather than failing the build. This applies to headless `svg-sprite` (not generated yet).
|
|
167
|
+
- **Version requirement:** headless `@fluentui/react-brand-icons` requires `>= 2.0.206`. The loader rewrites imports statically and does not check the installed version, so an older brand-icons will fail to resolve the `/headless/*` entries at build time.
|
|
168
|
+
- **Context is shared:** `useIconContext` / `IconDirectionContextProvider` always resolve to `@fluentui/react-icons/providers` — it is framework-agnostic and reused by both APIs.
|
|
169
|
+
- **CSS is your responsibility:** the loader only rewrites component/utility imports. You must still import the headless CSS in your app entry point:
|
|
170
|
+
```js
|
|
171
|
+
import '@fluentui/react-icons/headless/styles.css';
|
|
172
|
+
// and, for font icons:
|
|
173
|
+
import '@fluentui/react-icons/headless/fonts/styles.css';
|
|
174
|
+
```
|
|
175
|
+
|
|
119
176
|
### Using SVG sprite icons
|
|
120
177
|
|
|
121
178
|
```js
|
|
@@ -139,6 +196,40 @@ This changes icon resolution from `@fluentui/react-icons/svg/*` to `@fluentui/re
|
|
|
139
196
|
|
|
140
197
|
The loader parses each module and rewrites import and re-export declarations that reference a supported module. Each named specifier is routed to an atomic subpath based on its name:
|
|
141
198
|
|
|
199
|
+
### Resolution flow
|
|
200
|
+
|
|
201
|
+
Each named specifier is resolved independently, so color and non-color icons — even within the same statement — can land on different variants.
|
|
202
|
+
|
|
203
|
+
```mermaid
|
|
204
|
+
flowchart TD
|
|
205
|
+
A["Named specifier from a supported module"] --> B{"Icon name? ends in Regular / Filled / Light / Color"}
|
|
206
|
+
B -->|"context / hook"| P["/providers"]
|
|
207
|
+
B -->|"utility"| U["/utils"]
|
|
208
|
+
B -->|"yes"| V{"Module supports iconVariant?"}
|
|
209
|
+
|
|
210
|
+
V -->|"yes"| R["variant = iconVariant"]
|
|
211
|
+
V -->|"no"| F{"fallbackVariant set?"}
|
|
212
|
+
F -->|"no"| ERR["Error: import left untouched, set fallbackVariant"]
|
|
213
|
+
F -->|"yes, supported"| R2["variant = fallbackVariant"]
|
|
214
|
+
F -->|"yes, unsupported"| RS["variant = svg (warning)"]
|
|
215
|
+
|
|
216
|
+
R --> C{"Color icon?"}
|
|
217
|
+
R2 --> C
|
|
218
|
+
RS --> C
|
|
219
|
+
|
|
220
|
+
C -->|"no"| H{"headless requested and available for variant?"}
|
|
221
|
+
C -->|"yes, already color-capable: svg / svg-sprite"| H
|
|
222
|
+
C -->|"yes, color-less variant: fonts"| CC["reroute to first color-capable of iconVariant, fallbackVariant, svg (warning)"]
|
|
223
|
+
CC --> H
|
|
224
|
+
|
|
225
|
+
H -->|"yes"| HP["prefix with /headless"]
|
|
226
|
+
H -->|"no / not available (warning)"| STD["standard build"]
|
|
227
|
+
HP --> OUT["resolved atomic path"]
|
|
228
|
+
STD --> OUT
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
> Steps marked "(warning)" emit a build warning — a best-effort degrade rather than a hard failure.
|
|
232
|
+
|
|
142
233
|
### `@fluentui/react-icons`
|
|
143
234
|
|
|
144
235
|
| Export type | Example | Resolved path |
|
|
@@ -161,3 +252,4 @@ Files that don't reference a supported module are passed through untouched (fast
|
|
|
161
252
|
- `webpack` >= 5
|
|
162
253
|
- `@fluentui/react-icons` >= 2 (with atomic subpath exports)
|
|
163
254
|
- `@fluentui/react-brand-icons` (with atomic subpath exports), if used
|
|
255
|
+
- `>= 2.0.206` when using `headless: true` — earlier versions do not ship the `/headless/svg/*` and `/headless/utils` entries, so the loader's rewritten imports will fail to resolve.
|
package/lib/index.d.ts
CHANGED
|
@@ -8,13 +8,36 @@ export interface FluentIconsAtomicImportLoaderOptions {
|
|
|
8
8
|
* Not every module supports every variant (e.g. `@fluentui/react-brand-icons`
|
|
9
9
|
* only ships `svg`). When a referenced module does not support this variant,
|
|
10
10
|
* `fallbackVariant` is used instead.
|
|
11
|
+
*
|
|
12
|
+
* Color icons are an exception: they are SVG-only (gradients cannot live in an
|
|
13
|
+
* icon font), so a `*Color` import under `iconVariant: 'fonts'` is rerouted to
|
|
14
|
+
* a color-capable variant (`svg` / `svg-sprite`) following the same
|
|
15
|
+
* `iconVariant → fallbackVariant → svg` precedence, with a warning.
|
|
11
16
|
*/
|
|
12
17
|
iconVariant?: IconVariant;
|
|
13
18
|
/**
|
|
14
19
|
* The variant to use for a referenced module that does not support
|
|
15
20
|
* `iconVariant`. When omitted and a module cannot honor `iconVariant`, the
|
|
16
21
|
* loader fails with a descriptive error.
|
|
22
|
+
*
|
|
23
|
+
* Also used as the preferred target when rerouting SVG-only color icons off a
|
|
24
|
+
* color-less `iconVariant` (e.g. `fonts`), provided the fallback itself is
|
|
25
|
+
* color-capable; otherwise the loader degrades to `svg`.
|
|
17
26
|
*/
|
|
18
27
|
fallbackVariant?: IconVariant;
|
|
28
|
+
/**
|
|
29
|
+
* Resolve atomic imports to the **headless** (Griffel-free) build where the
|
|
30
|
+
* referenced module ships one. Defaults to `false`.
|
|
31
|
+
*
|
|
32
|
+
* Headless is best-effort per module: a module without a headless build for
|
|
33
|
+
* the resolved variant (e.g. headless `svg-sprite` which isn't generated yet)
|
|
34
|
+
* degrades to its standard implementation with a warning instead of failing
|
|
35
|
+
* the build.
|
|
36
|
+
*
|
|
37
|
+
* NOTE: the loader only rewrites component/utility imports — you must still
|
|
38
|
+
* import the headless CSS (`@fluentui/react-icons/headless/styles.css`, plus
|
|
39
|
+
* `headless/fonts/styles.css` for font icons) in your app entry point.
|
|
40
|
+
*/
|
|
41
|
+
headless?: boolean;
|
|
19
42
|
}
|
|
20
43
|
export default function fluentIconsAtomicImportLoader(this: LoaderContext<FluentIconsAtomicImportLoaderOptions>, sourceCode: string): void;
|
package/lib/index.js
CHANGED
|
@@ -9,12 +9,17 @@ function fluentIconsAtomicImportLoader(sourceCode) {
|
|
|
9
9
|
if (!modules_1.SUPPORTED_MODULE_NAMES.some((name) => sourceCode.includes(name))) {
|
|
10
10
|
return this.callback(null, sourceCode);
|
|
11
11
|
}
|
|
12
|
-
const { iconVariant = 'svg', fallbackVariant } = this.getOptions();
|
|
12
|
+
const { iconVariant = 'svg', fallbackVariant, headless = false } = this.getOptions();
|
|
13
13
|
let code;
|
|
14
14
|
let map;
|
|
15
15
|
let diagnostics;
|
|
16
16
|
try {
|
|
17
|
-
({ code, map, diagnostics } = (0, transform_1.transformSource)(sourceCode, {
|
|
17
|
+
({ code, map, diagnostics } = (0, transform_1.transformSource)(sourceCode, {
|
|
18
|
+
iconVariant,
|
|
19
|
+
fallbackVariant,
|
|
20
|
+
headless,
|
|
21
|
+
path: resourcePath,
|
|
22
|
+
}));
|
|
18
23
|
}
|
|
19
24
|
catch (error) {
|
|
20
25
|
const reason = error instanceof Error ? error.message : String(error);
|
package/lib/modules.d.ts
CHANGED
|
@@ -5,6 +5,14 @@ export type IconVariant = 'svg' | 'fonts' | 'svg-sprite';
|
|
|
5
5
|
* Every module supports `svg`, so it is always a valid resolution target.
|
|
6
6
|
*/
|
|
7
7
|
export declare const DEFAULT_SAFETY_VARIANT: IconVariant;
|
|
8
|
+
/**
|
|
9
|
+
* Whether an import name is a *color* icon variant, i.e. its style suffix is
|
|
10
|
+
* `Color` (e.g. `AddCircleColor`, `AddCircle20Color`). Every icon export carries
|
|
11
|
+
* exactly one trailing style suffix, so a `Color` suffix unambiguously marks a
|
|
12
|
+
* color variant — icons whose base name merely contains the word "Color" (e.g.
|
|
13
|
+
* `TextColorRegular`) end in a different style suffix.
|
|
14
|
+
*/
|
|
15
|
+
export declare function isColorIconName(importName: string): boolean;
|
|
8
16
|
/**
|
|
9
17
|
* Describes how a single supported module's barrel imports are rewritten into
|
|
10
18
|
* atomic deep paths.
|
|
@@ -14,13 +22,27 @@ export interface ModuleDescriptor {
|
|
|
14
22
|
name: string;
|
|
15
23
|
/** Icon variants this module ships atomic entry points for. */
|
|
16
24
|
supportedVariants: IconVariant[];
|
|
25
|
+
/**
|
|
26
|
+
* Icon variants for which this module ships a *headless* (Griffel-free) build.
|
|
27
|
+
* Empty when the module has no headless build at all.
|
|
28
|
+
*/
|
|
29
|
+
headlessVariants: IconVariant[];
|
|
30
|
+
/**
|
|
31
|
+
* Icon variants for which this module ships *color* icon atoms.
|
|
32
|
+
*
|
|
33
|
+
* Color icons are SVG-only by nature — their gradients cannot be represented
|
|
34
|
+
* in an icon font — so the font builds contain no color glyphs. Empty when the
|
|
35
|
+
* module has no color icons at all.
|
|
36
|
+
*/
|
|
37
|
+
colorVariants: IconVariant[];
|
|
17
38
|
/**
|
|
18
39
|
* Resolves the atomic subpath for a single named import.
|
|
19
40
|
*
|
|
20
41
|
* @param importName - The imported binding name (e.g. `AddFilled`, `bundleIcon`).
|
|
21
42
|
* @param variant - The already-resolved, supported icon variant for this module.
|
|
43
|
+
* @param headless - Whether to resolve to the headless (Griffel-free) build.
|
|
22
44
|
*/
|
|
23
|
-
resolve(importName: string, variant: IconVariant): string;
|
|
45
|
+
resolve(importName: string, variant: IconVariant, headless: boolean): string;
|
|
24
46
|
}
|
|
25
47
|
export declare const MODULES: ModuleDescriptor[];
|
|
26
48
|
export declare const SUPPORTED_MODULE_NAMES: string[];
|
|
@@ -45,3 +67,32 @@ export interface VariantResolution {
|
|
|
45
67
|
* policy logic; callers decide how to surface `error`/`warning`.
|
|
46
68
|
*/
|
|
47
69
|
export declare function resolveModuleVariant(descriptor: ModuleDescriptor, iconVariant: IconVariant, fallbackVariant: IconVariant | undefined): VariantResolution;
|
|
70
|
+
/**
|
|
71
|
+
* Refines an already module-resolved `variant` for a single *color* icon import.
|
|
72
|
+
*
|
|
73
|
+
* Color icons are SVG-only (gradients cannot live in an icon font), so the font
|
|
74
|
+
* builds ship no color glyphs. When the module-resolved `variant` has no color
|
|
75
|
+
* atoms, the color import is rerouted using the same
|
|
76
|
+
* `iconVariant → fallbackVariant → svg` precedence as {@link resolveModuleVariant},
|
|
77
|
+
* constrained to variants that are both supported *and* color-capable. `svg` is
|
|
78
|
+
* always both, so a resolution always exists.
|
|
79
|
+
*
|
|
80
|
+
* Callers must only invoke this for color imports (see {@link isColorIconName})
|
|
81
|
+
* and only after module resolution has succeeded.
|
|
82
|
+
*/
|
|
83
|
+
export declare function resolveColorVariant(descriptor: ModuleDescriptor, variant: IconVariant, iconVariant: IconVariant, fallbackVariant: IconVariant | undefined): {
|
|
84
|
+
variant: IconVariant;
|
|
85
|
+
warning?: string;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Resolves whether to use a module's headless build, given the requested
|
|
89
|
+
* `headless` flag and the already-resolved `variant`.
|
|
90
|
+
*
|
|
91
|
+
* Headless is best-effort: when a module has no headless build for the resolved
|
|
92
|
+
* variant, the loader degrades to the standard (Griffel) implementation and
|
|
93
|
+
* records a warning rather than failing the build.
|
|
94
|
+
*/
|
|
95
|
+
export declare function resolveModuleHeadless(descriptor: ModuleDescriptor, variant: IconVariant, headless: boolean): {
|
|
96
|
+
headless: boolean;
|
|
97
|
+
warning?: string;
|
|
98
|
+
};
|
package/lib/modules.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolveModuleVariant = exports.getModuleDescriptor = exports.SUPPORTED_MODULE_NAMES = exports.MODULES = exports.DEFAULT_SAFETY_VARIANT = void 0;
|
|
3
|
+
exports.resolveModuleHeadless = exports.resolveColorVariant = exports.resolveModuleVariant = exports.getModuleDescriptor = exports.SUPPORTED_MODULE_NAMES = exports.MODULES = exports.isColorIconName = exports.DEFAULT_SAFETY_VARIANT = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* The variant used as the ultimate safety net when neither the requested
|
|
6
6
|
* `iconVariant` nor the configured `fallbackVariant` is supported by a module.
|
|
@@ -11,6 +11,17 @@ const ICON_SUFFIX_REGEX = /(\d*)?(Regular|Filled|Light|Color)$/;
|
|
|
11
11
|
function isIconName(importName) {
|
|
12
12
|
return ICON_SUFFIX_REGEX.test(importName);
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Whether an import name is a *color* icon variant, i.e. its style suffix is
|
|
16
|
+
* `Color` (e.g. `AddCircleColor`, `AddCircle20Color`). Every icon export carries
|
|
17
|
+
* exactly one trailing style suffix, so a `Color` suffix unambiguously marks a
|
|
18
|
+
* color variant — icons whose base name merely contains the word "Color" (e.g.
|
|
19
|
+
* `TextColorRegular`) end in a different style suffix.
|
|
20
|
+
*/
|
|
21
|
+
function isColorIconName(importName) {
|
|
22
|
+
return ICON_SUFFIX_REGEX.exec(importName)?.[2] === 'Color';
|
|
23
|
+
}
|
|
24
|
+
exports.isColorIconName = isColorIconName;
|
|
14
25
|
function toKebabCase(value) {
|
|
15
26
|
return value.replace(/[a-z\d](?=[A-Z])|[a-zA-Z](?=\d)|[A-Z](?=[A-Z][a-z])/g, '$&-').toLowerCase();
|
|
16
27
|
}
|
|
@@ -20,24 +31,35 @@ function iconBaseName(importName) {
|
|
|
20
31
|
const reactIcons = {
|
|
21
32
|
name: '@fluentui/react-icons',
|
|
22
33
|
supportedVariants: ['svg', 'fonts', 'svg-sprite'],
|
|
23
|
-
|
|
34
|
+
// Headless ships svg + fonts today; headless svg-sprite is not generated yet.
|
|
35
|
+
headlessVariants: ['svg', 'fonts'],
|
|
36
|
+
// Color icons ship in svg + svg-sprite; the font build has no color glyphs.
|
|
37
|
+
colorVariants: ['svg', 'svg-sprite'],
|
|
38
|
+
resolve(importName, variant, headless) {
|
|
24
39
|
if (importName === 'useIconContext' || importName === 'IconDirectionContextProvider') {
|
|
40
|
+
// Context is framework-agnostic and shared by both APIs.
|
|
25
41
|
return '@fluentui/react-icons/providers';
|
|
26
42
|
}
|
|
43
|
+
const pkg = headless ? '@fluentui/react-icons/headless' : '@fluentui/react-icons';
|
|
27
44
|
if (!isIconName(importName)) {
|
|
28
|
-
return
|
|
45
|
+
return `${pkg}/utils`;
|
|
29
46
|
}
|
|
30
|
-
return
|
|
47
|
+
return `${pkg}/${variant}/${iconBaseName(importName)}`;
|
|
31
48
|
},
|
|
32
49
|
};
|
|
33
50
|
const reactBrandIcons = {
|
|
34
51
|
name: '@fluentui/react-brand-icons',
|
|
35
52
|
supportedVariants: ['svg'],
|
|
36
|
-
|
|
53
|
+
// Brand icons ship a headless (Griffel-free) svg build.
|
|
54
|
+
headlessVariants: ['svg'],
|
|
55
|
+
// Brand icons ship a single svg build, which already includes color icons.
|
|
56
|
+
colorVariants: ['svg'],
|
|
57
|
+
resolve(importName, _variant, headless) {
|
|
58
|
+
const pkg = headless ? '@fluentui/react-brand-icons/headless' : '@fluentui/react-brand-icons';
|
|
37
59
|
if (!isIconName(importName)) {
|
|
38
|
-
return
|
|
60
|
+
return `${pkg}/utils`;
|
|
39
61
|
}
|
|
40
|
-
return
|
|
62
|
+
return `${pkg}/svg/${iconBaseName(importName)}`;
|
|
41
63
|
},
|
|
42
64
|
};
|
|
43
65
|
exports.MODULES = [reactIcons, reactBrandIcons];
|
|
@@ -73,3 +95,59 @@ function resolveModuleVariant(descriptor, iconVariant, fallbackVariant) {
|
|
|
73
95
|
};
|
|
74
96
|
}
|
|
75
97
|
exports.resolveModuleVariant = resolveModuleVariant;
|
|
98
|
+
/**
|
|
99
|
+
* Refines an already module-resolved `variant` for a single *color* icon import.
|
|
100
|
+
*
|
|
101
|
+
* Color icons are SVG-only (gradients cannot live in an icon font), so the font
|
|
102
|
+
* builds ship no color glyphs. When the module-resolved `variant` has no color
|
|
103
|
+
* atoms, the color import is rerouted using the same
|
|
104
|
+
* `iconVariant → fallbackVariant → svg` precedence as {@link resolveModuleVariant},
|
|
105
|
+
* constrained to variants that are both supported *and* color-capable. `svg` is
|
|
106
|
+
* always both, so a resolution always exists.
|
|
107
|
+
*
|
|
108
|
+
* Callers must only invoke this for color imports (see {@link isColorIconName})
|
|
109
|
+
* and only after module resolution has succeeded.
|
|
110
|
+
*/
|
|
111
|
+
function resolveColorVariant(descriptor, variant, iconVariant, fallbackVariant) {
|
|
112
|
+
// Already color-capable — nothing to reroute (e.g. svg, svg-sprite).
|
|
113
|
+
if (descriptor.colorVariants.includes(variant)) {
|
|
114
|
+
return { variant };
|
|
115
|
+
}
|
|
116
|
+
const candidates = [iconVariant, fallbackVariant, exports.DEFAULT_SAFETY_VARIANT].filter((candidate) => candidate !== undefined);
|
|
117
|
+
const colorVariant = candidates.find((candidate) => descriptor.supportedVariants.includes(candidate) && descriptor.colorVariants.includes(candidate)) ?? exports.DEFAULT_SAFETY_VARIANT;
|
|
118
|
+
return {
|
|
119
|
+
variant: colorVariant,
|
|
120
|
+
warning: `"${descriptor.name}" has no color icons for variant "${variant}" ` +
|
|
121
|
+
`(color icons are SVG-only). Resolving Color imports to "${colorVariant}".`,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
exports.resolveColorVariant = resolveColorVariant;
|
|
125
|
+
/**
|
|
126
|
+
* Resolves whether to use a module's headless build, given the requested
|
|
127
|
+
* `headless` flag and the already-resolved `variant`.
|
|
128
|
+
*
|
|
129
|
+
* Headless is best-effort: when a module has no headless build for the resolved
|
|
130
|
+
* variant, the loader degrades to the standard (Griffel) implementation and
|
|
131
|
+
* records a warning rather than failing the build.
|
|
132
|
+
*/
|
|
133
|
+
function resolveModuleHeadless(descriptor, variant, headless) {
|
|
134
|
+
if (!headless) {
|
|
135
|
+
return { headless: false };
|
|
136
|
+
}
|
|
137
|
+
if (descriptor.headlessVariants.includes(variant)) {
|
|
138
|
+
return { headless: true };
|
|
139
|
+
}
|
|
140
|
+
if (descriptor.headlessVariants.length === 0) {
|
|
141
|
+
return {
|
|
142
|
+
headless: false,
|
|
143
|
+
warning: `"${descriptor.name}" has no headless build; using its standard (Griffel) ` + `implementation for this import.`,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
headless: false,
|
|
148
|
+
warning: `"${descriptor.name}" has no headless build for variant "${variant}" ` +
|
|
149
|
+
`(headless supports: ${descriptor.headlessVariants.join(', ')}); using its standard ` +
|
|
150
|
+
`(Griffel) implementation for this import.`,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
exports.resolveModuleHeadless = resolveModuleHeadless;
|
package/lib/transform.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ interface TransformOptions {
|
|
|
5
5
|
iconVariant: IconVariant;
|
|
6
6
|
/** The variant to fall back to when a module does not support `iconVariant`. */
|
|
7
7
|
fallbackVariant?: IconVariant;
|
|
8
|
+
/** Resolve to the headless (Griffel-free) build where the module supports it. */
|
|
9
|
+
headless?: boolean;
|
|
8
10
|
path: string;
|
|
9
11
|
}
|
|
10
12
|
export interface Diagnostic {
|
package/lib/transform.js
CHANGED
|
@@ -8,7 +8,7 @@ const oxc_parser_1 = require("oxc-parser");
|
|
|
8
8
|
const magic_string_1 = __importDefault(require("magic-string"));
|
|
9
9
|
const modules_1 = require("./modules");
|
|
10
10
|
function transformSource(source, options) {
|
|
11
|
-
const { iconVariant, fallbackVariant, path } = options;
|
|
11
|
+
const { iconVariant, fallbackVariant, headless = false, path } = options;
|
|
12
12
|
const result = (0, oxc_parser_1.parseSync)(path, source, {
|
|
13
13
|
sourceType: 'module',
|
|
14
14
|
});
|
|
@@ -18,39 +18,80 @@ function transformSource(source, options) {
|
|
|
18
18
|
const { staticImports, staticExports } = result.module;
|
|
19
19
|
const src = new magic_string_1.default(source);
|
|
20
20
|
const diagnostics = [];
|
|
21
|
-
//
|
|
22
|
-
|
|
21
|
+
// Dedupe diagnostics by message so a module's variant / color / headless
|
|
22
|
+
// concern surfaces at most once, even though resolution now runs per
|
|
23
|
+
// (module, color-ness) rather than per module.
|
|
24
|
+
const seenDiagnostics = new Set();
|
|
25
|
+
const pushDiagnostic = (diagnostic) => {
|
|
26
|
+
const key = `${diagnostic.level}:${diagnostic.message}`;
|
|
27
|
+
if (seenDiagnostics.has(key))
|
|
28
|
+
return;
|
|
29
|
+
seenDiagnostics.add(key);
|
|
30
|
+
diagnostics.push(diagnostic);
|
|
31
|
+
};
|
|
32
|
+
// Resolve each referenced module at most once per color-ness: color icons may
|
|
33
|
+
// route to a different variant than their non-color siblings, so the cache key
|
|
34
|
+
// is `${name}:${isColor}`. Resolution stays O(#modules × 2) regardless of how
|
|
35
|
+
// many icons a file imports.
|
|
36
|
+
const resolvedTargets = new Map();
|
|
23
37
|
/**
|
|
24
|
-
* Returns the variant to rewrite a referenced
|
|
25
|
-
* could not be resolved (an error
|
|
26
|
-
* should be left untouched).
|
|
38
|
+
* Returns the target (variant + headless) to rewrite a single referenced
|
|
39
|
+
* import with, or `null` when the module could not be resolved (an error
|
|
40
|
+
* diagnostic has been recorded and the import should be left untouched).
|
|
27
41
|
*/
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
42
|
+
const targetFor = (descriptor, isColor) => {
|
|
43
|
+
const cacheKey = `${descriptor.name}:${isColor}`;
|
|
44
|
+
if (resolvedTargets.has(cacheKey)) {
|
|
45
|
+
return resolvedTargets.get(cacheKey);
|
|
31
46
|
}
|
|
32
47
|
const resolution = (0, modules_1.resolveModuleVariant)(descriptor, iconVariant, fallbackVariant);
|
|
33
48
|
if (resolution.warning) {
|
|
34
|
-
|
|
49
|
+
pushDiagnostic({ level: 'warning', message: resolution.warning });
|
|
35
50
|
}
|
|
36
51
|
if (resolution.error) {
|
|
37
|
-
|
|
52
|
+
pushDiagnostic({ level: 'error', message: resolution.error });
|
|
53
|
+
}
|
|
54
|
+
if (!resolution.variant) {
|
|
55
|
+
resolvedTargets.set(cacheKey, null);
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
let variant = resolution.variant;
|
|
59
|
+
// Color icons are SVG-only; reroute them off any color-less variant (fonts)
|
|
60
|
+
// to a color-capable one, honoring the fallback precedence.
|
|
61
|
+
if (isColor) {
|
|
62
|
+
const colorResolution = (0, modules_1.resolveColorVariant)(descriptor, variant, iconVariant, fallbackVariant);
|
|
63
|
+
if (colorResolution.warning) {
|
|
64
|
+
pushDiagnostic({ level: 'warning', message: colorResolution.warning });
|
|
65
|
+
}
|
|
66
|
+
variant = colorResolution.variant;
|
|
38
67
|
}
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
68
|
+
const headlessResolution = (0, modules_1.resolveModuleHeadless)(descriptor, variant, headless);
|
|
69
|
+
if (headlessResolution.warning) {
|
|
70
|
+
pushDiagnostic({ level: 'warning', message: headlessResolution.warning });
|
|
71
|
+
}
|
|
72
|
+
const target = { variant, headless: headlessResolution.headless };
|
|
73
|
+
resolvedTargets.set(cacheKey, target);
|
|
74
|
+
return target;
|
|
42
75
|
};
|
|
43
76
|
for (const imp of staticImports) {
|
|
44
77
|
const moduleName = imp.moduleRequest.value;
|
|
45
78
|
const descriptor = (0, modules_1.getModuleDescriptor)(moduleName);
|
|
46
79
|
if (!descriptor)
|
|
47
80
|
continue;
|
|
48
|
-
const variant = variantFor(descriptor);
|
|
49
|
-
if (!variant)
|
|
50
|
-
continue;
|
|
51
81
|
const namedEntries = imp.entries.filter((e) => e.importName.kind === 'Name');
|
|
52
82
|
if (namedEntries.length === 0)
|
|
53
83
|
continue;
|
|
84
|
+
// Resolve each named specifier independently — color icons may route to a
|
|
85
|
+
// different variant than their non-color siblings in the same statement.
|
|
86
|
+
const resolvedEntries = namedEntries.map((entry) => ({
|
|
87
|
+
entry,
|
|
88
|
+
importedName: entry.importName.name,
|
|
89
|
+
target: targetFor(descriptor, (0, modules_1.isColorIconName)(entry.importName.name)),
|
|
90
|
+
}));
|
|
91
|
+
// A module-level resolution error is independent of color-ness, so if any
|
|
92
|
+
// specifier is unresolved they all are — leave the whole statement untouched.
|
|
93
|
+
if (resolvedEntries.some(({ target }) => !target))
|
|
94
|
+
continue;
|
|
54
95
|
const otherEntries = imp.entries.filter((e) => e.importName.kind !== 'Name');
|
|
55
96
|
const lines = [];
|
|
56
97
|
if (otherEntries.length > 0) {
|
|
@@ -59,10 +100,9 @@ function transformSource(source, options) {
|
|
|
59
100
|
.join(', ');
|
|
60
101
|
lines.push(`import ${names} from '${moduleName}';`);
|
|
61
102
|
}
|
|
62
|
-
for (const entry of
|
|
63
|
-
const importedName = entry.importName.name;
|
|
103
|
+
for (const { entry, importedName, target } of resolvedEntries) {
|
|
64
104
|
const localName = entry.localName.value;
|
|
65
|
-
const newSource = descriptor.resolve(importedName, variant);
|
|
105
|
+
const newSource = descriptor.resolve(importedName, target.variant, target.headless);
|
|
66
106
|
const spec = importedName === localName ? importedName : `${importedName} as ${localName}`;
|
|
67
107
|
lines.push(`import { ${spec} } from '${newSource}';`);
|
|
68
108
|
}
|
|
@@ -81,12 +121,12 @@ function transformSource(source, options) {
|
|
|
81
121
|
for (const entry of relevantEntries) {
|
|
82
122
|
const moduleName = entry.moduleRequest.value;
|
|
83
123
|
const descriptor = (0, modules_1.getModuleDescriptor)(moduleName);
|
|
84
|
-
const variant = variantFor(descriptor);
|
|
85
|
-
if (!variant)
|
|
86
|
-
continue;
|
|
87
124
|
const importedName = entry.importName.name;
|
|
125
|
+
const target = targetFor(descriptor, (0, modules_1.isColorIconName)(importedName));
|
|
126
|
+
if (!target)
|
|
127
|
+
continue;
|
|
88
128
|
const exportedName = entry.exportName.name;
|
|
89
|
-
const newSource = descriptor.resolve(importedName, variant);
|
|
129
|
+
const newSource = descriptor.resolve(importedName, target.variant, target.headless);
|
|
90
130
|
const spec = importedName === exportedName ? importedName : `${importedName} as ${exportedName}`;
|
|
91
131
|
lines.push(`export { ${spec} } from '${newSource}';`);
|
|
92
132
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluentui/react-icons-atomic-webpack-loader",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Webpack loader that transforms barrel imports and re-exports from @fluentui/react-icons into atomic deep paths",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|