|
| 1 | +# parcel-transformer-lit-css |
| 2 | + |
| 3 | +Parcel transformer to import CSS files as Lit tagged template literals. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install --save-dev parcel-transformer-lit-css |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +Add the transformer to your `.parcelrc`: |
| 14 | + |
| 15 | +```json |
| 16 | +{ |
| 17 | + "extends": "@parcel/config-default", |
| 18 | + "transformers": { |
| 19 | + "*.css": ["parcel-transformer-lit-css"] |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +Then import CSS files in your JavaScript/TypeScript code: |
| 25 | + |
| 26 | +```javascript |
| 27 | +import { LitElement, html } from 'lit'; |
| 28 | +import styles from './my-element.css'; |
| 29 | + |
| 30 | +export class MyElement extends LitElement { |
| 31 | + static styles = styles; |
| 32 | + |
| 33 | + render() { |
| 34 | + return html`<h1>Hello World</h1>`; |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +## Configuration |
| 40 | + |
| 41 | +Options can be configured in your `package.json`: |
| 42 | + |
| 43 | +```json |
| 44 | +{ |
| 45 | + "lit-css": { |
| 46 | + "specifier": "@microsoft/fast-element", |
| 47 | + "tag": "css", |
| 48 | + "cssnano": true |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Options |
| 54 | + |
| 55 | +- `specifier` (string): The module to import the CSS tag from. Defaults to `'lit'`. |
| 56 | +- `tag` (string): The name of the CSS tag function. Defaults to `'css'`. |
| 57 | +- `cssnano` (boolean): Enable CSS minification. Defaults to `false`. |
| 58 | + |
| 59 | +## Example |
| 60 | + |
| 61 | +### Input (`styles.css`) |
| 62 | + |
| 63 | +```css |
| 64 | +:host { |
| 65 | + display: block; |
| 66 | + padding: 16px; |
| 67 | +} |
| 68 | + |
| 69 | +h1 { |
| 70 | + color: hotpink; |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### Output (JavaScript) |
| 75 | + |
| 76 | +```javascript |
| 77 | +import { css } from "lit"; |
| 78 | +const styles = css`:host { |
| 79 | + display: block; |
| 80 | + padding: 16px; |
| 81 | +} |
| 82 | +
|
| 83 | +h1 { |
| 84 | + color: hotpink; |
| 85 | +} |
| 86 | +`; |
| 87 | +export { |
| 88 | + styles as default, |
| 89 | + styles |
| 90 | +}; |
| 91 | +``` |
| 92 | + |
| 93 | +## Preprocessors |
| 94 | + |
| 95 | +You can use CSS preprocessors by chaining Parcel's preprocessor transformers before this transformer in your `.parcelrc`: |
| 96 | + |
| 97 | +```json |
| 98 | +{ |
| 99 | + "extends": "@parcel/config-default", |
| 100 | + "transformers": { |
| 101 | + "*.scss": ["@parcel/transformer-sass", "parcel-transformer-lit-css"] |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Make sure to install the preprocessor transformer: |
| 107 | + |
| 108 | +```bash |
| 109 | +npm install --save-dev @parcel/transformer-sass |
| 110 | +``` |
| 111 | + |
| 112 | +This works with any Parcel transformer, including PostCSS, Less, and Stylus. |
| 113 | + |
| 114 | +## License |
| 115 | + |
| 116 | +ISC |
0 commit comments