|
| 1 | +# rollup-plugin-css-lit |
| 2 | + |
| 3 | +[ |
| 4 | +  |
| 5 | +](https://www.npmjs.com/package/rollup-plugin-css-lit) |
| 6 | +[](https://codecov.io/gh/prantlf/rollup-plugin-css-lit) |
| 7 | + |
| 8 | +[Rollup] plugin for importing CSS sources as constructable stylesheets to projects using [lit] ([lit-html] and [lit-element]) or [fast-element]. |
| 9 | + |
| 10 | +Faster than the combination of [rollup-plugin-styles] and [rollup-plugin-lit-css]. Supports minifying by [cssnano], inlining by [postcss-import] and [postcss-ulrl] or fully customisable transformations of the CSS input by [PostCSS]. |
| 11 | + |
| 12 | +## Synopsis |
| 13 | + |
| 14 | +Custom element: |
| 15 | + |
| 16 | +```js |
| 17 | +import { LitElement } from 'lit'; |
| 18 | +import styles from './styles.css' |
| 19 | + |
| 20 | +class MyElement extends LitElement { |
| 21 | + static styles = styles |
| 22 | + // the rest of the implementation |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +Build configuration: |
| 27 | + |
| 28 | +```js |
| 29 | +import { litCss } from 'rollup-plugin-css-lit' |
| 30 | + |
| 31 | +export default { |
| 32 | + plugins: [ |
| 33 | + litCss({ minify: process.env.NODE_ENV === 'production' }), |
| 34 | + ] |
| 35 | + // the rest of the configuration |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +Make sure that you use [Node.js] 14 or newer and [Rollup] 2 or newer. Use your favourite package manager - [NPM], [PNPM] or [Yarn]: |
| 40 | + |
| 41 | +## Installation |
| 42 | + |
| 43 | +Make sure that you use [Node.js] 14 or newer and [Rollup] 2 or newer. Use your favourite package manager - [NPM], [PNPM] or [Yarn]: |
| 44 | + |
| 45 | +```sh |
| 46 | +npm i -D rollup-plugin-css-lit |
| 47 | +pnpm i -D rollup-plugin-css-lit |
| 48 | +yarn add -D rollup-plugin-css-lit |
| 49 | +``` |
| 50 | + |
| 51 | +## Usage |
| 52 | + |
| 53 | +Create a `rollup.config.js` [configuration file] and import the plugin: |
| 54 | + |
| 55 | +```js |
| 56 | +import { litCss } from 'rollup-plugin-css-lit' |
| 57 | + |
| 58 | +export default { |
| 59 | + input: 'src/index.js', |
| 60 | + output: { file: 'dist/main.js', format: 'iife', sourcemap: true }, |
| 61 | + plugins: [ |
| 62 | + litCss({ |
| 63 | + minify: process.env.NODE_ENV === 'production', |
| 64 | + inline: { assets: {} } |
| 65 | + }) |
| 66 | + ] |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +Then call `rollup` either via the [command-line] or [programmatically]. |
| 71 | + |
| 72 | +## Options |
| 73 | + |
| 74 | +The following options can be passed in an object to the plugin function to change the default values. |
| 75 | + |
| 76 | +### `include` |
| 77 | + |
| 78 | +Type: `Array<String>`<br> |
| 79 | +Default: `['**/*.css']` |
| 80 | + |
| 81 | +[Pattern] to match files which will be processed by the plugin. |
| 82 | + |
| 83 | +### `exclude` |
| 84 | + |
| 85 | +Type: `Array<String>`<br> |
| 86 | +Default: `[]` |
| 87 | + |
| 88 | +[Pattern] to match files which will be ignored by the plugin. |
| 89 | + |
| 90 | +### `options` |
| 91 | + |
| 92 | +Type: `Object`<br> |
| 93 | +Default: `undefined` |
| 94 | + |
| 95 | +Options for the Sass compiler. Use any [options] supported by the [compileString] method from the Sass package. |
| 96 | + |
| 97 | +### `minify` |
| 98 | + |
| 99 | +Type: `Boolean | Object`<br> |
| 100 | +Default: `false` |
| 101 | + |
| 102 | +Enables minifying of the transformed CSS output. If an object is specified, it will be passed to the [cssnano] plugin. |
| 103 | + |
| 104 | +### `inline` |
| 105 | + |
| 106 | +Type: `Boolean | Object`<br> |
| 107 | +Default: `false` |
| 108 | + |
| 109 | +Enables inlining of stylesheets and other assets. If an object is specified, it will have to include two properties pointing to objects: `{ stylesheets, assets }`. The `stylesheets` objects will be passed to the [postcss-import] plugin. The `assets` objects will be passed to the [postcss-url] plugin. |
| 110 | + |
| 111 | +### `plugins` |
| 112 | + |
| 113 | +Type: `Array<Object>`<br> |
| 114 | +Default: `undefined` |
| 115 | + |
| 116 | +An array of [PostCSS] plugins to fully customise the transformation of the CSS input. |
| 117 | + |
| 118 | +### `tag` |
| 119 | + |
| 120 | +Type: `String`<br> |
| 121 | +Default: `'css'` |
| 122 | + |
| 123 | +The tag used for the tagged template literal exported from the generated module. Use `'css'` (default) with both `lit-html` and `fast-element`. |
| 124 | + |
| 125 | +```js |
| 126 | +export default css`...` |
| 127 | +``` |
| 128 | + |
| 129 | +### `specifier` |
| 130 | + |
| 131 | +Type: `String`<br> |
| 132 | +Default: `'lit'` |
| 133 | + |
| 134 | +The import specifier used in the imnport declaration of the tag above. Use `'lit'` (default) with `lit-html` and `'@microsoft/fast-element'` with `fast-element`. |
| 135 | + |
| 136 | +```js |
| 137 | +import { css } from 'lit' |
| 138 | +``` |
| 139 | + |
| 140 | +```js |
| 141 | +import { css } from '@microsoft/fast-element' |
| 142 | +``` |
| 143 | + |
| 144 | +## How It Works |
| 145 | + |
| 146 | +Let us have a stylesheet called `src/styles.css`: |
| 147 | + |
| 148 | +```css |
| 149 | +:host { display: block } |
| 150 | +``` |
| 151 | + |
| 152 | +And import it for a custom element in `src/index.js`: |
| 153 | + |
| 154 | +```js |
| 155 | +import { LitElement } from 'lit'; |
| 156 | +import styles from './styles.css' |
| 157 | + |
| 158 | +class MyElement extends LitElement { |
| 159 | + static styles = styles |
| 160 | + // the rest of the implementation |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +The stylesheet will be converted to the following script on-the-fly during the build and bundled into `dist/browser.js`: |
| 165 | + |
| 166 | +```js |
| 167 | +import { css } from 'lit' |
| 168 | +export default css`:host { display: block }` |
| 169 | +``` |
| 170 | + |
| 171 | +### Optimisation |
| 172 | + |
| 173 | +Before converting to the tagged template literal, the CSS output can be optimised by [PostCSS]. The minifying is performed by the [cssnano] plugin. Inlining of other stylesheets imported by the `@import` directives is performed by the [postcss-import] plugin. Inlining of other assets like pictures referred to by absolute or relative URLs is performed by the [postcss-url] plugin. If an error occurs during the transformation, the whole bundling operation will fail, using the [postcss-fail-on-warn] plugin. |
| 174 | + |
| 175 | +Passing a booleans to the `litCss` plugin - `{ minify: true, inline: true }` - will use the defaults. You can override them by passing an object instead of `true`: |
| 176 | + |
| 177 | +```js |
| 178 | +{ |
| 179 | + minify: { |
| 180 | + preset: ['default', { discardComments: { removeAll: true } }] |
| 181 | + }, |
| 182 | + inline: { |
| 183 | + stylesheets: {}, |
| 184 | + assets: { url: 'inline' } |
| 185 | + } |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +Pass [options for cssnano] to `minify`, [options for postcss-import] to `inline.stylesheets` and [options for postcss-url] to `inline.assets`. |
| 190 | + |
| 191 | +## Contributing |
| 192 | + |
| 193 | +In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code. |
| 194 | + |
| 195 | +## License |
| 196 | + |
| 197 | +Copyright (C) 2022 Ferdinand Prantl |
| 198 | + |
| 199 | +Licensed under the [MIT License]. |
| 200 | + |
| 201 | +[MIT License]: http://en.wikipedia.org/wiki/MIT_License |
| 202 | +[Rollup]: https://rollupjs.org/ |
| 203 | +[Node.js]: https://nodejs.org/ |
| 204 | +[NPM]: https://www.npmjs.com/ |
| 205 | +[PNPM]: https://pnpm.io/ |
| 206 | +[Yarn]: https://yarnpkg.com/ |
| 207 | +[configuration file]: https://www.rollupjs.org/guide/en/#configuration-files |
| 208 | +[command-line]: https://www.rollupjs.org/guide/en/#command-line-reference |
| 209 | +[programmatically]: https://www.rollupjs.org/guide/en/#javascript-api |
| 210 | +[Pattern]: https://www.linuxjournal.com/content/bash-extended-globbing |
| 211 | +[PostCSS]: https://postcss.org/ |
| 212 | +[cssnano]: https://cssnano.co/ |
| 213 | +[postcss-import]: https://www.npmjs.com/package/postcss-import |
| 214 | +[postcss-url]: https://www.npmjs.com/package/postcss-url |
| 215 | +[postcss-fail-on-warn]: https://www.npmjs.com/package/postcss-fail-on-warn |
| 216 | +[options supported by PostCSS for source maps]: https://postcss.org/api/#sourcemapoptions |
| 217 | +[options for cssnano]: https://cssnano.co/docs/config-file/ |
| 218 | +[options for postcss-import]: https://github.com/postcss/postcss-import#options |
| 219 | +[options for postcss-url]: https://github.com/postcss/postcss-url#options-combinations |
| 220 | +[lit]: https://lit.dev/ |
| 221 | +[lit-html]: https://lit.dev/docs/components/styles/ |
| 222 | +[lit-element]: https://lit.dev/docs/api/LitElement/ |
| 223 | +[fast-element]: https://www.fast.design/docs/fast-element/leveraging-css/ |
| 224 | +[rollup-plugin-styles]: https://www.npmjs.com/package/rollup-plugin-styles |
| 225 | +[rollup-plugin-lit-css]: https://www.npmjs.com/package/rollup-plugin-lit-css |
0 commit comments