出力ターゲット
コンパイラのより強力な機能の1つは、コンポーネントが使用される 方法 に応じてさまざまなビルドを生成する機能です。 Stencilは、アプリのソースを取得して、httpサーバーにデプロイするWebアプリ、npmで配布するサードパーティコンポーネントの遅延読み込みライブラリ、またはVanillaJSのカスタムエレメントなど、多数のターゲットにコンパイルできます。 デフォルトでは、Stencilの出力ターゲットタイプは wwwであり、これはWebアプリに最適です。
Output Target Types:
例:
import { Config } from '@stencil/core';
export const config: Config = {
outputTargets: [
{
type: 'dist'
},
{
type: 'www'
}
]
};
ディファレンシャルバンドリング
「差分バンドリング」をサポートするために、コンパイラーが多数のバンドルを自動的に生成することに注意することも重要です。 これが意味するのは、本番ビルド中に、ステンシルは同じソースコードから最新のブラウザーとレガシーブラウザー(IE11)の両方のコードを生成するということです。 ディファレンシャルバンドルの利点は、最新のブラウザーがすべてのポリフィルと肥大化したレガシーJavaScriptを回避し、ブラウザーに既に組み込まれている最新のAPIを使用できることです。
以下の例では、2つのスクリプトタグがありますが、ユーザーが要求するのはそのうちの1つだけです。 IE11ユーザーの場合、ES5構文ですべてのポリフィルを含むapp.jsファイルをダウンロードします。 最新のブラウザを使用しているユーザーは、ESモジュール、動的インポート、非同期/待機、クラスなどの最新のJavaScript機能を使用する app.esm.jsファイルのみをダウンロードします。
buildEs5 must be set to true to generate the IE11 ES5 file
<script type="module" src="/build/app.esm.js"></script>
<script nomodule src="/build/app.js"></script>
As of Stencil v3, legacy browser support is deprecated, and will be removed in a future major version of Stencil.
Primary Package Output Target Validation
If validatePrimaryPackageOutputTarget: true is set in your project's Stencil config Stencil will
attempt to validate certain fields in your package.json that correspond with the generated distribution code. Because Stencil can output many different formats
from a single project, it can only validate that the package.json has field values that align with one of the specified output targets in your project's config.
So, Stencil allows you to designate which output target should be used for this validation and thus which will be the default distribution when bundling you
project.
This behavior only affects a small subset of output targets so a flag exists on the following targets that are eligible for this level of validation: dist, dist-types,
dist-collection, and dist-custom-elements. For any of these output targets, you can configure the target to be validated as follows:
import { Config } from '@stencil/core';
export const config: Config = {
...,
outputTargets: [
{
type: 'dist',
// This flag is what tells Stencil to use this target for validation
isPrimaryPackageOutputTarget: true,
...
},
...
],
// If this is not set, Stencil will not validate any targets
validatePrimaryPackageOutputTarget: true,
};
Stencil can only validate one of these output targets for your build. If multiple output targets are marked for validation, Stencil will use the first designated target in the array and ignore all others.