A suite of polyfills supporting the Web Components specs including Custom Elements v1, Shadow DOM v1, HTMLTemplateElement, and platform polyfills
npx @tessl/cli install tessl/npm-webcomponents--webcomponentsjs@2.8.0Web Components Polyfills is a comprehensive suite of polyfills that provides Web Components support for browsers that don't natively implement the specifications. It includes polyfills for Custom Elements v1, Shadow DOM v1, HTMLTemplateElement, and essential platform features like Promise, Event constructors, and DOM methods.
npm install @webcomponents/webcomponentsjsScript Tags (Browser):
<!-- Dynamic loader (recommended) -->
<script src="node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
<!-- Complete bundle -->
<script src="node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>
<!-- ES5 adapter (when needed) -->
<script src="node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>CDN Usage:
<script src="https://unpkg.com/@webcomponents/webcomponentsjs@^2/webcomponents-loader.js"></script>Synchronous Loading (Simple):
<!-- Load polyfills synchronously -->
<script src="webcomponents-loader.js"></script>
<!-- Your custom elements can be loaded immediately -->
<script type="module" src="my-element.js"></script>
<!-- Use custom elements -->
<my-element></my-element>Asynchronous Loading (Recommended):
<!-- Load polyfills asynchronously with defer -->
<script src="webcomponents-loader.js" defer></script>
<!-- Load custom elements after polyfills are ready -->
<script type="module">
WebComponents.waitFor(() => {
// Polyfills are guaranteed to be ready here
return import('./my-element.js');
});
</script>
<!-- Use custom elements -->
<my-element></my-element>The Web Components Polyfills package is built around several key components:
Smart loader that detects browser capabilities and dynamically loads only the necessary polyfill bundles, optimizing performance by avoiding unnecessary code.
// Global WebComponents object available after loader runs
interface WebComponents {
/** Indicates if polyfills are loaded and ready */
ready: boolean;
/** Optional root path for loading polyfill bundles */
root?: string | TrustedScriptURL;
/** Register callback to run after polyfills load (async loading only) */
waitFor(callback: () => void | Promise<any>): void;
/** Internal method for batching custom element upgrades */
_batchCustomElements(): void;
}
declare global {
interface Window {
WebComponents: WebComponents;
}
}All-in-one bundle containing every Web Components polyfill, suitable for simple usage scenarios where loading optimization is not critical.
// No API - just include the script tag
// <script src="webcomponents-bundle.js"></script>
// All polyfills load immediately and Web Components APIs become availableCompatibility layer that allows ES5-style custom element classes to work with native Custom Elements implementation when serving transpiled code to all browsers.
// No direct API - include before defining custom elements
// <script src="custom-elements-es5-adapter.js"></script>
// Automatically wraps ES5 classes for native Custom Elements compatibilityWebComponentsReady event system that signals when polyfills are loaded and custom elements are ready for use.
// WebComponentsReady event fired on document
interface WebComponentsReadyEvent extends CustomEvent {
type: 'WebComponentsReady';
bubbles: true;
}
// Event listener pattern
document.addEventListener('WebComponentsReady', () => {
// Polyfills loaded, custom elements upgraded
});// Trusted Types support for CSP environments
type TrustedScriptURL = any; // Browser-specific type
// Callback function for waitFor
type WaitForCallback = () => void | Promise<any>;
// WebComponents global interface
interface WebComponents {
ready: boolean;
root?: string | TrustedScriptURL;
waitFor(callback: WaitForCallback): void;
_batchCustomElements(): void;
}