Registry / web-framework / vite-plugin-shadow-style

vite-plugin-shadow-style

JSON →
library1.2.0jsnpmunverified

A Vite plugin (v1.2.0, stable) that injects CSS styles into web component shadow DOMs. It rewrites CSS imports to be available as a global SHADOW_STYLE string inside the component bundle, allowing styles to be appended to shadow roots without leaking. Differentiates from standard Vite CSS handling by targeting shadow DOM encapsulation. Ships TypeScript declarations.

npm install vite-plugin-shadow-style
INSTALL
IMPORT
SIG · VITE-PLUGIN-SHADOW
V
vite-plugin-shadow-style
web-frameworkjavascriptv1.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

shadowStyle
import { shadowStyle } from 'vite-plugin-shadow-style'
import shadowStyle from 'vite-plugin-shadow-style'
Named export only; no default export.
SHADOW_STYLE
declare const SHADOW_STYLE: string; // in globals.d.ts
import { SHADOW_STYLE } from 'vite-plugin-shadow-style'
SHADOW_STYLE is a global variable injected by the plugin, not an import. You must declare it in a .d.ts file for TypeScript.
vite.config.ts usage
import { shadowStyle } from 'vite-plugin-shadow-style'; export default defineConfig({ plugins: [shadowStyle()] })
import shadowStyle from 'vite-plugin-shadow-style'; // then use shadowStyle() as a standalone function without plugin context
The plugin returns a Vite plugin object; it must be added to the plugins array.

Configures the plugin in vite.config.ts, uses the global SHADOW_STYLE variable inside a web component, and declares the type for SHADOW_STYLE.

// vite.config.ts import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import { shadowStyle } from 'vite-plugin-shadow-style'; export default defineConfig({ build: { rollupOptions: { plugins: [ react(), shadowStyle(), ], }, }, }); // In your web component entry file (e.g., my-component.ts): export class MyComponent extends HTMLElement { connectedCallback() { const style = document.createElement('style'); style.innerHTML = SHADOW_STYLE; // injected by plugin const shadow = this.attachShadow({ mode: 'open' }); shadow.appendChild(style); shadow.appendChild(document.createElement('div')); } } // globals.d.ts declare const SHADOW_STYLE: string;
Debug
Known issues
gotchaSHADOW_STYLE is not imported; it's a global variable injected at build time. You must declare it in a .d.ts file for TypeScript to compile.
fix
Add a globals.d.ts file with: declare const SHADOW_STYLE: string;
affects: >=1.0.0
gotchaThe plugin should be added to rollupOptions.plugins, not the top-level plugins array in Vite config.
fix
Add shadowStyle() inside build.rollupOptions.plugins, as shown in the README.
affects: >=1.0.0
gotchaIf your component uses external CSS imports, Vite may bundle them as const declarations that can conflict when multiple components are imported. Use the iife option to wrap output in an IIFE to avoid global scope pollution.
fix
Pass { iife: true } to shadowStyle(): shadowStyle({ iife: true })
affects: >=1.0.0
gotchaThe plugin only works with Vite's build mode; dev server is not supported for SHADOW_STYLE injection.
fix
Use the plugin only for production builds. For development, consider an alternative or mock SHADOW_STYLE.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find name 'SHADOW_STYLE'.
TypeScript does not recognize the global SHADOW_STYLE variable injected by the plugin.
fix
Create a globals.d.ts file in your project root with: declare const SHADOW_STYLE: string;
The requested module 'vite-plugin-shadow-style' does not provide an export named 'default'
Trying to import a default export that doesn't exist.
fix
Use named import: import { shadowStyle } from 'vite-plugin-shadow-style'
SHADOW_STYLE is not defined
SHADOW_STYLE is a build-time injected global; it is not available at runtime in the dev server or if the plugin is not configured correctly.
fix
Ensure the plugin is added to build.rollupOptions.plugins in vite.config.ts and that you are building (not running dev server).
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies
viterequiredpeer dependency; plugin requires Vite's build and plugin system
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
2
Resources
vite-plugin-shadow-style — npm install vite-plugin-shadow-style · libregistry