@pwrs/vite-plugin-lit-css 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +171 -0
- package/package.json +40 -0
- package/vite-plugin-lit-css.cjs +1680 -0
- package/vite-plugin-lit-css.d.ts +22 -0
- package/vite-plugin-lit-css.js +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# @pwrs/vite-plugin-lit-css
|
|
2
|
+
|
|
3
|
+
Vite plugin to import CSS files as JavaScript tagged-template literal objects.
|
|
4
|
+
|
|
5
|
+
> _The "Lit" stands for "Literal"_
|
|
6
|
+
|
|
7
|
+
You can use it to import CSS for various libraries like `lit`, `@microsoft/fast-element`, or others.
|
|
8
|
+
|
|
9
|
+
## Do I Need This?
|
|
10
|
+
|
|
11
|
+
No. This is an optional package whose sole purpose is to make it easier to write CSS-in-CSS while working on lit-element projects. You can just as easily write your CSS in some '`styles.css.js`' modules a la:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { css } from 'lit';
|
|
15
|
+
export default css`:host { display: block; }`;
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
And this may actually be preferred.
|
|
19
|
+
|
|
20
|
+
Hopefully this package will become quickly obsolete when the [CSS Modules Proposal](https://github.com/w3c/webcomponents/issues/759) (or something like it) is accepted and implemented.
|
|
21
|
+
|
|
22
|
+
In the mean time, enjoy importing your CSS into your component files.
|
|
23
|
+
|
|
24
|
+
## Options
|
|
25
|
+
|
|
26
|
+
| Name | Accepts | Default |
|
|
27
|
+
| ----------- | ----------------------------------------------------------------------------- | -------------- |
|
|
28
|
+
| `include` | Array of glob of files to include. | `['**/*.css']` |
|
|
29
|
+
| `exclude` | Array of glob of files to exclude. | `undefined` |
|
|
30
|
+
| `cssnano` | Boolean or Object of [cssnano][nanoopts] options. | `false` |
|
|
31
|
+
| `specifier` | Package to import `css` from | `lit` |
|
|
32
|
+
| `tag` | Name of the template-tag function | `css` |
|
|
33
|
+
| `transform` | Optional function (sync or async) which transforms css sources (e.g. postcss) | `x => x` |
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install --save-dev @pwrs/vite-plugin-lit-css
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
// vite.config.js
|
|
45
|
+
import { defineConfig } from 'vite';
|
|
46
|
+
import litCSS from '@pwrs/vite-plugin-lit-css';
|
|
47
|
+
|
|
48
|
+
export default defineConfig({
|
|
49
|
+
plugins: [
|
|
50
|
+
litCSS({ include: '**/*.css', cssnano: true })
|
|
51
|
+
]
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Then import your CSS:
|
|
56
|
+
|
|
57
|
+
```css
|
|
58
|
+
:host {
|
|
59
|
+
display: block;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
h1 {
|
|
63
|
+
color: hotpink;
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { LitElement, html } from 'lit';
|
|
69
|
+
import { customElement } from 'lit/decorators.js';
|
|
70
|
+
|
|
71
|
+
import style from './css-in-css.css';
|
|
72
|
+
|
|
73
|
+
@customElement('css-in-css')
|
|
74
|
+
class CSSInCSS extends LitElement {
|
|
75
|
+
static readonly styles = [style];
|
|
76
|
+
|
|
77
|
+
render() {
|
|
78
|
+
return html`<h1>It's Lit!</h1>`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Usage with FAST
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
// vite.config.js
|
|
87
|
+
import { defineConfig } from 'vite';
|
|
88
|
+
import litCSS from '@pwrs/vite-plugin-lit-css';
|
|
89
|
+
|
|
90
|
+
export default defineConfig({
|
|
91
|
+
plugins: [
|
|
92
|
+
litCSS({ specifier: '@microsoft/fast-element' })
|
|
93
|
+
]
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { FASTElement, customElement, html } from '@microsoft/fast-element';
|
|
99
|
+
|
|
100
|
+
import styles from './css-in-css.css';
|
|
101
|
+
|
|
102
|
+
const template = html<CSSinCSS>`<h1>It's Lit!</h1>`;
|
|
103
|
+
|
|
104
|
+
@customElement({ name: 'css-in-css', template, styles })
|
|
105
|
+
class CSSinCSS extends FASTElement {}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Usage with Sass, Less, PostCSS, etc.
|
|
109
|
+
|
|
110
|
+
To load scss files:
|
|
111
|
+
|
|
112
|
+
1. Specify an `include` option which includes scss files (i.e. a glob or regexp)
|
|
113
|
+
2. Define a `transform` function in the plugin options.
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
// vite.config.js
|
|
117
|
+
import { defineConfig } from 'vite';
|
|
118
|
+
import litCSS from '@pwrs/vite-plugin-lit-css';
|
|
119
|
+
import Sass from 'sass';
|
|
120
|
+
|
|
121
|
+
export default defineConfig({
|
|
122
|
+
plugins: [
|
|
123
|
+
litCSS({
|
|
124
|
+
include: '**/*.scss',
|
|
125
|
+
transform: (data, { filePath }) =>
|
|
126
|
+
Sass.compile(filePath).css,
|
|
127
|
+
})
|
|
128
|
+
]
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Similarly, to transform sources using PostCSS, specify a `transform` function:
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
// vite.config.js
|
|
136
|
+
import { defineConfig } from 'vite';
|
|
137
|
+
import litCSS from '@pwrs/vite-plugin-lit-css';
|
|
138
|
+
import postcss from 'postcss';
|
|
139
|
+
import postcssNesting from 'postcss-nesting';
|
|
140
|
+
|
|
141
|
+
const processor = postcss(postcssNesting());
|
|
142
|
+
|
|
143
|
+
export default defineConfig({
|
|
144
|
+
plugins: [
|
|
145
|
+
litCSS({
|
|
146
|
+
transform: (css, { filePath }) =>
|
|
147
|
+
processor.process(css, { from: filePath }).css,
|
|
148
|
+
})
|
|
149
|
+
]
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Hot Module Replacement (HMR)
|
|
154
|
+
|
|
155
|
+
This plugin works seamlessly with Vite's HMR. When you modify a CSS file, Vite will automatically reload the module and update your component styles without a full page refresh.
|
|
156
|
+
|
|
157
|
+
## How It Works
|
|
158
|
+
|
|
159
|
+
This plugin uses Vite's `load` hook with `enforce: 'pre'` to intercept CSS imports before Vite's built-in CSS handling. Since Vite plugins are Rollup plugins under the hood, this plugin works with Vite's build process.
|
|
160
|
+
|
|
161
|
+
**Note:** This plugin is designed to transform CSS files into JavaScript tagged template literals. If you experience conflicts with Vite's built-in CSS handling in development mode, you may need to adjust your Vite configuration. The plugin has been tested primarily with production builds. For development server and Vitest usage, please test in your specific environment and report any issues.
|
|
162
|
+
|
|
163
|
+
## Other Plugins
|
|
164
|
+
|
|
165
|
+
Looking for other build tools?
|
|
166
|
+
- Webpack: [lit-css-loader](../lit-css-loader)
|
|
167
|
+
- esbuild: [esbuild-plugin-lit-css](../esbuild-plugin-lit-css)
|
|
168
|
+
- Rollup: [rollup-plugin-lit-css](../rollup-plugin-lit-css)
|
|
169
|
+
- TypeScript: [typescript-transform-lit-css](../typescript-transform-lit-css)
|
|
170
|
+
|
|
171
|
+
[nanoopts]: https://cssnano.co/docs/config-file/#configuration-options
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pwrs/vite-plugin-lit-css",
|
|
3
|
+
"description": "Vite plugin to import CSS files as tagged template literals",
|
|
4
|
+
"version": "2.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "vite-plugin-lit-css.js",
|
|
7
|
+
"types": "vite-plugin-lit-css.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"import": "./vite-plugin-lit-css.js",
|
|
10
|
+
"require": "./vite-plugin-lit-css.cjs"
|
|
11
|
+
},
|
|
12
|
+
"author": "Benny Powers",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/bennypowers/lit-css.git",
|
|
17
|
+
"directory": "packages/vite-plugin-lit-css"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/bennypowers/lit-css/issues"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"lit",
|
|
24
|
+
"web-components",
|
|
25
|
+
"css",
|
|
26
|
+
"vite"
|
|
27
|
+
],
|
|
28
|
+
"files": [
|
|
29
|
+
"vite-plugin-lit-css.cjs",
|
|
30
|
+
"vite-plugin-lit-css.js",
|
|
31
|
+
"vite-plugin-lit-css.d.ts"
|
|
32
|
+
],
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@pwrs/lit-css": "^4.1.0",
|
|
35
|
+
"@rollup/pluginutils": "^5.1.4"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"vite": "^4.0.0 || ^5.0.0 || ^6.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|