rollup-plugin-lib-style 2.4.5 → 2.4.7
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/LICENSE +1 -1
- package/README.md +95 -57
- package/lib/index.es.js +1 -1
- package/lib/index.js +2 -2
- package/package.json +6 -6
- package/types/index.d.ts +13 -5
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,27 +1,36 @@
|
|
|
1
1
|
# rollup-plugin-lib-style
|
|
2
2
|
|
|
3
|
-
[](https://github.com/danielamenou/rollup-plugin-lib-style/blob/
|
|
3
|
+
[](https://github.com/danielamenou/rollup-plugin-lib-style/blob/master/LICENSE) [](https://www.npmjs.com/package/rollup-plugin-lib-style) [](https://www.npmjs.com/package/rollup-plugin-lib-style)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
When creating a library, you may want to use CSS modules to create scoped styles for your components. However, when publishing your library as a standalone package, you also need to include the CSS styles that are used by your components. Rollup Plugin Lib Style automates this process by generating a CSS file that includes all the imported CSS modules.
|
|
5
|
+
A Rollup plugin for building component libraries with **tree-shakeable, per-component CSS**. Write your styles in CSS, SASS/SCSS, Less, Stylus, or any other CSS preprocessor -- the plugin extracts scoped CSS modules and generates a separate CSS file for each component. Consumers only pay for the styles they actually use.
|
|
8
6
|
|
|
9
7
|
## Why
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
There are two common approaches to bundling and distributing styles from a library:
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
| Approach | Drawback |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| **Single CSS file** | Consumers must import _all_ styles, even for components they never use. Increases total CSS payload and can cause unused-style bloat. |
|
|
14
|
+
| **CSS-in-JS** (styled-components, emotion, etc.) | Adds runtime overhead, increases JavaScript bundle size, and can delay first paint since styles are computed at runtime. |
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
**rollup-plugin-lib-style** takes a fundamentally better approach for libraries: it generates a **separate CSS file per component**, automatically scoped with CSS modules. This gives you:
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
- **Zero runtime cost** -- styles are plain CSS files, not JavaScript. No runtime style injection, no extra computation on the client.
|
|
19
|
+
- **Tree-shakeable CSS** -- when a consumer imports a component, only that component's CSS is loaded. Unused components contribute zero CSS to the final bundle.
|
|
20
|
+
- **Scoped by default** -- CSS module hashing prevents class name collisions between your library and the consuming application, with no extra setup.
|
|
21
|
+
- **Preprocessor agnostic** -- works out of the box with CSS and SASS/SCSS, and supports Less, Stylus, and any other preprocessor via custom loaders.
|
|
22
|
+
- **No consumer-side configuration** -- consumers simply import your components; the CSS is automatically referenced and loaded alongside the JavaScript.
|
|
19
23
|
|
|
20
24
|
## Install
|
|
21
25
|
|
|
26
|
+
```bash
|
|
27
|
+
npm install rollup-plugin-lib-style --save-dev
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
or
|
|
31
|
+
|
|
22
32
|
```bash
|
|
23
33
|
yarn add rollup-plugin-lib-style --dev
|
|
24
|
-
npm i rollup-plugin-lib-style --save-dev
|
|
25
34
|
```
|
|
26
35
|
|
|
27
36
|
## Usage
|
|
@@ -35,23 +44,26 @@ export default {
|
|
|
35
44
|
}
|
|
36
45
|
```
|
|
37
46
|
|
|
38
|
-
After adding this plugin
|
|
39
|
-
|
|
47
|
+
After adding this plugin, you can use CSS and SASS/SCSS files out of the box. Support for Less, Stylus, and other preprocessors can be added via custom loaders (see [loaders](#loaders)).
|
|
48
|
+
|
|
49
|
+
Each imported style file is transformed into a CSS module. A new `.css` file is generated alongside a `.css.js` file that re-exports the class name mappings and imports the CSS.
|
|
40
50
|
|
|
41
|
-
|
|
51
|
+
For example, given this import:
|
|
42
52
|
|
|
43
53
|
```js
|
|
44
54
|
import style from "./style.css"
|
|
45
55
|
```
|
|
46
56
|
|
|
57
|
+
The plugin transforms it into:
|
|
58
|
+
|
|
47
59
|
```js
|
|
48
60
|
import style from "./style.css.js"
|
|
49
61
|
```
|
|
50
62
|
|
|
51
|
-
The
|
|
63
|
+
The generated `style.css.js` file looks like this:
|
|
52
64
|
|
|
53
65
|
```js
|
|
54
|
-
// style.css.js
|
|
66
|
+
// style.css.js
|
|
55
67
|
import "./myComponent/style.css"
|
|
56
68
|
|
|
57
69
|
var style = {test: "test_cPySKa"}
|
|
@@ -59,45 +71,48 @@ var style = {test: "test_cPySKa"}
|
|
|
59
71
|
export {style as default}
|
|
60
72
|
```
|
|
61
73
|
|
|
62
|
-
This gives
|
|
74
|
+
This gives consumers the ability to import only the styles they actually use.
|
|
63
75
|
|
|
64
76
|
## Options
|
|
65
77
|
|
|
66
78
|
### importCSS
|
|
67
79
|
|
|
68
80
|
Type: `boolean`<br />
|
|
69
|
-
Default: true
|
|
70
|
-
Description:
|
|
81
|
+
Default: `true`<br />
|
|
82
|
+
Description: Automatically import the generated CSS file from the JavaScript module.
|
|
71
83
|
|
|
72
84
|
### classNamePrefix
|
|
73
85
|
|
|
74
86
|
Type: `string`<br />
|
|
75
|
-
Default: ""
|
|
76
|
-
Description: prefix
|
|
87
|
+
Default: `""`<br />
|
|
88
|
+
Description: A prefix added to all generated class names.
|
|
77
89
|
|
|
78
90
|
### scopedName
|
|
79
91
|
|
|
80
92
|
Type: `string`<br />
|
|
81
|
-
Default: "[local]
|
|
82
|
-
Description:
|
|
83
|
-
Available placeholders: [local]
|
|
84
|
-
Available
|
|
93
|
+
Default: `"[local]_[hash:hex:6]"`<br />
|
|
94
|
+
Description: Customize the scoped name format for generated class names.<br />
|
|
95
|
+
Available placeholders: `[local]`, `[hash]`, `[hash:<digest>]`, `[hash:<digest>:<length>]`, `[hash:<length>]`<br />
|
|
96
|
+
Available digests: `"latin1"`, `"hex"`, `"base64"`
|
|
85
97
|
|
|
86
98
|
### postCssPlugins
|
|
87
99
|
|
|
88
|
-
Type: object[]
|
|
89
|
-
Default: []
|
|
90
|
-
Description: [PostCSS
|
|
100
|
+
Type: `object[]`<br />
|
|
101
|
+
Default: `[]`<br />
|
|
102
|
+
Description: An array of [PostCSS plugins](https://postcss.org/docs/postcss-plugins) to apply during CSS processing.
|
|
91
103
|
|
|
92
104
|
### sassOptions
|
|
93
105
|
|
|
94
|
-
Type: object
|
|
95
|
-
Default: {}
|
|
96
|
-
Description: Options passed to the
|
|
106
|
+
Type: `object`<br />
|
|
107
|
+
Default: `{}`<br />
|
|
108
|
+
Description: Options passed to the Sass compiler. Can be used to set `loadPaths` for global imports and mixins.
|
|
109
|
+
|
|
97
110
|
Example:
|
|
98
111
|
|
|
99
112
|
```js
|
|
100
113
|
// rollup.config.js
|
|
114
|
+
import {libStylePlugin} from "rollup-plugin-lib-style"
|
|
115
|
+
|
|
101
116
|
export default {
|
|
102
117
|
plugins: [
|
|
103
118
|
libStylePlugin({
|
|
@@ -111,17 +126,20 @@ export default {
|
|
|
111
126
|
|
|
112
127
|
### loaders
|
|
113
128
|
|
|
114
|
-
Type: Loader[]
|
|
115
|
-
Default: []
|
|
116
|
-
Description: loaders for CSS
|
|
129
|
+
Type: `Loader[]`<br />
|
|
130
|
+
Default: `[]`<br />
|
|
131
|
+
Description: Custom loaders for additional CSS preprocessors like Less, Stylus, etc. Each loader must have a `name`, a `regex` to match file extensions, and a `process` function that returns an object with a `code` property containing the CSS string.
|
|
132
|
+
|
|
117
133
|
Example:
|
|
118
134
|
|
|
119
135
|
```js
|
|
120
136
|
// rollup.config.js
|
|
137
|
+
import {libStylePlugin} from "rollup-plugin-lib-style"
|
|
138
|
+
|
|
121
139
|
const lessLoader = {
|
|
122
|
-
name: "lessLoader"
|
|
123
|
-
regex: /\.less
|
|
124
|
-
process: ({code, filePath}) =>
|
|
140
|
+
name: "lessLoader",
|
|
141
|
+
regex: /\.less$/,
|
|
142
|
+
process: ({code, filePath}) => ({code: compileLess(code)}),
|
|
125
143
|
}
|
|
126
144
|
|
|
127
145
|
export default {
|
|
@@ -129,10 +147,11 @@ export default {
|
|
|
129
147
|
}
|
|
130
148
|
```
|
|
131
149
|
|
|
132
|
-
You can also override the
|
|
150
|
+
You can also override the built-in SCSS loader to customize Sass compilation:
|
|
133
151
|
|
|
134
152
|
```js
|
|
135
153
|
// rollup.config.js
|
|
154
|
+
import {libStylePlugin} from "rollup-plugin-lib-style"
|
|
136
155
|
import sass from "sass"
|
|
137
156
|
|
|
138
157
|
const customSassLoader = {
|
|
@@ -152,44 +171,63 @@ export default {
|
|
|
152
171
|
}
|
|
153
172
|
```
|
|
154
173
|
|
|
174
|
+
### include
|
|
175
|
+
|
|
176
|
+
Type: `Array<string | RegExp> | string | RegExp | null`<br />
|
|
177
|
+
Default: `null`<br />
|
|
178
|
+
Description: A pattern or array of patterns specifying which files to include. By default, all files matched by a loader are included.
|
|
179
|
+
|
|
155
180
|
### exclude
|
|
156
181
|
|
|
157
|
-
Type: Array<string | RegExp> | string | RegExp
|
|
158
|
-
Default: null
|
|
159
|
-
Description:
|
|
182
|
+
Type: `Array<string | RegExp> | string | RegExp | null`<br />
|
|
183
|
+
Default: `null`<br />
|
|
184
|
+
Description: A pattern or array of patterns specifying which files to exclude from processing.
|
|
160
185
|
|
|
161
186
|
### customPath
|
|
162
187
|
|
|
163
|
-
Type: string
|
|
164
|
-
Default: "."
|
|
165
|
-
Description:
|
|
188
|
+
Type: `string`<br />
|
|
189
|
+
Default: `"."`<br />
|
|
190
|
+
Description: Custom base path used as the prefix for CSS file import references. Useful for nested component structures.
|
|
166
191
|
|
|
167
192
|
### customCSSPath
|
|
168
193
|
|
|
169
|
-
Type: (id: string) => string
|
|
170
|
-
Default: undefined
|
|
171
|
-
Description: A callback
|
|
194
|
+
Type: `(id: string) => string`<br />
|
|
195
|
+
Default: `undefined`<br />
|
|
196
|
+
Description: A callback to transform the path used for the generated CSS file. This affects both the emitted CSS filename and the `import` statement in the generated JavaScript.
|
|
197
|
+
|
|
198
|
+
For example, if your source uses `.module.scss` files but the output should use plain `.css`, you can strip the `.module` portion:
|
|
199
|
+
|
|
200
|
+
```js
|
|
201
|
+
libStylePlugin({
|
|
202
|
+
customCSSPath: (id) =>
|
|
203
|
+
id
|
|
204
|
+
.replace(process.cwd(), "")
|
|
205
|
+
.replace(/\\/g, "/")
|
|
206
|
+
.replace(".module", ""),
|
|
207
|
+
})
|
|
208
|
+
```
|
|
172
209
|
|
|
173
210
|
### customCSSInjectedPath
|
|
174
211
|
|
|
175
|
-
Type: (id: string) => string
|
|
176
|
-
Default: undefined
|
|
177
|
-
Description: A callback
|
|
212
|
+
Type: `(id: string) => string`<br />
|
|
213
|
+
Default: `undefined`<br />
|
|
214
|
+
Description: A callback to transform the CSS import path in the generated JavaScript. Useful when CSS files end up in nested directories but you want flat import paths. This affects both the emitted CSS filename and the `import` statement.
|
|
178
215
|
|
|
179
216
|
## Global Styles
|
|
180
217
|
|
|
181
|
-
In some cases,
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
Example: myStyle.global.css, mySecondStyle.global.scss
|
|
218
|
+
In some cases, you may want class names to remain unscoped (without a hash). You can do this by adding `.global` to the style file name. For global styles, the `scopedName` is set to `"[local]"`, preserving the original class name.
|
|
219
|
+
|
|
220
|
+
Examples: `myStyle.global.css`, `mySecondStyle.global.scss`
|
|
185
221
|
|
|
186
222
|
```css
|
|
187
|
-
|
|
223
|
+
/* myStyle.global.css */
|
|
188
224
|
.myStyle {
|
|
189
225
|
background-color: red;
|
|
190
226
|
}
|
|
191
227
|
```
|
|
192
228
|
|
|
229
|
+
The generated JavaScript module:
|
|
230
|
+
|
|
193
231
|
```js
|
|
194
232
|
// myStyle.global.css.js
|
|
195
233
|
import "./myComponent/style.css"
|
|
@@ -199,9 +237,9 @@ var style = {myStyle: "myStyle"}
|
|
|
199
237
|
export {style as default}
|
|
200
238
|
```
|
|
201
239
|
|
|
202
|
-
##
|
|
240
|
+
## Suppressing "Unresolved dependencies" Warnings
|
|
203
241
|
|
|
204
|
-
|
|
242
|
+
The plugin uses an internal placeholder path during the build process, which can cause Rollup to emit warnings like:
|
|
205
243
|
|
|
206
244
|
```
|
|
207
245
|
(!) Unresolved dependencies
|
|
@@ -209,7 +247,7 @@ https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
|
|
|
209
247
|
@@_MAGIC_PATH_@@/src/components/Component/style.css (imported by "src/components/Component/style.scss")
|
|
210
248
|
```
|
|
211
249
|
|
|
212
|
-
|
|
250
|
+
To suppress these warnings, use the `onwarn` handler exported by the plugin:
|
|
213
251
|
|
|
214
252
|
```js
|
|
215
253
|
// rollup.config.js
|
package/lib/index.es.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var pluginutils = require('@rollup/pluginutils');
|
|
4
4
|
var postcss = require('postcss');
|
|
5
5
|
var postcssModules = require('postcss-modules');
|
|
6
6
|
var crypto = require('node:crypto');
|
|
@@ -137,7 +137,7 @@ const replaceMagicPath = (fileContent, customPath = ".") => fileContent.replace(
|
|
|
137
137
|
const libStylePlugin = (options = {}) => {
|
|
138
138
|
const {customPath, customCSSPath, customCSSInjectedPath, loaders, include, exclude, importCSS = true, sassOptions = {}, ...postCssOptions} = options;
|
|
139
139
|
const allLoaders = [...(loaders || []), ...defaultLoaders];
|
|
140
|
-
const filter =
|
|
140
|
+
const filter = pluginutils.createFilter(include, exclude);
|
|
141
141
|
const getLoader = (filepath) => allLoaders.find((loader) => loader.regex.test(filepath));
|
|
142
142
|
|
|
143
143
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rollup-plugin-lib-style",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.7",
|
|
4
4
|
"description": "A Rollup plugin that converts CSS and extensions for CSS into CSS modules and imports the generated CSS files",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "lib/index.es.js",
|
|
@@ -60,10 +60,10 @@
|
|
|
60
60
|
"rollup": "^3.5.0"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"fs-extra": "^11.
|
|
64
|
-
"postcss": "8.
|
|
65
|
-
"postcss-modules": "
|
|
66
|
-
"rollup
|
|
67
|
-
"sass": "1.
|
|
63
|
+
"fs-extra": "^11.3.0",
|
|
64
|
+
"postcss": "8.5.6",
|
|
65
|
+
"postcss-modules": "6.0.1",
|
|
66
|
+
"@rollup/pluginutils": "5.3.0",
|
|
67
|
+
"sass": "1.97.3"
|
|
68
68
|
}
|
|
69
69
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -3,7 +3,14 @@ import {PluginImpl, RollupWarning} from "rollup"
|
|
|
3
3
|
declare interface ProcessArgs {
|
|
4
4
|
code: string
|
|
5
5
|
filePath: string
|
|
6
|
-
options?:
|
|
6
|
+
options?: {
|
|
7
|
+
sassOptions?: SassOptions
|
|
8
|
+
[key: string]: any
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare interface ProcessResult {
|
|
13
|
+
code: string
|
|
7
14
|
}
|
|
8
15
|
|
|
9
16
|
declare interface SassOptions {
|
|
@@ -13,13 +20,13 @@ declare interface SassOptions {
|
|
|
13
20
|
|
|
14
21
|
declare interface Loader {
|
|
15
22
|
name: string
|
|
16
|
-
regex:
|
|
17
|
-
process: (
|
|
23
|
+
regex: RegExp
|
|
24
|
+
process: (args: ProcessArgs) => ProcessResult | Promise<ProcessResult>
|
|
18
25
|
}
|
|
19
26
|
|
|
20
27
|
declare interface Options {
|
|
21
|
-
include?: string | string
|
|
22
|
-
exclude?: string | string
|
|
28
|
+
include?: Array<string | RegExp> | string | RegExp | null
|
|
29
|
+
exclude?: Array<string | RegExp> | string | RegExp | null
|
|
23
30
|
loaders?: Loader[]
|
|
24
31
|
importCSS?: boolean
|
|
25
32
|
postCssPlugins?: object[]
|
|
@@ -36,3 +43,4 @@ declare const onwarn: (warning: RollupWarning, defaultHandler: (warning: string
|
|
|
36
43
|
declare const libStylePlugin: PluginImpl<Options>
|
|
37
44
|
|
|
38
45
|
export {onwarn, libStylePlugin}
|
|
46
|
+
export type {Options, Loader, ProcessArgs, ProcessResult, SassOptions}
|