You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/lit-dev-content/site/docs/v3/tools/publishing.md
+102-1Lines changed: 102 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,6 +104,101 @@ You can adjust the `"targets"` option to target browsers you wish to support. Se
104
104
105
105
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.
106
106
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:
|[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
+
importresolvefrom"@rollup/plugin-node-resolve";
144
+
importtypescriptfrom"@rollup/plugin-typescript";
145
+
146
+
exportdefault [
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
+
107
202
## Publishing best practices
108
203
109
204
The following are other good practices to follow when publishing reusable Web Components.
@@ -126,7 +221,7 @@ Optimizing modules before publication may also prevent application-level optimiz
126
221
127
222
Bundling and other optimizations can be valuable when serving a module from a CDN, but since users may need to use multiple packages that depend on Lit, serving from a CDN can result in users loading more code than necessary. For these reasons we recommend performance-sensitive applications always build from npm where packages can be deduplicated, rather than loading bundled packages off of a CDN.
128
223
129
-
If you want to support usage from a CDN, we recommend making a clear separation between the CDN modules and the modules intended for production use. For example, placing them in a separate folder, or only adding them as part of a GitHub release and not adding them to the published npm module.
224
+
If you want to support usage from a CDN, we recommend making a clear separation between the CDN modules and the modules intended for production use. For example, placing them in a separate folder, or only adding them as part of a GitHub release and not adding them to the published npm module.
130
225
131
226
### Include file extensions in import specifiers
132
227
@@ -154,6 +249,12 @@ could greatly bloat the import map.
154
249
Thus, to prepare your source now to be optimally compatible with import maps, we
155
250
recommend authoring with file extensions on imports.
156
251
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
+
157
258
### Publish TypeScript typings
158
259
159
260
To make your element easy to use from TypeScript, we recommend that you:
0 commit comments