bug: since 2.2.2, `import * as StencilCore` stops Rollup removing `Build.isBrowser` branches from the hydrate app

Aperta
#760 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

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

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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di stenciljs/store

Tutte le issue di stenciljs/store

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.