@elastic/distillate 0.0.0 → 0.1.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.txt +93 -0
- package/NOTICE.txt +30 -0
- package/README.md +142 -0
- package/THIRD_PARTY_LICENSES.md +768 -0
- package/dist/cjs/collector.js +420 -0
- package/dist/cjs/collector.js.map +1 -0
- package/dist/cjs/css_scan.js +186 -0
- package/dist/cjs/css_scan.js.map +1 -0
- package/dist/cjs/dom_sink.js +40 -0
- package/dist/cjs/dom_sink.js.map +1 -0
- package/dist/cjs/emotion.js +189 -0
- package/dist/cjs/emotion.js.map +1 -0
- package/dist/cjs/engine.js +79 -0
- package/dist/cjs/engine.js.map +1 -0
- package/dist/cjs/environment.js +9 -0
- package/dist/cjs/environment.js.map +1 -0
- package/dist/cjs/hash.js +86 -0
- package/dist/cjs/hash.js.map +1 -0
- package/dist/cjs/idents.js +23 -0
- package/dist/cjs/idents.js.map +1 -0
- package/dist/cjs/index.js +42 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/instance.js +34 -0
- package/dist/cjs/instance.js.map +1 -0
- package/dist/cjs/local_vars.js +100 -0
- package/dist/cjs/local_vars.js.map +1 -0
- package/dist/cjs/minify.js +77 -0
- package/dist/cjs/minify.js.map +1 -0
- package/dist/cjs/names.js +77 -0
- package/dist/cjs/names.js.map +1 -0
- package/dist/cjs/nesting.js +406 -0
- package/dist/cjs/nesting.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/runtime.js +257 -0
- package/dist/cjs/runtime.js.map +1 -0
- package/dist/cjs/styles/authoring.js +169 -0
- package/dist/cjs/styles/authoring.js.map +1 -0
- package/dist/cjs/styles/declarations.js +139 -0
- package/dist/cjs/styles/declarations.js.map +1 -0
- package/dist/cjs/styles/index.js +32 -0
- package/dist/cjs/styles/index.js.map +1 -0
- package/dist/cjs/styles/module.js +233 -0
- package/dist/cjs/styles/module.js.map +1 -0
- package/dist/cjs/styles/registry.js +187 -0
- package/dist/cjs/styles/registry.js.map +1 -0
- package/dist/cjs/styles/types.js +9 -0
- package/dist/cjs/styles/types.js.map +1 -0
- package/dist/cjs/testing.js +37 -0
- package/dist/cjs/testing.js.map +1 -0
- package/dist/cjs/theme.js +249 -0
- package/dist/cjs/theme.js.map +1 -0
- package/dist/cjs/tokens/contextual_var.js +39 -0
- package/dist/cjs/tokens/contextual_var.js.map +1 -0
- package/dist/cjs/tokens/index.js +24 -0
- package/dist/cjs/tokens/index.js.map +1 -0
- package/dist/cjs/tokens/kind.js +14 -0
- package/dist/cjs/tokens/kind.js.map +1 -0
- package/dist/cjs/tokens/light_dark.js +42 -0
- package/dist/cjs/tokens/light_dark.js.map +1 -0
- package/dist/cjs/tokens/scale_token.js +28 -0
- package/dist/cjs/tokens/scale_token.js.map +1 -0
- package/dist/cjs/tokens/theme_token.js +28 -0
- package/dist/cjs/tokens/theme_token.js.map +1 -0
- package/dist/cjs/var_invariant.js +115 -0
- package/dist/cjs/var_invariant.js.map +1 -0
- package/dist/collector.d.ts +84 -0
- package/dist/collector.d.ts.map +1 -0
- package/dist/collector.js +416 -0
- package/dist/collector.js.map +1 -0
- package/dist/css_scan.d.ts +57 -0
- package/dist/css_scan.d.ts.map +1 -0
- package/dist/css_scan.js +180 -0
- package/dist/css_scan.js.map +1 -0
- package/dist/dom_sink.d.ts +27 -0
- package/dist/dom_sink.d.ts.map +1 -0
- package/dist/dom_sink.js +36 -0
- package/dist/dom_sink.js.map +1 -0
- package/dist/emotion.d.ts +47 -0
- package/dist/emotion.d.ts.map +1 -0
- package/dist/emotion.js +184 -0
- package/dist/emotion.js.map +1 -0
- package/dist/engine.d.ts +41 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +75 -0
- package/dist/engine.js.map +1 -0
- package/dist/environment.d.ts +43 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +8 -0
- package/dist/environment.js.map +1 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +82 -0
- package/dist/hash.js.map +1 -0
- package/dist/idents.d.ts +9 -0
- package/dist/idents.d.ts.map +1 -0
- package/dist/idents.js +19 -0
- package/dist/idents.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/instance.d.ts +16 -0
- package/dist/instance.d.ts.map +1 -0
- package/dist/instance.js +30 -0
- package/dist/instance.js.map +1 -0
- package/dist/local_vars.d.ts +75 -0
- package/dist/local_vars.d.ts.map +1 -0
- package/dist/local_vars.js +92 -0
- package/dist/local_vars.js.map +1 -0
- package/dist/minify.d.ts +11 -0
- package/dist/minify.d.ts.map +1 -0
- package/dist/minify.js +73 -0
- package/dist/minify.js.map +1 -0
- package/dist/names.d.ts +39 -0
- package/dist/names.d.ts.map +1 -0
- package/dist/names.js +70 -0
- package/dist/names.js.map +1 -0
- package/dist/nesting.d.ts +59 -0
- package/dist/nesting.d.ts.map +1 -0
- package/dist/nesting.js +400 -0
- package/dist/nesting.js.map +1 -0
- package/dist/runtime.d.ts +36 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +252 -0
- package/dist/runtime.js.map +1 -0
- package/dist/styles/authoring.d.ts +59 -0
- package/dist/styles/authoring.d.ts.map +1 -0
- package/dist/styles/authoring.js +155 -0
- package/dist/styles/authoring.js.map +1 -0
- package/dist/styles/declarations.d.ts +10 -0
- package/dist/styles/declarations.d.ts.map +1 -0
- package/dist/styles/declarations.js +132 -0
- package/dist/styles/declarations.js.map +1 -0
- package/dist/styles/index.d.ts +7 -0
- package/dist/styles/index.d.ts.map +1 -0
- package/dist/styles/index.js +12 -0
- package/dist/styles/index.js.map +1 -0
- package/dist/styles/module.d.ts +5 -0
- package/dist/styles/module.d.ts.map +1 -0
- package/dist/styles/module.js +229 -0
- package/dist/styles/module.js.map +1 -0
- package/dist/styles/registry.d.ts +48 -0
- package/dist/styles/registry.d.ts.map +1 -0
- package/dist/styles/registry.js +182 -0
- package/dist/styles/registry.js.map +1 -0
- package/dist/styles/types.d.ts +211 -0
- package/dist/styles/types.d.ts.map +1 -0
- package/dist/styles/types.js +8 -0
- package/dist/styles/types.js.map +1 -0
- package/dist/testing.d.ts +10 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +31 -0
- package/dist/testing.js.map +1 -0
- package/dist/theme.d.ts +85 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +240 -0
- package/dist/theme.js.map +1 -0
- package/dist/tokens/contextual_var.d.ts +35 -0
- package/dist/tokens/contextual_var.d.ts.map +1 -0
- package/dist/tokens/contextual_var.js +33 -0
- package/dist/tokens/contextual_var.js.map +1 -0
- package/dist/tokens/index.d.ts +5 -0
- package/dist/tokens/index.d.ts.map +1 -0
- package/dist/tokens/index.js +11 -0
- package/dist/tokens/index.js.map +1 -0
- package/dist/tokens/kind.d.ts +2 -0
- package/dist/tokens/kind.d.ts.map +1 -0
- package/dist/tokens/kind.js +10 -0
- package/dist/tokens/kind.js.map +1 -0
- package/dist/tokens/light_dark.d.ts +20 -0
- package/dist/tokens/light_dark.d.ts.map +1 -0
- package/dist/tokens/light_dark.js +37 -0
- package/dist/tokens/light_dark.js.map +1 -0
- package/dist/tokens/scale_token.d.ts +20 -0
- package/dist/tokens/scale_token.d.ts.map +1 -0
- package/dist/tokens/scale_token.js +23 -0
- package/dist/tokens/scale_token.js.map +1 -0
- package/dist/tokens/theme_token.d.ts +20 -0
- package/dist/tokens/theme_token.d.ts.map +1 -0
- package/dist/tokens/theme_token.js +23 -0
- package/dist/tokens/theme_token.js.map +1 -0
- package/dist/var_invariant.d.ts +25 -0
- package/dist/var_invariant.d.ts.map +1 -0
- package/dist/var_invariant.js +108 -0
- package/dist/var_invariant.js.map +1 -0
- package/docs/concepts/authoring.md +115 -0
- package/docs/concepts/collection.md +66 -0
- package/docs/concepts/distillery.md +47 -0
- package/docs/concepts/index.md +18 -0
- package/docs/concepts/naming-and-output.md +123 -0
- package/docs/concepts/ordering.md +61 -0
- package/docs/concepts/single-copy.md +103 -0
- package/docs/concepts/tokens.md +99 -0
- package/docs/docset.yml +53 -0
- package/docs/examples/01-stylesheet.ts +20 -0
- package/docs/examples/02-artifact.ts +26 -0
- package/docs/examples/03-variants.ts +40 -0
- package/docs/examples/04-emotion.ts +33 -0
- package/docs/examples/05-var-invariant.ts +31 -0
- package/docs/examples/06-payload.ts +85 -0
- package/docs/examples/fixture.ts +22 -0
- package/docs/getting-started/index.md +14 -0
- package/docs/getting-started/installation.md +30 -0
- package/docs/getting-started/quick-start.md +61 -0
- package/docs/getting-started/your-first-stylesheet.md +57 -0
- package/docs/guides/compact-artifacts.md +87 -0
- package/docs/guides/emotion-migration.md +60 -0
- package/docs/guides/index.md +17 -0
- package/docs/guides/name-collisions.md +37 -0
- package/docs/guides/non-css-surfaces.md +73 -0
- package/docs/guides/react-renderer.md +158 -0
- package/docs/guides/theming.md +107 -0
- package/docs/guides/var-invariant.md +24 -0
- package/docs/guides/vs-emotion.md +61 -0
- package/docs/index.md +73 -0
- package/docs/logo.svg +7 -0
- package/docs/playground/index.html +12 -0
- package/docs/playground/package.json +23 -0
- package/docs/playground/src/app/app.tsx +109 -0
- package/docs/playground/src/app/css_panel.tsx +119 -0
- package/docs/playground/src/app/editor_panel.tsx +87 -0
- package/docs/playground/src/app/index.ts +8 -0
- package/docs/playground/src/app/monaco_css_editor.tsx +84 -0
- package/docs/playground/src/app/monaco_source_editor.tsx +132 -0
- package/docs/playground/src/app/preview_panel.tsx +99 -0
- package/docs/playground/src/app/tokens_panel.tsx +117 -0
- package/docs/playground/src/app/types.ts +10 -0
- package/docs/playground/src/examples/snippets.test.ts +28 -0
- package/docs/playground/src/examples/snippets.ts +418 -0
- package/docs/playground/src/lib/compile.test.ts +157 -0
- package/docs/playground/src/lib/compile.ts +242 -0
- package/docs/playground/src/lib/demo_environment.test.ts +123 -0
- package/docs/playground/src/lib/demo_environment.ts +140 -0
- package/docs/playground/src/lib/editor_lib.test.ts +39 -0
- package/docs/playground/src/lib/editor_lib.ts +142 -0
- package/docs/playground/src/lib/format_css.test.ts +41 -0
- package/docs/playground/src/lib/format_css.ts +71 -0
- package/docs/playground/src/main.tsx +25 -0
- package/docs/playground/src/setup/chrome.test.ts +47 -0
- package/docs/playground/src/setup/chrome.ts +365 -0
- package/docs/playground/src/setup/monaco_env.ts +64 -0
- package/docs/playground/tsconfig.json +22 -0
- package/docs/playground/vite.config.ts +37 -0
- package/docs/playground/vitest.config.ts +35 -0
- package/docs/playground.md +28 -0
- package/docs/reference/api/emotion.md +372 -0
- package/docs/reference/api/index-1.md +2180 -0
- package/docs/reference/api/index.md +9 -0
- package/docs/reference/api/testing.md +92 -0
- package/docs/reference/api.md +12 -0
- package/docs/reference/entry-points.md +125 -0
- package/docs/reference/environment.md +60 -0
- package/docs/reference/index.md +11 -0
- package/docs/reference/okf-map.md +92 -0
- package/package.json +127 -4
- package/src/collector.ts +526 -0
- package/src/css_scan.ts +263 -0
- package/src/dom_sink.ts +65 -0
- package/src/emotion.ts +311 -0
- package/src/engine.ts +191 -0
- package/src/environment.ts +56 -0
- package/src/hash.ts +111 -0
- package/src/idents.ts +21 -0
- package/src/index.ts +93 -0
- package/src/instance.ts +49 -0
- package/src/local_vars.ts +220 -0
- package/src/minify.ts +87 -0
- package/src/names.ts +129 -0
- package/src/nesting.ts +596 -0
- package/src/runtime.ts +428 -0
- package/src/styles/authoring.ts +244 -0
- package/src/styles/declarations.ts +172 -0
- package/src/styles/index.ts +58 -0
- package/src/styles/module.ts +396 -0
- package/src/styles/registry.ts +261 -0
- package/src/styles/types.ts +283 -0
- package/src/testing.ts +48 -0
- package/src/theme.ts +454 -0
- package/src/tokens/contextual_var.ts +72 -0
- package/src/tokens/index.ts +17 -0
- package/src/tokens/kind.ts +13 -0
- package/src/tokens/light_dark.ts +58 -0
- package/src/tokens/scale_token.ts +41 -0
- package/src/tokens/theme_token.ts +42 -0
- package/src/var_invariant.ts +147 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
4
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
5
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
6
|
+
* 2.0.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.assertVarRefsHaveDeclarations = exports.findVarRefViolations = exports.collectReferencedCustomProperties = exports.collectDeclaredCustomProperties = void 0;
|
|
10
|
+
const css_scan_1 = require("./css_scan");
|
|
11
|
+
const DECLARATION_RE = /(--[A-Za-z_][\w-]*)\s*:/g;
|
|
12
|
+
const REFERENCE_RE = /var\(\s*(--[A-Za-z_][\w-]*)/g;
|
|
13
|
+
const scan = (css) => {
|
|
14
|
+
const masked = [];
|
|
15
|
+
const blockAt = [];
|
|
16
|
+
for (const span of (0, css_scan_1.scanCss)(css)) {
|
|
17
|
+
const blank = (0, css_scan_1.isOpaque)(span);
|
|
18
|
+
for (let offset = 0; offset < span.text.length; offset += 1) {
|
|
19
|
+
masked.push(blank ? ' ' : (span.text[offset] ?? ''));
|
|
20
|
+
blockAt.push(span.block);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return { masked: masked.join(''), blockAt };
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* A custom property is declared where a declaration can start: inside a
|
|
27
|
+
* declarations block, as the first thing after `{` or `;`. A selector carrying
|
|
28
|
+
* a BEM-style modifier and a pseudo-class (`.button--active:hover`) matches the
|
|
29
|
+
* same characters and declares nothing.
|
|
30
|
+
*/
|
|
31
|
+
const isDeclarationStart = ({ masked, blockAt }, index) => {
|
|
32
|
+
if (blockAt[index] !== 'declarations') {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
for (let cursor = index - 1; cursor >= 0; cursor -= 1) {
|
|
36
|
+
const char = masked[cursor];
|
|
37
|
+
if (char === undefined || !/\s/.test(char)) {
|
|
38
|
+
return char === '{' || char === ';';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* `var` has to open a function token rather than end a longer identifier:
|
|
45
|
+
* `--expr:myvar(--x)` names some other function.
|
|
46
|
+
*/
|
|
47
|
+
const isFunctionTokenStart = (masked, index) => {
|
|
48
|
+
const before = masked[index - 1];
|
|
49
|
+
return before === undefined || !/[\w\\-]/.test(before);
|
|
50
|
+
};
|
|
51
|
+
/** Collects every custom property declared in a declaration position in `css`. */
|
|
52
|
+
const collectDeclaredCustomProperties = (css) => {
|
|
53
|
+
const scanned = scan(css);
|
|
54
|
+
const declared = new Set();
|
|
55
|
+
for (const match of scanned.masked.matchAll(DECLARATION_RE)) {
|
|
56
|
+
const name = match[1];
|
|
57
|
+
if (name && isDeclarationStart(scanned, match.index ?? 0)) {
|
|
58
|
+
declared.add(name);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return declared;
|
|
62
|
+
};
|
|
63
|
+
exports.collectDeclaredCustomProperties = collectDeclaredCustomProperties;
|
|
64
|
+
/** Collects all custom property names referenced inside `var(...)` calls in `css`. */
|
|
65
|
+
const collectReferencedCustomProperties = (css) => {
|
|
66
|
+
const { masked } = scan(css);
|
|
67
|
+
const names = [];
|
|
68
|
+
for (const match of masked.matchAll(REFERENCE_RE)) {
|
|
69
|
+
const name = match[1];
|
|
70
|
+
if (name && isFunctionTokenStart(masked, match.index ?? 0)) {
|
|
71
|
+
names.push(name);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return names;
|
|
75
|
+
};
|
|
76
|
+
exports.collectReferencedCustomProperties = collectReferencedCustomProperties;
|
|
77
|
+
/**
|
|
78
|
+
* Returns all `var(...)` references whose custom property is never declared in `css`.
|
|
79
|
+
*
|
|
80
|
+
* @param css Full CSS text to scan.
|
|
81
|
+
*/
|
|
82
|
+
const findVarRefViolations = (css) => {
|
|
83
|
+
const { masked } = scan(css);
|
|
84
|
+
const declared = (0, exports.collectDeclaredCustomProperties)(css);
|
|
85
|
+
const violations = [];
|
|
86
|
+
for (const match of masked.matchAll(REFERENCE_RE)) {
|
|
87
|
+
const ref = match[1];
|
|
88
|
+
const index = match.index ?? 0;
|
|
89
|
+
if (!ref || declared.has(ref) || !isFunctionTokenStart(masked, index)) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
violations.push({
|
|
93
|
+
reference: ref,
|
|
94
|
+
context: css.slice(Math.max(0, index - 40), index + ref.length + 8),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return violations;
|
|
98
|
+
};
|
|
99
|
+
exports.findVarRefViolations = findVarRefViolations;
|
|
100
|
+
/**
|
|
101
|
+
* Throws if any `var(...)` in `css` has no matching custom-property declaration.
|
|
102
|
+
*
|
|
103
|
+
* @param css Full CSS text to scan.
|
|
104
|
+
* @throws If any reference has no declaration in the same text.
|
|
105
|
+
*/
|
|
106
|
+
const assertVarRefsHaveDeclarations = (css) => {
|
|
107
|
+
const violations = (0, exports.findVarRefViolations)(css);
|
|
108
|
+
if (violations.length === 0) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const lines = violations.map(({ reference, context }) => `- ${reference} referenced near "${context.replace(/\s+/g, ' ')}"`);
|
|
112
|
+
throw new Error(`${violations.length} var(...) reference(s) without a matching declaration:\n${lines.join('\n')}`);
|
|
113
|
+
};
|
|
114
|
+
exports.assertVarRefsHaveDeclarations = assertVarRefsHaveDeclarations;
|
|
115
|
+
//# sourceMappingURL=var_invariant.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"var_invariant.js","sourceRoot":"","sources":["../../src/var_invariant.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,yCAAkE;AAElE,MAAM,cAAc,GAAG,0BAA0B,CAAC;A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,CAAC,CAAC;IAC7C,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO;IACT,CAAC;IACD,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,CAC1B,CAAC,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,EAAE,CACzB,KAAK,SAAS,qBAAqB,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CACrE,CAAC;IACF,MAAM,IAAI,KAAK,CACb,GAAG,UAAU,CAAC,MAAM,2DAA2D,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAClG,CAAC;AACJ,CAAC,CAAC;AAZW,QAAA,6BAA6B,iCAYxC","sourcesContent":["/*\n * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one\n * or more contributor license agreements. Licensed under the Elastic License\n * 2.0; you may not use this file except in compliance with the Elastic License\n * 2.0.\n */\n\nimport { type CssBlockKind, isOpaque, scanCss } from './css_scan';\n\nconst DECLARATION_RE = /(--[A-Za-z_][\\w-]*)\\s*:/g;\nconst REFERENCE_RE = /var\\(\\s*(--[A-Za-z_][\\w-]*)/g;\n\n/** A `var(...)` whose custom property is never declared in the same CSS text. */\nexport interface VarInvariantViolation {\n /** The undeclared custom property name (e.g. `--aui-color-bg`). */\n readonly reference: string;\n /** Surrounding CSS text for error reporting. */\n readonly context: string;\n}\n\ninterface ScannedCss {\n /**\n * `css` with comments, strings, `url(...)` tokens, and escapes blanked to\n * spaces, positions preserved. A `var(` inside a string is text, a `;`\n * inside an escape is part of a value, and neither should match here.\n */\n readonly masked: string;\n /** The enclosing block kind at each character offset. */\n readonly blockAt: readonly CssBlockKind[];\n}\n\nconst scan = (css: string): ScannedCss => {\n const masked: string[] = [];\n const blockAt: CssBlockKind[] = [];\n for (const span of scanCss(css)) {\n const blank = isOpaque(span);\n for (let offset = 0; offset < span.text.length; offset += 1) {\n masked.push(blank ? ' ' : (span.text[offset] ?? ''));\n blockAt.push(span.block);\n }\n }\n return { masked: masked.join(''), blockAt };\n};\n\n/**\n * A custom property is declared where a declaration can start: inside a\n * declarations block, as the first thing after `{` or `;`. A selector carrying\n * a BEM-style modifier and a pseudo-class (`.button--active:hover`) matches the\n * same characters and declares nothing.\n */\nconst isDeclarationStart = (\n { masked, blockAt }: ScannedCss,\n index: number\n): boolean => {\n if (blockAt[index] !== 'declarations') {\n return false;\n }\n for (let cursor = index - 1; cursor >= 0; cursor -= 1) {\n const char = masked[cursor];\n if (char === undefined || !/\\s/.test(char)) {\n return char === '{' || char === ';';\n }\n }\n return false;\n};\n\n/**\n * `var` has to open a function token rather than end a longer identifier:\n * `--expr:myvar(--x)` names some other function.\n */\nconst isFunctionTokenStart = (masked: string, index: number): boolean => {\n const before = masked[index - 1];\n return before === undefined || !/[\\w\\\\-]/.test(before);\n};\n\n/** Collects every custom property declared in a declaration position in `css`. */\nexport const collectDeclaredCustomProperties = (css: string): Set<string> => {\n const scanned = scan(css);\n const declared = new Set<string>();\n for (const match of scanned.masked.matchAll(DECLARATION_RE)) {\n const name = match[1];\n if (name && isDeclarationStart(scanned, match.index ?? 0)) {\n declared.add(name);\n }\n }\n return declared;\n};\n\n/** Collects all custom property names referenced inside `var(...)` calls in `css`. */\nexport const collectReferencedCustomProperties = (\n css: string\n): readonly string[] => {\n const { masked } = scan(css);\n const names: string[] = [];\n for (const match of masked.matchAll(REFERENCE_RE)) {\n const name = match[1];\n if (name && isFunctionTokenStart(masked, match.index ?? 0)) {\n names.push(name);\n }\n }\n return names;\n};\n\n/**\n * Returns all `var(...)` references whose custom property is never declared in `css`.\n *\n * @param css Full CSS text to scan.\n */\nexport const findVarRefViolations = (\n css: string\n): readonly VarInvariantViolation[] => {\n const { masked } = scan(css);\n const declared = collectDeclaredCustomProperties(css);\n const violations: VarInvariantViolation[] = [];\n for (const match of masked.matchAll(REFERENCE_RE)) {\n const ref = match[1];\n const index = match.index ?? 0;\n if (!ref || declared.has(ref) || !isFunctionTokenStart(masked, index)) {\n continue;\n }\n violations.push({\n reference: ref,\n context: css.slice(Math.max(0, index - 40), index + ref.length + 8),\n });\n }\n return violations;\n};\n\n/**\n * Throws if any `var(...)` in `css` has no matching custom-property declaration.\n *\n * @param css Full CSS text to scan.\n * @throws If any reference has no declaration in the same text.\n */\nexport const assertVarRefsHaveDeclarations = (css: string): void => {\n const violations = findVarRefViolations(css);\n if (violations.length === 0) {\n return;\n }\n const lines = violations.map(\n ({ reference, context }) =>\n `- ${reference} referenced near \"${context.replace(/\\s+/g, ' ')}\"`\n );\n throw new Error(\n `${violations.length} var(...) reference(s) without a matching declaration:\\n${lines.join('\\n')}`\n );\n};\n"]}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type StyleNameMode, type StyleNameResolver, type StyleTarget } from './names.js';
|
|
2
|
+
import type { ContextualVarPath, StyleEntry, StyleHandle, StyleRegistry, StylesModule } from './styles/index.js';
|
|
3
|
+
/** Target, naming mode, registry, and prefix for one {@link StylesCollector}. */
|
|
4
|
+
export interface StylesCollectorOptions {
|
|
5
|
+
/** Target context (e.g. `artifact` or `stylesheet`). */
|
|
6
|
+
target: StyleTarget;
|
|
7
|
+
/** Desired name mode (`compact` or `readable`). */
|
|
8
|
+
names: StyleNameMode;
|
|
9
|
+
/** Registry modules were authored against; used for auto-collection and compact-name snapshots. */
|
|
10
|
+
registry: StyleRegistry;
|
|
11
|
+
/** Brand prefix forwarded to the name resolver. */
|
|
12
|
+
prefix: string;
|
|
13
|
+
/** When `true`, emit no-op-handle warnings through `warn`. Inherited from `createDistillery({ dev })`. */
|
|
14
|
+
dev?: boolean;
|
|
15
|
+
/** Warning sink for no-op handles. Used only when `dev` is `true`. Defaults to `console.warn`. */
|
|
16
|
+
warn?: (message: string) => void;
|
|
17
|
+
}
|
|
18
|
+
/** Reachable entries for one render or one stylesheet. */
|
|
19
|
+
export declare class StylesCollector {
|
|
20
|
+
/** The target collection mode. */
|
|
21
|
+
readonly target: StyleTarget;
|
|
22
|
+
/** The naming mode (compact vs readable). */
|
|
23
|
+
readonly names: StyleNameMode;
|
|
24
|
+
/** Associated registry. */
|
|
25
|
+
readonly registry: StyleRegistry;
|
|
26
|
+
/** Emitted class name prefix. */
|
|
27
|
+
readonly prefix: string;
|
|
28
|
+
private readonly dev;
|
|
29
|
+
private readonly warn;
|
|
30
|
+
private readonly entries;
|
|
31
|
+
private readonly handles;
|
|
32
|
+
private readonly themeDeps;
|
|
33
|
+
private readonly varDeps;
|
|
34
|
+
constructor({ target, names, registry, prefix, dev, warn, }: StylesCollectorOptions);
|
|
35
|
+
/**
|
|
36
|
+
* Collects a module's non-variant entries, or one explicit entry.
|
|
37
|
+
*
|
|
38
|
+
* Variant entries stay off this path; collect them via `useHandles`.
|
|
39
|
+
*
|
|
40
|
+
* @param value Module (all non-variant entries), a single entry, or an array of entries.
|
|
41
|
+
*/
|
|
42
|
+
use(value: StylesModule | StyleEntry | readonly StyleEntry[]): void;
|
|
43
|
+
/**
|
|
44
|
+
* Collects every entry on `module`, including variants. Used by the stylesheet target.
|
|
45
|
+
*
|
|
46
|
+
* @param module Module whose entries should all be collected.
|
|
47
|
+
*/
|
|
48
|
+
useAllEntries(module: StylesModule): void;
|
|
49
|
+
/**
|
|
50
|
+
* Collects handles and auto-includes rules whose selector deps are all present.
|
|
51
|
+
*
|
|
52
|
+
* Media blocks include only inner rules whose deps are met.
|
|
53
|
+
*
|
|
54
|
+
* @param handles Handles resolved during this render pass (e.g. from `resolveClassName`).
|
|
55
|
+
* @returns Handles that were retained. Empty untargeted handles are omitted in compact mode.
|
|
56
|
+
*/
|
|
57
|
+
useHandles(handles: readonly StyleHandle[]): readonly StyleHandle[];
|
|
58
|
+
/** Collects `auto` rules on `module` whose every selector dependency is already collected. */
|
|
59
|
+
useRulesWhenDepsMet(module: StylesModule): void;
|
|
60
|
+
private tryActivateDependent;
|
|
61
|
+
private allDepsMet;
|
|
62
|
+
/** Marks a theme token path as reachable so it appears in the emitted theme block, even if no collected declaration reads it. */
|
|
63
|
+
useThemeVar(path: string): void;
|
|
64
|
+
/** Creates a {@link StyleNameResolver} scoped to this collector's current dep set. */
|
|
65
|
+
createResolver(): StyleNameResolver;
|
|
66
|
+
/** Collected style entries sorted for emission (base rules before all `@media`/`@container` blocks). */
|
|
67
|
+
get collectedEntries(): readonly StyleEntry[];
|
|
68
|
+
/** Theme token paths that survive reachability; emitted in the theme scope block. */
|
|
69
|
+
get collectedThemeDeps(): ReadonlySet<string>;
|
|
70
|
+
/** Contextual var paths that survive reachability; used by the runtime for compact-name substitution. */
|
|
71
|
+
get collectedVarDeps(): ReadonlySet<ContextualVarPath>;
|
|
72
|
+
/**
|
|
73
|
+
* Returns the variable keys within `groupPath` that are reachable from `handle` or from any collected rule targeting it.
|
|
74
|
+
*
|
|
75
|
+
* @param handle Handle whose declaration refs and dependent rules are consulted.
|
|
76
|
+
* @param groupPath Group prefix path (e.g. `vars/chip/look`).
|
|
77
|
+
*/
|
|
78
|
+
reachableDefaults(handle: StyleHandle, groupPath: `vars/${string}`): ReadonlySet<string>;
|
|
79
|
+
private finalizeDeps;
|
|
80
|
+
private retainHandle;
|
|
81
|
+
private addEntry;
|
|
82
|
+
private addDeclarationDeps;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=collector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collector.d.ts","sourceRoot":"","sources":["../src/collector.ts"],"names":[],"mappings":"AAQA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACtB,KAAK,WAAW,EACjB,MAAM,SAAS,CAAC;AACjB,OAAO,KAAK,EACV,iBAAiB,EAIjB,UAAU,EACV,WAAW,EAEX,aAAa,EAEb,YAAY,EACb,MAAM,UAAU,CAAC;AA0DlB,iFAAiF;AACjF,MAAM,WAAW,sBAAsB;IACrC,wDAAwD;IACxD,MAAM,EAAE,WAAW,CAAC;IACpB,mDAAmD;IACnD,KAAK,EAAE,aAAa,CAAC;IACrB,mGAAmG;IACn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B,iIAAiI;IACjI,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAI/B,sFAAsF;IACtF,cAAc,IAAI,iBAAiB;IAiBnC,wGAAwG;IACxG,IAAI,gBAAgB,IAAI,SAAS,UAAU,EAAE,CAE5C;IAED,qFAAqF;IACrF,IAAI,kBAAkB,IAAI,WAAW,CAAC,MAAM,CAAC,CAE5C;IAED,yGAAyG;IACzG,IAAI,gBAAgB,IAAI,WAAW,CAAC,iBAAiB,CAAC,CAErD;IAOD;;;;;OAKG;IACH,iBAAiB,CACf,MAAM,EAAE,WAAW,EACnB,SAAS,EAAE,QAAQ,MAAM,EAAE,GAC1B,WAAW,CAAC,MAAM,CAAC;IAoCtB,OAAO,CAAC,YAAY;IAwCpB,OAAO,CAAC,YAAY;IAgBpB,OAAO,CAAC,QAAQ;IAmBhB,OAAO,CAAC,kBAAkB;CAI3B"}
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
3
|
+
* or more contributor license agreements. Licensed under the Elastic License
|
|
4
|
+
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
5
|
+
* 2.0.
|
|
6
|
+
*/
|
|
7
|
+
import { isLocalVarOverrideMarker } from './local_vars.js';
|
|
8
|
+
import { createStyleNameResolver, } from './names.js';
|
|
9
|
+
import { isEmptyDeclarations } from './styles/declarations.js';
|
|
10
|
+
const warnedHandles = new WeakSet();
|
|
11
|
+
const dependentEntriesCache = new WeakMap();
|
|
12
|
+
const dependentEntries = (module, handleKey) => {
|
|
13
|
+
let cached = dependentEntriesCache.get(module);
|
|
14
|
+
if (!cached) {
|
|
15
|
+
cached = new Map();
|
|
16
|
+
for (const entry of module.entries) {
|
|
17
|
+
if (entry.kind === 'rule') {
|
|
18
|
+
if (!entry.auto) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
for (const dep of entry.dependsOn) {
|
|
22
|
+
appendDependent(cached, dep, entry);
|
|
23
|
+
}
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (entry.kind === 'media') {
|
|
27
|
+
const deps = new Set();
|
|
28
|
+
for (const nested of entry.rules) {
|
|
29
|
+
if (!nested.auto) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
nested.dependsOn.forEach((key) => deps.add(key));
|
|
33
|
+
}
|
|
34
|
+
for (const dep of deps) {
|
|
35
|
+
appendDependent(cached, dep, entry);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
dependentEntriesCache.set(module, cached);
|
|
40
|
+
}
|
|
41
|
+
return cached.get(handleKey) ?? [];
|
|
42
|
+
};
|
|
43
|
+
const appendDependent = (cache, key, entry) => {
|
|
44
|
+
const list = cache.get(key);
|
|
45
|
+
if (list) {
|
|
46
|
+
list.push(entry);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
cache.set(key, [entry]);
|
|
50
|
+
};
|
|
51
|
+
/** Reachable entries for one render or one stylesheet. */
|
|
52
|
+
export class StylesCollector {
|
|
53
|
+
/** The target collection mode. */
|
|
54
|
+
target;
|
|
55
|
+
/** The naming mode (compact vs readable). */
|
|
56
|
+
names;
|
|
57
|
+
/** Associated registry. */
|
|
58
|
+
registry;
|
|
59
|
+
/** Emitted class name prefix. */
|
|
60
|
+
prefix;
|
|
61
|
+
dev;
|
|
62
|
+
warn;
|
|
63
|
+
entries = new Map();
|
|
64
|
+
handles = new Map();
|
|
65
|
+
themeDeps = new Set();
|
|
66
|
+
varDeps = new Set();
|
|
67
|
+
constructor({ target, names, registry, prefix, dev = false, warn = (message) => console.warn(message), }) {
|
|
68
|
+
this.target = target;
|
|
69
|
+
this.names = names;
|
|
70
|
+
this.registry = registry;
|
|
71
|
+
this.prefix = prefix;
|
|
72
|
+
this.dev = dev;
|
|
73
|
+
this.warn = warn;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Collects a module's non-variant entries, or one explicit entry.
|
|
77
|
+
*
|
|
78
|
+
* Variant entries stay off this path; collect them via `useHandles`.
|
|
79
|
+
*
|
|
80
|
+
* @param value Module (all non-variant entries), a single entry, or an array of entries.
|
|
81
|
+
*/
|
|
82
|
+
use(value) {
|
|
83
|
+
if (isReadonlyArray(value)) {
|
|
84
|
+
value.forEach((entry) => this.use(entry));
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (isStylesModule(value)) {
|
|
88
|
+
// Aggregate deps from non-variant entries only. The module-level
|
|
89
|
+
// `themeDeps`/`varDeps` snapshot would also include variant entries'
|
|
90
|
+
// deps, which would push their var paths into the resolver's compact
|
|
91
|
+
// registry and steal short names from actually-rendered handles.
|
|
92
|
+
for (const entry of value.entries) {
|
|
93
|
+
if (entry.variant) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (entry.kind === 'handle' && !this.retainHandle(entry)) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
this.entries.set(entryKey(entry), entry);
|
|
100
|
+
if (entry.kind === 'handle' || entry.kind === 'rule') {
|
|
101
|
+
this.addDeclarationDeps(entry.declarations);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
for (const inner of entry.rules) {
|
|
105
|
+
this.addDeclarationDeps(inner.declarations);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
for (const handle of value.handleList) {
|
|
110
|
+
if (handle.variant) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (!this.retainHandle(handle)) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
this.handles.set(handle.key, handle);
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
this.addEntry(value);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Collects every entry on `module`, including variants. Used by the stylesheet target.
|
|
124
|
+
*
|
|
125
|
+
* @param module Module whose entries should all be collected.
|
|
126
|
+
*/
|
|
127
|
+
useAllEntries(module) {
|
|
128
|
+
for (const entry of module.entries) {
|
|
129
|
+
if (entry.kind === 'handle' && !this.retainHandle(entry)) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
this.entries.set(entryKey(entry), entry);
|
|
133
|
+
}
|
|
134
|
+
for (const handle of module.handleList) {
|
|
135
|
+
if (!this.retainHandle(handle)) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
this.handles.set(handle.key, handle);
|
|
139
|
+
}
|
|
140
|
+
module.themeDeps.forEach((path) => this.themeDeps.add(path));
|
|
141
|
+
module.varDeps.forEach((path) => this.varDeps.add(path));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Collects handles and auto-includes rules whose selector deps are all present.
|
|
145
|
+
*
|
|
146
|
+
* Media blocks include only inner rules whose deps are met.
|
|
147
|
+
*
|
|
148
|
+
* @param handles Handles resolved during this render pass (e.g. from `resolveClassName`).
|
|
149
|
+
* @returns Handles that were retained. Empty untargeted handles are omitted in compact mode.
|
|
150
|
+
*/
|
|
151
|
+
useHandles(handles) {
|
|
152
|
+
const retained = [];
|
|
153
|
+
for (const handle of handles) {
|
|
154
|
+
if (!this.retainHandle(handle)) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
retained.push(handle);
|
|
158
|
+
if (this.handles.has(handle.key)) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
this.entries.set(entryKey(handle), handle);
|
|
162
|
+
this.handles.set(handle.key, handle);
|
|
163
|
+
this.addDeclarationDeps(handle.declarations);
|
|
164
|
+
}
|
|
165
|
+
for (const handle of retained) {
|
|
166
|
+
const module = this.registry.module(handle.moduleName);
|
|
167
|
+
if (!module) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
for (const dependent of dependentEntries(module, handle.key)) {
|
|
171
|
+
this.tryActivateDependent(dependent);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return retained;
|
|
175
|
+
}
|
|
176
|
+
/** Collects `auto` rules on `module` whose every selector dependency is already collected. */
|
|
177
|
+
useRulesWhenDepsMet(module) {
|
|
178
|
+
for (const entry of module.entries) {
|
|
179
|
+
if (entry.kind !== 'rule') {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (this.entries.has(entryKey(entry))) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (this.allDepsMet(entry.dependsOn)) {
|
|
186
|
+
this.addEntry(entry);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
tryActivateDependent(entry) {
|
|
191
|
+
if (entry.kind === 'rule') {
|
|
192
|
+
if (!entry.auto) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (this.entries.has(entryKey(entry))) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (!this.allDepsMet(entry.dependsOn)) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
this.addEntry(entry);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (entry.kind === 'media') {
|
|
205
|
+
const liveRules = entry.rules.filter((rule) => rule.auto && this.allDepsMet(rule.dependsOn));
|
|
206
|
+
if (liveRules.length === 0) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
const filtered = liveRules.length === entry.rules.length
|
|
210
|
+
? entry
|
|
211
|
+
: { ...entry, rules: liveRules };
|
|
212
|
+
this.entries.set(entryKey(filtered), filtered);
|
|
213
|
+
for (const rule of liveRules) {
|
|
214
|
+
this.addDeclarationDeps(rule.declarations);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
allDepsMet(dependsOn) {
|
|
219
|
+
for (const dep of dependsOn) {
|
|
220
|
+
if (!this.handles.has(dep)) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
/** Marks a theme token path as reachable so it appears in the emitted theme block, even if no collected declaration reads it. */
|
|
227
|
+
useThemeVar(path) {
|
|
228
|
+
this.themeDeps.add(path);
|
|
229
|
+
}
|
|
230
|
+
/** Creates a {@link StyleNameResolver} scoped to this collector's current dep set. */
|
|
231
|
+
createResolver() {
|
|
232
|
+
const finalized = this.finalizeDeps();
|
|
233
|
+
const snapshot = this.registry.freeze({
|
|
234
|
+
classKeys: this.names === 'compact'
|
|
235
|
+
? [...this.handles.keys()]
|
|
236
|
+
: this.registry.classKeys,
|
|
237
|
+
cssVarKeys: cssVarKeysFrom(finalized),
|
|
238
|
+
});
|
|
239
|
+
return createStyleNameResolver({
|
|
240
|
+
names: this.names,
|
|
241
|
+
prefix: this.prefix,
|
|
242
|
+
classKeys: snapshot.classKeys,
|
|
243
|
+
cssVarKeys: snapshot.cssVarKeys,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
/** Collected style entries sorted for emission (base rules before all `@media`/`@container` blocks). */
|
|
247
|
+
get collectedEntries() {
|
|
248
|
+
return [...this.entries.values()].sort(compareEntriesForEmission);
|
|
249
|
+
}
|
|
250
|
+
/** Theme token paths that survive reachability; emitted in the theme scope block. */
|
|
251
|
+
get collectedThemeDeps() {
|
|
252
|
+
return this.finalizeDeps().theme;
|
|
253
|
+
}
|
|
254
|
+
/** Contextual var paths that survive reachability; used by the runtime for compact-name substitution. */
|
|
255
|
+
get collectedVarDeps() {
|
|
256
|
+
return this.finalizeDeps().vars;
|
|
257
|
+
}
|
|
258
|
+
// Reachability-aware default-emission set used by the runtime. For a
|
|
259
|
+
// given handle and `${group}` site, returns the keys whose declarations
|
|
260
|
+
// should ship: union of group-keys appearing in `handle.declarations.deps.refs`
|
|
261
|
+
// and in `deps.refs` of every collected rule/media-inner-rule whose
|
|
262
|
+
// selector targets `handle`.
|
|
263
|
+
/**
|
|
264
|
+
* Returns the variable keys within `groupPath` that are reachable from `handle` or from any collected rule targeting it.
|
|
265
|
+
*
|
|
266
|
+
* @param handle Handle whose declaration refs and dependent rules are consulted.
|
|
267
|
+
* @param groupPath Group prefix path (e.g. `vars/chip/look`).
|
|
268
|
+
*/
|
|
269
|
+
reachableDefaults(handle, groupPath) {
|
|
270
|
+
const groupPrefix = `${groupPath}/`;
|
|
271
|
+
const reachable = new Set();
|
|
272
|
+
const harvest = (refs) => {
|
|
273
|
+
for (const path of refs) {
|
|
274
|
+
if (path.startsWith(groupPrefix)) {
|
|
275
|
+
reachable.add(path.slice(groupPrefix.length));
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
harvest(handle.declarations.deps.refs);
|
|
280
|
+
for (const entry of this.entries.values()) {
|
|
281
|
+
if (entry.kind === 'rule') {
|
|
282
|
+
if (entry.dependsOn.has(handle.key)) {
|
|
283
|
+
harvest(entry.declarations.deps.refs);
|
|
284
|
+
}
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (entry.kind === 'media') {
|
|
288
|
+
for (const innerRule of entry.rules) {
|
|
289
|
+
if (innerRule.dependsOn.has(handle.key)) {
|
|
290
|
+
harvest(innerRule.declarations.deps.refs);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return reachable;
|
|
296
|
+
}
|
|
297
|
+
// Computes the final theme/var dep sets, folding in default-marker
|
|
298
|
+
// contributions only for keys that survive reachability. Called once per
|
|
299
|
+
// `createResolver()` and per `collectedThemeDeps`/`collectedVarDeps`
|
|
300
|
+
// access; collection mutations between calls re-derive the result.
|
|
301
|
+
finalizeDeps() {
|
|
302
|
+
const theme = new Set(this.themeDeps);
|
|
303
|
+
const vars = new Set(this.varDeps);
|
|
304
|
+
for (const handle of this.handles.values()) {
|
|
305
|
+
for (const group of handle.declarations.deps.defaults) {
|
|
306
|
+
const reachable = this.reachableDefaults(handle, group.groupPath);
|
|
307
|
+
for (const item of group.keys) {
|
|
308
|
+
if (!reachable.has(item.key)) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
vars.add(item.path);
|
|
312
|
+
item.valueDeps.theme.forEach((path) => theme.add(path));
|
|
313
|
+
item.valueDeps.vars.forEach((path) => vars.add(path));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// Default markers inside rule / media-inner-rule declarations don't have
|
|
318
|
+
// a host handle to anchor reachability against. The runtime emits every
|
|
319
|
+
// listed key when rendering such a declaration, so we mirror that here
|
|
320
|
+
// and include all of them in the registry.
|
|
321
|
+
for (const entry of this.entries.values()) {
|
|
322
|
+
if (entry.kind === 'rule') {
|
|
323
|
+
includeAllDefaults(entry.declarations.deps.defaults, theme, vars);
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
if (entry.kind === 'media') {
|
|
327
|
+
for (const inner of entry.rules) {
|
|
328
|
+
includeAllDefaults(inner.declarations.deps.defaults, theme, vars);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return { theme, vars };
|
|
333
|
+
}
|
|
334
|
+
retainHandle(handle) {
|
|
335
|
+
if (!isEmptyDeclarations(handle.declarations)) {
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
if (this.registry.targetsHandle(handle.key)) {
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
if (this.dev && !warnedHandles.has(handle)) {
|
|
342
|
+
warnedHandles.add(handle);
|
|
343
|
+
this.warn(`Style module "${handle.moduleName}" handle "${handle.localName}" has no declarations and no rule targets it; delete the empty template.`);
|
|
344
|
+
}
|
|
345
|
+
return this.names !== 'compact';
|
|
346
|
+
}
|
|
347
|
+
addEntry(entry) {
|
|
348
|
+
if (entry.kind === 'handle' && !this.retainHandle(entry)) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
this.entries.set(entryKey(entry), entry);
|
|
352
|
+
if (entry.kind === 'handle') {
|
|
353
|
+
this.handles.set(entry.key, entry);
|
|
354
|
+
this.addDeclarationDeps(entry.declarations);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (entry.kind === 'rule') {
|
|
358
|
+
this.addDeclarationDeps(entry.declarations);
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
for (const rule of entry.rules) {
|
|
362
|
+
this.addDeclarationDeps(rule.declarations);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
addDeclarationDeps(declarations) {
|
|
366
|
+
declarations.deps.theme.forEach((path) => this.themeDeps.add(path));
|
|
367
|
+
declarations.deps.vars.forEach((path) => this.varDeps.add(path));
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const includeAllDefaults = (defaults, theme, vars) => {
|
|
371
|
+
for (const group of defaults) {
|
|
372
|
+
for (const item of group.keys) {
|
|
373
|
+
vars.add(item.path);
|
|
374
|
+
item.valueDeps.theme.forEach((path) => theme.add(path));
|
|
375
|
+
item.valueDeps.vars.forEach((path) => vars.add(path));
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
const cssVarKeysFrom = (finalized) => {
|
|
380
|
+
const keys = new Set();
|
|
381
|
+
finalized.theme.forEach((path) => keys.add(path));
|
|
382
|
+
finalized.vars.forEach((path) => keys.add(path));
|
|
383
|
+
return [...keys].sort();
|
|
384
|
+
};
|
|
385
|
+
const entryKey = (entry) => {
|
|
386
|
+
if (entry.kind === 'media') {
|
|
387
|
+
return `media/${entry.key}`;
|
|
388
|
+
}
|
|
389
|
+
return entry.key;
|
|
390
|
+
};
|
|
391
|
+
const mediaSortRank = (entry) => entry.kind === 'media' ? 1 : 0;
|
|
392
|
+
const localVarOverrideRank = (entry) => {
|
|
393
|
+
if (entry.kind === 'media') {
|
|
394
|
+
return 0;
|
|
395
|
+
}
|
|
396
|
+
return entry.declarations.css.some(isLocalVarOverrideMarker) ? 1 : 0;
|
|
397
|
+
};
|
|
398
|
+
// Base entries before every `@media`/`@container` block (no specificity). See finding `media-block-ordering`.
|
|
399
|
+
// `${group.set()}` after `${group}` defaults so a stacked override class wins. See finding `local-var-override-ordering`.
|
|
400
|
+
const compareEntriesForEmission = (a, b) => {
|
|
401
|
+
const rankDelta = mediaSortRank(a) - mediaSortRank(b);
|
|
402
|
+
if (rankDelta !== 0) {
|
|
403
|
+
return rankDelta;
|
|
404
|
+
}
|
|
405
|
+
const varDelta = localVarOverrideRank(a) - localVarOverrideRank(b);
|
|
406
|
+
if (varDelta !== 0) {
|
|
407
|
+
return varDelta;
|
|
408
|
+
}
|
|
409
|
+
if (a.moduleName !== b.moduleName) {
|
|
410
|
+
return a.moduleName.localeCompare(b.moduleName);
|
|
411
|
+
}
|
|
412
|
+
return a.key.localeCompare(b.key);
|
|
413
|
+
};
|
|
414
|
+
const isReadonlyArray = (value) => Array.isArray(value);
|
|
415
|
+
const isStylesModule = (value) => 'handles' in value && 'handleList' in value;
|
|
416
|
+
//# sourceMappingURL=collector.js.map
|