bug: since 2.2.2, `import * as StencilCore` stops Rollup removing `Build.isBrowser` branches from the hydrate app
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- javascript, rollup, typescript
- Domain
- build-system, testing
Research direction
Start with src/subscriptions/stencil.ts and review the namespace access introduced in #647. Reproduce the hydrate build using the linked reproduction and check whether the browser-only module remains in hydrate/index.mjs. The issue says a fix and regression test have been opened in #761; check that work and verify the hydrate output and renderToString behavior.
Written by the indexing model from the issue text.
Description
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already report this problem, without success.
Stencil Store Version
2.2.2 – 2.2.5 (2.2.1 and earlier are fine)
Stencil Version
4.45.2
Current Behavior
Since 2.2.2, importing @stencil/store into a Stencil project stops Rollup from removing if (Build.isBrowser) { … } branches from the dist-hydrate-script output. Browser-only code that should never reach the hydrate app (for example a library loaded with await import(...) behind Build.isBrowser) gets bundled in and runs when the hydrate module loads. If that code touches DOM APIs that Stencil's mock-doc doesn't implement, renderToString throws. In the reproduction the error is:
TypeError: document.createTreeWalker is not a function
The cause is the change to src/subscriptions/stencil.ts in #647 (commit fc0312d, first released in 2.2.2):
import * as StencilCore from '@stencil/core';
const core = StencilCore as unknown as { … };
const forceUpdate = core.forceUpdate;
const getRenderingRef = core.getRenderingRef;
Assigning the namespace object to a variable makes Rollup build the whole @stencil/core module object, including Build. After that it can no longer fold Build.isBrowser to false in the hydrate build, so the dead branch and everything it imports are kept.
Expected Behavior
Using @stencil/store should not change what ends up in the hydrate app. if (Build.isBrowser) branches should still be removed, as they are with 2.2.1 and earlier.
Steps to Reproduce
The reproduction below is the blank component starter (npm init stencil component) with these changes: my-component uses createStore and loads a browser-only module behind if (Build.isBrowser), a dist-hydrate-script output target is added, and a small script checks the hydrate output and calls renderToString.
git clone https://github.com/Unipro-DanMunn/stencil-store-hydrate-repro
cd stencil-store-hydrate-repro
npm install
npm run repro
Output with 2.2.5:
@stencil/[email protected], @stencil/[email protected]
browser-only module IS in hydrate/index.mjs
renderToString: TypeError: document.createTreeWalker is not a function
To compare, run npm install --no-save @stencil/store@<version> and then npm run repro again:
@stencil/store |
Browser-only module in hydrate app | renderToString |
|---|---|---|
| 2.0.11 | no | ✅ ok |
| 2.2.1 | no | ✅ ok |
| 2.2.2 | yes | ❌ TypeError: document.createTreeWalker is not a function |
| 2.2.5 | yes | ❌ TypeError: document.createTreeWalker is not a function |
Code Reproduction URL
https://github.com/Unipro-DanMunn/stencil-store-hydrate-repro
Additional Information
Suggested fix: keep the namespace import from #647, but read each export straight off it instead of copying the namespace into a variable:
-const core = StencilCore as unknown as {
- forceUpdate?: (elm: any) => boolean;
- getRenderingRef?: () => any;
-};
-
-const forceUpdate = core.forceUpdate;
-const getRenderingRef = core.getRenderingRef;
+type StencilInternals = {
+ forceUpdate?: (elm: any) => boolean;
+ getRenderingRef?: () => any;
+};
+
+// Read each export straight off the namespace. Copying the namespace into a variable makes
+// bundlers keep the whole @stencil/core module object, which stops them dropping
+// `if (Build.isBrowser)` branches from hydrate builds.
+const forceUpdate = (StencilCore as unknown as StencilInternals).forceUpdate;
+const getRenderingRef = (StencilCore as unknown as StencilInternals).getRenderingRef;
TypeScript compiles this to const forceUpdate = StencilCore.forceUpdate;, which Rollup treats as a reference to that one export.
I tested this by patching the published 2.2.5 dist/index.js and building the reproduction:
| Build | Browser-only code in hydrate app | renderToString |
@stencil/core without forceUpdate/getRenderingRef |
|---|---|---|---|
| 2.2.5 as published | included | ❌ throws | ✅ returns {} subscription |
| Suggested fix | removed | ✅ | ✅ returns {} subscription; forceUpdate still called when the exports exist |
| Revert to named imports | removed | ✅ | ❌ SyntaxError: The requested module '@stencil/core' does not provide an export named 'forceUpdate' |
So the suggested fix keeps what #647 set out to do (tolerating a @stencil/core without these exports) without affecting tree-shaking. A plain revert would fix the hydrate build but bring back the missing-export failure.
A regression test can build a small Stencil app that imports the pre-built store from node_modules and has a dist-hydrate-script output, then assert that a module imported behind if (Build.isBrowser) isn't in the hydrate app. The bug doesn't reproduce when Stencil compiles the store from its TypeScript source; it only shows up when the store is consumed as pre-built JavaScript, as it is from npm.
I have opened #761 with this fix and a regression test.
- Dominant language
- TypeScript
- Stars
- 181
- Forks
- 17
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from stenciljs/store
-
bug: release pipeline does not iterate package version or create tagsMay be free again @gnbm claimed this 351 days ago, and no pull request is open. Open
-
Enhancement Help Wanted
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
Bug: Validated Help Wanted
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Bug: Validated Resolution: Needs Investigation
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Add delete methodMay be free again A pull request for this issue was closed without being merged. OpenEnhancement Resolution: Refine
Difficulty 3/5 1-2 days Newbie friendliness 38/100
Similar issues
-
bug zoo
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
software-mansion/runntime#151 ·
Maintainers usually reply within 4 days
-
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
sidebandstudio/clawtrate#728 ·
Maintainers usually reply within 1 day
-
enhancement github_actions help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
tranquilo-art/tranquilo-app#254 ·
Maintainers usually reply within 1 day
-
docs good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 87/100
DojoCodingLabs/hermit#73 ·
Maintainers usually reply within 1 day
-
documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Tfoucher5/fuzzy-toolkit#44 ·
Maintainers usually reply within 1 day