Skip to content

Commit 7990f1b

Browse files
committed
Add bundling with rollup section to publishing guide
1 parent bd81b0c commit 7990f1b

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

packages/lit-dev-content/site/docs/v3/tools/publishing.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,101 @@ You can adjust the `"targets"` option to target browsers you wish to support. Se
104104

105105
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.
106106

107+
### Bundling with Rollup
108+
109+
While the TypeScript compiler (or Babel) transforms your source code into JavaScript, the output remains a collection of individual files. Bundling with Rollup takes those compiled files and produces a build - combining modules and generating distributable formats (such as ES modules). This process ensures your Lit components are easy to consume, efficient in the browser, and ready to publish.
110+
111+
Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application.
112+
113+
First, install Rollup along with the plugins needed for TypeScript and Node-style module resolution:
114+
115+
```sh
116+
npm install --save-dev \
117+
rollup \
118+
@rollup/plugin-node-resolve \
119+
@rollup/plugin-typescript
120+
```
121+
122+
When using `@rollup/plugin-typescript` tslib may be required:
123+
124+
```sh
125+
npm install tslib
126+
```
127+
128+
> `tslib` is a runtime helper library for TypeScript. When TypeScript transpiles modern JavaScript/TypeScript features like classes, decorators, extends, async/await, or object spread - it sometimes needs helper functions.
129+
130+
Other rollup plugins you might find helpful, depending on use case:
131+
132+
| Name | Use Case |
133+
|-----------------------|------------------------------------------------------------------------|
134+
| [rollup-plugin-cleanup](https://www.npmjs.com/package/rollup-plugin-cleanup) | Trims trailing spaces, compact empty lines, and normalize line endings |
135+
| [rollup-plugin-delete](https://www.npmjs.com/package/rollup-plugin-delete) | Delete files and folders using Rollup |
136+
| [rollup-plugin-dts](https://www.npmjs.com/package/rollup-plugin-dts) | Bundles up your `.d.ts` definition files |
137+
138+
Then create a Rollup configuration file. For example:
139+
140+
**rollup.config.js**
141+
142+
```javaScript
143+
import resolve from "@rollup/plugin-node-resolve";
144+
import typescript from "@rollup/plugin-typescript";
145+
146+
export default [
147+
// Browser build (ESM)
148+
{
149+
input: "my-element.ts",
150+
external: ["lit", "lit/decorators.js", "tslib"],
151+
// ^ Exclude Lit, tslib and other external dpendencies from the bundle so it isn't duplicated.
152+
// consumers should install and import their own Lit version.
153+
output: {
154+
file: "dist/my-element.js", // Output folder for compiled bundle files
155+
format: "esm",
156+
},
157+
plugins: [
158+
resolve(),
159+
typescript({
160+
tsconfig: "./tsconfig.json",
161+
}),
162+
],
163+
},
164+
];
165+
```
166+
167+
Also, update your `package.json` and `tsconfig.json` to ensure compatibility with Rollup bundling.
168+
169+
**package.json**
170+
171+
```json
172+
"type": "module",
173+
"main": "dist/my-element.js",
174+
"module": "dist/my-element.js",
175+
"types": "dist/my-element.d.ts",
176+
"scripts": {
177+
"build": "rollup -c"
178+
}
179+
```
180+
181+
**tsconfig.json**
182+
183+
```jsonc
184+
{
185+
"compilerOptions": {
186+
"target": "es2021",
187+
"module": "es2015",
188+
"moduleResolution": "node",
189+
"lib": ["es2021", "dom"],
190+
"declaration": true,
191+
"declarationMap": true,
192+
"experimentalDecorators": true,
193+
"useDefineForClassFields": false,
194+
"outDir": "dist"
195+
},
196+
"exclude": ["dist", "node_modules"]
197+
}
198+
```
199+
200+
Now, running `npm run build` will output your compiled files to the `dist/` folder. This is a minimal setup - see the [Rollup documentation](https://rollupjs.org/configuration-options/) for additional configuration options.
201+
107202
## Publishing best practices
108203

109204
The following are other good practices to follow when publishing reusable Web Components.
@@ -154,6 +249,12 @@ could greatly bloat the import map.
154249
Thus, to prepare your source now to be optimally compatible with import maps, we
155250
recommend authoring with file extensions on imports.
156251

252+
### Include and mark peer dependencies
253+
254+
Including peer dependencies specifies packages that are required by your library but are expected to be provided by the consumer.
255+
256+
For example, you should mark Lit (and, in most cases, any other runtime dependency) as a peer dependency in your `package.json`. See the [npm documentation on peerDependencies](https://docs.npmjs.com/cli/v11/configuring-npm/package-json#peerdependencies) for more details.
257+
157258
### Publish TypeScript typings
158259

159260
To make your element easy to use from TypeScript, we recommend that you:

0 commit comments

Comments
 (0)