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)
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
./vitebut readsgetComponentStyleDependencies()/stencilBuildEventsfrom the main entry (.) sees an empty map, because the plugin populated a differentcached.Detail
index.cjsdefines its ownvar cached = nulland exportsgetComponentStyleDependencies/rebuildStyleMap/stencilBuildEventsbound to it.vite.cjsdefines a separatevar cached = nulland its ownstencilBuildEventsemitter, and its plugin'sbuildStartcallsrebuildStyleMapagainst that copy.require()s a shared chunk for the style-dependencies/build-events modules — each is fully inlined.So:
The ESM build is fine —
index.jsand the vite plugin both import the sharedchunk-*.jsthat ownscached, so state is shared.Impact
@stencil/storybook-plugin@0.7.0relies on exactly this cross-entry sharing for SCSS HMR: it builds the Stencil plugin from@stencil-community/unplugin-stencil/viteand 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.scssnever 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
cachedandstencilBuildEventsare true singletons across.,./vite,./rollup, etc.Environment
@stencil-community/unplugin-stencil0.5.2 (latest)@stencil/storybook-plugin0.7.0 +storybook10.2.19 (Vite builder)