UNPKG

9.39 kBMarkdownView Raw
1[![npm][npm-badge]][npm-badge-url]
2[![Build Status][circle-badge]][circle-badge-url]
3[![Appveyor Build status][appveyor-badge]][appveyor-badge-url]
4[![BrowserStack Status][browserstack-badge]][browserstack-badge-url]
5[![license][npm-license]][npm-license-url]
6
7
8# Stencil: A Compiler for Web Components and PWAs
9
10```bash
11npm init stencil
12```
13
14[Stencil](https://stenciljs.com/) is a simple compiler for generating Web Components and progressive web apps (PWA). Stencil was built by the [Ionic Framework](http://ionicframework.com/) team for its next generation of performant mobile and desktop Web Components.
15
16Stencil combines the best concepts of the most popular frontend frameworks into a compile-time rather than run-time tool. It takes TypeScript, JSX, a tiny virtual DOM layer, efficient one-way data binding, an asynchronous rendering pipeline (similar to React Fiber), and lazy-loading out of the box, and generates 100% standards-based Web Components that run on both [modern browsers and legacy browsers](#browser-support) back to Internet Explorer 11.
17
18Stencil components are just Web Components, so they work in any major framework or with no framework at all. In many cases, Stencil can be used as a drop in replacement for traditional frontend frameworks given the capabilities now available in the browser, though using it as such is certainly not required.
19
20Stencil also enables a number of key capabilities on top of Web Components, in particular Server Side Rendering (SSR) without the need to run a headless browser, pre-rendering, and objects-as-properties (instead of just strings).
21
22*Note: Stencil and [Ionic](https://ionicframework.com/) are completely independent projects. Stencil does not prescribe any specific UI framework, but Ionic is the largest user of Stencil (today!)*
23
24
25## Why Stencil?
26
27Stencil is a new approach to a popular idea: building fast and feature-rich apps in the browser. Stencil was created to take advantage of major new capabilities available natively in the browser, such as Custom Elements v1, enabling developers to ship far less code and build faster apps that are compatible with any and all frameworks.
28
29Stencil is also a solution to organizations and library authors struggling to build reusable components across a diverse spectrum of frontend frameworks, each with their own component system. Stencil components work in Angular, React, Ember, and Vue as well as they work with jQuery or with no framework at all, because they are just plain HTML elements.
30
31Compared to using Custom Elements directly, inside of every Stencil component is an efficient Virtual DOM rendering system, JSX rendering capabilities, asynchronous rendering pipeline (like React Fiber), and more. This makes Stencil components more performant while maintaining full compatibility with plain Custom Elements. Think of Stencil as creating pre-baked Custom Elements as if you wrote in those features yourself.
32
33
34## Getting Started
35
36To create a new project using an interactive cli, run:
37
38```bash
39npm init stencil
40```
41
42To start developing your new Stencil project, run:
43
44```bash
45npm start
46```
47
48
49## Creating components
50
51Stencil components are plain ES6/TypeScript classes with some decorator metadata.
52
53Create new components by creating files with a `.tsx` extension, such as `my-component.tsx`, and place them in `src/components`.
54
55```typescript
56import { Component, Prop } from '@stencil/core';
57
58@Component({
59 tag: 'my-component',
60 styleUrl: 'my-component.css'
61})
62export class MyComponent {
63
64 @Prop() first: string;
65
66 @Prop() last: string;
67
68 render() {
69 return (
70 <div>
71 Hello, my name is {this.first} {this.last}
72 </div>
73 );
74 }
75}
76```
77
78Note: the `.tsx` extension is required, as this is the standard for TypeScript classes that use JSX.
79
80To use this component, just use it like any other HTML element:
81
82```html
83<my-component first="Stencil" last="JS"></my-component>
84```
85
86
87## Naming Components
88
89When creating new component tags, we recommend _not_ using `stencil` in the component name (ex: `<stencil-datepicker>`). This is because the generated component has little to nothing to do with Stencil; it's just a web component!
90
91Instead, use a prefix that fits your company or any name for a group of related components. For example, all of the [Ionic](https://ionicframework.com/docs/) generated web components use the prefix `ion`.
92
93
94## Hosting the app
95
96Stencil components run directly in the browser through script includes just like normal Custom Elements (because they are just that!), and run by using the tag just like any other HTML component:
97
98Here's an example `index.html` file that runs a Stencil app:
99
100```html
101<!DOCTYPE html>
102<html>
103<head>
104 <title>My App</title>
105 <script src="build/app.js"></script>
106</head>
107<body>
108 <my-component first="Stencil" last="JS"></my-component>
109</body>
110</html>
111```
112
113
114## API
115
116The API for stencil closely mirrors the API for Custom Elements v1.
117
118### Components
119
120| Decorator | Description |
121| -------------- | --- |
122| `@Component()` | Indicate a class is a Stencil component. |
123| | |
124| `@Prop()` | Creates a property that will exist on the element and be data-bound to this component. |
125| `@State()` | Creates a local state variable that will not be placed on the element. |
126| `@Method()` | Expose specific methods to be publicly accessible. |
127
128
129## Why "Stencil?"
130
131A Stencil is a tool artists use for drawing perfect shapes easily. We want Stencil to be a similar tool for web developers: a tool that helps web developers build powerful Web Components and apps that use them, but without creating non-standard runtime requirements.
132
133Stencil is a tool developers use to create Web Components with some powerful features baked in, but it gets out of the way at runtime.
134
135[Using Web Components in Ionic - Polymer Summit 2017](https://youtu.be/UfD-k7aHkQE)
136
137
138## Browser Support
139
140Web Components, specifically Custom Elements, are natively supported in Chrome and Safari and are coming to both Edge and Firefox. A dynamic polyfill loader is already included in order to only load the polyfills for the browsers that are missing specific features.
141
142 - Chrome (and all Chromium based browsers)
143 - Safari
144 - Edge
145 - Firefox
146 - IE 11
147
148
149## Polyfills
150
151Stencil includes a subset of the `core-js` polyfills for old browsers like IE11, `fetch` and conditionally downloads the [Custom Elements v1](https://github.com/webcomponents/polyfills/tree/master/packages/custom-elements) only when it's needed for modern browsers (EDGE and old versions of Firefox.)
152
153
154### Internet Explorer 11
155
156Browser that does not support native ESM (at the moment, only IE11 and older) will download a subset of [`core-js`](https://github.com/zloirock/core-js).
157
158This subset is generated using the [`core-js-builder` tool](https://github.com/zloirock/core-js/tree/master/packages/core-js-builder) with the following configuration:
159
160```js
161require('core-js-builder')({
162 targets: 'ie 11',
163 modules: [
164 'es',
165 'web.url',
166 'web.url.to-json',
167 'web.url-search-params'
168 ],
169 blacklist: [
170 'es.math',
171 'es.date',
172 'es.symbol',
173 'es.array-buffer',
174 'es.data-view',
175 'es.typed-array',
176 'es.reflect'
177 ]
178});
179```
180
181In addition, the following set of polyfills are also included:
182
183 - [Promise](https://github.com/stefanpenner/es6-promise)
184 - [fetch()](https://github.com/github/fetch)
185 - [CSS variables](https://github.com/ionic-team/stencil/tree/master/src/client/polyfills/css-shim): We implemented out own CSS variables polyfill that integrates into the Stencil¡s run
186
187### All browsers
188
189Some modern browsers today like Edge does not include native support for web components, in that case we conditionally load the [Custom Elements v1](https://github.com/webcomponents/polyfills/tree/master/packages/custom-elements) polyfill.
190
191
192## Related
193
194 - [Stencil Documentation](https://stenciljs.com/)
195 - [Stencil Worldwide Slack](https://stencil-worldwide.herokuapp.com)
196 - [Ionic](https://ionicframework.com/)
197 - [Ionic Worldwide Slack](http://ionicworldwide.herokuapp.com/)
198 - [Ionicons](http://ionicons.com/)
199 - [Capacitor](https://capacitor.ionicframework.com/)
200
201
202## License
203
204 - [MIT](https://raw.githubusercontent.com/ionic-team/stencil/master/LICENSE)
205
206
207[npm-badge]: https://img.shields.io/npm/v/@stencil/core.svg
208[npm-badge-url]: https://www.npmjs.com/package/@stencil/core
209[npm-license]: https://img.shields.io/npm/l/@stencil/core.svg
210[npm-license-url]: https://github.com/ionic-team/stencil/blob/master/LICENSE
211[circle-badge]: https://circleci.com/gh/ionic-team/stencil.svg?style=shield
212[circle-badge-url]: https://circleci.com/gh/ionic-team/stencil
213[browserstack-badge]: https://www.browserstack.com/automate/badge.svg?badge_key=WVNVbkRJdDBJQnBEMzZuWUdlMEZuTjlPUm9sOHZsSVNkUlJTRkJVQkx0ST0tLTFhbk5jRUNEVWxJL1J0SVR0WUFndnc9PQ==--90c84981a2ed2ede760ca48fbfc3fdd5b71d3e5e
214[browserstack-badge-url]: https://www.browserstack.com/automate/public-build/WVNVbkRJdDBJQnBEMzZuWUdlMEZuTjlPUm9sOHZsSVNkUlJTRkJVQkx0ST0tLTFhbk5jRUNEVWxJL1J0SVR0WUFndnc9PQ==--90c84981a2ed2ede760ca48fbfc3fdd5b71d3e5e
215[appveyor-badge]: https://ci.appveyor.com/api/projects/status/92d75dgkohgyap5r/branch/master?svg=true
216[appveyor-badge-url]: https://ci.appveyor.com/project/Ionitron/stencil/branch/master