コンポーネントライブラリの公開
There are numerous strategies to publish and distribute your component library to be consumed by external projects. One of the benefits of Stencil is that is makes it easy to generate the various output targets that are right for your use-case.
Use Cases
To use your Stencil components in other projects, there are two different output targets to consider: dist and dist-custom-elements. Both export your components for different use cases. Luckily, both can be generated at the same time, using the same source code, and shipped in the same distribution. It would be up to the consumer of your component library to decide which build to use.
Lazy Loading
If you prefer to have your components automatically loaded when used in your application, we recommend enabling the dist output target. The bundle gives you a small entry file that registers all your components and defers loading the full component logic until it is rendered in your application. It doesn't matter if the actual application is written in HTML or created with vanilla JavaScript, jQuery, React, etc.
Your users can import your component library, e.g. called my-design-system, either via a script tag:
<script type="module" src="https://unpkg.com/my-design-system"></script>
or by importing it in the bootstrap script of your application:
import 'my-design-system';
To ensure that the right entry file is loaded when importing the project, define the following fields in your package.json:
{
"exports": "./dist/esm/my-design-system.js",
"main": "./dist/cjs/my-design-system.js",
"unpkg": "dist/my-design-system/my-design-system.esm.js",
}
Read more about various options when it comes to configuring your project's components for lazy loading in the dist output target section.
Considerations
To start, Stencil was designed to lazy-load itself only when the component was actually used on a page. There are many benefits to this approach, such as simply adding a script tag to any page and the entire library is available for use, yet only the components actually used are downloaded. For example, @ionic/core comes with over 100 components, but a webpage may only need ion-toggle. Instead of requesting the entire component library, or generating a custom bundle for just ion-toggle, the dist output target is able to generate a tiny entry build ready to load any of its components on-demand.
However be aware that this approach is not ideal in all cases. It requires your application to ship the bundled components as static assets in order for them to load properly. Furthermore, having many nested component dependencies can have an impact on the performance of your application. For example, given you have a component CmpA which uses a Stencil component CmpB which itself uses another Stencil component CmpC. In order to fully render CmpA the browser has to load 3 scripts sequentially which can result in undesired rendering delays.
Standalone
The dist-custom-elements output target builds each component as a stand-alone class that extends HTMLElement. The output is a standardized custom element with the styles already attached and without any of Stencil's lazy-loading. This may be preferred for projects that are already handling bundling, lazy-loading and defining the custom elements themselves.
The generated files will each export a component class and will already have the styles bundled. However, this build does not define the custom elements or apply any polyfills. Static assets referenced within components will need to be set using setAssetPath (see Making Assets Available).
You can use these standalone components by importing them via:
import { MyComponent, defineCustomElementMyComponent } from 'my-design-system'
// register to CustomElementRegistry
defineCustomElementMyComponent()
// or extend custom element via
class MyCustomComponent extends MyComponent {
// ...
}
define('my-custom-component', MyCustomComponent)
To ensure that the right entry file is loaded when importing the project, define different exports fields in your package.json:
{
"exports": {
".": {
"import": "./dist/components/index.js",
"types": "./dist/components/index.d.ts"
},
"./my-component": {
"import": "./dist/components/my-component.js",
"types": "./dist/components/my-component.d.ts"
}
},
"types": "dist/components/index.d.ts",
}
This allows us to map certain import paths to specific components within our project and allows users to only import the component code they are interested in and reduce the amount of code that needs to downloaded by the browser, e.g.:
// this import loads all compiled components
import { MyComponent } from 'my-design-system'
// only import compiled code for MyComponent
import { MyComponent } from 'my-design-system/my-component'
If you define exports targets for all your components as shown above and by using customElementsExportBehavior: 'auto-define-custom-elements' as output target option, you can skip the defineCustomElement call and directly import the component where you need it:
import 'my-design-system/my-component'