fix: make module importable without a DOM (SSR / pure calculate())#37
Open
dmchaledev wants to merge 1 commit into
Open
fix: make module importable without a DOM (SSR / pure calculate())#37dmchaledev wants to merge 1 commit into
dmchaledev wants to merge 1 commit into
Conversation
The README advertises `import { calculate }` as "the pure DOM-free
calculator" and index.d.ts documents it as "no DOM required", but the
module crashed on import in any non-DOM environment (Node, SSR, Next.js /
Vite server components, plain unit tests) because of three top-level DOM
accesses:
- `document.createElement('template')`
- `class ... extends HTMLElement`
- `customElements.define(...)`
Existing tests only passed because they inject a global DOM shim before
importing, so real consumers following the README hit an immediate
ReferenceError.
Guard all three behind a HAS_DOM check: build the template lazily, fall
back to a plain base class when HTMLElement is absent, and register the
custom element only in the browser (now idempotent, avoiding
"already defined" errors under HMR / double import). The pure
`calculate()` export now works in Node with no shim.
Add test/no-dom.test.mjs, which imports the module with no DOM shim to
lock in the fix (every other test file installs a shim and could not
catch a regression).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C5r4KaikJi9KNTp7PLvuAa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The README advertises the pure calculator:
and
index.d.tsdocuments it as "Pure infrastructure-sizing calculation — no DOM required."But the module crashes on
importin any non-DOM environment — Node, SSR, Next.js / Vite server components, or a plain unit test — because of three top-level DOM accesses that run at module-evaluation time:Reproduction (following the README verbatim):
The existing test suite never caught this because every test file injects a global DOM shim before importing the module — something real consumers don't do.
Fix
Detect DOM support once (
HAS_DOM) and guard all three top-level accesses:<template>lazily; store the HTML string separately so it costs nothing without a DOM.HTMLElementis absent (the element is only ever instantiated in a browser; only its class binding needs to exist for the exports).customElements.get(...)check), which also prevents "already defined" errors under HMR / double import.The pure
calculate()export now works in Node with no shim, and the component still registers normally in the browser.Tests
test/no-dom.test.mjs, which imports the module with no DOM shim and verifiescalculate(), the default export, and the named class are all usable. This locks in the fix, since every other test file installs a shim and could not catch a regression.🤖 Generated with Claude Code
https://claude.ai/code/session_01C5r4KaikJi9KNTp7PLvuAa
Generated by Claude Code