Bundle Stencil Components for Plain HTML with dist-custom-elements
Generate Stencil's self-registering custom-elements bundle and consume it in plain HTML without a bundler or frontend framework.
28 Aug 2026, 13:22 UTC

The problem
Stencil components compile to framework-specific output by default, but a design system often needs to work in plain HTML pages, CMS templates, or legacy backends where no bundler or frontend framework is available. You want a single JavaScript file that registers every component as a native custom element the moment it is imported.
Stencil's dist-custom-elements output target produces exactly that: a self-registering bundle under dist/custom-elements. This guide shows how to enable it, verify the output, load it in a plain HTML page, and point package consumers at the right entry points.
Prerequisites
- A Stencil project (confirm the compiler with
npx stencil --version). - Node.js and npm installed.
- Component source files under
src/components. - A browser with native custom elements support, or a polyfill for older browsers.
Custom elements are a browser standard that lets you define new HTML tags, such as <my-button>. Shadow DOM is Stencil's default style encapsulation: styles inside a component do not leak out, and page styles do not leak in.
1. Configure the output target
Edit stencil.config.ts in the repository root. Add dist-custom-elements to the outputTargets array.
import { Config } from '@stencil/core';
export const config: Config = {
outputTargets: [
{
type: 'dist',
esmLoaderPath: '../loader',
},
{
type: 'dist-custom-elements',
},
],
};
Where: run all commands from the repository root. Permissions: ordinary read/write access to the project folder; no sudo required. esmLoaderPath is optional and tells the ESM build where to find its loader; adjust it to match your project layout.
2. Build the bundle
npx stencil build
Expected result: the command exits with code 0 and creates a dist/ directory. Confirm the custom-elements output:
find dist/custom-elements -type f
You should see at least one JavaScript bundle, commonly named custom-elements.js, plus a loader entry point. Exact file names vary by Stencil version, so treat these as placeholders and verify against your installed compiler.
Risk: the bundle calls customElements.define() at import time and registers every component in the package. Importing it adds the full component payload even if the page uses only one tag.
3. Consume the bundle in plain HTML
Serve the build output locally. From the repository root:
npx http-server dist -p 8080
Then open this page:
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8' />
<title>Custom element smoke test</title>
<script type='module' src='/custom-elements/custom-elements.js'></script>
</head>
<body>
<my-component name='Reader'></my-component>
</body>
</html>
Replace my-component with an actual component tag from your library and adjust the script path to match the bundle file name from step 2.
Expected check: the element appears in the page and renders its shadow DOM content. Open the browser's developer tools and confirm there are no 404 responses for the script and no customElements is not defined errors. If you see that error, the browser needs the official web components polyfill; Stencil does not inject it automatically.
4. Use TypeScript declarations
Stencil emits declaration files alongside JavaScript, so typed consumers can inspect properties and events. In a consumer project:
import { MyComponent } from 'my-ui-library';
const el = document.createElement('my-component') as MyComponent;
el.name = 'Reader';
document.body.appendChild(el);
Verify with:
npx tsc --noEmit
Expected check: TypeScript resolves the component class, properties, and events without "cannot find module" errors. Declaration paths depend on your package.json exports map.
5. Publish entry points
Point package.json at the generated ESM and custom-elements files. Example (paths are placeholders):
{
"name": "my-ui-library",
"version": "1.0.0",
"main": "dist/esm/index.js",
"module": "dist/esm/index.mjs",
"exports": {
".": {
"types": "./dist/types/components.d.ts",
"import": "./dist/esm/index.mjs"
},
"./custom-elements": {
"types": "./dist/custom-elements/custom-elements.d.ts",
"import": "./dist/custom-elements/custom-elements.js"
}
},
"files": [
"dist/esm",
"dist/custom-elements",
"dist/types"
]
}
Consumers can then import the bundle directly:
<script type='module' src='https://unpkg.com/my-ui-library@1.0.0/dist/custom-elements/custom-elements.js'></script>
Do not commit dist/ to version control unless your release pipeline requires it. Generate the bundle during CI instead, then publish the artifacts.
Verification checklist
| Check | Command or action | Expected result |
|---|---|---|
| Build succeeds | npx stencil build | Exit code 0; dist/ created |
| Bundle exists | find dist/custom-elements -type f | At least one JS bundle and loader file |
| Plain HTML loads | Open smoke-test page served from dist | Component tag renders; no console errors |
| Types resolve | npx tsc --noEmit in consumer project | No module-resolution errors |
| Publish map | Inspect package.json exports | Fields point to generated ESM and custom-elements files |
Limitations and recovery
- Bundle size. The auto-define bundle registers all components at import time. If you ship many components but pages use only one, consider separate output targets or lazy loading instead.
- Shadow DOM. Global page styles do not penetrate component shadow roots by default. Use CSS custom properties or
::partif your components expose them. - Browser support. Native custom elements are required. Add a polyfill such as the official webcomponentsjs package for older browsers; Stencil does not add it for you.
- Version drift. Output target names and file layout can change between Stencil major versions. Verify behavior against the compiler version in
package.json.
Cleanup after a failed or unwanted build
The build command changes state by writing to dist/. To undo, remove the generated output and rebuild with the previous configuration:
rm -rf dist
git checkout -- stencil.config.ts
npx stencil build
If dist/ was accidentally committed, remove it from tracking without deleting local files:
git rm -r --cached dist
echo 'dist/' >> .gitignore
git commit -m 'Stop tracking build output'
After verification, the library is consumable as native custom elements in any HTML page that can load a module script.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.