@vanelsas/baredom 4.3.0 → 4.5.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/CHANGELOG.md +54 -0
- package/dist/baremirror-alpha.js +27 -27
- package/dist/base.js +322 -322
- package/dist/integrity.json +112 -112
- package/dist/x-alert.js +17 -17
- package/dist/x-avatar-group.js +10 -10
- package/dist/x-avatar.js +13 -13
- package/dist/x-badge.js +9 -9
- package/dist/x-bento-grid.js +6 -6
- package/dist/x-bento-item.js +4 -4
- package/dist/x-breadcrumbs.js +16 -16
- package/dist/x-button.js +1 -1
- package/dist/x-calendar.js +41 -41
- package/dist/x-cancel-dialogue.js +16 -16
- package/dist/x-card.js +7 -7
- package/dist/x-carousel.js +32 -32
- package/dist/x-chart.js +48 -48
- package/dist/x-checkbox.js +1 -1
- package/dist/x-chip.js +1 -1
- package/dist/x-code.js +31 -28
- package/dist/x-collapse.js +9 -9
- package/dist/x-color-picker.js +44 -44
- package/dist/x-combobox.js +34 -34
- package/dist/x-command-palette.js +30 -30
- package/dist/x-confetti.js +24 -24
- package/dist/x-container.js +6 -6
- package/dist/x-context-menu.js +18 -18
- package/dist/x-copy.js +1 -1
- package/dist/x-currency-field.js +24 -24
- package/dist/x-date-picker.js +43 -43
- package/dist/x-divider.js +8 -8
- package/dist/x-drag-panel.js +32 -32
- package/dist/x-drawer.js +12 -12
- package/dist/x-drop-zone.js +11 -11
- package/dist/x-dropdown.js +12 -12
- package/dist/x-fieldset.js +6 -6
- package/dist/x-file-download.js +14 -14
- package/dist/x-file-upload.js +26 -26
- package/dist/x-floating-panel.js +21 -21
- package/dist/x-form-field.js +17 -17
- package/dist/x-form.js +10 -10
- package/dist/x-gaussian-blur.js +12 -12
- package/dist/x-grid.js +8 -8
- package/dist/x-i18n-provider.js +7 -7
- package/dist/x-i18n.js +4 -4
- package/dist/x-icon.js +7 -7
- package/dist/x-image.js +18 -17
- package/dist/x-kbd.js +13 -13
- package/dist/x-kinetic-canvas.js +24 -24
- package/dist/x-kinetic-font.js +20 -20
- package/dist/x-kinetic-typography.js +33 -33
- package/dist/x-liquid-dock.js +29 -29
- package/dist/x-liquid-fill.js +35 -35
- package/dist/x-liquid-glass.js +31 -31
- package/dist/x-menu-item.js +9 -9
- package/dist/x-menu.js +10 -10
- package/dist/x-metaball-cursor.js +25 -25
- package/dist/x-modal.js +10 -10
- package/dist/x-morph-stack.js +38 -38
- package/dist/x-multi-combobox.js +39 -39
- package/dist/x-navbar.js +13 -13
- package/dist/x-neural-glow.js +28 -28
- package/dist/x-notification-center.js +9 -9
- package/dist/x-organic-divider.js +18 -18
- package/dist/x-organic-progress.js +31 -31
- package/dist/x-organic-shape.js +12 -12
- package/dist/x-otp-input.js +22 -22
- package/dist/x-pagination.js +16 -16
- package/dist/x-particle-button.js +43 -43
- package/dist/x-popover.js +26 -26
- package/dist/x-progress-circle.js +7 -7
- package/dist/x-progress.js +7 -7
- package/dist/x-proximity-list.js +20 -20
- package/dist/x-radio.js +13 -13
- package/dist/x-range-slider.js +30 -30
- package/dist/x-rating.js +21 -21
- package/dist/x-ripple-effect.js +10 -10
- package/dist/x-scroll-parallax.js +17 -17
- package/dist/x-scroll-stack.js +17 -17
- package/dist/x-scroll-story.js +29 -29
- package/dist/x-scroll-timeline.js +46 -46
- package/dist/x-scroll.js +35 -35
- package/dist/x-search-field.js +1 -1
- package/dist/x-select.js +1 -1
- package/dist/x-sidebar.js +15 -15
- package/dist/x-skeleton-group.js +13 -13
- package/dist/x-skeleton.js +1 -1
- package/dist/x-slider.js +17 -17
- package/dist/x-soft-body.js +16 -16
- package/dist/x-spacer.js +4 -4
- package/dist/x-spinner.js +4 -4
- package/dist/x-splash.js +13 -13
- package/dist/x-split-pane.js +15 -15
- package/dist/x-spotlight-card.js +12 -12
- package/dist/x-stat.js +10 -10
- package/dist/x-stepper.js +14 -14
- package/dist/x-switch.js +12 -12
- package/dist/x-tab.js +9 -9
- package/dist/x-table-cell.js +20 -20
- package/dist/x-table-row.js +9 -9
- package/dist/x-table.js +13 -13
- package/dist/x-tabs.js +11 -11
- package/dist/x-text-area.js +18 -18
- package/dist/x-theme.js +25 -25
- package/dist/x-timeline-item.js +20 -20
- package/dist/x-timeline.js +8 -8
- package/dist/x-toast.js +21 -21
- package/dist/x-toaster.js +6 -6
- package/dist/x-tooltip.js +13 -13
- package/dist/x-trace-history.js +116 -116
- package/dist/x-typography.js +6 -6
- package/dist/x-welcome-tour.js +62 -62
- package/package.json +5 -1
- package/scripts/prerender.mjs +284 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Writes a page back out with every shadow tree in its markup, as a declarative shadow root, so
|
|
3
|
+
// the page shows its components before any script has run.
|
|
4
|
+
//
|
|
5
|
+
// Usage: node scripts/prerender.mjs [--static] [--tabs <n>] <url> <output.html> [<url> <output.html> ...]
|
|
6
|
+
// --static leaves out every script and noscript element and sets data-static on the
|
|
7
|
+
// html element, so the page can style a copy that runs no script.
|
|
8
|
+
// --tabs says how many pages are opened at once: 4 when it is not given, or fewer on a
|
|
9
|
+
// machine with fewer processors.
|
|
10
|
+
// Each url is written to the output that follows it. One browser opens them all.
|
|
11
|
+
// Needs: Node 22 or later, and Chrome. Set CHROME_PATH when Chrome is not found, and
|
|
12
|
+
// CHROME_FLAGS for more flags, such as --no-sandbox in a container.
|
|
13
|
+
//
|
|
14
|
+
// A page must be served: the script opens its url in a headless Chrome. Write the output where
|
|
15
|
+
// the page's relative urls still resolve, which is beside the page it was made from.
|
|
16
|
+
|
|
17
|
+
import { spawn } from 'node:child_process';
|
|
18
|
+
import { existsSync } from 'node:fs';
|
|
19
|
+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
20
|
+
import { availableParallelism, tmpdir } from 'node:os';
|
|
21
|
+
import { basename, dirname, join } from 'node:path';
|
|
22
|
+
|
|
23
|
+
const STEP_LIMIT_MS = 10_000;
|
|
24
|
+
const PAGE_LIMIT_MS = 30_000;
|
|
25
|
+
// A tab draws its page on a processor of its own, and a page that waits for one may not finish
|
|
26
|
+
// within its limit. So no more tabs are opened than there are processors.
|
|
27
|
+
const TABS = Math.min(4, availableParallelism());
|
|
28
|
+
|
|
29
|
+
const CHROME_PATHS = [
|
|
30
|
+
process.env.CHROME_PATH,
|
|
31
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
32
|
+
'/usr/bin/google-chrome',
|
|
33
|
+
'/usr/bin/chromium',
|
|
34
|
+
'/usr/bin/chromium-browser',
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
const findChrome = () => CHROME_PATHS.find(path => path && existsSync(path));
|
|
38
|
+
|
|
39
|
+
const extraFlags = () => (process.env.CHROME_FLAGS ?? '').split(/\s+/).filter(Boolean);
|
|
40
|
+
|
|
41
|
+
const within = (ms, what, promise) =>
|
|
42
|
+
Promise.race([
|
|
43
|
+
promise,
|
|
44
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error(`Timed out: ${what}`)), ms)),
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
// This function runs in the page. It waits until every custom element is defined, in the
|
|
48
|
+
// document and in every shadow tree, until the html element has no data-loading, and until
|
|
49
|
+
// every image has loaded or failed. Then it waits for two frames and returns the page as markup.
|
|
50
|
+
// It stops, and says what it waited for, when one of these takes longer than the limit.
|
|
51
|
+
const pageAsMarkup = async (limitMs, isStatic) => {
|
|
52
|
+
const shadowRoots = node =>
|
|
53
|
+
[...node.querySelectorAll('*')]
|
|
54
|
+
.filter(el => el.shadowRoot)
|
|
55
|
+
.flatMap(el => [el.shadowRoot, ...shadowRoots(el.shadowRoot)]);
|
|
56
|
+
const undefinedTags = () => [...new Set(
|
|
57
|
+
[document, ...shadowRoots(document)]
|
|
58
|
+
.flatMap(root => [...root.querySelectorAll(':not(:defined)')])
|
|
59
|
+
.map(el => el.localName))];
|
|
60
|
+
const allDefined = async () => {
|
|
61
|
+
const tags = undefinedTags();
|
|
62
|
+
if (tags.length === 0) return;
|
|
63
|
+
await Promise.all(tags.map(tag => customElements.whenDefined(tag)));
|
|
64
|
+
return allDefined();
|
|
65
|
+
};
|
|
66
|
+
const frame = () => new Promise(done => requestAnimationFrame(done));
|
|
67
|
+
const loading = () => document.documentElement.hasAttribute('data-loading');
|
|
68
|
+
const loaded = async () => {
|
|
69
|
+
while (loading()) await frame();
|
|
70
|
+
};
|
|
71
|
+
const limit = () => new Promise(done => setTimeout(done, limitMs));
|
|
72
|
+
// An image that the browser loads lazily, and that is outside the window, would never load
|
|
73
|
+
// here. It is loaded now, and is lazy again in the markup that is written, also when the wait
|
|
74
|
+
// is cut short.
|
|
75
|
+
const images = () =>
|
|
76
|
+
[document, ...shadowRoots(document)].flatMap(root => [...root.querySelectorAll('img')]);
|
|
77
|
+
const settled = img => new Promise(done => {
|
|
78
|
+
if (img.complete) return done();
|
|
79
|
+
img.addEventListener('load', done, { once: true });
|
|
80
|
+
img.addEventListener('error', done, { once: true });
|
|
81
|
+
});
|
|
82
|
+
const pictured = async () => {
|
|
83
|
+
const lazy = images().filter(img => img.loading === 'lazy' && !img.complete);
|
|
84
|
+
lazy.forEach(img => { img.loading = 'eager'; });
|
|
85
|
+
try {
|
|
86
|
+
await Promise.race([Promise.all(images().map(settled)), limit()]);
|
|
87
|
+
} finally {
|
|
88
|
+
lazy.forEach(img => { img.loading = 'lazy'; });
|
|
89
|
+
}
|
|
90
|
+
return images().filter(img => !img.complete).map(img => img.currentSrc || img.src);
|
|
91
|
+
};
|
|
92
|
+
const makeStatic = () => {
|
|
93
|
+
document.querySelectorAll('script, noscript, link[rel="modulepreload"]').forEach(el => el.remove());
|
|
94
|
+
document.documentElement.setAttribute('data-static', '');
|
|
95
|
+
};
|
|
96
|
+
await Promise.race([allDefined().then(loaded), limit()]);
|
|
97
|
+
const missing = undefinedTags();
|
|
98
|
+
if (missing.length > 0) throw new Error(`Never defined: ${missing.join(', ')}`);
|
|
99
|
+
if (loading()) throw new Error('The page still has data-loading.');
|
|
100
|
+
const unfinished = await pictured();
|
|
101
|
+
if (unfinished.length > 0) throw new Error(`An image never finished loading: ${unfinished.join(', ')}`);
|
|
102
|
+
await frame();
|
|
103
|
+
await frame();
|
|
104
|
+
if (isStatic) makeStatic();
|
|
105
|
+
const htmlTag = document.documentElement.cloneNode(false).outerHTML.replace('</html>', '');
|
|
106
|
+
const inside = document.documentElement.getHTML({ shadowRoots: shadowRoots(document) });
|
|
107
|
+
return `<!doctype html>\n${htmlTag}${inside}</html>\n`;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// Pages are opened in several tabs at once. Chrome slows down a tab that it takes to be in
|
|
111
|
+
// the background, its frames and its timers, and the script waits for frames. So no tab is
|
|
112
|
+
// slowed down, and each tab is told that it has the focus.
|
|
113
|
+
const startChrome = (chromePath, profileDir) =>
|
|
114
|
+
spawn(chromePath, [
|
|
115
|
+
'--headless=new', '--remote-debugging-port=0', `--user-data-dir=${profileDir}`,
|
|
116
|
+
'--no-first-run', '--disable-gpu',
|
|
117
|
+
'--disable-background-timer-throttling', '--disable-backgrounding-occluded-windows',
|
|
118
|
+
'--disable-renderer-backgrounding',
|
|
119
|
+
...extraFlags(), 'about:blank',
|
|
120
|
+
]);
|
|
121
|
+
|
|
122
|
+
const chromeAddress = chrome =>
|
|
123
|
+
new Promise((resolve, reject) => {
|
|
124
|
+
chrome.stderr.on('data', chunk => {
|
|
125
|
+
const found = /DevTools listening on (ws:\/\/\S+)/.exec(String(chunk));
|
|
126
|
+
if (found) resolve(found[1]);
|
|
127
|
+
});
|
|
128
|
+
chrome.once('exit', () => reject(new Error('Chrome stopped before it could be reached.')));
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const stopChrome = async (chrome, profileDir) => {
|
|
132
|
+
const running = chrome.exitCode === null && chrome.signalCode === null;
|
|
133
|
+
const stopped = running ? new Promise(resolve => chrome.once('exit', resolve)) : Promise.resolve();
|
|
134
|
+
chrome.kill();
|
|
135
|
+
await stopped;
|
|
136
|
+
await rm(profileDir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// A connection to Chrome over its debugging protocol: `send` gives the answer to one command,
|
|
140
|
+
// and `next` the next event of one name in one tab. Several tabs share the connection, so an
|
|
141
|
+
// event is matched by the session of its tab as well as by its name.
|
|
142
|
+
const connect = async address => {
|
|
143
|
+
const socket = new WebSocket(address);
|
|
144
|
+
await new Promise((resolve, reject) => {
|
|
145
|
+
socket.onopen = resolve;
|
|
146
|
+
socket.onerror = () => reject(new Error('No connection to Chrome.'));
|
|
147
|
+
});
|
|
148
|
+
const answers = new Map();
|
|
149
|
+
const events = new Map();
|
|
150
|
+
let lastId = 0;
|
|
151
|
+
const settle = (waiting, name, message) => {
|
|
152
|
+
const resolve = waiting.get(name);
|
|
153
|
+
waiting.delete(name);
|
|
154
|
+
resolve?.(message);
|
|
155
|
+
};
|
|
156
|
+
const onAnswer = message => settle(answers, message.id, message);
|
|
157
|
+
const eventKey = (method, sessionId) => `${sessionId ?? ''} ${method}`;
|
|
158
|
+
const onEvent = message => settle(events, eventKey(message.method, message.sessionId), message);
|
|
159
|
+
socket.onmessage = ({ data }) => {
|
|
160
|
+
const message = JSON.parse(data);
|
|
161
|
+
(message.id ? onAnswer : onEvent)(message);
|
|
162
|
+
};
|
|
163
|
+
return {
|
|
164
|
+
send: (method, params = {}, sessionId) =>
|
|
165
|
+
new Promise((resolve, reject) => {
|
|
166
|
+
lastId += 1;
|
|
167
|
+
answers.set(lastId, ({ error, result }) =>
|
|
168
|
+
error ? reject(new Error(`${method}: ${error.message}`)) : resolve(result));
|
|
169
|
+
socket.send(JSON.stringify({ id: lastId, method, params, sessionId }));
|
|
170
|
+
}),
|
|
171
|
+
next: (method, sessionId) => new Promise(resolve => events.set(eventKey(method, sessionId), resolve)),
|
|
172
|
+
close: () => socket.close(),
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const prerender = async (browser, url, isStatic) => {
|
|
177
|
+
const { targetId } = await browser.send('Target.createTarget', { url: 'about:blank' });
|
|
178
|
+
try {
|
|
179
|
+
const { sessionId } = await browser.send('Target.attachToTarget', { targetId, flatten: true });
|
|
180
|
+
await browser.send('Page.enable', {}, sessionId);
|
|
181
|
+
await browser.send('Emulation.setFocusEmulationEnabled', { enabled: true }, sessionId);
|
|
182
|
+
const loaded = browser.next('Page.loadEventFired', sessionId);
|
|
183
|
+
const { errorText } = await browser.send('Page.navigate', { url }, sessionId);
|
|
184
|
+
if (errorText) throw new Error(`Could not open ${url}: ${errorText}`);
|
|
185
|
+
await loaded;
|
|
186
|
+
const { result, exceptionDetails } = await browser.send('Runtime.evaluate', {
|
|
187
|
+
expression: `(${pageAsMarkup})(${STEP_LIMIT_MS}, ${isStatic})`, awaitPromise: true, returnByValue: true,
|
|
188
|
+
}, sessionId);
|
|
189
|
+
if (exceptionDetails) {
|
|
190
|
+
const description = exceptionDetails.exception?.description ?? exceptionDetails.text;
|
|
191
|
+
throw new Error(`${url}: ${description.split('\n')[0]}`);
|
|
192
|
+
}
|
|
193
|
+
return result.value;
|
|
194
|
+
} finally {
|
|
195
|
+
// A tab that cannot be closed must not hide why the page failed.
|
|
196
|
+
await browser.send('Target.closeTarget', { targetId }).catch(() => {});
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const shadowRootCount = markup => markup.split('shadowrootmode').length - 1;
|
|
201
|
+
|
|
202
|
+
const writePage = async (browser, { url, output }, isStatic) => {
|
|
203
|
+
const markup = await within(PAGE_LIMIT_MS, `prerendering ${url}`, prerender(browser, url, isStatic));
|
|
204
|
+
await mkdir(dirname(output), { recursive: true });
|
|
205
|
+
await writeFile(output, markup);
|
|
206
|
+
console.log(`${output}: ${markup.length} characters, ${shadowRootCount(markup)} shadow roots`);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// The error of a run in which the pages of `failures` failed: the first, and how many more.
|
|
210
|
+
const failed = failures =>
|
|
211
|
+
failures.length === 1
|
|
212
|
+
? failures[0]
|
|
213
|
+
: new Error(`${failures[0].message}\n${failures.length - 1} more of the pages that were open failed too.`);
|
|
214
|
+
|
|
215
|
+
// Each page is opened in a tab of its own, and written as soon as it is read. `tabs` pages are
|
|
216
|
+
// open at once: each tab takes the next page of the list when it is done with one, so the pages
|
|
217
|
+
// are written in the order in which they are done. A page that fails stops the run: no tab
|
|
218
|
+
// takes another page, the pages that are open are written, and the run ends with the first
|
|
219
|
+
// error, and says how many more failed.
|
|
220
|
+
const writePages = async (browser, pages, isStatic, tabs) => {
|
|
221
|
+
let next = 0;
|
|
222
|
+
const failures = [];
|
|
223
|
+
const work = async () => {
|
|
224
|
+
while (failures.length === 0 && next < pages.length) {
|
|
225
|
+
const page = pages[next];
|
|
226
|
+
next += 1;
|
|
227
|
+
try {
|
|
228
|
+
await writePage(browser, page, isStatic);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
failures.push(error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
await Promise.all(Array.from({ length: Math.min(tabs, pages.length) }, work));
|
|
235
|
+
if (failures.length > 0) throw failed(failures);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const STATIC_FLAG = '--static';
|
|
239
|
+
const TABS_FLAG = '--tabs';
|
|
240
|
+
|
|
241
|
+
// The script is run by its file, or as the command of the package. It names itself as it was run.
|
|
242
|
+
const USAGE = `Usage: ${basename(process.argv[1] ?? 'prerender.mjs')} [--static] [--tabs <n>] <url> <output.html> [<url> <output.html> ...]`;
|
|
243
|
+
|
|
244
|
+
// The flags, and the arguments that are left: `--static`, and `--tabs` with the number that
|
|
245
|
+
// follows it.
|
|
246
|
+
const optionsOf = args => {
|
|
247
|
+
const at = args.indexOf(TABS_FLAG);
|
|
248
|
+
const tabs = at === -1 ? TABS : Number(args[at + 1]);
|
|
249
|
+
if (!Number.isInteger(tabs) || tabs < 1) throw new Error(`${TABS_FLAG} takes a whole number of 1 or more.\n${USAGE}`);
|
|
250
|
+
const rest = at === -1 ? args : [...args.slice(0, at), ...args.slice(at + 2)];
|
|
251
|
+
return { isStatic: rest.includes(STATIC_FLAG), tabs, rest: rest.filter(arg => arg !== STATIC_FLAG) };
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// The arguments after the flag are pairs: a url, and the file it is written to.
|
|
255
|
+
const pagesOf = args => {
|
|
256
|
+
if (args.length === 0 || args.length % 2 !== 0) throw new Error(USAGE);
|
|
257
|
+
return Array.from({ length: args.length / 2 }, (_, i) => ({ url: args[2 * i], output: args[2 * i + 1] }));
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const main = async args => {
|
|
261
|
+
const { isStatic, tabs, rest } = optionsOf(args);
|
|
262
|
+
const pages = pagesOf(rest);
|
|
263
|
+
const chromePath = findChrome();
|
|
264
|
+
if (!chromePath) throw new Error('Chrome was not found. Set CHROME_PATH.');
|
|
265
|
+
const profileDir = await mkdtemp(join(tmpdir(), 'prerender-'));
|
|
266
|
+
const chrome = startChrome(chromePath, profileDir);
|
|
267
|
+
try {
|
|
268
|
+
const browser = await connect(await chromeAddress(chrome));
|
|
269
|
+
try {
|
|
270
|
+
await writePages(browser, pages, isStatic, tabs);
|
|
271
|
+
} finally {
|
|
272
|
+
browser.close();
|
|
273
|
+
}
|
|
274
|
+
} finally {
|
|
275
|
+
await stopChrome(chrome, profileDir);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
main(process.argv.slice(2)).then(
|
|
280
|
+
() => process.exit(0),
|
|
281
|
+
error => {
|
|
282
|
+
console.error(error.message);
|
|
283
|
+
process.exit(1);
|
|
284
|
+
});
|