@chr33s/solarflare 0.0.7 → 0.0.10

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/package.json CHANGED
@@ -1,16 +1,15 @@
1
1
  {
2
2
  "name": "@chr33s/solarflare",
3
- "version": "0.0.7",
3
+ "version": "0.0.10",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/chr33s/solarflare"
8
8
  },
9
9
  "bin": {
10
- "solarflare": "bin/solarflare"
10
+ "solarflare": "./src/build.ts"
11
11
  },
12
12
  "files": [
13
- "bin",
14
13
  "src",
15
14
  "tsconfig.json",
16
15
  "!src/*.test.ts"
@@ -40,6 +39,7 @@
40
39
  "preact-custom-element": "4.6.0",
41
40
  "preact-render-to-string": "6.6.6",
42
41
  "rolldown": "1.0.0-rc.5",
42
+ "tsx": "4.21.0",
43
43
  "turbo-stream": "3.1.0"
44
44
  },
45
45
  "devDependencies": {
package/src/build.ts CHANGED
@@ -1,3 +1,4 @@
1
+ #!/usr/bin/env -S npx tsx
1
2
  import { spawn, type ChildProcess } from "node:child_process";
2
3
  import { watch } from "node:fs";
3
4
  import { writeFile } from "node:fs/promises";
@@ -1,4 +1,8 @@
1
- import { stylesheets, supportsConstructableStylesheets } from "./stylesheets.ts";
1
+ import {
2
+ safeAdoptStylesheets,
3
+ stylesheets,
4
+ supportsConstructableStylesheets,
5
+ } from "./stylesheets.ts";
2
6
 
3
7
  /** Style loading state for a component. */
4
8
  interface StyleState {
@@ -51,12 +55,11 @@ export function applyStyles(element: HTMLElement, sheets: CSSStyleSheet[]) {
51
55
  const shadowRoot = element.shadowRoot;
52
56
 
53
57
  if (shadowRoot) {
54
- shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, ...sheets];
58
+ safeAdoptStylesheets(shadowRoot, [...shadowRoot.adoptedStyleSheets, ...sheets]);
55
59
  } else {
56
- for (const sheet of sheets) {
57
- if (!document.adoptedStyleSheets.includes(sheet)) {
58
- document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
59
- }
60
+ const toAdd = sheets.filter((s) => !document.adoptedStyleSheets.includes(s));
61
+ if (toAdd.length) {
62
+ safeAdoptStylesheets(document, [...document.adoptedStyleSheets, ...toAdd]);
60
63
  }
61
64
  }
62
65
  }
package/src/client.ts CHANGED
@@ -3,7 +3,11 @@ import { parsePath } from "./paths.ts";
3
3
  import { hydrateStore, initHydrationCoordinator } from "./hydration.ts";
4
4
  import { installHeadHoisting, createHeadContext, setHeadContext } from "./head.ts";
5
5
  import { getRuntime } from "./runtime.ts";
6
- import { stylesheets, supportsConstructableStylesheets } from "./stylesheets.ts";
6
+ import {
7
+ safeAdoptStylesheets,
8
+ stylesheets,
9
+ supportsConstructableStylesheets,
10
+ } from "./stylesheets.ts";
7
11
  import { getPreloadedStylesheet } from "./server.styles.ts";
8
12
 
9
13
  export { initHmrEntry, reloadAllStylesheets } from "./hmr.ts";
@@ -51,15 +55,15 @@ export function registerInlineStyles(tag: string, styles: InlineStyleEntry[]) {
51
55
  const sheets = stylesheets.getForConsumer(tag);
52
56
  const shadowRoot = el?.shadowRoot;
53
57
  if (shadowRoot) {
54
- shadowRoot.adoptedStyleSheets = [
58
+ safeAdoptStylesheets(shadowRoot, [
55
59
  ...shadowRoot.adoptedStyleSheets.filter((s) => !sheets.includes(s)),
56
60
  ...sheets,
57
- ];
61
+ ]);
58
62
  } else {
59
- document.adoptedStyleSheets = [
63
+ safeAdoptStylesheets(document, [
60
64
  ...document.adoptedStyleSheets.filter((s) => !sheets.includes(s)),
61
65
  ...sheets,
62
- ];
66
+ ]);
63
67
  }
64
68
  }
65
69
 
@@ -9,6 +9,29 @@ export const supportsConstructableStylesheets = () => {
9
9
  }
10
10
  };
11
11
 
12
+ /** Recreates a stylesheet in the current document context. */
13
+ function cloneSheet(sheet: CSSStyleSheet): CSSStyleSheet {
14
+ const fresh = new CSSStyleSheet();
15
+ fresh.replaceSync([...sheet.cssRules].map((r) => r.cssText).join(""));
16
+ return fresh;
17
+ }
18
+
19
+ /** Safely adopts stylesheets, recreating them if cross-document sharing fails. */
20
+ export function safeAdoptStylesheets(
21
+ target: Document | ShadowRoot,
22
+ sheets: CSSStyleSheet[],
23
+ ): CSSStyleSheet[] {
24
+ try {
25
+ target.adoptedStyleSheets = sheets;
26
+ return sheets;
27
+ } catch {
28
+ // Stylesheet was created in a different document context - recreate
29
+ const cloned = sheets.map(cloneSheet);
30
+ target.adoptedStyleSheets = cloned;
31
+ return cloned;
32
+ }
33
+ }
34
+
12
35
  /** Stylesheet entry with metadata. */
13
36
  interface StylesheetEntry {
14
37
  sheet: CSSStyleSheet;
@@ -143,7 +166,7 @@ class StylesheetManager {
143
166
  // Include global sheets
144
167
  const globalSheets = [...this.#sheets.values()].filter((e) => e.isGlobal).map((e) => e.sheet);
145
168
 
146
- shadowRoot.adoptedStyleSheets = [...globalSheets, ...sheets];
169
+ safeAdoptStylesheets(shadowRoot, [...globalSheets, ...sheets]);
147
170
  }
148
171
 
149
172
  /** Removes a consumer from all its stylesheets. */
@@ -171,7 +194,7 @@ class StylesheetManager {
171
194
  #adoptToDocument(sheet: CSSStyleSheet) {
172
195
  if (!this.#documentSheets.includes(sheet)) {
173
196
  this.#documentSheets.push(sheet);
174
- document.adoptedStyleSheets = [...this.#documentSheets];
197
+ safeAdoptStylesheets(document, [...this.#documentSheets]);
175
198
  }
176
199
  }
177
200
 
package/bin/solarflare DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env bash
2
- SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
3
- exec node --experimental-transform-types "$SCRIPT_DIR/../src/build.ts" "$@"