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

Open
#760 0 comments 1 reaction 0 assignees View on GitHub

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

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stenciljs/store

All issues in stenciljs/store

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.