@dxos/vite-plugin-icons 0.0.0-stage → 0.0.0
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/LICENSE +105 -0
- package/dist/src/dev-sprite.test.d.ts +2 -0
- package/dist/src/dev-sprite.test.d.ts.map +1 -0
- package/dist/src/dev-sprite.test.js +99 -0
- package/dist/src/dev-sprite.test.js.map +1 -0
- package/dist/src/icon-assets.d.ts +22 -0
- package/dist/src/icon-assets.d.ts.map +1 -0
- package/dist/src/icon-assets.js +68 -0
- package/dist/src/icon-assets.js.map +1 -0
- package/dist/src/icon-assets.test.d.ts +2 -0
- package/dist/src/icon-assets.test.d.ts.map +1 -0
- package/dist/src/icon-assets.test.js +36 -0
- package/dist/src/icon-assets.test.js.map +1 -0
- package/dist/src/index.d.ts +26 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +352 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/normalize-sprite.d.ts +22 -0
- package/dist/src/normalize-sprite.d.ts.map +1 -0
- package/dist/src/normalize-sprite.js +65 -0
- package/dist/src/normalize-sprite.js.map +1 -0
- package/dist/src/normalize-sprite.test.d.ts +2 -0
- package/dist/src/normalize-sprite.test.d.ts.map +1 -0
- package/dist/src/normalize-sprite.test.js +73 -0
- package/dist/src/normalize-sprite.test.js.map +1 -0
- package/dist/src/resolve-symbols.d.ts +42 -0
- package/dist/src/resolve-symbols.d.ts.map +1 -0
- package/dist/src/resolve-symbols.js +36 -0
- package/dist/src/resolve-symbols.js.map +1 -0
- package/dist/src/resolve-symbols.test.d.ts +2 -0
- package/dist/src/resolve-symbols.test.d.ts.map +1 -0
- package/dist/src/resolve-symbols.test.js +58 -0
- package/dist/src/resolve-symbols.test.js.map +1 -0
- package/dist/src/symbol-pattern.d.ts +20 -0
- package/dist/src/symbol-pattern.d.ts.map +1 -0
- package/dist/src/symbol-pattern.js +29 -0
- package/dist/src/symbol-pattern.js.map +1 -0
- package/dist/src/symbol-pattern.test.d.ts +2 -0
- package/dist/src/symbol-pattern.test.d.ts.map +1 -0
- package/dist/src/symbol-pattern.test.js +66 -0
- package/dist/src/symbol-pattern.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +35 -3
- package/src/dev-sprite.test.ts +112 -0
- package/src/icon-assets.test.ts +44 -0
- package/src/icon-assets.ts +82 -0
- package/src/index.ts +425 -0
- package/src/normalize-sprite.test.ts +101 -0
- package/src/normalize-sprite.ts +77 -0
- package/src/resolve-symbols.test.ts +74 -0
- package/src/resolve-symbols.ts +76 -0
- package/src/symbol-pattern.test.ts +77 -0
- package/src/symbol-pattern.ts +45 -0
- package/README.md +0 -3
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, expect, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { type AssetStat, resolveSymbols } from './resolve-symbols.ts';
|
|
8
|
+
|
|
9
|
+
describe('resolveSymbols', () => {
|
|
10
|
+
test('resolves a symbol whose asset exists', () => {
|
|
11
|
+
const { resolved, missing } = resolve(['px--circle--regular'], { '/assets/px/regular/circle.svg': stat() });
|
|
12
|
+
expect(resolved).toEqual([{ symbol: 'px--circle--regular', path: '/assets/px/regular/circle.svg' }]);
|
|
13
|
+
expect(missing).toEqual([]);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('reports a symbol with no asset instead of resolving it', () => {
|
|
17
|
+
const { resolved, missing } = resolve(['ph--github--regular'], {});
|
|
18
|
+
expect(resolved).toEqual([]);
|
|
19
|
+
expect(missing).toEqual([{ symbol: 'ph--github--regular', path: '/assets/ph/regular/github.svg' }]);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('keeps the good symbols when one is missing', () => {
|
|
23
|
+
const { resolved, missing } = resolve(['px--circle--regular', 'ph--github--regular', 'px--a--bold'], {
|
|
24
|
+
'/assets/px/regular/circle.svg': stat(),
|
|
25
|
+
'/assets/px/bold/a.svg': stat(),
|
|
26
|
+
});
|
|
27
|
+
expect(resolved.map(({ symbol }) => symbol)).toEqual(['px--circle--regular', 'px--a--bold']);
|
|
28
|
+
expect(missing.map(({ symbol }) => symbol)).toEqual(['ph--github--regular']);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('skips a name the pattern cannot parse', () => {
|
|
32
|
+
const { resolved, missing } = resolve(['not-an-icon'], {});
|
|
33
|
+
expect(resolved).toEqual([]);
|
|
34
|
+
expect(missing).toEqual([]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('fingerprint', () => {
|
|
38
|
+
test('is stable across iteration order', () => {
|
|
39
|
+
const files = { '/assets/px/regular/a.svg': stat(), '/assets/px/regular/b.svg': stat(2, 200) };
|
|
40
|
+
const first = resolve(['px--a--regular', 'px--b--regular'], files).fingerprint;
|
|
41
|
+
const second = resolve(['px--b--regular', 'px--a--regular'], files).fingerprint;
|
|
42
|
+
expect(second).toEqual(first);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('changes when an asset is redrawn, though the symbol set is identical', () => {
|
|
46
|
+
const before = resolve(['px--a--regular'], { '/assets/px/regular/a.svg': stat(1, 100) }).fingerprint;
|
|
47
|
+
const after = resolve(['px--a--regular'], { '/assets/px/regular/a.svg': stat(2, 140) }).fingerprint;
|
|
48
|
+
expect(after).not.toEqual(before);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('changes when a symbol is added', () => {
|
|
52
|
+
const files = { '/assets/px/regular/a.svg': stat(), '/assets/px/regular/b.svg': stat() };
|
|
53
|
+
const before = resolve(['px--a--regular'], files).fingerprint;
|
|
54
|
+
const after = resolve(['px--a--regular', 'px--b--regular'], files).fingerprint;
|
|
55
|
+
expect(after).not.toEqual(before);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('ignores a missing symbol, so a typo does not force a rewrite every pass', () => {
|
|
59
|
+
const files = { '/assets/px/regular/a.svg': stat() };
|
|
60
|
+
expect(resolve(['px--a--regular', 'ph--github--regular'], files).fingerprint).toEqual(
|
|
61
|
+
resolve(['px--a--regular'], files).fingerprint,
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const symbolPattern = '(ph|px)--([a-z]+[a-z-]*)--(bold|regular)';
|
|
68
|
+
|
|
69
|
+
const assetPath = (iconSet: string, name: string, variant: string) => `/assets/${iconSet}/${variant}/${name}.svg`;
|
|
70
|
+
|
|
71
|
+
const resolve = (symbols: string[], files: Record<string, AssetStat>) =>
|
|
72
|
+
resolveSymbols({ symbols, symbolPattern, assetPath, stat: (path) => files[path] });
|
|
73
|
+
|
|
74
|
+
const stat = (mtimeMs = 1, size = 100): AssetStat => ({ mtimeMs, size });
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export type AssetStat = { mtimeMs: number; size: number };
|
|
6
|
+
|
|
7
|
+
export type ResolvedSymbol = {
|
|
8
|
+
symbol: string;
|
|
9
|
+
path: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type ResolveSymbolsResult = {
|
|
13
|
+
/** Symbols whose asset exists, in detection order. */
|
|
14
|
+
resolved: ResolvedSymbol[];
|
|
15
|
+
/**
|
|
16
|
+
* Symbols with no asset behind them — a mistyped name, or an SVG not added yet — paired with the
|
|
17
|
+
* path that was looked for. Excluded from the sprite rather than passed to `makeSprite`, whose
|
|
18
|
+
* `readFile` would reject and, from the fire-and-forget debounced write, take the dev server down
|
|
19
|
+
* with it. The path lets the caller watch for the file appearing.
|
|
20
|
+
*/
|
|
21
|
+
missing: ResolvedSymbol[];
|
|
22
|
+
/**
|
|
23
|
+
* Identity of what a sprite built from `resolved` would contain: the symbol names plus each
|
|
24
|
+
* asset's mtime and size. Unchanged fingerprint means an identical sprite, so the write can be
|
|
25
|
+
* skipped; a redrawn SVG changes it even though the symbol set has not grown.
|
|
26
|
+
*/
|
|
27
|
+
fingerprint: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type ResolveSymbolsParams = {
|
|
31
|
+
symbols: Iterable<string>;
|
|
32
|
+
symbolPattern: string;
|
|
33
|
+
/** Called with the pattern's capture groups, matching `@ch-ui/icons`' own contract. */
|
|
34
|
+
assetPath: (...matches: string[]) => string;
|
|
35
|
+
/** Returns undefined when the path does not exist. */
|
|
36
|
+
stat: (path: string) => AssetStat | undefined;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Splits detected symbols into those backed by an asset and those not, and fingerprints the former.
|
|
41
|
+
*
|
|
42
|
+
* Both halves exist to keep the dev server usable while icons are being drawn: a name with no file
|
|
43
|
+
* is reported instead of thrown, and the fingerprint makes an edit to an existing SVG produce a
|
|
44
|
+
* fresh sprite (counting symbols cannot, since redrawing changes no count).
|
|
45
|
+
*/
|
|
46
|
+
export const resolveSymbols = ({
|
|
47
|
+
symbols,
|
|
48
|
+
symbolPattern,
|
|
49
|
+
assetPath,
|
|
50
|
+
stat,
|
|
51
|
+
}: ResolveSymbolsParams): ResolveSymbolsResult => {
|
|
52
|
+
const expression = new RegExp(symbolPattern);
|
|
53
|
+
const resolved: ResolvedSymbol[] = [];
|
|
54
|
+
const missing: ResolvedSymbol[] = [];
|
|
55
|
+
const stamps: string[] = [];
|
|
56
|
+
|
|
57
|
+
for (const symbol of symbols) {
|
|
58
|
+
const match = symbol.match(expression);
|
|
59
|
+
if (!match?.[1]) {
|
|
60
|
+
// Unparseable names never reached `makeSprite` either; it skips them silently.
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const [, ...groups] = match;
|
|
64
|
+
const path = assetPath(...groups.map((group) => group ?? ''));
|
|
65
|
+
const stats = stat(path);
|
|
66
|
+
if (stats) {
|
|
67
|
+
resolved.push({ symbol, path });
|
|
68
|
+
stamps.push(`${symbol}:${stats.mtimeMs}:${stats.size}`);
|
|
69
|
+
} else {
|
|
70
|
+
missing.push({ symbol, path });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
stamps.sort();
|
|
75
|
+
return { resolved, missing, fingerprint: stamps.join('|') };
|
|
76
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, expect, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { iconSymbolPattern } from './symbol-pattern.ts';
|
|
8
|
+
|
|
9
|
+
// The composer / storybook configuration: Phosphor at every weight, the custom sets at `regular`.
|
|
10
|
+
const pattern = iconSymbolPattern({ sets: ['ph', 'dx', 'px'], regularOnly: ['dx', 'px'] });
|
|
11
|
+
|
|
12
|
+
/** First match, or undefined — mirrors how `@ch-ui/icons` scans (unanchored, global). */
|
|
13
|
+
const scan = (content: string) => content.match(new RegExp(pattern, 'gm'))?.[0];
|
|
14
|
+
|
|
15
|
+
describe('iconSymbolPattern', () => {
|
|
16
|
+
test.for([
|
|
17
|
+
['ph--house--regular', 'ph--house--regular'],
|
|
18
|
+
['ph--house--bold', 'ph--house--bold'],
|
|
19
|
+
['ph--house--duotone', 'ph--house--duotone'],
|
|
20
|
+
['ph--github-logo--regular', 'ph--github-logo--regular'],
|
|
21
|
+
['dx--dxos--regular', 'dx--dxos--regular'],
|
|
22
|
+
['px--anthropic--regular', 'px--anthropic--regular'],
|
|
23
|
+
["icon='ph--house--regular'", 'ph--house--regular'],
|
|
24
|
+
['url(#ph--house--regular)', 'ph--house--regular'],
|
|
25
|
+
] as const)('matches %s', ([content, expected]) => {
|
|
26
|
+
expect(scan(content)).toEqual(expected);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test.for([
|
|
30
|
+
// Single-weight sets carry no variants.
|
|
31
|
+
'dx--dxos--bold',
|
|
32
|
+
'px--anthropic--fill',
|
|
33
|
+
// A name assembled at runtime is not a literal the scanner can resolve.
|
|
34
|
+
'`${set}--house--regular`',
|
|
35
|
+
// Digits are not part of an icon name.
|
|
36
|
+
'ph--house2--regular',
|
|
37
|
+
] as const)('does not match %s', (content) => {
|
|
38
|
+
expect(scan(content)).toBeUndefined();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('boundaries', () => {
|
|
42
|
+
test.for([
|
|
43
|
+
// A set name that is the tail of a preceding token. `4px` is the one that shows up for real:
|
|
44
|
+
// `px` is also a CSS unit, and stylesheets are scanned.
|
|
45
|
+
'margin: 4px--foo--regular',
|
|
46
|
+
'MYph--house--regular',
|
|
47
|
+
'_ph--house--regular',
|
|
48
|
+
'foo-ph--house--regular',
|
|
49
|
+
// A longer word that merely starts with a weight.
|
|
50
|
+
'ph--house--regularized',
|
|
51
|
+
'ph--house--bolder',
|
|
52
|
+
] as const)('rejects %s', (content) => {
|
|
53
|
+
expect(scan(content)).toBeUndefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('rejects the set-suffix leak the single-weight lookahead would otherwise open', () => {
|
|
57
|
+
// Declining `dx--x--bold` at position 0 makes the engine retry at position 1, where `x--…`
|
|
58
|
+
// must not be read as a set of its own.
|
|
59
|
+
expect(scan('dx--x--bold')).toBeUndefined();
|
|
60
|
+
expect(iconSymbolPattern({ sets: ['ph', 'dx', 'x'], regularOnly: ['dx'] })).toBeDefined();
|
|
61
|
+
expect(
|
|
62
|
+
'dx--foo--bold'.match(new RegExp(iconSymbolPattern({ sets: ['ph', 'dx', 'x'], regularOnly: ['dx'] }), 'gm')),
|
|
63
|
+
).toBeNull();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('omits the single-weight lookahead when every set carries all weights', () => {
|
|
68
|
+
const all = iconSymbolPattern({ sets: ['ph'] });
|
|
69
|
+
// The trailing boundary is also a negative lookahead, so match on the weight alternation.
|
|
70
|
+
expect(all).not.toContain('--(?:bold');
|
|
71
|
+
expect('ph--house--bold'.match(new RegExp(all))?.[0]).toEqual('ph--house--bold');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('exposes the capture groups `assetPath` is called with', () => {
|
|
75
|
+
expect('px--anthropic--regular'.match(new RegExp(pattern))?.slice(1)).toEqual(['px', 'anthropic', 'regular']);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/** Phosphor's weights; the only variants an icon name may carry. */
|
|
6
|
+
export const WEIGHTS = ['bold', 'duotone', 'fill', 'light', 'regular', 'thin'] as const;
|
|
7
|
+
|
|
8
|
+
// A symbol reference is always delimited by a quote, brace, `#`, or whitespace, never by another
|
|
9
|
+
// identifier character. Required because `@ch-ui/icons` scans unanchored: without these, a set name
|
|
10
|
+
// that is a suffix of a preceding token matches (`4px--x--regular` in any scanned CSS, or the `x` in
|
|
11
|
+
// `dx--x--bold` once the single-weight lookahead below has declined position 0), and so does a
|
|
12
|
+
// longer word that merely starts with a weight (`--regularized` matching `--regular`). Each yields a
|
|
13
|
+
// symbol name nothing put there, whose asset is then missing.
|
|
14
|
+
const BOUNDARY_BEFORE = '(?<![a-zA-Z0-9_-])';
|
|
15
|
+
const BOUNDARY_AFTER = '(?![a-zA-Z0-9_-])';
|
|
16
|
+
|
|
17
|
+
// Icon names are lowercase kebab-case; digits are deliberately excluded, matching the icon sets.
|
|
18
|
+
const NAME = '[a-z]+[a-z-]*';
|
|
19
|
+
|
|
20
|
+
export type SymbolPatternParams = {
|
|
21
|
+
/** Icon-set prefixes in match order, e.g. `['ph', 'dx', 'px']`. */
|
|
22
|
+
sets: string[];
|
|
23
|
+
/**
|
|
24
|
+
* Sets drawn at a single weight, so only `regular` is a valid variant for them. Without this a
|
|
25
|
+
* name like `dx--dxos--bold` resolves to a file that does not exist.
|
|
26
|
+
*/
|
|
27
|
+
regularOnly?: string[];
|
|
28
|
+
weights?: readonly string[];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Builds the `symbolPattern` for `IconsPlugin`.
|
|
33
|
+
*
|
|
34
|
+
* Defined here rather than written out per host: every host needs the identical pattern, and the
|
|
35
|
+
* boundary and single-weight rules are subtle enough that three hand-maintained copies drifted.
|
|
36
|
+
*/
|
|
37
|
+
export const iconSymbolPattern = ({ sets, regularOnly = [], weights = WEIGHTS }: SymbolPatternParams): string => {
|
|
38
|
+
const others = weights.filter((weight) => weight !== 'regular');
|
|
39
|
+
const singleWeight =
|
|
40
|
+
regularOnly.length > 0 && others.length > 0
|
|
41
|
+
? `(?!(?:${regularOnly.join('|')})--${NAME}--(?:${others.join('|')}))`
|
|
42
|
+
: '';
|
|
43
|
+
|
|
44
|
+
return BOUNDARY_BEFORE + singleWeight + `(${sets.join('|')})--(${NAME})--(${weights.join('|')})` + BOUNDARY_AFTER;
|
|
45
|
+
};
|
package/README.md
DELETED