42 lines
2.4 KiB
JavaScript
42 lines
2.4 KiB
JavaScript
// Builds ide/ui/js/bootstrap-classes.js: every class of Bootstrap's real stylesheets (the IDE's Bootstrap editor checks class names
|
|
// against it). Run in Docker, with the two bootstrap.min.css files of the npm packages:
|
|
// docker run --rm -v "$PWD:/w" -w /w node:22-alpine sh -c 'cd /tmp && npm pack [email protected] [email protected] --silent && \
|
|
// for f in bootstrap-*.tgz; do mkdir ${f%.tgz} && tar xzf $f -C ${f%.tgz} package/dist/css/bootstrap.min.css; done; \
|
|
// cd /w && node tools/bootstrap-catalog.mjs 5.3.3=/tmp/bootstrap-5.3.3/package/dist/css/bootstrap.min.css 4.6.2=/tmp/bootstrap-4.6.2/package/dist/css/bootstrap.min.css'
|
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
|
|
function classesOf(css) {
|
|
const s = css.replace(/\/\*[\s\S]*?\*\//g, '').replace(/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/g, '""').replace(/url\([^)]*\)/g, '');
|
|
const out = new Set();
|
|
let buffer = '';
|
|
let depth = 0;
|
|
const declarations = []; // depth at which a { opened a declaration block (no selectors inside it)
|
|
for (const ch of s) {
|
|
if (ch === '{') {
|
|
const prelude = buffer.trim();
|
|
// a rule's prelude is its selector list (an at-rule's, like @media, has none); inside a declaration block nothing is one
|
|
if (!declarations.length && prelude && !prelude.startsWith('@')) {
|
|
for (const m of prelude.matchAll(/(?<![\w\d)\]-])\.(-?[_a-zA-Z](?:[\w-]|\\.)*)/g)) out.add(m[1].replace(/\\(.)/g, '$1'));
|
|
declarations.push(depth);
|
|
} else if (declarations.length) declarations.push(depth);
|
|
depth++;
|
|
buffer = '';
|
|
} else if (ch === '}') {
|
|
depth--;
|
|
if (declarations.length && declarations[declarations.length - 1] === depth) declarations.pop();
|
|
buffer = '';
|
|
} else if (ch === ';' && !declarations.length) buffer = '';
|
|
else buffer += ch;
|
|
}
|
|
return [...out].sort();
|
|
}
|
|
|
|
const args = process.argv.slice(2).map((a) => a.split('='));
|
|
const lines = ['// GENERATED by tools/bootstrap-catalog.mjs from the real bootstrap.min.css files: do not edit.', '// version -> every class of its stylesheet (space separated)', 'export const BOOTSTRAP_CLASSES = {'];
|
|
for (const [version, file] of args) {
|
|
const list = classesOf(readFileSync(file, 'utf8'));
|
|
lines.push(` '${version.split('.')[0]}': '${list.join(' ')}',`);
|
|
console.log(version, list.length);
|
|
}
|
|
lines.push('};', '');
|
|
writeFileSync(new URL('../ide/ui/js/bootstrap-classes.js', import.meta.url), lines.join('\n'));
|