Skip to content

Commit a7148bc

Browse files
author
Kate
authored
[docs] Publish and Use an Element docs (#426)
Document how to publish and use a LitElement-based component
1 parent 582d39c commit a7148bc

File tree

3 files changed

+236
-3
lines changed

3 files changed

+236
-3
lines changed

docs/_data/guide.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ toc:
88
url: /guide/start
99
templates:
1010
title: Templates
11-
desc: Create and render LitElement templates. Use JavaScript expressions to add properties and logic.
1211
url: /guide/templates
1312
styles:
1413
title: Styles
1514
desc: Style your elements with CSS.
1615
url: /guide/styles
1716
properties:
1817
title: Properties
19-
desc: Declare and configure a component's properties and attributes.
2018
url: /guide/properties
2119
events:
2220
title: Events
2321
url: /guide/events
2422
lifecycle:
2523
title: Lifecycle
26-
desc: Specify when an element should update. Respond to updates, or wait for an update to complete.
2724
url: /guide/lifecycle
25+
publish:
26+
title: Publish a component
27+
url: /guide/publish
28+
use:
29+
title: Use a component
30+
url: /guide/use

docs/_guide/publish.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
layout: guide
3+
title: Publish an element
4+
slug: publish
5+
---
6+
7+
{::options toc_levels="1..3" /}
8+
* ToC
9+
{:toc}
10+
11+
This page describes how to publish a LitElement component to npm.
12+
13+
We recommend publishing JavaScript modules in standard ES2017. If you're writing your element in standard ES2017, you don't need to transpile for publication. If you're using TypeScript, or ES2017+ features such as decorators or class fields, you will need to transpile your element for publication.
14+
15+
## Publishing to npm
16+
17+
To publish your component to npm, [see the instructions on contributing npm packages](https://docs.npmjs.com/packages-and-modules/contributing-packages-to-the-registry).
18+
19+
Your package.json configuration should have both the `main` and `module` fields:
20+
21+
**package.json**
22+
23+
```json
24+
{
25+
"main": "my-element.js",
26+
"module": "my-element.js"
27+
}
28+
```
29+
30+
You should also create a README describing how to consume your component. A basic guide to consuming LitElement components is documented at [Use a component](use).
31+
32+
## Transpiling with TypeScript
33+
34+
When compiling your code from TypeScript to JavaScript, we recommend targeting ES2017 with Node.js module resolution.
35+
36+
The following JSON sample is a partial tsconfig.json that uses recommended options for targeting ES2017:
37+
38+
```json
39+
"compilerOptions": {
40+
"target": "ES2017",
41+
"module": "ES2017",
42+
"moduleResolution": "node",
43+
"lib": ["ES2017", "DOM"],
44+
"experimentalDecorators": true
45+
}
46+
```
47+
48+
See the [tsconfig.json documentation](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) for more information.
49+
50+
## Transpiling with Babel
51+
52+
To transpile a LitElement component that uses proposed JavaScript features, use Babel.
53+
54+
Install Babel and the Babel plugins you need. For example:
55+
56+
```
57+
npm install --save-dev @babel/core
58+
npm install --save-dev @babel/plugin-proposal-class-properties
59+
npm install --save-dev @babel/proposal-decorators
60+
```
61+
62+
Configure Babel. For example:
63+
64+
**babel.config.js**
65+
66+
```js
67+
const plugins = [
68+
'@babel/plugin-proposal-class-properties',
69+
['@babel/proposal-decorators', { decoratorsBeforeExport: true } ],
70+
];
71+
72+
module.exports = { plugins };
73+
```
74+
75+
You can run Babel via a bundler plugin such as [rollup-plugin-babel](https://www.npmjs.com/package/rollup-plugin-babel), or from the command line. See the [Babel documentation](https://babeljs.io/docs/en/) for more information.

docs/_guide/use.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
layout: guide
3+
title: Use a component
4+
slug: use
5+
---
6+
7+
{::options toc_levels="1..3" /}
8+
* ToC
9+
{:toc}
10+
11+
This page describes how to [use a LitElement component in your application](#use). It also describes how to make sure your deployed code is browser-ready by [building it for production](#build) and [loading the Web Components polyfills](#polyfills).
12+
13+
## Use a LitElement component {#use}
14+
15+
This is a general guide to using third-party LitElement components. Refer to a component's README or other documentation for specific details.
16+
17+
To use a LitElement component in your code:
18+
19+
1. From your project folder, install the component from npm.
20+
21+
```
22+
npm install --save some-package-name
23+
```
24+
25+
2. Import the component.
26+
27+
In a JavaScript module:
28+
29+
```js
30+
import 'some-package-name';
31+
```
32+
33+
In an HTML page:
34+
35+
```html
36+
<script type="module">
37+
import './path-to/some-package-name/some-component.js';
38+
</script>
39+
```
40+
41+
Or:
42+
43+
```html
44+
<script type="module" src="./path-to/some-package-name/some-component.js"></script>
45+
```
46+
47+
3. Add the component to your application or component:
48+
49+
```html
50+
<some-component></some-component>
51+
```
52+
53+
## Develop {#develop}
54+
55+
Elements built with LitElement are published to npm as standard JavaScript modules, which all major browsers can now load.
56+
57+
However, LitElement and elements built with it import their dependencies using bare module specifiers (for example, `import { ... } from 'module-name'`) instead of full paths (`import {...} from '../path/to/module-name'`).
58+
59+
At the time of writing, browsers must still be provided with the full path to a standard JavaScript module in order to load it. To convert bare module specifiers to full paths, a light transform is required.
60+
61+
For a local server that does this automatically, try the [Open Web Components development server](https://open-wc.org/developing/owc-dev-server.html).
62+
63+
## Build for production {#build}
64+
65+
To build for production, you can use a bundler such as WebPack or Rollup.
66+
67+
The following example configuration for [Rollup](https://rollupjs.org/guide/en) resolves dependencies, converts bare module specifers to paths, and bundles the output.
68+
69+
**rollup.config.js**
70+
71+
```js
72+
import resolve from 'rollup-plugin-node-resolve';
73+
74+
export default {
75+
// If using any exports from a symlinked project, uncomment the following:
76+
// preserveSymlinks: true,
77+
input: ['src/index.js'],
78+
output: {
79+
file: 'build/index.js',
80+
format: 'es',
81+
sourcemap: true
82+
},
83+
plugins: [
84+
resolve()
85+
]
86+
};
87+
```
88+
89+
See a [sample build configuration for LitElement with Babel and Rollup](https://github.com/PolymerLabs/lit-element-build-rollup/blob/master/src/index.html).
90+
91+
## Load the WebComponents polyfills {#polyfills}
92+
93+
Elements built with LitElement use the Web Components set of standards, which are currently supported by all major browsers with the exception of Edge.
94+
95+
For compatibility with older browsers and Edge, load the Web Components polyfills.
96+
97+
To load the WebComponents polyfills:
98+
99+
1. From your project folder, install the `@webcomponents/webcomponentsjs` package:
100+
101+
```
102+
npm install --save-dev @webcomponents/webcomponentsjs
103+
```
104+
105+
2. Add the polyfills to your HTML entrypoint:
106+
107+
```html
108+
<head>
109+
<!--
110+
If you are loading es5 code you will need
111+
custom-elements-es5-loader to make the element work in
112+
es6-capable browsers.
113+
114+
If you are not loading es5 code, you don't need
115+
custom-elements-es5-loader.
116+
-->
117+
<!--
118+
<script src="./path-to/custom-elements-es5-loader.js"></script>
119+
-->
120+
121+
<!-- Load polyfills -->
122+
<script
123+
src="path-to/webcomponents-loader.js"
124+
defer>
125+
</script>
126+
127+
<!-- Load component when polyfills are definitely ready -->
128+
<script type="module">
129+
// Take care of cases in which the browser runs this
130+
// script before it has finished running
131+
// webcomponents-loader.js (e.g. Firefox script execution order)
132+
window.WebComponents = window.WebComponents || {
133+
waitFor(cb){ addEventListener('WebComponentsReady', cb) }
134+
}
135+
136+
WebComponents.waitFor(async () => {
137+
import('./path-to/some-element.js');
138+
});
139+
</script>
140+
</head>
141+
<body>
142+
<!-- Add the element to the page -->
143+
<some-element></some-element>
144+
</body>
145+
```
146+
147+
3. Ensure that `node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js` and `node_modules/@webcomponents/webcomponentsjs/bundles/**.*` are included in your build.
148+
149+
<div class="alert">
150+
151+
**Do not transpile the polyfills.** Bundling them is okay.
152+
153+
</div>
154+
155+
See [the Webcomponentsjs documentation](https://github.com/webcomponents/webcomponentsjs) for more information.

0 commit comments

Comments
 (0)