Skip to content

Commit b3182c0

Browse files
committed
build: extract correct tokens set from core theme
The tokens extraction script assumes that all the imported tokens will be used in the `overrides` mixin. That's true for all themes, except for `core` which only supports some of the mixins. These changes add a separate function that `core` can use to tell the tokens extraction script about which tokens are supported.
1 parent be543d1 commit b3182c0

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

src/material/core/_core-theme.scss

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,19 @@
7171
}
7272
}
7373

74-
@mixin overrides($tokens: ()) {
74+
// This theme is a special case where not all of the imported tokens are supported in `overrides`.
75+
// To aid the docs token extraction, we have to pull the `overrides` token config out into a
76+
// separate function.
77+
// !!!Important!!! renaming or removal of this function requires the `extract-tokens.ts` script to
78+
// be updated as well.
79+
@function _get-supported-overrides-tokens() {
7580
$app-tokens: tokens-mat-app.get-token-slots();
7681
$ripple-tokens: tokens-mat-ripple.get-token-slots();
7782
$option-tokens: tokens-mat-option.get-token-slots();
7883
$full-pseudo-checkbox-tokens: tokens-mat-full-pseudo-checkbox.get-token-slots();
7984
$minimal-pseudo-checkbox-tokens: tokens-mat-minimal-pseudo-checkbox.get-token-slots();
8085

81-
@include token-utils.batch-create-token-values(
82-
$tokens,
86+
@return (
8387
(prefix: tokens-mat-app.$prefix, tokens: $app-tokens),
8488
(prefix: tokens-mat-ripple.$prefix, tokens: $ripple-tokens),
8589
(prefix: tokens-mat-option.$prefix, tokens: $option-tokens),
@@ -88,6 +92,10 @@
8892
);
8993
}
9094

95+
@mixin overrides($tokens: ()) {
96+
@include token-utils.batch-create-token-values($tokens, _get-supported-overrides-tokens()...);
97+
}
98+
9199
// Mixin that renders all of the core styles that depend on the theme.
92100
@mixin theme($theme, $options...) {
93101
// Wrap the sub-theme includes in the duplicate theme styles mixin. This ensures that

tools/extract-tokens/extract-tokens.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,35 @@ function getTokenExtractionCode(
187187

188188
// Goes through all the available namespaces looking for a `$prefix` variable. This allows us to
189189
// determine the prefixes that are used within the theme. Once we know the prefixes we can use
190-
// them to extract only the tokens we care about from the full token set.
190+
// them to extract only the tokens we care about from the full token set. Note: `core-theme`
191+
// is a special case where not all of the imported tokens are supported in the `overrides`
192+
// mixin. Such cases will expose a `_get-supported-overrides-tokens` private function that
193+
// can be used to determine the exact set of prefixes that are used.
191194
// After the tokens are extracted, we log them out using `@debug` and then intercept the log
192195
// in order to get the raw data. We have to go through `@debug`, because there's no way to
193196
// output a Sass map to CSS.
194197
// The tokens are encoded as JSON so we don't have to implement parsing of Sass maps in TS.
195198
const append = `
196-
$__namespaces: (${useStatements.map(stmt => `"${extractNamespace(stmt)}"`).join(', ')});
199+
$__prefixes: ();
200+
201+
@if ${meta}.function-exists('_get-supported-overrides-tokens') {
202+
$__configs: _get-supported-overrides-tokens();
203+
204+
@each $config in $__configs {
205+
$__prefixes: ${list}.append($__prefixes, ${map}.get($config, prefix));
206+
}
207+
} @else {
208+
$__namespaces: (${useStatements.map(stmt => `"${extractNamespace(stmt)}"`).join(', ')});
209+
210+
@each $name in $__namespaces {
211+
$prefix: ${map}.get(${meta}.module-variables($name), prefix);
212+
213+
@if ($prefix) {
214+
$__prefixes: ${list}.append($__prefixes, $prefix);
215+
}
216+
}
217+
}
218+
197219
$__all-color: ${m3Tokens}.generate-color-tokens(light, ${palettes}.$azure-palette,
198220
${palettes}.$azure-palette, ${palettes}.$azure-palette, 'sys');
199221
$__all-typography: ${m3Tokens}.generate-typography-tokens(font, 100, 100, 100, 100, 'sys');
@@ -204,15 +226,11 @@ function getTokenExtractionCode(
204226
$__density: ();
205227
$__base: ();
206228
207-
@each $name in $__namespaces {
208-
$prefix: map-get(${meta}.module-variables($name), 'prefix');
209-
210-
@if ($prefix) {
211-
$__color: ${map}.set($__color, $prefix, map-get($__all-color, $prefix));
212-
$__typography: ${map}.set($__typography, $prefix, map-get($__all-typography, $prefix));
213-
$__density: ${map}.set($__density, $prefix, map-get($__all-density, $prefix));
214-
$__base: ${map}.set($__base, $prefix, map-get($__all-base, $prefix));
215-
}
229+
@each $prefix in $__prefixes {
230+
$__color: ${map}.set($__color, $prefix, ${map}.get($__all-color, $prefix));
231+
$__typography: ${map}.set($__typography, $prefix, ${map}.get($__all-typography, $prefix));
232+
$__density: ${map}.set($__density, $prefix, ${map}.get($__all-density, $prefix));
233+
$__base: ${map}.set($__base, $prefix, ${map}.get($__all-base, $prefix));
216234
}
217235
218236
// Define our JSON.stringify implementation so it can be used below.

0 commit comments

Comments
 (0)