bug: since 2.2.2, `import * as StencilCore` stops Rollup removing `Build.isBrowser` branches from the hydrate app
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Ferma
- Stack tecnologico
- javascript, rollup, typescript
- Ambito
- build-system, testing
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- TypeScript
- Stelle
- 181
- Fork
- 17
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di stenciljs/store
-
bug: release pipeline does not iterate package version or create tagsForse di nuovo libera @gnbm l’ha presa 351 giorni fa e non c’è nessuna pull request aperta. Aperta
-
Enhancement Help Wanted
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
-
Bug: Validated Help Wanted
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
-
Bug: Validated Resolution: Needs Investigation
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
-
Add delete methodForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. ApertaEnhancement Resolution: Refine
Difficoltà 3/5 1-2 giorni Idoneità per principianti 38/100
Tutte le issue di stenciljs/store
Issue simili
-
bug zoo
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
software-mansion/runntime#151 ·
I maintainer di solito rispondono entro 4 giorni
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 86/100
sidebandstudio/clawtrate#728 ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement github_actions help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
tranquilo-art/tranquilo-app#254 ·
I maintainer di solito rispondono entro 1 giorno
-
docs good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 87/100
DojoCodingLabs/hermit#73 ·
I maintainer di solito rispondono entro 1 giorno
-
documentation good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
Tfoucher5/fuzzy-toolkit#44 ·
I maintainer di solito rispondono entro 1 giorno