メインコンテンツまでスキップ
バージョン: v2

Distributionアウトプットターゲット

The dist type is to generate the component(s) as a reusable library that can be self-lazy loading, such as Ionic. When creating a distribution, the project's package.json will also have to be updated. However, the generated bundle is tree-shakable, ensuring that only imported components will end up in the build.

outputTargets: [
{
type: 'dist'
}
]

How is this different from "dist-custom-elements-bundle" output target?​

The dist-custom-elements-bundle output target has been deprecated in Stencil v2.12.0 in favor of the dist-custom-elements output target. The information below is for historical purposes only.

まず、Stencilは、コンポーネントが実際にページで使用された場合にのみ、それ自体を遅延ロードするように設計されました。このアプローチには多くの利点があります。たとえば、任意のページにスクリプトタグを追加するだけで、ライブラリ全体を使用できますが、実際に使用されるコンポーネントのみがダウンロードされます。たとえば、@ionic/coreには100を超えるコンポーネントが付属していますが、1つのWebページに必要なのはion-toggleだけです。コンポーネントライブラリ全体をリクエストしたり、 ion-toggleだけのカスタムバンドルを生成したりする代わりに、dist出力ターゲットは、そのコンポーネントをオンデマンドでロードする準備ができた小さなエントリビルドを生成できます。

一方、 dist-custom-elements-bundleは、遅延読み込みを行わずに、HTMLElementを拡張するカスタム要素を直接ビルドしたものです。カスタム要素バンドルは、ポリフィルを適用せず、各カスタム要素を自動的に定義しません。これは、カスタム要素自体のバンドル、遅延読み込み、および定義を処理するプロジェクトに適している場合があります。

幸い、両方のビルドを同時に生成して、同じディストリビューションで出荷することができます。使用するビルドを決定するのは、コンポーネントライブラリの利用者次第です。

構成​

dir​

The dir config specifies the public distribution directory. This directory is commonly the dist directory found within npm packages. This directory is built and rebuilt directly from the source files. Additionally, since this is a build target, all files will be deleted and rebuilt after each build, so it's best to always copy source files into this directory. It's recommended that this directory not be committed to a repository.

This option defaults to dist when omitted from a Stencil configuration file.

empty​

By default, before each build the dir directory will be emptied of all files. To prevent this directory from being emptied, change this value to false.

This flag defaults to true when omitted from a Stencil configuration file.

collectionDir​

The collectionDir config specifies the output directory within the distribution directory where the transpiled output of Stencil components will be written.

This option defaults to collection when omitted from a Stencil configuration file.

transformAliasedImportPathsInCollection​

This option will allow path aliases defined in a project's tsconfig.json to be transformed into relative paths in the code output under the collectionDir subdirectory for this output target. This does not affect imports for external packages.

An example of path transformation could look something like:

// Source code
import * as utils from '@utils';

// Output code
import * as utils from '../path/to/utils';

This flag defaults to false when omitted from a Stencil configuration file.

ヒント

If using the dist-collection output target directly, the same result can be achieved using the transformAliasedImportPaths flag on the target's config.

公開​

Next you can publish your library to Node Package Manager (NPM). For more information about setting up the package.json file, and publishing, see: Publishing A Component Library.

配布オプション​

Each output target's form of bundling and distribution has its own pros and cons. Luckily you can just worry about writing good source code for your component. Stencil will handle generating the various bundles and consumers of your library can decide how to apply your components to their external projects. Below are a few of the options.

スクリプトタグ​

  • Use a script tag linked to a CDN copy of your published NPM module, for example: <script type="module" src='https://cdn.jsdelivr.net/npm/my-name@0.0.1/dist/myname.js'></script>.
  • The initial script itself is extremely tiny and does not represent the entire library. It's only a small registry.
  • You can use any or all components within your library anywhere within that webpage.
  • It doesn't matter if the actual component was written within the HTML or created with vanilla JavaScript, jQuery, React, etc.
  • Only the components used on that page will be requested and lazy-loaded.

Importing the dist library using a bundler​

  • Run npm install my-name --save
  • Add an import within the root component: import my-component;
  • Stencil will automatically setup the lazy-loading capabilities for the Stencil library.
  • Then you can use the element anywhere in your template, JSX, HTML etc.

Importing the dist library into another Stencil app​

  • Run npm install my-name --save
  • Add an import within the root component: import my-component;
  • Stencil will automatically setup the lazy-loading capabilities for the Stencil library.
  • Then you can use the element anywhere in your template, JSX, HTML etc.