Tag Transformation
Tag transformation registers your components under different tag names, so a second copy of a library can't clash with the first. See Avoiding Tag Name Clashes for how it compares with scoped registries.
setTagTransformer and transformTag
Stencil has two utilities to manage dynamic tag names:
setTagTransformerallows application developers (your component library consumers) to assign a tag transformer function.transformTagis mainly used within your component libraries - transforming any static string tag names using the tag transformer function assigned viasetTagTransformer(Alternatively, you can auto applytransformTagto all tag names via thecompat.additionalTagTransformersconfig option.)
Using setTagTransformer
Your library's outputs export setTagTransformer for application developers to use. They must call it before the components are defined.
Usage via the loader-bundle output
setTagTransformer is exported from the loader:
<script type="module">
import { defineCustomElements, setTagTransformer } from 'https://cdn.jsdelivr.net/npm/@component/library/dist/loader-bundle/loader/index.js';
// Setup a tag transformer
setTagTransformer((tag) => {
if (tag.startsWith('my-')) {
return tag.replace('my-', 'your-');
}
return tag;
});
// Define components as normal
defineCustomElements();
</script>
<body>
<your-button></your-button>
<!-- ^^^ note the transformed tag name -->
</body>
Usage via the standalone output
setTagTransformer is exported from the output's index file, and from its loader.js for use before the auto-loader's start():
import { setTagTransformer } from '@component/library/dist/standalone';
import { defineCustomElement } from '@component/library/dist/standalone/my-button.js';
// setup a tag transformer
setTagTransformer((tag) => {
if (tag.startsWith('my-')) {
return tag.replace('my-', 'your-');
}
return tag;
})
// define components as normal
defineCustomElement();
Usage via ssr output (on a Node.js server)
The ssr module exports its own setTagTransformer. Once it's set, renderToString renders the transformed tags it finds in the HTML:
import { renderToString, setTagTransformer } from '@component/library/dist/ssr';
// setup a tag transformer
setTagTransformer((tag) => {
if (tag.startsWith('my-')) {
return tag.replace('my-', 'your-');
}
return tag;
})
const { html } = await renderToString(`
<html>
<body>
<your-button></your-button>
</body>
</html>
`);