@stencil/core、lit、svelte は、すべてモダンな Web 開発においてコンポーネントベースのアーキテクチャを実現するツールですが、その目的と出力形式に明確な違いがあります。@stencil/core は Web コンポーネントを生成するためのコンパイラであり、フレームワークに依存しないデザインシステムの構築に特化しています。lit は Google が開発したライブラリで、軽量かつ高速な Web コンポーネント作成を標準に則って行います。一方、svelte はビルド時に最適化された Vanilla JS を生成するコンパイラであり、主にアプリケーション開発において優れた開発者体験を提供しますが、Web コンポーネントとしての出力も可能です。
@stencil/core、lit、svelte は、いずれもコンポーネント指向開発を支援する強力なツールですが、その背後にある仕組みと目指すゴールは異なります。これら 3 つを比較し、実務での選定基準を明確にしていきましょう。
コンポーネントをどのように記述するかは、開発者の日常的な作業に直結します。
@stencil/core は TypeScript デコレータを用いたクラスベースの定義を採用しています。
@Component デコレータでメタデータを定義します。// stencil: my-component.tsx
import { Component, h } from '@stencil/core';
@Component({
tag: 'my-component',
shadow: true
})
export class MyComponent {
render() {
return <div>Hello Stencil</div>;
}
}
lit もクラスベースですが、より軽量な LitElement を継承します。
@customElement デコレータで登録します。// lit: my-element.ts
import { LitElement, html } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-element')
export class MyElement extends LitElement {
render() {
return html`<div>Hello Lit</div>`;
}
}
svelte は独自の .svelte ファイルを使用し、HTML・CSS・JS を単一ファイルにまとめます。
<!-- svelte: MyComponent.svelte -->
<script>
let name = 'Svelte';
</script>
<div>Hello {name}</div>
<style>
div { color: blue; }
</style>
状態(State)の扱い方は、アプリケーションの複雑さに影響します。
@stencil/core は @State デコレータを使用します。
// stencil: counter.tsx
import { Component, State, h } from '@stencil/core';
@Component({ tag: 'my-counter' })
export class MyCounter {
@State() count = 0;
increment() {
this.count += 1;
}
render() {
return <button onClick={() => this.increment()}>{this.count}</button>;
}
}
lit は @state デコレータを使用します(lit-html の reactive properties)。
requestUpdate を呼び出します。// lit: counter.ts
import { LitElement, html } from 'lit';
import { state, customElement } from 'lit/decorators.js';
@customElement('my-counter')
export class MyCounter extends LitElement {
@state() count = 0;
increment() {
this.count += 1;
}
render() {
return html`<button @click=${() => this.increment()}>${this.count}</button>`;
}
}
svelte は変数代入そのもので状態更新を扱います(Svelte 5 では $state ルーン)。
<!-- svelte: Counter.svelte -->
<script>
let count = $state(0);
function increment() {
count += 1;
}
</script>
<button onclick={increment}>{count}</button>
スタイルのカプセル化手法も重要な選定ポイントです。
@stencil/core は Shadow DOM を標準でサポートします。
shadow: true で真のカプセル化を実現します。// stencil: styled-component.tsx
@Component({
tag: 'my-style',
shadow: true
})
export class MyStyle {
render() {
return (
<style>{`div { color: red; }`}</style>
<div>Scoped Style</div>
);
}
}
lit も Shadow DOM を採用しています。
createRenderRoot をオーバーライドしない限り、デフォルトで Shadow Root を使用します。// lit: styled-element.ts
@customElement('my-style')
export class MyStyle extends LitElement {
render() {
return html`
<style>div { color: red; }</style>
<div>Scoped Style</div>
`;
}
}
svelte はスコープ付き CSS を生成します。
<!-- svelte: Styled.svelte -->
<div>Scoped Style</div>
<style>
div { color: red; }
</style>
既存のプロジェクトにどう組み込むかも考慮が必要です。
@stencil/core は Web コンポーネントとして出力されます。
// Any Framework usage
<my-component></my-component>
lit も Web コンポーネントとして出力されます。
// Any Framework usage
<my-element></my-element>
svelte は主に Svelte アプリ内で使用されます。
<!-- Svelte App usage -->
<script>
import Counter from './Counter.svelte';
</script>
<Counter />
| 特徴 | @stencil/core | lit | svelte |
|---|---|---|---|
| タイプ | コンパイラ | ライブラリ | コンパイラ |
| 出力 | Web Components | Web Components | JS / Web Components |
| 構文 | TypeScript Class | TypeScript Class | .svelte File |
| スタイル | Shadow DOM | Shadow DOM | Scoped CSS |
| 主な用途 | デザインシステム | 汎用コンポーネント | アプリ開発 |
@stencil/core は、大規模なデザインシステムを構築し、それを複数の異なるフレームワーク環境で配布したい場合に最適です — 特に Ionic のようなエコシステムとの連携を重視する際に力を発揮します。
lit は、Web 標準に忠実で軽量なコンポーネントを作りたい場合に適しています — 特定のフレームワークに縛られず、ブラウザのネイティブ機能を活かした開発をしたいチームに向いています。
svelte は、アプリケーションそのものの開発を加速させたい場合に最も効果的です — 記述量が少なくパフォーマンスが高いため、スタートアップから大規模アプリまで幅広いプロジェクトで採用されています。
最終的には、プロジェクトが「コンポーネントの配布」を主目的とするか、「アプリケーションの構築」を主目的とするかで選択を分けるのが賢明です。
単一または複数のフレームワークを用いたアプリケーション開発を主目的とし、最高の開発者体験とパフォーマンスを求める場合に選択します。コンパイラが不要なコードを削除するためバンドルサイズが小さく、状態管理の記述が直感的であるため、プロトタイプから本番環境まで迅速に開発を進めたいプロジェクトに最適です。
標準的な Web コンポーネント仕様に基づき、軽量で高速なカスタム要素を作成したい場合に選択します。Google のエコシステムとの親和性が高く、ポリフィルに依存しない現代的なブラウザ環境で、最小限のボイラープレートで実装を進めたいチームに向いています。
エンタープライズ規模のデザインシステムを構築し、React、Vue、Angular など複数のフレームワークで同一のコンポーネントを利用したい場合に選択します。Ionic Framework の基盤としても知られ、長期的な保守性とフレームワーク非依存性が最優先されるプロジェクトに適しています。
Svelte is a new way to build web applications. It's a compiler that takes your declarative components and converts them into efficient JavaScript that surgically updates the DOM.
Learn more at the Svelte website, or stop by the Discord chatroom.
You can play around with Svelte in the tutorial, examples, and REPL.
When you're ready to build a full-fledge application, we recommend using SvelteKit:
npx sv create my-app
cd my-app
npm install
npm run dev
See the SvelteKit documentation to learn more.
The Changelog for this package is available on GitHub.
Svelte is an MIT-licensed open source project with its ongoing development made possible entirely by fantastic volunteers. If you'd like to support their efforts, please consider:
Funds donated via Open Collective will be used for compensating expenses related to Svelte's development.