|
| 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