@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,147 @@
|
|
|
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
|
+
|
|
8
|
+
import { type CssBlockKind, isOpaque, scanCss } from './css_scan';
|
|
9
|
+
|
|
10
|
+
const DECLARATION_RE = /(--[A-Za-z_][\w-]*)\s*:/g;
|
|
11
|
+
const REFERENCE_RE = /var\(\s*(--[A-Za-z_][\w-]*)/g;
|
|
12
|
+
|
|
13
|
+
/** A `var(...)` whose custom property is never declared in the same CSS text. */
|
|
14
|
+
export interface VarInvariantViolation {
|
|
15
|
+
/** The undeclared custom property name (e.g. `--aui-color-bg`). */
|
|
16
|
+
readonly reference: string;
|
|
17
|
+
/** Surrounding CSS text for error reporting. */
|
|
18
|
+
readonly context: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface ScannedCss {
|
|
22
|
+
/**
|
|
23
|
+
* `css` with comments, strings, `url(...)` tokens, and escapes blanked to
|
|
24
|
+
* spaces, positions preserved. A `var(` inside a string is text, a `;`
|
|
25
|
+
* inside an escape is part of a value, and neither should match here.
|
|
26
|
+
*/
|
|
27
|
+
readonly masked: string;
|
|
28
|
+
/** The enclosing block kind at each character offset. */
|
|
29
|
+
readonly blockAt: readonly CssBlockKind[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const scan = (css: string): ScannedCss => {
|
|
33
|
+
const masked: string[] = [];
|
|
34
|
+
const blockAt: CssBlockKind[] = [];
|
|
35
|
+
for (const span of scanCss(css)) {
|
|
36
|
+
const blank = isOpaque(span);
|
|
37
|
+
for (let offset = 0; offset < span.text.length; offset += 1) {
|
|
38
|
+
masked.push(blank ? ' ' : (span.text[offset] ?? ''));
|
|
39
|
+
blockAt.push(span.block);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { masked: masked.join(''), blockAt };
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A custom property is declared where a declaration can start: inside a
|
|
47
|
+
* declarations block, as the first thing after `{` or `;`. A selector carrying
|
|
48
|
+
* a BEM-style modifier and a pseudo-class (`.button--active:hover`) matches the
|
|
49
|
+
* same characters and declares nothing.
|
|
50
|
+
*/
|
|
51
|
+
const isDeclarationStart = (
|
|
52
|
+
{ masked, blockAt }: ScannedCss,
|
|
53
|
+
index: number
|
|
54
|
+
): boolean => {
|
|
55
|
+
if (blockAt[index] !== 'declarations') {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
for (let cursor = index - 1; cursor >= 0; cursor -= 1) {
|
|
59
|
+
const char = masked[cursor];
|
|
60
|
+
if (char === undefined || !/\s/.test(char)) {
|
|
61
|
+
return char === '{' || char === ';';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* `var` has to open a function token rather than end a longer identifier:
|
|
69
|
+
* `--expr:myvar(--x)` names some other function.
|
|
70
|
+
*/
|
|
71
|
+
const isFunctionTokenStart = (masked: string, index: number): boolean => {
|
|
72
|
+
const before = masked[index - 1];
|
|
73
|
+
return before === undefined || !/[\w\\-]/.test(before);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** Collects every custom property declared in a declaration position in `css`. */
|
|
77
|
+
export const collectDeclaredCustomProperties = (css: string): Set<string> => {
|
|
78
|
+
const scanned = scan(css);
|
|
79
|
+
const declared = new Set<string>();
|
|
80
|
+
for (const match of scanned.masked.matchAll(DECLARATION_RE)) {
|
|
81
|
+
const name = match[1];
|
|
82
|
+
if (name && isDeclarationStart(scanned, match.index ?? 0)) {
|
|
83
|
+
declared.add(name);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return declared;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** Collects all custom property names referenced inside `var(...)` calls in `css`. */
|
|
90
|
+
export const collectReferencedCustomProperties = (
|
|
91
|
+
css: string
|
|
92
|
+
): readonly string[] => {
|
|
93
|
+
const { masked } = scan(css);
|
|
94
|
+
const names: string[] = [];
|
|
95
|
+
for (const match of masked.matchAll(REFERENCE_RE)) {
|
|
96
|
+
const name = match[1];
|
|
97
|
+
if (name && isFunctionTokenStart(masked, match.index ?? 0)) {
|
|
98
|
+
names.push(name);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return names;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Returns all `var(...)` references whose custom property is never declared in `css`.
|
|
106
|
+
*
|
|
107
|
+
* @param css Full CSS text to scan.
|
|
108
|
+
*/
|
|
109
|
+
export const findVarRefViolations = (
|
|
110
|
+
css: string
|
|
111
|
+
): readonly VarInvariantViolation[] => {
|
|
112
|
+
const { masked } = scan(css);
|
|
113
|
+
const declared = collectDeclaredCustomProperties(css);
|
|
114
|
+
const violations: VarInvariantViolation[] = [];
|
|
115
|
+
for (const match of masked.matchAll(REFERENCE_RE)) {
|
|
116
|
+
const ref = match[1];
|
|
117
|
+
const index = match.index ?? 0;
|
|
118
|
+
if (!ref || declared.has(ref) || !isFunctionTokenStart(masked, index)) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
violations.push({
|
|
122
|
+
reference: ref,
|
|
123
|
+
context: css.slice(Math.max(0, index - 40), index + ref.length + 8),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return violations;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Throws if any `var(...)` in `css` has no matching custom-property declaration.
|
|
131
|
+
*
|
|
132
|
+
* @param css Full CSS text to scan.
|
|
133
|
+
* @throws If any reference has no declaration in the same text.
|
|
134
|
+
*/
|
|
135
|
+
export const assertVarRefsHaveDeclarations = (css: string): void => {
|
|
136
|
+
const violations = findVarRefViolations(css);
|
|
137
|
+
if (violations.length === 0) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const lines = violations.map(
|
|
141
|
+
({ reference, context }) =>
|
|
142
|
+
`- ${reference} referenced near "${context.replace(/\s+/g, ' ')}"`
|
|
143
|
+
);
|
|
144
|
+
throw new Error(
|
|
145
|
+
`${violations.length} var(...) reference(s) without a matching declaration:\n${lines.join('\n')}`
|
|
146
|
+
);
|
|
147
|
+
};
|