@skra/shadow-dom-bridge 0.4.0 → 0.6.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.
@@ -1 +1 @@
1
- {"version":3,"file":"skra-shadow-dom-bridge.mjs","sources":["../../../../libs/shadow-dom-bridge/src/lib/register-skra-styles.ts","../../../../libs/shadow-dom-bridge/src/index.ts","../../../../libs/shadow-dom-bridge/src/skra-shadow-dom-bridge.ts"],"sourcesContent":["/**\r\n * Forge Shadow DOM style bridge — O-8 / ADR 012 §5 / ADR 042 §3.4.\r\n * ============================================================================\r\n * PROBLEM. Forge components use Angular `ViewEncapsulation.Emulated` (ADR 001):\r\n * their CSS is injected as `<style>` blocks into the document `<head>`, scoped by\r\n * generated `[_nghost-*]` / `[_ngcontent-*]` attributes. Style rules in document\r\n * scope do NOT cross into a shadow root — so when a consumer renders Forge inside\r\n * their own shadow root (the microfrontend isolation pattern), the elements carry\r\n * the right attributes but the matching stylesheet lives in a `<head>` the shadow\r\n * root cannot see. Every `:host` / `:host([data-skra-*])` / slot / state rule\r\n * silently no-ops: the library reads the right token VALUES (custom properties DO\r\n * inherit across the boundary) but cannot paint the right state.\r\n *\r\n * FIX. `registerSkraStyles(shadowRoot)` adopts Skra's STRUCTURAL stylesheets into\r\n * the given shadow root (via the shared-`CSSStyleSheet` `adoptedStyleSheets` API,\r\n * with a `<style>`-injection fallback), so the selector-bearing rules apply inside.\r\n *\r\n * VALUE/STRUCTURE SPLIT (ADR 042 §3.4). This bridge delivers ONLY the structural\r\n * layer — per-component Emulated styles (incl. the variant matrix), `_baseline.scss`,\r\n * `_form-controls.scss`, utilities. It deliberately SKIPS the token VALUE layers\r\n * (`_theme.scss` ref+sys, `_comp.scss` comp defaults, overlays): those inherit across\r\n * the boundary, and re-declaring them inside a shadow root would FREEZE shell-driven\r\n * morph (an own-declaration inside the root shadows the inherited value from the\r\n * shell). A \"value sheet\" is detected structurally: every declaration it carries is a\r\n * custom property. See `collectSkraStructuralCss`.\r\n *\r\n * LIMITATIONS (documented, not hidden):\r\n * - Forge sheets are identified by a text marker (default /skra/i). A consumer\r\n * stylesheet that happens to mention \"skra\" would be bridged too; pass a\r\n * stricter `marker`, or call after Forge styles are present and rely on the\r\n * value/structure split which already excludes token layers.\r\n * - Cross-origin stylesheets throw on `.cssRules`; they are skipped (Forge styles\r\n * are same-origin: injected `<style>` or same-origin slices).\r\n * - Component styles are injected lazily by Angular on first render. Call this\r\n * AFTER the Forge content inside the shadow root has rendered (or pass\r\n * `{ refresh: true }` to rebuild the cached aggregate).\r\n * - Safari verification is pending (no macOS in the authoring environment). The\r\n * mechanism is spec-standard (`adoptedStyleSheets`, Safari >=16.4) and the\r\n * `<style>` fallback covers engines without constructable stylesheets.\r\n */\r\n\r\nexport interface RegisterSkraStylesOptions {\r\n /** Identifies which document stylesheets are Forge's. Default: `/skra/i`. */\r\n marker?: RegExp;\r\n /** Allow `<style>` injection when constructable stylesheets are unavailable. Default: `true`. */\r\n fallback?: boolean;\r\n /** Rebuild the cached aggregated CSS (e.g. after more Forge styles were injected). Default: `false`. */\r\n refresh?: boolean;\r\n}\r\n\r\nconst DEFAULT_MARKER = /skra/i;\r\n\r\n/** Marks the fallback `<style>` element the bridge injects, so it can be removed. */\r\nconst FALLBACK_ATTR = 'data-skra-bridge';\r\n\r\nlet cachedCss: string | null = null;\r\nlet cachedSheet: CSSStyleSheet | null = null;\r\n\r\n// ---------------------------------------------------------------------------\r\n// Pure core (engine-agnostic, unit-testable without a real DOM)\r\n// ---------------------------------------------------------------------------\r\n\r\n/**\r\n * True if a CSS rule declares at least one NON-custom property — i.e. a real\r\n * structural declaration (`background`, `padding`, …), not just `--token: …`.\r\n * Recurses into grouping rules (`@media` / `@supports`); other at-rules\r\n * (`@keyframes`, `@font-face`) count as structural.\r\n */\r\nexport function ruleIsStructural(rule: CSSRule): boolean {\r\n const style = (rule as CSSStyleRule).style as CSSStyleDeclaration | undefined;\r\n if (style && typeof style.length === 'number') {\r\n for (let i = 0; i < style.length; i++) {\r\n const prop = typeof style.item === 'function' ? style.item(i) : '';\r\n if (prop && !prop.startsWith('--')) return true;\r\n }\r\n return false; // declares only custom properties → a value rule\r\n }\r\n const nested = (rule as CSSGroupingRule).cssRules;\r\n if (nested && nested.length) {\r\n return Array.from(nested).some(ruleIsStructural);\r\n }\r\n // At-rule with neither `.style` nor nested rules — treat as structural.\r\n return true;\r\n}\r\n\r\n/**\r\n * Selectors that scope the token VALUE layers (ADR 002/042): the skin root\r\n * (`:root`, `:where(:root, :host)`), overlay presets (`[data-skra-overlay]`),\r\n * density (`[data-skra-density]`) and colour-scheme (`[data-skra-color-scheme]`).\r\n * A custom-property-only rule with such a selector is a value layer and MUST NOT be\r\n * bridged — it inherits across the boundary, and re-declaring it inside a shadow\r\n * root would freeze shell-driven morph (ADR 042 §3.4).\r\n */\r\nconst VALUE_LAYER_SELECTOR =\r\n /(?:^|[\\s,(]):root\\b|\\bhtml\\b|\\[data-skra-(?:overlay|density|color-scheme)/;\r\n\r\n/**\r\n * True if a rule is a token VALUE-LAYER rule (skip when bridging): it declares ONLY\r\n * custom properties AND is scoped to a skin-root/overlay/density/colour-scheme\r\n * context. Crucially this does NOT match the variant matrix\r\n * (`:host([data-skra-intent='…'])`), which also declares only custom properties but\r\n * is a per-component re-bind that MUST apply inside the shadow root — its selector is\r\n * host+attribute, not a root/overlay context.\r\n */\r\nexport function isTokenValueLayerRule(rule: CSSRule): boolean {\r\n if (ruleIsStructural(rule)) return false; // declares a real property → not a value layer\r\n const selector = (rule as CSSStyleRule).selectorText;\r\n if (selector == null) return true; // non-structural grouping/at-rule (e.g. dark-scheme value block)\r\n return VALUE_LAYER_SELECTOR.test(selector);\r\n}\r\n\r\n/**\r\n * Aggregate the CSS text of every Forge STRUCTURAL rule in `styleSheets`, at RULE\r\n * granularity. Skips (a) non-Forge sheets (no `marker` match anywhere in the sheet)\r\n * and (b) token VALUE-LAYER rules (`isTokenValueLayerRule`) — including when value\r\n * layers and structural slices share one compiled stylesheet (the common case: an\r\n * app's global `styles.scss` `@import`s theme + comp + overlays + slices into a single\r\n * sheet). Pure: operates on any `CSSStyleSheet`-like list; cross-origin sheets (whose\r\n * `.cssRules` throws) are skipped.\r\n */\r\nexport function collectSkraStructuralCss(\r\n styleSheets: Iterable<CSSStyleSheet>,\r\n marker: RegExp = DEFAULT_MARKER,\r\n): string {\r\n const chunks: string[] = [];\r\n for (const sheet of styleSheets) {\r\n let rules: CSSRule[];\r\n try {\r\n rules = Array.from(sheet.cssRules ?? []);\r\n } catch {\r\n continue; // cross-origin — cannot read; skip\r\n }\r\n if (rules.length === 0) continue;\r\n const sheetText = rules.map((r) => r.cssText).join('\\n');\r\n if (!marker.test(sheetText)) continue; // not a Forge sheet\r\n for (const rule of rules) {\r\n if (isTokenValueLayerRule(rule)) continue; // token value layer → inherits; don't bridge\r\n chunks.push(rule.cssText);\r\n }\r\n }\r\n return chunks.join('\\n\\n');\r\n}\r\n\r\n// ---------------------------------------------------------------------------\r\n// Imperative shell (DOM side effects)\r\n// ---------------------------------------------------------------------------\r\n\r\nfunction documentStyleSheets(): CSSStyleSheet[] {\r\n const linked = Array.from(document.styleSheets);\r\n const adopted = (document.adoptedStyleSheets ?? []) as CSSStyleSheet[];\r\n return [...linked, ...adopted];\r\n}\r\n\r\nfunction supportsConstructableStyleSheets(shadowRoot: ShadowRoot): boolean {\r\n return (\r\n typeof CSSStyleSheet !== 'undefined' &&\r\n typeof CSSStyleSheet.prototype.replaceSync === 'function' &&\r\n 'adoptedStyleSheets' in shadowRoot\r\n );\r\n}\r\n\r\n/**\r\n * Bridge Forge's structural CSS into `shadowRoot` so Emulated component rules\r\n * (and the slices) apply inside it. Idempotent per shadow root. See file header.\r\n */\r\nexport function registerSkraStyles(\r\n shadowRoot: ShadowRoot,\r\n options: RegisterSkraStylesOptions = {},\r\n): void {\r\n if (!shadowRoot) return;\r\n const { marker = DEFAULT_MARKER, fallback = true, refresh = false } = options;\r\n\r\n if (refresh) {\r\n cachedCss = null;\r\n cachedSheet = null;\r\n }\r\n if (cachedCss === null) {\r\n cachedCss = collectSkraStructuralCss(documentStyleSheets(), marker);\r\n }\r\n if (!cachedCss) return; // no Forge structural styles found yet\r\n\r\n if (supportsConstructableStyleSheets(shadowRoot)) {\r\n if (!cachedSheet) {\r\n cachedSheet = new CSSStyleSheet();\r\n cachedSheet.replaceSync(cachedCss);\r\n }\r\n if (!shadowRoot.adoptedStyleSheets.includes(cachedSheet)) {\r\n shadowRoot.adoptedStyleSheets = [\r\n ...shadowRoot.adoptedStyleSheets,\r\n cachedSheet,\r\n ];\r\n }\r\n return;\r\n }\r\n\r\n if (fallback && !shadowRoot.querySelector(`style[${FALLBACK_ATTR}]`)) {\r\n const style = document.createElement('style');\r\n style.setAttribute(FALLBACK_ATTR, '');\r\n style.textContent = cachedCss;\r\n shadowRoot.appendChild(style);\r\n }\r\n}\r\n\r\n/**\r\n * Remove a previously-registered Forge bridge from `shadowRoot` (both the adopted\r\n * sheet and any fallback `<style>`). Mainly for demos/tests that toggle the bridge.\r\n */\r\nexport function unregisterSkraStyles(shadowRoot: ShadowRoot): void {\r\n if (!shadowRoot) return;\r\n if (cachedSheet && 'adoptedStyleSheets' in shadowRoot) {\r\n shadowRoot.adoptedStyleSheets = shadowRoot.adoptedStyleSheets.filter(\r\n (s) => s !== cachedSheet,\r\n );\r\n }\r\n shadowRoot\r\n .querySelectorAll(`style[${FALLBACK_ATTR}]`)\r\n .forEach((el) => el.remove());\r\n}\r\n","// Public API for @skra/shadow-dom-bridge (O-8 · ADR 012 §5 · ADR 042 §3.4)\r\nexport * from './lib/register-skra-styles';\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCG;AAWH,MAAM,cAAc,GAAG,OAAO;AAE9B;AACA,MAAM,aAAa,GAAG,kBAAkB;AAExC,IAAI,SAAS,GAAkB,IAA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AAG,UAAU,CAAC,kBAAkB,CAAC,MAAM,CAClE,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW,CACzB;IACH;IACA;AACG,SAAA,gBAAgB,CAAC,CAAA,MAAA,EAAS,aAAa,CAAA,CAAA,CAAG;SAC1C,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,MAAM,EAAE,CAAC;AACjC;;ACzNA;;ACAA;;AAEG;;;;"}
1
+ {"version":3,"file":"skra-shadow-dom-bridge.mjs","sources":["../../../../libs/shadow-dom-bridge/src/lib/register-skra-styles.ts","../../../../libs/shadow-dom-bridge/src/index.ts","../../../../libs/shadow-dom-bridge/src/skra-shadow-dom-bridge.ts"],"sourcesContent":["/**\n * Forge Shadow DOM style bridge — O-8 / ADR 012 §5 / ADR 042 §3.4.\n * ============================================================================\n * PROBLEM. Forge components use Angular `ViewEncapsulation.Emulated` (ADR 001):\n * their CSS is injected as `<style>` blocks into the document `<head>`, scoped by\n * generated `[_nghost-*]` / `[_ngcontent-*]` attributes. Style rules in document\n * scope do NOT cross into a shadow root — so when a consumer renders Forge inside\n * their own shadow root (the microfrontend isolation pattern), the elements carry\n * the right attributes but the matching stylesheet lives in a `<head>` the shadow\n * root cannot see. Every `:host` / `:host([data-skra-*])` / slot / state rule\n * silently no-ops: the library reads the right token VALUES (custom properties DO\n * inherit across the boundary) but cannot paint the right state.\n *\n * FIX. `registerSkraStyles(shadowRoot)` adopts Skra's STRUCTURAL stylesheets into\n * the given shadow root (via the shared-`CSSStyleSheet` `adoptedStyleSheets` API,\n * with a `<style>`-injection fallback), so the selector-bearing rules apply inside.\n *\n * VALUE/STRUCTURE SPLIT (ADR 042 §3.4). This bridge delivers ONLY the structural\n * layer — per-component Emulated styles (incl. the variant matrix), `_baseline.scss`,\n * `_form-controls.scss`, utilities. It deliberately SKIPS the token VALUE layers\n * (`_theme.scss` ref+sys, `_comp.scss` comp defaults, overlays): those inherit across\n * the boundary, and re-declaring them inside a shadow root would FREEZE shell-driven\n * morph (an own-declaration inside the root shadows the inherited value from the\n * shell). A \"value sheet\" is detected structurally: every declaration it carries is a\n * custom property. See `collectSkraStructuralCss`.\n *\n * LIMITATIONS (documented, not hidden):\n * - Forge sheets are identified by a text marker (default /skra/i). A consumer\n * stylesheet that happens to mention \"skra\" would be bridged too; pass a\n * stricter `marker`, or call after Forge styles are present and rely on the\n * value/structure split which already excludes token layers.\n * - Cross-origin stylesheets throw on `.cssRules`; they are skipped (Forge styles\n * are same-origin: injected `<style>` or same-origin slices).\n * - Component styles are injected lazily by Angular on first render. Call this\n * AFTER the Forge content inside the shadow root has rendered (or pass\n * `{ refresh: true }` to rebuild the cached aggregate).\n * - Safari verification is pending (no macOS in the authoring environment). The\n * mechanism is spec-standard (`adoptedStyleSheets`, Safari >=16.4) and the\n * `<style>` fallback covers engines without constructable stylesheets.\n */\n\nexport interface RegisterSkraStylesOptions {\n /** Identifies which document stylesheets are Forge's. Default: `/skra/i`. */\n marker?: RegExp;\n /** Allow `<style>` injection when constructable stylesheets are unavailable. Default: `true`. */\n fallback?: boolean;\n /** Rebuild the cached aggregated CSS (e.g. after more Forge styles were injected). Default: `false`. */\n refresh?: boolean;\n}\n\nconst DEFAULT_MARKER = /skra/i;\n\n/** Marks the fallback `<style>` element the bridge injects, so it can be removed. */\nconst FALLBACK_ATTR = 'data-skra-bridge';\n\nlet cachedCss: string | null = null;\nlet cachedSheet: CSSStyleSheet | null = null;\n\n// ---------------------------------------------------------------------------\n// Pure core (engine-agnostic, unit-testable without a real DOM)\n// ---------------------------------------------------------------------------\n\n/**\n * True if a CSS rule declares at least one NON-custom property — i.e. a real\n * structural declaration (`background`, `padding`, …), not just `--token: …`.\n * Recurses into grouping rules (`@media` / `@supports`); other at-rules\n * (`@keyframes`, `@font-face`) count as structural.\n */\nexport function ruleIsStructural(rule: CSSRule): boolean {\n const style = (rule as CSSStyleRule).style as CSSStyleDeclaration | undefined;\n if (style && typeof style.length === 'number') {\n for (let i = 0; i < style.length; i++) {\n const prop = typeof style.item === 'function' ? style.item(i) : '';\n if (prop && !prop.startsWith('--')) return true;\n }\n return false; // declares only custom properties → a value rule\n }\n const nested = (rule as CSSGroupingRule).cssRules;\n if (nested && nested.length) {\n return Array.from(nested).some(ruleIsStructural);\n }\n // At-rule with neither `.style` nor nested rules — treat as structural.\n return true;\n}\n\n/**\n * Selectors that scope the token VALUE layers (ADR 002/042): the skin root\n * (`:root`, `:where(:root, :host)`), overlay presets (`[data-skra-overlay]`),\n * density (`[data-skra-density]`) and colour-scheme (`[data-skra-color-scheme]`).\n * A custom-property-only rule with such a selector is a value layer and MUST NOT be\n * bridged — it inherits across the boundary, and re-declaring it inside a shadow\n * root would freeze shell-driven morph (ADR 042 §3.4).\n */\nconst VALUE_LAYER_SELECTOR =\n /(?:^|[\\s,(]):root\\b|\\bhtml\\b|\\[data-skra-(?:overlay|density|color-scheme)/;\n\n/**\n * True if a rule is a token VALUE-LAYER rule (skip when bridging): it declares ONLY\n * custom properties AND is scoped to a skin-root/overlay/density/colour-scheme\n * context. Crucially this does NOT match the variant matrix\n * (`:host([data-skra-intent='…'])`), which also declares only custom properties but\n * is a per-component re-bind that MUST apply inside the shadow root — its selector is\n * host+attribute, not a root/overlay context.\n */\nexport function isTokenValueLayerRule(rule: CSSRule): boolean {\n if (ruleIsStructural(rule)) return false; // declares a real property → not a value layer\n const selector = (rule as CSSStyleRule).selectorText;\n if (selector == null) return true; // non-structural grouping/at-rule (e.g. dark-scheme value block)\n return VALUE_LAYER_SELECTOR.test(selector);\n}\n\n/**\n * Aggregate the CSS text of every Forge STRUCTURAL rule in `styleSheets`, at RULE\n * granularity. Skips (a) non-Forge sheets (no `marker` match anywhere in the sheet)\n * and (b) token VALUE-LAYER rules (`isTokenValueLayerRule`) — including when value\n * layers and structural slices share one compiled stylesheet (the common case: an\n * app's global `styles.scss` `@import`s theme + comp + overlays + slices into a single\n * sheet). Pure: operates on any `CSSStyleSheet`-like list; cross-origin sheets (whose\n * `.cssRules` throws) are skipped.\n */\nexport function collectSkraStructuralCss(\n styleSheets: Iterable<CSSStyleSheet>,\n marker: RegExp = DEFAULT_MARKER,\n): string {\n const chunks: string[] = [];\n for (const sheet of styleSheets) {\n let rules: CSSRule[];\n try {\n rules = Array.from(sheet.cssRules ?? []);\n } catch {\n continue; // cross-origin — cannot read; skip\n }\n if (rules.length === 0) continue;\n const sheetText = rules.map((r) => r.cssText).join('\\n');\n if (!marker.test(sheetText)) continue; // not a Forge sheet\n for (const rule of rules) {\n if (isTokenValueLayerRule(rule)) continue; // token value layer → inherits; don't bridge\n chunks.push(rule.cssText);\n }\n }\n return chunks.join('\\n\\n');\n}\n\n// ---------------------------------------------------------------------------\n// Imperative shell (DOM side effects)\n// ---------------------------------------------------------------------------\n\nfunction documentStyleSheets(): CSSStyleSheet[] {\n const linked = Array.from(document.styleSheets);\n const adopted = (document.adoptedStyleSheets ?? []) as CSSStyleSheet[];\n return [...linked, ...adopted];\n}\n\nfunction supportsConstructableStyleSheets(shadowRoot: ShadowRoot): boolean {\n return (\n typeof CSSStyleSheet !== 'undefined' &&\n typeof CSSStyleSheet.prototype.replaceSync === 'function' &&\n 'adoptedStyleSheets' in shadowRoot\n );\n}\n\n/**\n * Bridge Forge's structural CSS into `shadowRoot` so Emulated component rules\n * (and the slices) apply inside it. Idempotent per shadow root. See file header.\n */\nexport function registerSkraStyles(\n shadowRoot: ShadowRoot,\n options: RegisterSkraStylesOptions = {},\n): void {\n if (!shadowRoot) return;\n const { marker = DEFAULT_MARKER, fallback = true, refresh = false } = options;\n\n if (refresh) {\n cachedCss = null;\n cachedSheet = null;\n }\n if (cachedCss === null) {\n cachedCss = collectSkraStructuralCss(documentStyleSheets(), marker);\n }\n if (!cachedCss) return; // no Forge structural styles found yet\n\n if (supportsConstructableStyleSheets(shadowRoot)) {\n if (!cachedSheet) {\n cachedSheet = new CSSStyleSheet();\n cachedSheet.replaceSync(cachedCss);\n }\n if (!shadowRoot.adoptedStyleSheets.includes(cachedSheet)) {\n shadowRoot.adoptedStyleSheets = [\n ...shadowRoot.adoptedStyleSheets,\n cachedSheet,\n ];\n }\n return;\n }\n\n if (fallback && !shadowRoot.querySelector(`style[${FALLBACK_ATTR}]`)) {\n const style = document.createElement('style');\n style.setAttribute(FALLBACK_ATTR, '');\n style.textContent = cachedCss;\n shadowRoot.appendChild(style);\n }\n}\n\n/**\n * Remove a previously-registered Forge bridge from `shadowRoot` (both the adopted\n * sheet and any fallback `<style>`). Mainly for demos/tests that toggle the bridge.\n */\nexport function unregisterSkraStyles(shadowRoot: ShadowRoot): void {\n if (!shadowRoot) return;\n if (cachedSheet && 'adoptedStyleSheets' in shadowRoot) {\n shadowRoot.adoptedStyleSheets = shadowRoot.adoptedStyleSheets.filter(\n (s) => s !== cachedSheet,\n );\n }\n shadowRoot\n .querySelectorAll(`style[${FALLBACK_ATTR}]`)\n .forEach((el) => el.remove());\n}\n","// Public API for @skra/shadow-dom-bridge (O-8 · ADR 012 §5 · ADR 042 §3.4)\nexport * from './lib/register-skra-styles';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCG;AAWH,MAAM,cAAc,GAAG,OAAO;AAE9B;AACA,MAAM,aAAa,GAAG,kBAAkB;AAExC,IAAI,SAAS,GAAkB,IAAI;AACnC,IAAI,WAAW,GAAyB,IAAI;AAE5C;AACA;AACA;AAEA;;;;;AAKG;AACG,SAAU,gBAAgB,CAAC,IAAa,EAAA;AAC5C,IAAA,MAAM,KAAK,GAAI,IAAqB,CAAC,KAAwC;IAC7E,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,QAAQ,EAAE;AAC7C,QAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YACrC,MAAM,IAAI,GAAG,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU,GAAG,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
5
  "publishConfig": {
6
6
  "access": "public"
@@ -21,4 +21,4 @@
21
21
  "dependencies": {
22
22
  "tslib": "^2.3.0"
23
23
  }
24
- }
24
+ }