Summary
Importing @oddbird/css-anchor-positioning (e.g. via esm.sh) auto-invokes polyfill() from src/index.ts. In environments that have window/document but do not expose a global ShadowRoot (incomplete DOM shims, some test harnesses), that call rejects with:
ReferenceError: ShadowRoot is not defined
if (root instanceof ShadowRoot) {
at src/dom.ts (querySelectorAllRoots).
Why this is hard to catch for consumers
index.ts calls polyfill() without awaiting or .catch()ing the returned promise, so the failure surfaces as an unhandled rejection, not as a failed import().
- Wrapping the dynamic import in
.catch() therefore does not protect callers — the module evaluates successfully, then the async polyfill blows up later.
Repro sketch
// Any host with document but no global ShadowRoot
globalThis.document = /* minimal document */;
// globalThis.ShadowRoot intentionally unset
await import('@oddbird/css-anchor-positioning');
// → unhandled rejection: ReferenceError: ShadowRoot is not defined
Seen with @oddbird/css-anchor-positioning@0.10.1 under Deno + a partial happy-dom global install (document present, ShadowRoot not copied onto globalThis).
Suggested fixes
- Guard the check, e.g.
typeof ShadowRoot !== 'undefined' && root instanceof ShadowRoot.
- Catch / await the auto-
polyfill() promise in index.ts so a missing DOM feature does not become an unhandled rejection on mere import.
- Optionally skip auto-apply when required DOM APIs (
ShadowRoot, etc.) are absent.
Happy to test a PR if useful.
Summary
Importing
@oddbird/css-anchor-positioning(e.g. viaesm.sh) auto-invokespolyfill()fromsrc/index.ts. In environments that havewindow/documentbut do not expose a globalShadowRoot(incomplete DOM shims, some test harnesses), that call rejects with:at
src/dom.ts(querySelectorAllRoots).Why this is hard to catch for consumers
index.tscallspolyfill()without awaiting or.catch()ing the returned promise, so the failure surfaces as an unhandled rejection, not as a failedimport()..catch()therefore does not protect callers — the module evaluates successfully, then the async polyfill blows up later.Repro sketch
Seen with
@oddbird/css-anchor-positioning@0.10.1under Deno + a partial happy-dom global install (document present,ShadowRootnot copied ontoglobalThis).Suggested fixes
typeof ShadowRoot !== 'undefined' && root instanceof ShadowRoot.polyfill()promise inindex.tsso a missing DOM feature does not become an unhandled rejection on mere import.ShadowRoot, etc.) are absent.Happy to test a PR if useful.