@panomc/plugin-kit 0.0.0-stage → 1.0.0-dev.62
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/assets/pano-bootstrap.css +17903 -0
- package/bin/__tests__/new/e2e.test.js +111 -0
- package/bin/__tests__/new/helpers.js +76 -0
- package/bin/__tests__/new/new.test.js +472 -0
- package/bin/new.js +530 -0
- package/bin/pano-plugin.js +162 -0
- package/bin/templates/plugin/README.md +30 -0
- package/bin/templates/plugin/_gitignore +15 -0
- package/bin/templates/plugin/build.gradle.kts +389 -0
- package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.properties +7 -0
- package/bin/templates/plugin/gradle.properties +55 -0
- package/bin/templates/plugin/gradlew +249 -0
- package/bin/templates/plugin/gradlew.bat +92 -0
- package/bin/templates/plugin/package.json.tpl +14 -0
- package/bin/templates/plugin/rollup.config.js +2 -0
- package/bin/templates/plugin/settings.gradle.kts +5 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/__CLASS__Plugin.kt +50 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/frontend/ExampleFallbackPage.kt +28 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/routes/GetHelloAPI.kt +19 -0
- package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/LicenseGuard.kt +30 -0
- package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/PluginLicenseClient.kt +144 -0
- package/bin/templates/plugin/src/main/resources/config.conf +1 -0
- package/bin/templates/plugin/src/main/resources/frontend-targets.json +1 -0
- package/bin/templates/plugin/src/main/resources/locales/en-US.json +3 -0
- package/bin/templates/plugin/src/main/resources/logo.png +0 -0
- package/bin/templates/plugin/src/theme/views/HelloPage.svelte +26 -0
- package/bin/ui.js +96 -0
- package/package.json +48 -4
- package/src/config.js +191 -0
- package/src/controller/__tests__/controller.test.js +647 -0
- package/src/controller/index.js +604 -0
- package/src/dev.js +208 -0
- package/src/index.js +22 -0
- package/src/rollup/__tests__/controllers/controllers.test.js +418 -0
- package/src/rollup/__tests__/controllers/fixture.js +143 -0
- package/src/rollup/__tests__/controllers/golden/controllers.json +34 -0
- package/src/rollup/__tests__/controllers/golden/controllers.types.js +69 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.json +16 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.lock.json +27 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.with-views.json +54 -0
- package/src/rollup/__tests__/preset/preset.test.js +418 -0
- package/src/rollup/__tests__/rules/_build.js +39 -0
- package/src/rollup/__tests__/rules/_fixture.js +83 -0
- package/src/rollup/__tests__/rules/helpers.test.js +148 -0
- package/src/rollup/__tests__/rules/rules.test.js +259 -0
- package/src/rollup/__tests__/rules/stamp.test.js +113 -0
- package/src/rollup/__tests__/samples/samples.test.js +481 -0
- package/src/rollup/__tests__/seams/config.test.js +78 -0
- package/src/rollup/__tests__/seams/lock.test.js +124 -0
- package/src/rollup/__tests__/seams/sdk-seams.test.js +93 -0
- package/src/rollup/__tests__/seams/stubs.test.js +52 -0
- package/src/rollup/__tests__/views/helpers.js +82 -0
- package/src/rollup/__tests__/views/views.test.js +522 -0
- package/src/rollup/__tests__/widgets/helpers.js +117 -0
- package/src/rollup/__tests__/widgets/widgets.test.js +422 -0
- package/src/rollup/cli-build.js +90 -0
- package/src/rollup/cli-check.js +280 -0
- package/src/rollup/cli-classes.js +75 -0
- package/src/rollup/client-gen.js +67 -0
- package/src/rollup/controllers.js +434 -0
- package/src/rollup/entry.js +250 -0
- package/src/rollup/helpers.js +193 -0
- package/src/rollup/index.js +601 -0
- package/src/rollup/integrate.js +205 -0
- package/src/rollup/lock.js +230 -0
- package/src/rollup/meta.js +479 -0
- package/src/rollup/rules.js +950 -0
- package/src/rollup/samples.js +667 -0
- package/src/rollup/stamp.js +410 -0
- package/src/rollup/types.js +248 -0
- package/src/rollup/views.js +1205 -0
- package/src/rollup/widgets.js +1000 -0
- package/src/styles/__tests__/fallback/__snapshots__/build-fallback-css.test.js.snap +173 -0
- package/src/styles/__tests__/fallback/build-fallback-css.test.js +526 -0
- package/src/styles/__tests__/fallback/fixtures/base.css +247 -0
- package/src/styles/__tests__/fallback/plugins.test.js +301 -0
- package/src/styles/__tests__/lint/check-styles.test.js +423 -0
- package/src/styles/__tests__/lint/fixtures/ProductCard.golden.svelte +13 -0
- package/src/styles/__tests__/lint/fixtures/ProductCard.in.svelte +13 -0
- package/src/styles/__tests__/lint/semantic-classes.test.js +364 -0
- package/src/styles/bootstrap-classes.js +155 -0
- package/src/styles/check-styles.js +626 -0
- package/src/styles/fallback-css.js +1182 -0
- package/src/styles/plugin-css.js +237 -0
- package/src/styles/scan.js +206 -0
- package/src/styles/semantic-classes.js +436 -0
- package/src/styles/tokens.map.js +53 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The scoped fallback stylesheet (doc 03 section 4, decision 47).
|
|
3
|
+
*
|
|
4
|
+
* `buildFallbackCss` is the pure core: it takes the compiled Bootstrap base sheet
|
|
5
|
+
* (`assets/pano-bootstrap.css`) and the classes a plugin's views use, and returns a layered,
|
|
6
|
+
* `@scope`d sheet that holds only those classes, with no `!important` and with Bootstrap's
|
|
7
|
+
* variables bound to the `--pano-*` tokens. `panoFallbackCss` is the client-build rollup plugin
|
|
8
|
+
* around it: it scans the views, writes `client/fallback.css` and tells the package index about the
|
|
9
|
+
* sheet (`styles`) and about each view (`roots`, `classes`).
|
|
10
|
+
*
|
|
11
|
+
* The seven steps of doc 03 section 4.2 are marked `STEP n` below.
|
|
12
|
+
*/
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { fileURLToPath } from 'node:url';
|
|
16
|
+
import postcss from 'postcss';
|
|
17
|
+
import { getBuildContext } from '../rollup/meta.js';
|
|
18
|
+
import { PALETTES, TOKENS } from './tokens.map.js';
|
|
19
|
+
import { classTokens, parseTags, splitSvelte } from './scan.js';
|
|
20
|
+
import { SemanticClassCollisionError, addRootClass, rootClassOf } from './semantic-classes.js';
|
|
21
|
+
import { clearStyleFile, recordStyleFile } from './plugin-css.js';
|
|
22
|
+
|
|
23
|
+
/** The base sheet shipped with the kit. */
|
|
24
|
+
export const BASE_CSS_FILE = fileURLToPath(new URL('../../assets/pano-bootstrap.css', import.meta.url));
|
|
25
|
+
|
|
26
|
+
/** Classes Bootstrap's JavaScript toggles at run time; never visible in a view's markup. */
|
|
27
|
+
export const JS_TOGGLED = Object.freeze([
|
|
28
|
+
'show',
|
|
29
|
+
'showing',
|
|
30
|
+
'hiding',
|
|
31
|
+
'fade',
|
|
32
|
+
'collapse',
|
|
33
|
+
'collapsing',
|
|
34
|
+
'active',
|
|
35
|
+
'disabled',
|
|
36
|
+
'was-validated',
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
/** Classless selectors that survive the purge (STEP 4). */
|
|
40
|
+
const RESET_TYPES = new Set([
|
|
41
|
+
'*',
|
|
42
|
+
'a',
|
|
43
|
+
'button',
|
|
44
|
+
'input',
|
|
45
|
+
'select',
|
|
46
|
+
'textarea',
|
|
47
|
+
'label',
|
|
48
|
+
'img',
|
|
49
|
+
'svg',
|
|
50
|
+
'hr',
|
|
51
|
+
'p',
|
|
52
|
+
'ul',
|
|
53
|
+
'ol',
|
|
54
|
+
'h1',
|
|
55
|
+
'h2',
|
|
56
|
+
'h3',
|
|
57
|
+
'h4',
|
|
58
|
+
'h5',
|
|
59
|
+
'h6',
|
|
60
|
+
'small',
|
|
61
|
+
'table',
|
|
62
|
+
'th',
|
|
63
|
+
'td',
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
const THEME_COLORS = ['primary', 'secondary', 'success', 'danger', 'warning', 'info'];
|
|
67
|
+
|
|
68
|
+
/** Contrast colour of a theme colour that has no `--pano-color-on-*` token (Bootstrap's own results). */
|
|
69
|
+
const ON_COLOR = {
|
|
70
|
+
primary: 'var(--pano-color-on-primary)',
|
|
71
|
+
secondary: 'var(--pano-color-on-secondary)',
|
|
72
|
+
success: '#fff',
|
|
73
|
+
danger: '#fff',
|
|
74
|
+
warning: '#000',
|
|
75
|
+
info: '#000',
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** Classes whose elements Bootstrap's JavaScript appends to `<body>`, outside the scope root. */
|
|
79
|
+
const BODY_CLASS = /^(?:modal-backdrop|modal-open|offcanvas-backdrop|tooltip(?:-[a-z]+)?|bs-tooltip-[a-z]+|popover(?:-[a-z]+)?|bs-popover-[a-z]+)$/;
|
|
80
|
+
|
|
81
|
+
/** Classes a view gets for free when it uses the component (the JS creates them). */
|
|
82
|
+
const FEATURE_CLASSES = {
|
|
83
|
+
modal: /^(?:modal-backdrop|modal-open|modal-static)$/,
|
|
84
|
+
offcanvas: /^offcanvas-backdrop$/,
|
|
85
|
+
tooltip: /^(?:tooltip(?:-[a-z]+)?|bs-tooltip-[a-z]+)$/,
|
|
86
|
+
popover: /^(?:popover(?:-[a-z]+)?|bs-popover-[a-z]+)$/,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const NS_PATTERN = /^[a-z0-9][a-z0-9-]*$/;
|
|
90
|
+
|
|
91
|
+
/** `--bs-*` variables the tokens mirror. */
|
|
92
|
+
const MAPPED_VARS = new Set(TOKENS.filter((t) => t.bs).map((t) => /** @type {string} */ (t.bs)));
|
|
93
|
+
|
|
94
|
+
/** `--bs-*` variables the scope block derives from the tokens itself (STEP 7, rows c and d). */
|
|
95
|
+
const DERIVED_VARS = new Set([
|
|
96
|
+
...THEME_COLORS.flatMap((c) => [`--bs-${c}-bg-subtle`, `--bs-${c}-text-emphasis`, `--bs-${c}-border-subtle`]),
|
|
97
|
+
'--bs-heading-color',
|
|
98
|
+
'--bs-border-color-translucent',
|
|
99
|
+
'--bs-focus-ring-color',
|
|
100
|
+
]);
|
|
101
|
+
|
|
102
|
+
/** Names `X` for which `rgba(var(--bs-X-rgb), a)` becomes a `color-mix()` of `var(--bs-X)` (STEP 6). */
|
|
103
|
+
const RGB_REWRITE = new Set([
|
|
104
|
+
...[...MAPPED_VARS].map((v) => v.slice('--bs-'.length)),
|
|
105
|
+
'emphasis-color',
|
|
106
|
+
...THEME_COLORS,
|
|
107
|
+
]);
|
|
108
|
+
|
|
109
|
+
/* ------------------------------------------------------------------------------------------------
|
|
110
|
+
* Selector scanning
|
|
111
|
+
* ---------------------------------------------------------------------------------------------- */
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Index just after the bracket that closes the one at `start`, honouring quotes and nesting.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} text
|
|
117
|
+
* @param {number} start index of the opening `(` or `[`
|
|
118
|
+
* @returns {number}
|
|
119
|
+
*/
|
|
120
|
+
function skipBalanced(text, start) {
|
|
121
|
+
const open = text[start];
|
|
122
|
+
const close = open === '(' ? ')' : ']';
|
|
123
|
+
let depth = 0;
|
|
124
|
+
let quote = '';
|
|
125
|
+
|
|
126
|
+
for (let i = start; i < text.length; i++) {
|
|
127
|
+
const c = text[i];
|
|
128
|
+
|
|
129
|
+
if (quote) {
|
|
130
|
+
if (c === '\\') i++;
|
|
131
|
+
else if (c === quote) quote = '';
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (c === '"' || c === "'") quote = c;
|
|
136
|
+
else if (c === '\\') i++;
|
|
137
|
+
else if (c === open) depth++;
|
|
138
|
+
else if (c === close && --depth === 0) return i + 1;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return text.length;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The class names and the type selectors of one selector. Class names inside `:not()`, `:is()`,
|
|
146
|
+
* `:where()` and `:has()` count; `:nth-*()` and `:lang()` arguments and attribute selectors do not.
|
|
147
|
+
*
|
|
148
|
+
* @param {string} selector
|
|
149
|
+
* @returns {{ classes: string[], types: string[] }}
|
|
150
|
+
*/
|
|
151
|
+
export function scanSelector(selector) {
|
|
152
|
+
/** @type {string[]} */
|
|
153
|
+
const classes = [];
|
|
154
|
+
/** @type {string[]} */
|
|
155
|
+
const types = [];
|
|
156
|
+
const n = selector.length;
|
|
157
|
+
let i = 0;
|
|
158
|
+
let atStart = true;
|
|
159
|
+
|
|
160
|
+
const ident = () => {
|
|
161
|
+
let out = '';
|
|
162
|
+
|
|
163
|
+
while (i < n) {
|
|
164
|
+
const c = selector[i];
|
|
165
|
+
|
|
166
|
+
if (c === '\\') {
|
|
167
|
+
out += selector[i + 1] ?? '';
|
|
168
|
+
i += 2;
|
|
169
|
+
} else if (/[\w\u0080--]/.test(c)) {
|
|
170
|
+
out += c;
|
|
171
|
+
i++;
|
|
172
|
+
} else break;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return out;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
while (i < n) {
|
|
179
|
+
const c = selector[i];
|
|
180
|
+
|
|
181
|
+
if (c === '[') {
|
|
182
|
+
i = skipBalanced(selector, i);
|
|
183
|
+
atStart = false;
|
|
184
|
+
} else if (c === '.') {
|
|
185
|
+
i++;
|
|
186
|
+
|
|
187
|
+
const name = ident();
|
|
188
|
+
|
|
189
|
+
if (name) classes.push(name);
|
|
190
|
+
|
|
191
|
+
atStart = false;
|
|
192
|
+
} else if (c === '#') {
|
|
193
|
+
i++;
|
|
194
|
+
ident();
|
|
195
|
+
atStart = false;
|
|
196
|
+
} else if (c === ':') {
|
|
197
|
+
i++;
|
|
198
|
+
|
|
199
|
+
if (selector[i] === ':') i++;
|
|
200
|
+
|
|
201
|
+
const name = ident();
|
|
202
|
+
|
|
203
|
+
if (selector[i] === '(') {
|
|
204
|
+
if (/^(?:not|is|where|has|matches|any)$/i.test(name)) {
|
|
205
|
+
i++;
|
|
206
|
+
atStart = true;
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
i = skipBalanced(selector, i);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
atStart = false;
|
|
214
|
+
} else if (c === '*') {
|
|
215
|
+
if (atStart) types.push('*');
|
|
216
|
+
|
|
217
|
+
i++;
|
|
218
|
+
atStart = false;
|
|
219
|
+
} else if (/[\s>+~,(]/.test(c)) {
|
|
220
|
+
i++;
|
|
221
|
+
atStart = true;
|
|
222
|
+
} else if (c === ')') {
|
|
223
|
+
i++;
|
|
224
|
+
atStart = false;
|
|
225
|
+
} else if (/[A-Za-z_\u0080--]/.test(c)) {
|
|
226
|
+
const name = ident();
|
|
227
|
+
|
|
228
|
+
if (atStart && name) types.push(name.toLowerCase());
|
|
229
|
+
|
|
230
|
+
atStart = false;
|
|
231
|
+
} else {
|
|
232
|
+
i++;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return { classes, types };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Variable block selectors: `:root`, `[data-bs-theme=X]`. @returns {string | null} palette name, `root` for `:root` */
|
|
240
|
+
function variableSelector(selector) {
|
|
241
|
+
const s = selector.trim();
|
|
242
|
+
|
|
243
|
+
if (s === ':root') return 'root';
|
|
244
|
+
|
|
245
|
+
const m = /^\[data-bs-theme\s*=\s*(?:"([\w-]+)"|'([\w-]+)'|([\w-]+))\s*\]$/.exec(s);
|
|
246
|
+
|
|
247
|
+
return m ? (m[1] ?? m[2] ?? m[3]) : null;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const THEME_PREFIX = /^\[data-bs-theme\s*=\s*(?:"([\w-]+)"|'([\w-]+)'|([\w-]+))\s*\]\s+(?=\S)/;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* STEP 3: `[data-bs-theme=X] rest` -> `:scope:where([data-bs-theme='X'] *) rest`.
|
|
254
|
+
*
|
|
255
|
+
* @param {string} selector
|
|
256
|
+
* @returns {{ rest: string, rewritten: string }}
|
|
257
|
+
*/
|
|
258
|
+
function rewriteThemePrefix(selector) {
|
|
259
|
+
const m = THEME_PREFIX.exec(selector);
|
|
260
|
+
|
|
261
|
+
if (!m) return { rest: selector, rewritten: selector };
|
|
262
|
+
|
|
263
|
+
const rest = selector.slice(m[0].length);
|
|
264
|
+
|
|
265
|
+
return { rest, rewritten: `:scope:where([data-bs-theme='${m[1] ?? m[2] ?? m[3]}'] *) ${rest}` };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/* ------------------------------------------------------------------------------------------------
|
|
269
|
+
* Values (STEP 6)
|
|
270
|
+
* ---------------------------------------------------------------------------------------------- */
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* `rgba(var(--bs-X-rgb), A)` -> `color-mix(in srgb, var(--bs-X) calc(A * 100%), transparent)` for the
|
|
274
|
+
* names in {@link RGB_REWRITE}. Any other `-rgb` use stays.
|
|
275
|
+
*
|
|
276
|
+
* @param {string} value
|
|
277
|
+
* @returns {string}
|
|
278
|
+
*/
|
|
279
|
+
export function rewriteRgb(value) {
|
|
280
|
+
if (!/rgba?\(/i.test(value)) return value;
|
|
281
|
+
|
|
282
|
+
let out = '';
|
|
283
|
+
let i = 0;
|
|
284
|
+
const re = /(?<![\w-])rgba?\(/gi;
|
|
285
|
+
|
|
286
|
+
for (;;) {
|
|
287
|
+
re.lastIndex = i;
|
|
288
|
+
|
|
289
|
+
const m = re.exec(value);
|
|
290
|
+
|
|
291
|
+
if (!m) break;
|
|
292
|
+
|
|
293
|
+
const open = m.index + m[0].length - 1;
|
|
294
|
+
const end = skipBalanced(value, open);
|
|
295
|
+
const inner = value.slice(open + 1, end - 1).trim();
|
|
296
|
+
const arg = /^var\(\s*--bs-([a-z0-9-]+)-rgb\s*\)\s*([\s\S]*)$/i.exec(inner);
|
|
297
|
+
|
|
298
|
+
out += value.slice(i, m.index);
|
|
299
|
+
|
|
300
|
+
if (arg && RGB_REWRITE.has(arg[1])) {
|
|
301
|
+
const rest = arg[2].trim();
|
|
302
|
+
let alpha = null;
|
|
303
|
+
let ok = true;
|
|
304
|
+
|
|
305
|
+
if (rest.startsWith(',') || rest.startsWith('/')) alpha = rest.slice(1).trim();
|
|
306
|
+
else if (rest !== '') ok = false;
|
|
307
|
+
|
|
308
|
+
if (ok && alpha !== '') {
|
|
309
|
+
out +=
|
|
310
|
+
alpha === null
|
|
311
|
+
? `var(--bs-${arg[1]})`
|
|
312
|
+
: `color-mix(in srgb, var(--bs-${arg[1]}) calc(${alpha} * 100%), transparent)`;
|
|
313
|
+
i = end;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
out += value.slice(m.index, end);
|
|
319
|
+
i = end;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return out + value.slice(i);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* STEP 6 on one declaration. Besides the `rgba()` rewrite, a swap of two rewritten colour channels
|
|
327
|
+
* (`a:hover { --bs-link-color-rgb: var(--bs-link-hover-color-rgb) }`) becomes a swap of the colours
|
|
328
|
+
* themselves, because nothing reads the `-rgb` variables of those colours any more.
|
|
329
|
+
*
|
|
330
|
+
* @param {string} prop
|
|
331
|
+
* @param {string} value
|
|
332
|
+
* @returns {Decl}
|
|
333
|
+
*/
|
|
334
|
+
function rewriteDecl(prop, value) {
|
|
335
|
+
const swap = /^--bs-([a-z0-9-]+)-rgb$/.exec(prop);
|
|
336
|
+
const from = /^var\(\s*--bs-([a-z0-9-]+)-rgb\s*\)$/.exec(value.trim());
|
|
337
|
+
|
|
338
|
+
if (swap && from && RGB_REWRITE.has(swap[1]) && RGB_REWRITE.has(from[1])) {
|
|
339
|
+
return { prop: `--bs-${swap[1]}`, value: `var(--bs-${from[1]})` };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return { prop, value: rewriteRgb(value.trim()) };
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/* ------------------------------------------------------------------------------------------------
|
|
346
|
+
* Tree model and output
|
|
347
|
+
* ---------------------------------------------------------------------------------------------- */
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* @typedef {{ prop: string, value: string }} Decl
|
|
351
|
+
* @typedef {{ type: 'rule', selectors: string[], decls: Decl[] }} RuleNode
|
|
352
|
+
* @typedef {{ type: 'at', name: string, params: string, nodes: Node[] }} AtNode
|
|
353
|
+
* @typedef {RuleNode | AtNode} Node
|
|
354
|
+
* @typedef {{ cs: Node[], cb: Node[], us: Node[], ub: Node[], fb: Node[] }} Buckets
|
|
355
|
+
* components / utilities, scoped / body-level, plus the generic `.fade` rules for body-level tooltips
|
|
356
|
+
*/
|
|
357
|
+
|
|
358
|
+
/** @returns {Buckets} */
|
|
359
|
+
const emptyBuckets = () => ({ cs: [], cb: [], us: [], ub: [], fb: [] });
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* @param {Node[]} nodes
|
|
363
|
+
* @param {string} indent
|
|
364
|
+
* @returns {string}
|
|
365
|
+
*/
|
|
366
|
+
function emit(nodes, indent) {
|
|
367
|
+
let out = '';
|
|
368
|
+
|
|
369
|
+
for (const node of nodes) {
|
|
370
|
+
if (node.type === 'at') {
|
|
371
|
+
out += `${indent}@${node.name} ${node.params} {\n${emit(node.nodes, indent + ' ')}${indent}}\n`;
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const body = node.decls.map((d) => `${indent} ${d.prop}: ${d.value};\n`).join('');
|
|
376
|
+
|
|
377
|
+
out += `${indent}${node.selectors.join(`,\n${indent}`)} {\n${body}${indent}}\n`;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return out;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** @param {Node[]} nodes @param {(rule: RuleNode) => void} visit */
|
|
384
|
+
function walkRules(nodes, visit) {
|
|
385
|
+
for (const node of nodes) {
|
|
386
|
+
if (node.type === 'rule') visit(node);
|
|
387
|
+
else walkRules(node.nodes, visit);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** @param {string} name @returns {string} */
|
|
392
|
+
const escapeRe = (name) => name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Pure core of the fallback stylesheet (doc 03 sections 4.2 and 4.3).
|
|
396
|
+
*
|
|
397
|
+
* `classes` is the sorted list of base classes the sheet covers (every class of a kept selector).
|
|
398
|
+
* An empty base gives an empty result.
|
|
399
|
+
*
|
|
400
|
+
* @param {{ baseCss: string, usedClasses: string[] | Set<string>, ns: string }} input
|
|
401
|
+
* @returns {{ css: string, classes: string[] }}
|
|
402
|
+
*/
|
|
403
|
+
export function buildFallbackCss({ baseCss, usedClasses, ns }) {
|
|
404
|
+
if (typeof ns !== 'string' || !NS_PATTERN.test(ns)) {
|
|
405
|
+
throw new Error(`[pano-plugin] buildFallbackCss: namespace "${ns}" must match ${NS_PATTERN}`);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (!baseCss || !baseCss.trim()) return { css: '', classes: [] };
|
|
409
|
+
|
|
410
|
+
const used = new Set([...usedClasses].map((c) => String(c).replace(/^\./, '')).filter(Boolean));
|
|
411
|
+
|
|
412
|
+
for (const c of JS_TOGGLED) used.add(c);
|
|
413
|
+
|
|
414
|
+
/** @type {Record<string, boolean>} */
|
|
415
|
+
const features = {};
|
|
416
|
+
|
|
417
|
+
for (const feature of Object.keys(FEATURE_CLASSES)) features[feature] = used.has(feature);
|
|
418
|
+
|
|
419
|
+
/** @param {string} cls */
|
|
420
|
+
const isUsable = (cls) =>
|
|
421
|
+
used.has(cls) || Object.entries(FEATURE_CLASSES).some(([feature, re]) => features[feature] && re.test(cls));
|
|
422
|
+
|
|
423
|
+
const root = postcss.parse(baseCss);
|
|
424
|
+
|
|
425
|
+
/** STEP 2 collections. @type {Map<string, string>} */
|
|
426
|
+
const defaults = new Map();
|
|
427
|
+
/** @type {Map<string, Map<string, string>>} */
|
|
428
|
+
const extras = new Map();
|
|
429
|
+
/** @type {Map<string, { name: string, params: string, nodes: Node[] }>} */
|
|
430
|
+
const keyframes = new Map();
|
|
431
|
+
const keptClasses = new Set();
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* STEP 3, 4, 5, 6 on a list of postcss nodes.
|
|
435
|
+
*
|
|
436
|
+
* @param {import('postcss').Container} container
|
|
437
|
+
* @param {boolean} top
|
|
438
|
+
* @returns {Buckets}
|
|
439
|
+
*/
|
|
440
|
+
function walk(container, top) {
|
|
441
|
+
const buckets = emptyBuckets();
|
|
442
|
+
|
|
443
|
+
container.each((node) => {
|
|
444
|
+
if (node.type === 'rule') {
|
|
445
|
+
if (top && collectVariables(node)) return;
|
|
446
|
+
|
|
447
|
+
processRule(node, buckets);
|
|
448
|
+
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (node.type !== 'atrule') return;
|
|
453
|
+
|
|
454
|
+
if (/keyframes$/i.test(node.name)) {
|
|
455
|
+
if (top) keyframes.set(node.params, { name: node.name, params: node.params, nodes: keyframeNodes(node) });
|
|
456
|
+
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
if (!node.nodes || /^(?:font-face|charset|import|page|property|namespace)$/i.test(node.name)) return;
|
|
461
|
+
|
|
462
|
+
const inner = walk(node, false);
|
|
463
|
+
|
|
464
|
+
for (const key of /** @type {(keyof Buckets)[]} */ (Object.keys(inner))) {
|
|
465
|
+
if (inner[key].length > 0) {
|
|
466
|
+
buckets[key].push({ type: 'at', name: node.name, params: node.params, nodes: inner[key] });
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
return buckets;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* STEP 2: a rule made only of `:root` / `[data-bs-theme=X]` selectors is a variable block.
|
|
476
|
+
*
|
|
477
|
+
* @param {import('postcss').Rule} rule
|
|
478
|
+
* @returns {boolean} true when it was one (and is now removed from the pipeline)
|
|
479
|
+
*/
|
|
480
|
+
function collectVariables(rule) {
|
|
481
|
+
const palettes = rule.selectors.map(variableSelector);
|
|
482
|
+
|
|
483
|
+
if (palettes.length === 0 || palettes.some((p) => p === null)) return false;
|
|
484
|
+
|
|
485
|
+
for (const palette of /** @type {string[]} */ (palettes)) {
|
|
486
|
+
const isDefault = palette === 'root' || palette === 'light';
|
|
487
|
+
|
|
488
|
+
rule.each((decl) => {
|
|
489
|
+
if (decl.type !== 'decl') return;
|
|
490
|
+
|
|
491
|
+
const prop = decl.prop;
|
|
492
|
+
const value = decl.value.trim();
|
|
493
|
+
|
|
494
|
+
if (prop.startsWith('--')) {
|
|
495
|
+
if (MAPPED_VARS.has(prop) || DERIVED_VARS.has(prop) || value.includes('$')) return;
|
|
496
|
+
} else if (isDefault) {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (isDefault) {
|
|
501
|
+
defaults.set(prop, value);
|
|
502
|
+
} else {
|
|
503
|
+
if (!extras.has(palette)) extras.set(palette, new Map());
|
|
504
|
+
|
|
505
|
+
/** @type {Map<string, string>} */ (extras.get(palette)).set(prop, value);
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return true;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* @param {import('postcss').Rule} rule
|
|
515
|
+
* @param {Buckets} buckets
|
|
516
|
+
*/
|
|
517
|
+
function processRule(rule, buckets) {
|
|
518
|
+
/** @type {string[]} */
|
|
519
|
+
const scoped = [];
|
|
520
|
+
/** @type {string[]} */
|
|
521
|
+
const body = [];
|
|
522
|
+
/** @type {string[]} */
|
|
523
|
+
const fade = [];
|
|
524
|
+
|
|
525
|
+
for (const selector of rule.selectors) {
|
|
526
|
+
const { rest, rewritten } = rewriteThemePrefix(selector.trim());
|
|
527
|
+
const { classes, types } = scanSelector(rest);
|
|
528
|
+
|
|
529
|
+
if (classes.length > 0) {
|
|
530
|
+
if (!classes.every(isUsable)) continue;
|
|
531
|
+
|
|
532
|
+
for (const cls of classes) keptClasses.add(cls);
|
|
533
|
+
|
|
534
|
+
if (classes.some((c) => BODY_CLASS.test(c))) {
|
|
535
|
+
body.push(rewritten);
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
scoped.push(rewritten);
|
|
540
|
+
|
|
541
|
+
if (classes.includes('fade') && classes.every((c) => c === 'fade' || c === 'show')) fade.push(rest);
|
|
542
|
+
} else if (types.length > 0 && types.every((t) => RESET_TYPES.has(t))) {
|
|
543
|
+
scoped.push(rewritten);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if (scoped.length === 0 && body.length === 0) return;
|
|
548
|
+
|
|
549
|
+
/** @type {Decl[]} */
|
|
550
|
+
const decls = [];
|
|
551
|
+
let important = 0;
|
|
552
|
+
|
|
553
|
+
rule.each((decl) => {
|
|
554
|
+
if (decl.type !== 'decl') return;
|
|
555
|
+
|
|
556
|
+
if (decl.important) important++;
|
|
557
|
+
|
|
558
|
+
decls.push(rewriteDecl(decl.prop, decl.value));
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
if (decls.length === 0) return;
|
|
562
|
+
|
|
563
|
+
// STEP 5: a rule that is !important all through is a utility, anything else a component
|
|
564
|
+
const utility = important === decls.length;
|
|
565
|
+
const [scopedKey, bodyKey] = utility ? ['us', 'ub'] : ['cs', 'cb'];
|
|
566
|
+
|
|
567
|
+
if (scoped.length > 0) buckets[/** @type {'cs' | 'us'} */ (scopedKey)].push({ type: 'rule', selectors: scoped, decls });
|
|
568
|
+
|
|
569
|
+
if (body.length > 0) buckets[/** @type {'cb' | 'ub'} */ (bodyKey)].push({ type: 'rule', selectors: body, decls });
|
|
570
|
+
|
|
571
|
+
if (fade.length > 0) buckets.fb.push({ type: 'rule', selectors: fade, decls });
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/** @param {import('postcss').AtRule} atRule @returns {Node[]} */
|
|
575
|
+
function keyframeNodes(atRule) {
|
|
576
|
+
/** @type {Node[]} */
|
|
577
|
+
const out = [];
|
|
578
|
+
|
|
579
|
+
atRule.each((step) => {
|
|
580
|
+
if (step.type !== 'rule') return;
|
|
581
|
+
|
|
582
|
+
/** @type {Decl[]} */
|
|
583
|
+
const decls = [];
|
|
584
|
+
|
|
585
|
+
step.each((decl) => {
|
|
586
|
+
if (decl.type === 'decl') decls.push(rewriteDecl(decl.prop, decl.value));
|
|
587
|
+
});
|
|
588
|
+
out.push({ type: 'rule', selectors: step.selectors.map((s) => s.trim()), decls });
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
return out;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
const buckets = walk(root, true);
|
|
595
|
+
|
|
596
|
+
// The base sheet repeats some blocks verbatim (it is two Sass compiles in a row). Of two identical
|
|
597
|
+
// nodes of one bucket the later one decides the cascade, so the earlier one is redundant.
|
|
598
|
+
for (const key of /** @type {(keyof Buckets)[]} */ (Object.keys(buckets))) {
|
|
599
|
+
const seen = new Set();
|
|
600
|
+
|
|
601
|
+
buckets[key] = buckets[key]
|
|
602
|
+
.reverse()
|
|
603
|
+
.filter((node) => {
|
|
604
|
+
const id = JSON.stringify(node);
|
|
605
|
+
|
|
606
|
+
return seen.has(id) ? false : (seen.add(id), true);
|
|
607
|
+
})
|
|
608
|
+
.reverse();
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/* ---- binding templates (doc 03 section 4.3) ---- */
|
|
612
|
+
|
|
613
|
+
/** @type {{ components: Node[], utilities: Node[] }} */
|
|
614
|
+
const templates = { components: [], utilities: [] };
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* @param {'components' | 'utilities'} layer
|
|
618
|
+
* @param {string[]} classes classes that must be in use
|
|
619
|
+
* @param {string} selector
|
|
620
|
+
* @param {[string, string][]} decls
|
|
621
|
+
*/
|
|
622
|
+
const template = (layer, classes, selector, decls) => {
|
|
623
|
+
if (!classes.every((c) => used.has(c))) return;
|
|
624
|
+
|
|
625
|
+
for (const c of classes) keptClasses.add(c);
|
|
626
|
+
|
|
627
|
+
templates[layer].push({
|
|
628
|
+
type: 'rule',
|
|
629
|
+
selectors: [selector],
|
|
630
|
+
decls: decls.map(([prop, value]) => ({ prop, value })),
|
|
631
|
+
});
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
/** @param {string} c @param {number} percent @param {string} [other] */
|
|
635
|
+
const mix = (c, percent, other = 'transparent') =>
|
|
636
|
+
`color-mix(in srgb, var(--pano-color-${c}) ${percent}%, ${other})`;
|
|
637
|
+
|
|
638
|
+
for (const c of THEME_COLORS) {
|
|
639
|
+
const color = `var(--pano-color-${c})`;
|
|
640
|
+
const on = ON_COLOR[/** @type {keyof typeof ON_COLOR} */ (c)];
|
|
641
|
+
|
|
642
|
+
template('components', [`btn-${c}`], `.btn-${c}`, [
|
|
643
|
+
['--bs-btn-color', on],
|
|
644
|
+
['--bs-btn-bg', color],
|
|
645
|
+
['--bs-btn-border-color', color],
|
|
646
|
+
['--bs-btn-hover-color', on],
|
|
647
|
+
['--bs-btn-hover-bg', mix(c, 85, '#000')],
|
|
648
|
+
['--bs-btn-hover-border-color', mix(c, 85, '#000')],
|
|
649
|
+
['--bs-btn-active-color', on],
|
|
650
|
+
['--bs-btn-active-bg', color],
|
|
651
|
+
['--bs-btn-active-border-color', color],
|
|
652
|
+
['--bs-btn-disabled-color', on],
|
|
653
|
+
['--bs-btn-disabled-bg', color],
|
|
654
|
+
['--bs-btn-disabled-border-color', color],
|
|
655
|
+
]);
|
|
656
|
+
template('components', [`btn-outline-${c}`], `.btn-outline-${c}`, [
|
|
657
|
+
['--bs-btn-color', color],
|
|
658
|
+
['--bs-btn-border-color', color],
|
|
659
|
+
['--bs-btn-hover-color', on],
|
|
660
|
+
['--bs-btn-hover-bg', color],
|
|
661
|
+
['--bs-btn-hover-border-color', color],
|
|
662
|
+
['--bs-btn-active-color', on],
|
|
663
|
+
['--bs-btn-active-bg', color],
|
|
664
|
+
['--bs-btn-active-border-color', color],
|
|
665
|
+
['--bs-btn-disabled-color', color],
|
|
666
|
+
['--bs-btn-disabled-border-color', color],
|
|
667
|
+
]);
|
|
668
|
+
template('components', [`alert-${c}`], `.alert-${c}`, [
|
|
669
|
+
['--bs-alert-color', mix(c, 85, '#000')],
|
|
670
|
+
['--bs-alert-border-color', mix(c, 25)],
|
|
671
|
+
['border-radius', 'var(--pano-radius)'],
|
|
672
|
+
]);
|
|
673
|
+
template('utilities', [`text-bg-${c}`], `.text-bg-${c}`, [
|
|
674
|
+
['background-color', color],
|
|
675
|
+
['color', on],
|
|
676
|
+
]);
|
|
677
|
+
template('utilities', [`bg-${c}-subtle`], `.bg-${c}-subtle`, [
|
|
678
|
+
['background-color', mix(c, 12)],
|
|
679
|
+
['color', color],
|
|
680
|
+
['border-color', mix(c, 20)],
|
|
681
|
+
]);
|
|
682
|
+
template('utilities', [`text-${c}-emphasis`], `.text-${c}-emphasis`, [['color', mix(c, 80, '#000')]]);
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
template('components', ['btn'], '.btn:not(.btn-link)', [
|
|
686
|
+
['border', '1.5px solid color-mix(in srgb, var(--pano-color-text) 10%, transparent)'],
|
|
687
|
+
]);
|
|
688
|
+
template('components', ['nav-pills'], '.nav-pills', [
|
|
689
|
+
['--bs-nav-pills-link-active-bg', 'var(--pano-color-primary)'],
|
|
690
|
+
['--bs-nav-pills-link-active-color', 'var(--pano-color-on-primary)'],
|
|
691
|
+
]);
|
|
692
|
+
|
|
693
|
+
/* ---- what the kept rules read ---- */
|
|
694
|
+
|
|
695
|
+
let keptText = '';
|
|
696
|
+
|
|
697
|
+
/** @param {Node[]} nodes */
|
|
698
|
+
const collectText = (nodes) =>
|
|
699
|
+
walkRules(nodes, (rule) => {
|
|
700
|
+
for (const d of rule.decls) keptText += `${d.value}\n`;
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
collectText(buckets.cs);
|
|
704
|
+
collectText(buckets.cb);
|
|
705
|
+
collectText(buckets.us);
|
|
706
|
+
collectText(buckets.ub);
|
|
707
|
+
collectText(templates.components);
|
|
708
|
+
collectText(templates.utilities);
|
|
709
|
+
|
|
710
|
+
// After STEP 6 nothing reads `--bs-X-rgb` of a rewritten colour any more (unless a use was not
|
|
711
|
+
// recognised), so those variables are left out of the literal defaults and the palette extras.
|
|
712
|
+
const stillUsedRgb = new Set([...keptText.matchAll(/var\(\s*--bs-([a-z0-9-]+)-rgb\b/g)].map((m) => m[1]));
|
|
713
|
+
/** @param {string} prop */
|
|
714
|
+
const deadVar = (prop) => {
|
|
715
|
+
const m = /^--bs-([a-z0-9-]+)-rgb$/.exec(prop);
|
|
716
|
+
|
|
717
|
+
return m !== null && RGB_REWRITE.has(m[1]) && !stillUsedRgb.has(m[1]);
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
/** @param {string} palette @returns {Decl[]} */
|
|
721
|
+
const extrasOf = (palette) =>
|
|
722
|
+
[.../** @type {Map<string, string>} */ (extras.get(palette))]
|
|
723
|
+
.filter(([prop]) => !deadVar(prop))
|
|
724
|
+
.map(([prop, value]) => ({ prop, value }));
|
|
725
|
+
|
|
726
|
+
/* ---- STEP 7: the :scope block ---- */
|
|
727
|
+
|
|
728
|
+
/** @type {[string, string][]} */
|
|
729
|
+
const scopeDecls = [];
|
|
730
|
+
|
|
731
|
+
for (const [prop, value] of defaults) if (!deadVar(prop)) scopeDecls.push([prop, value]);
|
|
732
|
+
|
|
733
|
+
for (const t of TOKENS) if (t.bs) scopeDecls.push([t.bs, `var(${t.token})`]);
|
|
734
|
+
|
|
735
|
+
for (const c of THEME_COLORS) {
|
|
736
|
+
scopeDecls.push([`--bs-${c}-bg-subtle`, mix(c, 12)]);
|
|
737
|
+
scopeDecls.push([`--bs-${c}-text-emphasis`, mix(c, 60, '#fff')]);
|
|
738
|
+
scopeDecls.push([`--bs-${c}-border-subtle`, mix(c, 75, '#000')]);
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
scopeDecls.push(['--bs-heading-color', 'var(--pano-color-heading)']);
|
|
742
|
+
scopeDecls.push(['--bs-border-color-translucent', 'color-mix(in srgb, var(--pano-color-text) 17.5%, transparent)']);
|
|
743
|
+
scopeDecls.push(['--bs-focus-ring-color', 'color-mix(in srgb, var(--pano-color-primary) 25%, transparent)']);
|
|
744
|
+
scopeDecls.push(['color', 'var(--bs-body-color)']);
|
|
745
|
+
scopeDecls.push(['font-family', 'var(--bs-body-font-family)']);
|
|
746
|
+
scopeDecls.push(['font-size', 'var(--bs-body-font-size)']);
|
|
747
|
+
scopeDecls.push(['line-height', 'var(--bs-body-line-height)']);
|
|
748
|
+
|
|
749
|
+
const paletteOrder = [
|
|
750
|
+
...PALETTES.filter((p) => p !== 'light' && extras.has(p)),
|
|
751
|
+
...[...extras.keys()].filter((p) => !PALETTES.includes(p)),
|
|
752
|
+
];
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* @param {string} head selector before `:where(...)`
|
|
756
|
+
* @returns {Node[]}
|
|
757
|
+
*/
|
|
758
|
+
const paletteBlocks = (head) =>
|
|
759
|
+
paletteOrder.map((palette) => ({
|
|
760
|
+
type: /** @type {const} */ ('rule'),
|
|
761
|
+
selectors: [`${head}:where([data-bs-theme='${palette}'] *)`],
|
|
762
|
+
decls: extrasOf(palette),
|
|
763
|
+
}));
|
|
764
|
+
|
|
765
|
+
const toDecls = (/** @type {[string, string][]} */ list) => list.map(([prop, value]) => ({ prop, value }));
|
|
766
|
+
|
|
767
|
+
/** @type {Node[]} */
|
|
768
|
+
const scopeNodes = [
|
|
769
|
+
{ type: 'rule', selectors: [':scope'], decls: toDecls(scopeDecls) },
|
|
770
|
+
...paletteBlocks(':scope'),
|
|
771
|
+
];
|
|
772
|
+
|
|
773
|
+
/* ---- body-level block ---- */
|
|
774
|
+
|
|
775
|
+
const bodyClasses = new Set();
|
|
776
|
+
|
|
777
|
+
for (const key of /** @type {const} */ (['cb', 'ub'])) {
|
|
778
|
+
walkRules(buckets[key], (rule) => {
|
|
779
|
+
for (const selector of rule.selectors) for (const c of scanSelector(selector).classes) bodyClasses.add(c);
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
const bodyRoots = ['tooltip', 'popover'].filter((c) => bodyClasses.has(c)).map((c) => `.${c}`);
|
|
784
|
+
/** @type {Node[]} */
|
|
785
|
+
const bodyHead = [];
|
|
786
|
+
|
|
787
|
+
if (bodyRoots.length > 0) {
|
|
788
|
+
bodyHead.push({ type: 'rule', selectors: bodyRoots, decls: toDecls(scopeDecls) });
|
|
789
|
+
|
|
790
|
+
for (const palette of paletteOrder) {
|
|
791
|
+
bodyHead.push({
|
|
792
|
+
type: 'rule',
|
|
793
|
+
selectors: bodyRoots.map((r) => `${r}:where([data-bs-theme='${palette}'] *)`),
|
|
794
|
+
decls: extrasOf(palette),
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
bodyHead.push(...buckets.fb);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/* ---- keyframes a kept rule names ---- */
|
|
802
|
+
|
|
803
|
+
/** @type {Node[]} */
|
|
804
|
+
const keptKeyframes = [];
|
|
805
|
+
|
|
806
|
+
for (const frames of keyframes.values()) {
|
|
807
|
+
if (new RegExp(`(?<![\\w-])${escapeRe(frames.params)}(?![\\w-])`).test(keptText)) {
|
|
808
|
+
keptKeyframes.push({ type: 'at', name: frames.name, params: frames.params, nodes: frames.nodes });
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/* ---- layers ---- */
|
|
813
|
+
|
|
814
|
+
const scopeRoot = `.pano-fb[data-pano-fb="${ns}"]`;
|
|
815
|
+
const scopeLimit = `.pano-fb-stop, .pano-fb:not([data-pano-fb="${ns}"])`;
|
|
816
|
+
const scopeHead = `@scope (${scopeRoot}) to (${scopeLimit}) {\n`;
|
|
817
|
+
|
|
818
|
+
let css =
|
|
819
|
+
`/* Pano fallback stylesheet for "${ns}" (generated by @panomc/plugin-kit; do not edit) */\n` +
|
|
820
|
+
'@layer pano-defaults, pano-fallback.components, pano-fallback.utilities, pano-plugin;\n';
|
|
821
|
+
|
|
822
|
+
const componentScoped = [...scopeNodes, ...buckets.cs, ...templates.components];
|
|
823
|
+
const componentBody = [...bodyHead, ...buckets.cb];
|
|
824
|
+
|
|
825
|
+
css += '@layer pano-fallback.components {\n';
|
|
826
|
+
css += ` ${scopeHead}${emit(componentScoped, ' ')} }\n`;
|
|
827
|
+
css += emit(componentBody, ' ');
|
|
828
|
+
css += emit(keptKeyframes, ' ');
|
|
829
|
+
css += '}\n';
|
|
830
|
+
|
|
831
|
+
const utilityScoped = [...buckets.us, ...templates.utilities];
|
|
832
|
+
|
|
833
|
+
if (utilityScoped.length > 0 || buckets.ub.length > 0) {
|
|
834
|
+
css += '@layer pano-fallback.utilities {\n';
|
|
835
|
+
|
|
836
|
+
if (utilityScoped.length > 0) css += ` ${scopeHead}${emit(utilityScoped, ' ')} }\n`;
|
|
837
|
+
|
|
838
|
+
css += emit(buckets.ub, ' ');
|
|
839
|
+
css += '}\n';
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
return { css, classes: [...keptClasses].sort() };
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/* ------------------------------------------------------------------------------------------------
|
|
846
|
+
* Scanning the views
|
|
847
|
+
* ---------------------------------------------------------------------------------------------- */
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Every string literal of a piece of JavaScript (quotes, backticks; the quasis of a template and
|
|
851
|
+
* the literals inside its `${}`), comments skipped.
|
|
852
|
+
*
|
|
853
|
+
* @param {string} code
|
|
854
|
+
* @param {string[]} [out]
|
|
855
|
+
* @returns {string[]}
|
|
856
|
+
*/
|
|
857
|
+
export function stringLiterals(code, out = []) {
|
|
858
|
+
const n = code.length;
|
|
859
|
+
let i = 0;
|
|
860
|
+
|
|
861
|
+
while (i < n) {
|
|
862
|
+
const c = code[i];
|
|
863
|
+
|
|
864
|
+
if (c === '/' && code[i + 1] === '/') {
|
|
865
|
+
while (i < n && code[i] !== '\n') i++;
|
|
866
|
+
} else if (c === '/' && code[i + 1] === '*') {
|
|
867
|
+
const end = code.indexOf('*/', i + 2);
|
|
868
|
+
|
|
869
|
+
i = end === -1 ? n : end + 2;
|
|
870
|
+
} else if (c === '"' || c === "'") {
|
|
871
|
+
let j = i + 1;
|
|
872
|
+
let text = '';
|
|
873
|
+
|
|
874
|
+
while (j < n && code[j] !== c && code[j] !== '\n') {
|
|
875
|
+
if (code[j] === '\\') {
|
|
876
|
+
text += code[j + 1] ?? '';
|
|
877
|
+
j += 2;
|
|
878
|
+
} else text += code[j++];
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
out.push(text);
|
|
882
|
+
i = j + 1;
|
|
883
|
+
} else if (c === '`') {
|
|
884
|
+
let j = i + 1;
|
|
885
|
+
let text = '';
|
|
886
|
+
|
|
887
|
+
while (j < n && code[j] !== '`') {
|
|
888
|
+
if (code[j] === '\\') {
|
|
889
|
+
text += code[j + 1] ?? '';
|
|
890
|
+
j += 2;
|
|
891
|
+
} else if (code[j] === '$' && code[j + 1] === '{') {
|
|
892
|
+
out.push(text);
|
|
893
|
+
text = '';
|
|
894
|
+
|
|
895
|
+
let depth = 0;
|
|
896
|
+
const start = j + 2;
|
|
897
|
+
|
|
898
|
+
for (j = j + 1; j < n; j++) {
|
|
899
|
+
if (code[j] === '{') depth++;
|
|
900
|
+
else if (code[j] === '}' && --depth === 0) break;
|
|
901
|
+
else if (code[j] === '"' || code[j] === "'" || code[j] === '`') {
|
|
902
|
+
// skip a nested literal so its braces do not count
|
|
903
|
+
const q = code[j];
|
|
904
|
+
|
|
905
|
+
for (j++; j < n && code[j] !== q; j++) if (code[j] === '\\') j++;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
stringLiterals(code.slice(start, j), out);
|
|
910
|
+
j++;
|
|
911
|
+
} else text += code[j++];
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
out.push(text);
|
|
915
|
+
i = j + 1;
|
|
916
|
+
} else {
|
|
917
|
+
i++;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
return out;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* Class names (selectors' classes) the base sheet defines.
|
|
926
|
+
*
|
|
927
|
+
* @param {string} baseCss
|
|
928
|
+
* @returns {Set<string>}
|
|
929
|
+
*/
|
|
930
|
+
export function baseClassSet(baseCss) {
|
|
931
|
+
const found = new Set();
|
|
932
|
+
|
|
933
|
+
postcss.parse(baseCss).walkRules((rule) => {
|
|
934
|
+
for (const selector of rule.selectors) for (const c of scanSelector(selector).classes) found.add(c);
|
|
935
|
+
});
|
|
936
|
+
|
|
937
|
+
return found;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
/** @param {string} a @param {string} b @returns {number} */
|
|
941
|
+
function distance(a, b) {
|
|
942
|
+
const row = Array.from({ length: b.length + 1 }, (_, k) => k);
|
|
943
|
+
|
|
944
|
+
for (let i = 1; i <= a.length; i++) {
|
|
945
|
+
let prev = row[0];
|
|
946
|
+
|
|
947
|
+
row[0] = i;
|
|
948
|
+
|
|
949
|
+
for (let j = 1; j <= b.length; j++) {
|
|
950
|
+
const next = row[j];
|
|
951
|
+
|
|
952
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
953
|
+
prev = next;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
return row[b.length];
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/** The base classes closest to `name`. @param {string} name @param {Set<string>} known @returns {string[]} */
|
|
961
|
+
export function nearMatches(name, known) {
|
|
962
|
+
return [...known]
|
|
963
|
+
.map((c) => ({ c, d: c.includes(name) || name.includes(c) ? 1 : distance(c, name) }))
|
|
964
|
+
.filter((x) => x.d <= Math.max(2, Math.floor(name.length / 3)))
|
|
965
|
+
.sort((x, y) => x.d - y.d || (x.c < y.c ? -1 : 1))
|
|
966
|
+
.slice(0, 3)
|
|
967
|
+
.map((x) => x.c);
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
/**
|
|
971
|
+
* @typedef {object} ViewsScan
|
|
972
|
+
* @property {Set<string>} used literal class tokens of the views plus the script literals that are base classes
|
|
973
|
+
* @property {boolean} fa a Font Awesome class (`fa-…`) is used
|
|
974
|
+
* @property {{ name: string, file: string, roots: string[], classes: string[] }[]} views
|
|
975
|
+
* @property {string[]} files every file read
|
|
976
|
+
*/
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* STEP 1: the classes a plugin's views use.
|
|
980
|
+
*
|
|
981
|
+
* @param {string} viewsDir
|
|
982
|
+
* @param {Set<string>} baseClasses
|
|
983
|
+
* @param {string} ns
|
|
984
|
+
* @returns {ViewsScan}
|
|
985
|
+
*/
|
|
986
|
+
export function scanViews(viewsDir, baseClasses, ns) {
|
|
987
|
+
/** @type {ViewsScan} */
|
|
988
|
+
const result = { used: new Set(), fa: false, views: [], files: [] };
|
|
989
|
+
|
|
990
|
+
if (!fs.existsSync(viewsDir)) return result;
|
|
991
|
+
|
|
992
|
+
/** @type {string[]} */
|
|
993
|
+
const files = [];
|
|
994
|
+
|
|
995
|
+
(function walk(dir) {
|
|
996
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : 1))) {
|
|
997
|
+
const full = path.join(dir, entry.name);
|
|
998
|
+
|
|
999
|
+
if (entry.isDirectory()) {
|
|
1000
|
+
if (entry.name !== 'node_modules') walk(full);
|
|
1001
|
+
} else if (/\.(?:svelte|js|mjs)$/.test(entry.name)) {
|
|
1002
|
+
files.push(full);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
})(viewsDir);
|
|
1006
|
+
|
|
1007
|
+
/** @param {string} text @param {boolean} onlyBase */
|
|
1008
|
+
const addTokens = (text, onlyBase) => {
|
|
1009
|
+
for (const token of text.split(/\s+/)) {
|
|
1010
|
+
if (!token) continue;
|
|
1011
|
+
|
|
1012
|
+
if (token.startsWith('fa-')) result.fa = true;
|
|
1013
|
+
|
|
1014
|
+
if (!onlyBase || baseClasses.has(token)) result.used.add(token);
|
|
1015
|
+
}
|
|
1016
|
+
};
|
|
1017
|
+
|
|
1018
|
+
for (const file of files) {
|
|
1019
|
+
const source = fs.readFileSync(file, 'utf8');
|
|
1020
|
+
|
|
1021
|
+
result.files.push(file);
|
|
1022
|
+
|
|
1023
|
+
if (file.endsWith('.svelte')) {
|
|
1024
|
+
const { markup, scripts } = splitSvelte(source);
|
|
1025
|
+
/** @type {string[]} */
|
|
1026
|
+
const markupTokens = [];
|
|
1027
|
+
|
|
1028
|
+
for (const tag of parseTags(markup)) markupTokens.push(...classTokens(tag.attrs));
|
|
1029
|
+
|
|
1030
|
+
for (const token of markupTokens) addTokens(token, false);
|
|
1031
|
+
|
|
1032
|
+
for (const script of scripts) for (const literal of stringLiterals(script.code)) addTokens(literal, true);
|
|
1033
|
+
|
|
1034
|
+
// Bootstrap's tooltips and popovers are created by script and appended to <body>
|
|
1035
|
+
if (/data-bs-toggle\s*=\s*["']?tooltip/.test(source)) result.used.add('tooltip');
|
|
1036
|
+
if (/data-bs-toggle\s*=\s*["']?popover/.test(source)) result.used.add('popover');
|
|
1037
|
+
|
|
1038
|
+
const name = path.basename(file, '.svelte');
|
|
1039
|
+
|
|
1040
|
+
if (/^[A-Z][A-Za-z0-9]*$/.test(name)) {
|
|
1041
|
+
const rootClass = rootClassOf(ns, name);
|
|
1042
|
+
let info = { roots: /** @type {string[]} */ ([]) };
|
|
1043
|
+
|
|
1044
|
+
try {
|
|
1045
|
+
info = addRootClass(source, { ns, view: name });
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
// a Svelte syntax error is the compiler's to report; a root-class collision fails the build
|
|
1048
|
+
if (error instanceof SemanticClassCollisionError) throw error;
|
|
1049
|
+
}
|
|
1050
|
+
const parts = [];
|
|
1051
|
+
|
|
1052
|
+
for (const token of markupTokens) {
|
|
1053
|
+
if (
|
|
1054
|
+
token.startsWith(`${rootClass}__`) &&
|
|
1055
|
+
/^[a-z0-9-]+$/.test(token.slice(rootClass.length + 2)) &&
|
|
1056
|
+
!parts.includes(token)
|
|
1057
|
+
) {
|
|
1058
|
+
parts.push(token);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
result.views.push({
|
|
1063
|
+
name,
|
|
1064
|
+
file,
|
|
1065
|
+
roots: info.roots,
|
|
1066
|
+
classes: info.roots.length > 0 ? [rootClass, ...parts] : [],
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
} else {
|
|
1070
|
+
for (const literal of stringLiterals(source)) addTokens(literal, true);
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
return result;
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
/** @type {{ css: string, classes: Set<string> } | null} */
|
|
1078
|
+
let cachedBase = null;
|
|
1079
|
+
|
|
1080
|
+
/** The base sheet and its class set, read once. @returns {{ css: string, classes: Set<string> }} */
|
|
1081
|
+
function loadBase() {
|
|
1082
|
+
if (!cachedBase) {
|
|
1083
|
+
const css = fs.readFileSync(BASE_CSS_FILE, 'utf8');
|
|
1084
|
+
|
|
1085
|
+
cachedBase = { css, classes: baseClassSet(css) };
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
return cachedBase;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
/**
|
|
1092
|
+
* Client-build rollup plugin: scans the views (STEP 1), runs {@link buildFallbackCss}, emits
|
|
1093
|
+
* `fallback.css` into the client folder and records `styles` and the views' `roots` / `classes` for
|
|
1094
|
+
* `pano-plugin.json`.
|
|
1095
|
+
*
|
|
1096
|
+
* `icons` `'auto'` (default) is true when any used class starts with `fa-`.
|
|
1097
|
+
*
|
|
1098
|
+
* @param {{ ns: string, viewsDir?: string | string[], safelist?: string[], icons?: boolean | 'auto',
|
|
1099
|
+
* root?: string, outDir?: string, side?: string }} options
|
|
1100
|
+
* @returns {import('rollup').Plugin}
|
|
1101
|
+
*/
|
|
1102
|
+
export function panoFallbackCss(options) {
|
|
1103
|
+
if (options?.side !== undefined && options.side !== 'client') return { name: 'pano-fallback-css' };
|
|
1104
|
+
|
|
1105
|
+
const ns = options?.ns;
|
|
1106
|
+
|
|
1107
|
+
if (typeof ns !== 'string' || !NS_PATTERN.test(ns)) {
|
|
1108
|
+
throw new Error(`[pano-plugin] panoFallbackCss: "ns" must match ${NS_PATTERN}, got "${ns}"`);
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
1112
|
+
const viewsDirs = [options.viewsDir ?? 'src/theme/views'].flat().map((dir) => path.resolve(root, dir));
|
|
1113
|
+
const safelist = (options.safelist ?? []).map((c) => String(c).replace(/^\./, ''));
|
|
1114
|
+
const iconsOption = options.icons ?? 'auto';
|
|
1115
|
+
|
|
1116
|
+
/** @type {{ css: string, icons: boolean } | null} */
|
|
1117
|
+
let built = null;
|
|
1118
|
+
|
|
1119
|
+
return {
|
|
1120
|
+
name: 'pano-fallback-css',
|
|
1121
|
+
|
|
1122
|
+
buildStart() {
|
|
1123
|
+
clearStyleFile(root, 'fallback');
|
|
1124
|
+
built = null;
|
|
1125
|
+
|
|
1126
|
+
const base = loadBase();
|
|
1127
|
+
const missing = safelist.filter((c) => !base.classes.has(c));
|
|
1128
|
+
|
|
1129
|
+
if (missing.length > 0) {
|
|
1130
|
+
const lines = missing.map((c) => {
|
|
1131
|
+
const near = nearMatches(c, base.classes);
|
|
1132
|
+
|
|
1133
|
+
return ` "${c}"${near.length ? ` (did you mean ${near.map((n) => `"${n}"`).join(', ')}?)` : ''}`;
|
|
1134
|
+
});
|
|
1135
|
+
|
|
1136
|
+
this.error(
|
|
1137
|
+
`[pano-plugin] styles.safelist in pano.plugin.js names classes that are not in the Bootstrap base sheet:\n${lines.join('\n')}\n` +
|
|
1138
|
+
'Fix the spelling, or remove the entry if the class is your own.',
|
|
1139
|
+
);
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
const used = new Set(safelist);
|
|
1143
|
+
let fa = false;
|
|
1144
|
+
/** @type {ViewsScan['views']} */
|
|
1145
|
+
let views = [];
|
|
1146
|
+
|
|
1147
|
+
for (const dir of viewsDirs) {
|
|
1148
|
+
let scan;
|
|
1149
|
+
|
|
1150
|
+
try {
|
|
1151
|
+
scan = scanViews(dir, base.classes, ns);
|
|
1152
|
+
} catch (error) {
|
|
1153
|
+
this.error(/** @type {Error} */ (error).message);
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
for (const token of scan.used) used.add(token);
|
|
1157
|
+
|
|
1158
|
+
fa ||= scan.fa;
|
|
1159
|
+
views = views.concat(scan.views);
|
|
1160
|
+
|
|
1161
|
+
for (const file of scan.files) this.addWatchFile(file);
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
const icons = iconsOption === 'auto' ? fa : iconsOption === true;
|
|
1165
|
+
|
|
1166
|
+
built = { css: buildFallbackCss({ baseCss: base.css, usedClasses: [...used], ns }).css, icons };
|
|
1167
|
+
|
|
1168
|
+
const context = getBuildContext(root);
|
|
1169
|
+
|
|
1170
|
+
for (const view of views) {
|
|
1171
|
+
context.views[view.name] = { ...(context.views[view.name] ?? {}), roots: view.roots, classes: view.classes };
|
|
1172
|
+
}
|
|
1173
|
+
},
|
|
1174
|
+
|
|
1175
|
+
generateBundle() {
|
|
1176
|
+
if (!built) return;
|
|
1177
|
+
|
|
1178
|
+
this.emitFile({ type: 'asset', fileName: 'fallback.css', source: built.css });
|
|
1179
|
+
recordStyleFile(root, 'fallback', built.css, { icons: built.icons });
|
|
1180
|
+
},
|
|
1181
|
+
};
|
|
1182
|
+
}
|