Skip to content

CJS build duplicates module state (cached style map, stencilBuildEvents) across entry points #18

Description

@gavmck

Summary

In the CJS build, the package's entry points each inline their own copy of the stateful style-dependencies module, so module-level singletons are duplicated rather than shared. A consumer that creates the plugin from ./vite but reads getComponentStyleDependencies() / stencilBuildEvents from the main entry (.) sees an empty map, because the plugin populated a different cached.

Detail

  • Main entry index.cjs defines its own var cached = null and exports getComponentStyleDependencies / rebuildStyleMap / stencilBuildEvents bound to it.
  • vite.cjs defines a separate var cached = null and its own stencilBuildEvents emitter, and its plugin's buildStart calls rebuildStyleMap against that copy.
  • Neither CJS entry require()s a shared chunk for the style-dependencies/build-events modules — each is fully inlined.

So:

vite.cjs  rebuildStyleMap → cached.byStyle.size = 100
index.cjs getComponentStyleDependencies() → cached.byStyle.size = 0   // different instance

The ESM build is fine — index.js and the vite plugin both import the shared chunk-*.js that owns cached, so state is shared.

Impact

@stencil/storybook-plugin@0.7.0 relies on exactly this cross-entry sharing for SCSS HMR: it builds the Stencil plugin from @stencil-community/unplugin-stencil/vite and reads the style→component map from @stencil-community/unplugin-stencil. Under CJS (how Storybook loads its preset) the map is always empty, so editing a component's .scss never triggers a rebuild. (Tracking issue on their side: stenciljs/storybook#54.)

Suggested fix

In the CJS build, have the entry points resolve the style-dependencies and build-events modules to a shared chunk (as the ESM build does) so cached and stencilBuildEvents are true singletons across ., ./vite, ./rollup, etc.

Environment

  • @stencil-community/unplugin-stencil 0.5.2 (latest)
  • consumed via @stencil/storybook-plugin 0.7.0 + storybook 10.2.19 (Vite builder)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions