1 | ---
|
2 | permalink: 'building/building-rollup.html'
|
3 | title: Rollup
|
4 | section: guides
|
5 | tags:
|
6 | - guides
|
7 | ---
|
8 |
|
9 | # Rollup
|
10 |
|
11 | Rollup configuration to help you get started building modern web applications.
|
12 | You write modern javascript using the latest browser features, rollup will optimize your code for production and ensure it runs on all supported browsers.
|
13 |
|
14 | > Part of [Open Web Components](https://github.com/open-wc/open-wc/): guides, tools and libraries for modern web development and web components
|
15 |
|
16 | [![CircleCI](https://circleci.com/gh/open-wc/open-wc.svg?style=shield)](https://circleci.com/gh/open-wc/open-wc)
|
17 | [![BrowserStack Status](https://www.browserstack.com/automate/badge.svg?badge_key=M2UrSFVRang2OWNuZXlWSlhVc3FUVlJtTDkxMnp6eGFDb2pNakl4bGxnbz0tLUE5RjhCU0NUT1ZWa0NuQ3MySFFWWnc9PQ==--86f7fac07cdbd01dd2b26ae84dc6c8ca49e45b50)](https://www.browserstack.com/automate/public-build/M2UrSFVRang2OWNuZXlWSlhVc3FUVlJtTDkxMnp6eGFDb2pNakl4bGxnbz0tLUE5RjhCU0NUT1ZWa0NuQ3MySFFWWnc9PQ==--86f7fac07cdbd01dd2b26ae84dc6c8ca49e45b50)
|
18 | [![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com/)
|
19 |
|
20 | ## Features
|
21 |
|
22 | - Set HTML or JS as input and/or output
|
23 | - Optimized for browsers which support modules
|
24 | - Optional separate build for legacy browsers
|
25 | - Loads polyfills using feature detection
|
26 | - Generates a service worker
|
27 | - Minifies JS
|
28 | - Minifies lit-html templates
|
29 |
|
30 | ## Automatic setup
|
31 |
|
32 | We recommend the open-wc [project generator](https://open-wc.org/init/) for automated setup, for new projects or to upgrade existing projects.
|
33 |
|
34 | ## Manual setup
|
35 |
|
36 | Install the required dependencies:
|
37 |
|
38 | <details>
|
39 | <summary>View</summary>
|
40 |
|
41 | ```bash
|
42 | npm i -D rollup @open-wc/building-rollup rimraf deepmerge es-dev-server
|
43 | ```
|
44 |
|
45 | </details>
|
46 |
|
47 | Create a `rollup.config.js` file:
|
48 |
|
49 | <details>
|
50 | <summary>View</summary>
|
51 |
|
52 | ```js
|
53 | import merge from 'deepmerge';
|
54 | // use createSpaConfig for bundling a Single Page App
|
55 | import { createSpaConfig } from '@open-wc/building-rollup';
|
56 |
|
57 | // use createBasicConfig to do regular JS to JS bundling
|
58 | // import { createBasicConfig } from '@open-wc/building-rollup';
|
59 |
|
60 | const baseConfig = createSpaConfig({
|
61 | // use the outputdir option to modify where files are output
|
62 | // outputDir: 'dist',
|
63 |
|
64 | // if you need to support older browsers, such as IE11, set the legacyBuild
|
65 | // option to generate an additional build just for this browser
|
66 | // legacyBuild: true,
|
67 |
|
68 | // development mode creates a non-minified build for debugging or development
|
69 | developmentMode: process.env.ROLLUP_WATCH === 'true',
|
70 |
|
71 | // set to true to inject the service worker registration into your index.html
|
72 | injectServiceWorker: false,
|
73 | });
|
74 |
|
75 | export default merge(baseConfig, {
|
76 | // if you use createSpaConfig, you can use your index.html as entrypoint,
|
77 | // any <script type="module"> inside will be bundled by rollup
|
78 | input: './index.html',
|
79 |
|
80 | // alternatively, you can use your JS as entrypoint for rollup and
|
81 | // optionally set a HTML template manually
|
82 | // input: './app.js',
|
83 | });
|
84 | ```
|
85 |
|
86 | </details>
|
87 |
|
88 | Add the following NPM scripts to your `package.json`:
|
89 |
|
90 | <details>
|
91 | <summary>View</summary>
|
92 |
|
93 | ```json
|
94 | {
|
95 | "scripts": {
|
96 | "build": "rimraf dist && rollup -c rollup.config.js",
|
97 | "start:build": "npm run build && es-dev-server --root-dir dist --app-index index.html --compatibility none --open"
|
98 | }
|
99 | }
|
100 | ```
|
101 |
|
102 | </details>
|
103 |
|
104 | ### SPA Projects
|
105 |
|
106 | If you have a SPA (Single Page App) project you can use `createSpaConfig` to generate your config. This will inject the rollup output into a single index.html file, generate a service worker and load polyfills using feature detection.
|
107 |
|
108 | #### HTML as input
|
109 |
|
110 | You can set an index.html file as rollup input. Any modules inside will be bundled by rollup and the final output will be injected into the final HTML file.
|
111 |
|
112 | <details>
|
113 | <summary>View example</summary>
|
114 |
|
115 | ```js
|
116 | import merge from 'deepmerge';
|
117 | import { createBasicConfig } from '@open-wc/building-rollup';
|
118 |
|
119 | const baseConfig = createBasicConfig();
|
120 |
|
121 | export default merge(baseConfig, {
|
122 | input: './index.html',
|
123 | });
|
124 | ```
|
125 |
|
126 | </details>
|
127 |
|
128 | #### Custom HTML template
|
129 |
|
130 | You can also provide the HTML as a template, as a string or read from a file. See the [html plugin docs](https://github.com/open-wc/open-wc/tree/master/packages/rollup-plugin-html#readme) for all possible options.
|
131 |
|
132 | <details>
|
133 | <summary>View example</summary>
|
134 |
|
135 | ```js
|
136 | import merge from 'deepmerge';
|
137 | import { createBasicConfig } from '@open-wc/building-rollup';
|
138 |
|
139 | const baseConfig = createBasicConfig({
|
140 | html: {
|
141 | template: /* your template goes here */,
|
142 | },
|
143 | });
|
144 |
|
145 | export default merge(baseConfig, {
|
146 | input: './src/app.js',
|
147 | });
|
148 | ```
|
149 |
|
150 | </details>
|
151 |
|
152 | ### Non-SPA projects
|
153 |
|
154 | If you are not building a single page app you can use `createBasicConfig` to set up regular JS to JS bundling.
|
155 |
|
156 | From there on it's easy to extend further for different use cases. For example, to bundle and generate multiple HTML files you can take a look at the documentation of [@open-wc/rollup-plugin-html](https://github.com/open-wc/open-wc/tree/master/packages/rollup-plugin-html#readme). See below how to add plugins to the rollup config.
|
157 |
|
158 | ## Injecting a service worker
|
159 |
|
160 | When you use `createSpaConfig` a service worker is generated automatically with Workbox. The service worker registration is not injected into your `index.html` by default to prevent unexpected results. You can turn this on by enabling the `injectServiceWorker` option:
|
161 |
|
162 | ```js
|
163 | const baseConfig = createSpaConfig({
|
164 | injectServiceWorker: true,
|
165 | });
|
166 | ```
|
167 |
|
168 | If you overwrite the workbox configuration and the `swDest` property of the workbox config, `injectServiceWorker` will automatically use the value of `swDest` in the service worker registration. Example:
|
169 |
|
170 | ```js
|
171 | const baseConfig = createSpaConfig({
|
172 | injectServiceWorker: true,
|
173 | workbox: {
|
174 | swDest: './my-sw.js',
|
175 | },
|
176 | });
|
177 | ```
|
178 |
|
179 | Will result in:
|
180 |
|
181 | ```js
|
182 | navigator.serviceWorker.register('./my-sw.js');
|
183 | ```
|
184 |
|
185 | ## Supporting older browsers
|
186 |
|
187 | The default build output works only on browsers that support modules. If you need to support older browsers, such as IE11 or the old Edge, you can set the `legacyBuild` option when you use the create config function.
|
188 |
|
189 | This will create a separate rollup build output for legacy browsers and makes sure the correct version is loaded. This has minimal impact on users with modern browsers.
|
190 |
|
191 | ```js
|
192 | const baseConfig = createSpaConfig({
|
193 | legacyBuild: true,
|
194 | });
|
195 | ```
|
196 |
|
197 | ## Customizations
|
198 |
|
199 | Our config generator sets you up with good defaults for most projects. It's easy to extend and customize this config further for your requirements.
|
200 |
|
201 | If you find yourself disabling a lot of the default functionality we recommend forking from the default config and taking control yourself. Rollup is relatively easy to configure compared to other build tools, it's better to be in full control if you know what you're doing.
|
202 |
|
203 | ### Customizing the babel config
|
204 |
|
205 | You can define custom babel plugins to be loaded by adding a `.babelrc` or `babel.config.js` to your project. See [babeljs config](https://babeljs.io/docs/en/configuration) for more information.
|
206 |
|
207 | For example to add support for class properties:
|
208 |
|
209 | ```json
|
210 | {
|
211 | "plugins": ["@babel/plugin-proposal-class-properties"]
|
212 | }
|
213 | ```
|
214 |
|
215 | ### Customizing default plugins
|
216 |
|
217 | Our config creators install a number of rollup plugins by default:
|
218 |
|
219 | Basic and SPA config plugins:
|
220 |
|
221 | - [nodeResolve](https://github.com/rollup/plugins/tree/master/packages/node-resolve#readme)
|
222 | - [babel](https://github.com/rollup/plugins/tree/master/packages/babel#readme)
|
223 | - [terser](https://github.com/TrySound/rollup-plugin-terser#readme)
|
224 |
|
225 | SPA config plugins:
|
226 |
|
227 | - [html](https://github.com/open-wc/open-wc/tree/master/packages/rollup-plugin-html#readme)
|
228 | - [polyfillsLoader](https://github.com/open-wc/open-wc/tree/master/packages/polyfills-loader#readme)
|
229 | - [workbox](https://www.npmjs.com/package/rollup-plugin-workbox)
|
230 |
|
231 | You can customize options for these plugins, or turn them off completely by setting them to false. Check the documentation for the respective plugins to learn which options can be configured.
|
232 |
|
233 | <details>
|
234 | <summary>View example</summary>
|
235 |
|
236 | Each plugin can be either "true", "false" or an object. If it's an object, this is used as a configuration for the plugin.
|
237 |
|
238 | ```js
|
239 | const baseConfig = createSpaConfig({
|
240 | nodeResolve: { browser: true, dedupe: ['lit-html'] },
|
241 | babel: true,
|
242 | terser: { exclude: ['node_modules*'] },
|
243 | html: false,
|
244 | polyfillsLoader: false,
|
245 | workbox: false,
|
246 | });
|
247 | ```
|
248 |
|
249 | </details>
|
250 |
|
251 | Examples:
|
252 |
|
253 | ### Customize HTML
|
254 |
|
255 | [@open-wc/rollup-plugin-html](https://github.com/open-wc/open-wc/tree/master/packages/rollup-plugin-html#readme) powers a lot of what our rollup config does. It has a lot of options available, for example, to transform the HTML output or set a different template.
|
256 |
|
257 | We recommend looking into the documentation to get an overview of all available options.
|
258 |
|
259 | <details>
|
260 | <summary>View example</summary>
|
261 |
|
262 | ```js
|
263 | import packageJson from './package.json';
|
264 |
|
265 | const baseConfig = createSpaConfig({
|
266 | html: {
|
267 | transform: [
|
268 | // inject lang attribute
|
269 | html => html.replace('<html>', '<html lang="en-GB">'),
|
270 | // inject app version
|
271 | html =>
|
272 | html.replace(
|
273 | '</body>',
|
274 | `<script>window.APP_VERSION = "${packageJson.version}"</script></body>`,
|
275 | ),
|
276 | ],
|
277 | },
|
278 | });
|
279 | ```
|
280 |
|
281 | </details>
|
282 |
|
283 | ### Customize polyfills
|
284 |
|
285 | [@open-wc/rollup-plugin-polyills-loader](https://github.com/open-wc/open-wc/tree/master/packages/rollup-plugin-polyfills-loader#readme) loads polyfills only when necessary based on feature detection.
|
286 |
|
287 | You can prevent certain polyfills from being loaded or add your own polyfills.
|
288 |
|
289 | <details>
|
290 | <summary>View example</summary>
|
291 |
|
292 | ```js
|
293 | const baseConfig = createSpaConfig({
|
294 | polyfillsLoader: {
|
295 | polyfills: {
|
296 | webcomponents: false,
|
297 | intersectionObserver: true,
|
298 | resizeObserver: true,
|
299 | custom: [
|
300 | {
|
301 | name: 'my-feature-polyfill',
|
302 | path: require.resolve('my-feature-polyfill'),
|
303 | test: "!('myFeature' in window)",
|
304 | minify: true,
|
305 | },
|
306 | ],
|
307 | },
|
308 | },
|
309 | });
|
310 | ```
|
311 |
|
312 | </details>
|
313 |
|
314 | ### Customize built-in babel plugins
|
315 |
|
316 | We add some babel plugins by default. These can be overwritten with a different configuration from the config. For example to change the html template minification, or add other modules to be minified:
|
317 |
|
318 | <details>
|
319 | <summary>View example</summary>
|
320 |
|
321 | ```js
|
322 | const baseConfig = createSpaConfig({
|
323 | babel: {
|
324 | plugins: [
|
325 | [
|
326 | require.resolve('babel-plugin-template-html-minifier'),
|
327 | {
|
328 | modules: {
|
329 | 'cool-html': ['html'],
|
330 | },
|
331 | htmlMinifier: {
|
332 | removeComments: false,
|
333 | },
|
334 | },
|
335 | ],
|
336 | ],
|
337 | },
|
338 | });
|
339 | ```
|
340 |
|
341 | </details>
|
342 |
|
343 | ## Extending the rollup config
|
344 |
|
345 | A rollup config is just a plain object. It's easy to extend it using javascript. We recommend using the `deepmerge` library because it is an easy way to merge objects and arrays:
|
346 |
|
347 | <details>
|
348 | <summary>View example</summary>
|
349 |
|
350 | ```javascript
|
351 | import merge from 'deepmerge';
|
352 | import { createSpaConfig } from '@open-wc/building-rollup';
|
353 |
|
354 | const baseConfig = createSpaConfig();
|
355 |
|
356 | export default merge(baseConfig, {
|
357 | // add your own rollup configuration here
|
358 | input: './index.html',
|
359 | output: {
|
360 | sourcemap: false,
|
361 | },
|
362 | plugins: [
|
363 | // add new plugins
|
364 | myPlugin(),
|
365 | ],
|
366 | });
|
367 | ```
|
368 |
|
369 | </details>
|
370 |
|
371 | If you have enabled the legacy build option, the `output` option is an array. In that case, you cannot use deepmerge if you need to make changes to the `output` option.
|
372 |
|
373 | <details>
|
374 | <summary>View example</summary>
|
375 |
|
376 | ```javascript
|
377 | import merge from 'deepmerge';
|
378 | import { createSpaConfig } from '@open-wc/building-rollup';
|
379 |
|
380 | const baseConfig = createSpaConfig({
|
381 | legacyBuild: true,
|
382 | });
|
383 |
|
384 | // set the sourcemap option on both outputs
|
385 | baseConfig.output[0].sourcemap = true;
|
386 | baseConfig.output[1].sourcemap = true;
|
387 |
|
388 | export default merge(baseConfig, {
|
389 | input: './index.html',
|
390 | plugins: [
|
391 | // add new plugins
|
392 | myPlugin(),
|
393 | ],
|
394 | });
|
395 | ```
|
396 |
|
397 | </details>
|
398 |
|
399 | #### Copying assets
|
400 |
|
401 | To copy over assets, such as images, css or json files, we recommend using [rollup-plugin-copy](https://www.npmjs.com/package/rollup-plugin-copy)
|
402 |
|
403 | <details>
|
404 | <summary>View example</summary>
|
405 |
|
406 | ```js
|
407 | import merge from 'deepmerge';
|
408 | import { createSpaConfig } from '@open-wc/building-rollup';
|
409 | import copy from 'rollup-plugin-copy';
|
410 |
|
411 | const baseConfig = createSpaConfig();
|
412 |
|
413 | export default merge(baseConfig, {
|
414 | input: './index.html',
|
415 | plugins: [
|
416 | copy({
|
417 | targets: [{ src: 'assets/**/*', dest: '/dist' }],
|
418 | // set flatten to false to preserve folder structure
|
419 | flatten: false,
|
420 | }),
|
421 | ],
|
422 | });
|
423 | ```
|
424 |
|
425 | </details>
|
426 |
|
427 | #### Support for CommonJs modules
|
428 |
|
429 | Rollup only supports standard es modules (using `import` and `export`). A lot of projects don't use this syntax yet, and instead use the CommonJs module format. This format uses `require` and `module.exports` statements, and is intended for NodeJs.
|
430 |
|
431 | To support this in Rollup, you can add the [@rollup/plugin-commonjs](https://github.com/rollup/plugins/tree/master/packages/commonjs) plugin.
|
432 |
|
433 | <details>
|
434 | <summary>View example</summary>
|
435 |
|
436 | ```js
|
437 | import merge from 'deepmerge';
|
438 | import { createSpaConfig } from '@open-wc/building-rollup';
|
439 | import commonjs from '@rollup/plugin-commonjs';
|
440 |
|
441 | const baseConfig = createSpaConfig();
|
442 |
|
443 | export default merge(baseConfig, {
|
444 | input: './index.html',
|
445 | plugins: [commonjs()],
|
446 | });
|
447 | ```
|
448 |
|
449 | </details>
|
450 |
|
451 | #### Support for Typescript
|
452 |
|
453 | <details>
|
454 |
|
455 | <summary>View example</summary>
|
456 |
|
457 | To support Typescript in rollup you have multiple options. You can run `tsc`, and then run rollup on the generated JS files. This is useful when you are already running `tsc` for use in other tools, such as a dev server. You can also use the [@rollup/plugin-typescript](https://github.com/rollup/plugins/tree/master/packages/typescript) plugin to integrate with rollup more directly. View their documentation for more information.
|
458 |
|
459 | ```js
|
460 | import merge from 'deepmerge';
|
461 | import { createSpaConfig } from '@open-wc/building-rollup';
|
462 | import typescript from '@rollup/plugin-typescript';
|
463 |
|
464 | const baseConfig = createSpaConfig();
|
465 |
|
466 | export default merge(baseConfig, {
|
467 | input: './index.html',
|
468 | plugins: [typescript()],
|
469 | });
|
470 | ```
|
471 |
|
472 | </details>
|