Files
iptvnator/tools/nx/check-material-theme-scope.test.mjs
T
4grayandClaude Fable 5.1 ffe6f974f7 perf(web): theme only the Material components the app imports
m3-theme.scss included mat.all-component-themes and
mat.all-component-colors, which emit the tokens of every Material component:
23,646 bytes of the initial stylesheet belonged to 17 components nothing
imports (datepicker, chips, stepper, expansion, radio, tabs, badge, table,
paginator, sidenav, bottom sheet, toolbar, timepicker, grid list, tree,
autocomplete, sort). A $material-components list, in Material's order, now
drives the *-theme and *-color mixins through meta.apply, so light and dark
cannot drift apart. tools/nx/check-material-theme-scope.mjs (CI) compares
the list with the @angular/material/<entry> imports of the runtime sources
in both directions: a newly imported component must be themed, and a
component whose last import went must leave the list. core (ripples,
options, pseudo-checkboxes) always stays, and the button entry point keeps
button, icon-button and fab together.

The compiled stylesheet lost exactly 426 declarations, all tokens of those
17 components plus the dead .mat-drawer-inner-container rule in styles.scss
(sidenav is never imported); nothing was added or changed.

renderer.initialBytes: 1,432,237 -> 1,408,099 bytes (-24,138) on a local
production build.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-13 09:49:55 +02:00

108 lines
3.1 KiB
JavaScript

import assert from 'node:assert/strict';
import { test } from 'node:test';
import {
compareThemeScope,
requiredThemes,
themedComponents,
usedEntryPoints,
} from './check-material-theme-scope.mjs';
const lines = (...rows) => rows.join('\n');
test('usedEntryPoints lists each Material entry point with its first importer', () => {
const importers = usedEntryPoints([
{
file: 'a.component.ts',
source: lines(
"import { MatButtonModule } from '@angular/material/button';",
'import {',
' MatDialog,',
"} from '@angular/material/dialog';",
'import { MatIcon } from "@angular/material/icon";'
),
},
{
file: 'b.component.ts',
source: "import { MatButton } from '@angular/material/button';",
},
]);
assert.deepEqual(
[...importers],
[
['button', 'a.component.ts'],
['dialog', 'a.component.ts'],
['icon', 'a.component.ts'],
]
);
});
test('requiredThemes always keeps core and expands the button entry point', () => {
const required = requiredThemes(
new Map([
['button', 'a.ts'],
['tooltip', 'b.ts'],
['testing', 'c.ts'],
])
);
assert.deepEqual(
[...required],
[
['core', 'always themed'],
['button', '@angular/material/button in a.ts'],
['icon-button', '@angular/material/button in a.ts'],
['fab', '@angular/material/button in a.ts'],
['tooltip', '@angular/material/tooltip in b.ts'],
]
);
});
test('themedComponents reads the list, direct includes and the all-components mixin', () => {
assert.deepEqual(
themedComponents(
lines(
'$material-components: (',
' core,',
' button, // used by every dialog',
' icon-button,',
');',
'@include mat.tooltip-theme($theme);'
)
),
{
themed: new Set(['core', 'button', 'icon-button', 'tooltip']),
all: false,
}
);
assert.deepEqual(
themedComponents('html { @include mat.all-component-themes($t); }'),
{ themed: new Set(), all: true }
);
});
test('compareThemeScope reports missing and surplus themes', () => {
const required = new Map([
['core', 'always themed'],
['button', '@angular/material/button in a.ts'],
]);
assert.deepEqual(
compareThemeScope(required, {
themed: new Set(['core', 'tabs']),
all: false,
}),
{
unthemed: [['button', '@angular/material/button in a.ts']],
unused: ['tabs'],
}
);
// all-component-themes leaves nothing unthemed, but is still surplus-free
// only when the list is empty.
assert.deepEqual(
compareThemeScope(required, { themed: new Set(), all: true }),
{ unthemed: [], unused: [] }
);
});