rollup-plugin-lib-style 2.4.4 → 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 -61
- 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,31 +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
|
-
[](https://www.npmjs.com/package/rollup-plugin-lib-style)
|
|
8
|
-
|
|
9
|
-
This plugin allows you to import CSS or SASS/SCSS files in your Rollup library and include them in the generated output. The plugin will extract the CSS or SASS/SCSS from the imported files and import them as a CSS file
|
|
10
|
-
|
|
11
|
-
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.
|
|
12
6
|
|
|
13
7
|
## Why
|
|
14
8
|
|
|
15
|
-
|
|
9
|
+
There are two common approaches to bundling and distributing styles from a library:
|
|
16
10
|
|
|
17
|
-
|
|
18
|
-
|
|
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. |
|
|
19
15
|
|
|
20
|
-
|
|
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:
|
|
21
17
|
|
|
22
|
-
|
|
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.
|
|
23
23
|
|
|
24
24
|
## Install
|
|
25
25
|
|
|
26
|
+
```bash
|
|
27
|
+
npm install rollup-plugin-lib-style --save-dev
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
or
|
|
31
|
+
|
|
26
32
|
```bash
|
|
27
33
|
yarn add rollup-plugin-lib-style --dev
|
|
28
|
-
npm i rollup-plugin-lib-style --save-dev
|
|
29
34
|
```
|
|
30
35
|
|
|
31
36
|
## Usage
|
|
@@ -39,23 +44,26 @@ export default {
|
|
|
39
44
|
}
|
|
40
45
|
```
|
|
41
46
|
|
|
42
|
-
After adding this plugin
|
|
43
|
-
The imported CSS file will be transformed into a CSS module and a new CSS file will be generated
|
|
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)).
|
|
44
48
|
|
|
45
|
-
|
|
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.
|
|
50
|
+
|
|
51
|
+
For example, given this import:
|
|
46
52
|
|
|
47
53
|
```js
|
|
48
54
|
import style from "./style.css"
|
|
49
55
|
```
|
|
50
56
|
|
|
57
|
+
The plugin transforms it into:
|
|
58
|
+
|
|
51
59
|
```js
|
|
52
60
|
import style from "./style.css.js"
|
|
53
61
|
```
|
|
54
62
|
|
|
55
|
-
The
|
|
63
|
+
The generated `style.css.js` file looks like this:
|
|
56
64
|
|
|
57
65
|
```js
|
|
58
|
-
// style.css.js
|
|
66
|
+
// style.css.js
|
|
59
67
|
import "./myComponent/style.css"
|
|
60
68
|
|
|
61
69
|
var style = {test: "test_cPySKa"}
|
|
@@ -63,45 +71,48 @@ var style = {test: "test_cPySKa"}
|
|
|
63
71
|
export {style as default}
|
|
64
72
|
```
|
|
65
73
|
|
|
66
|
-
This gives
|
|
74
|
+
This gives consumers the ability to import only the styles they actually use.
|
|
67
75
|
|
|
68
76
|
## Options
|
|
69
77
|
|
|
70
78
|
### importCSS
|
|
71
79
|
|
|
72
80
|
Type: `boolean`<br />
|
|
73
|
-
Default: true
|
|
74
|
-
Description:
|
|
81
|
+
Default: `true`<br />
|
|
82
|
+
Description: Automatically import the generated CSS file from the JavaScript module.
|
|
75
83
|
|
|
76
84
|
### classNamePrefix
|
|
77
85
|
|
|
78
86
|
Type: `string`<br />
|
|
79
|
-
Default: ""
|
|
80
|
-
Description: prefix
|
|
87
|
+
Default: `""`<br />
|
|
88
|
+
Description: A prefix added to all generated class names.
|
|
81
89
|
|
|
82
90
|
### scopedName
|
|
83
91
|
|
|
84
92
|
Type: `string`<br />
|
|
85
|
-
Default: "[local]
|
|
86
|
-
Description:
|
|
87
|
-
Available placeholders: [local]
|
|
88
|
-
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"`
|
|
89
97
|
|
|
90
98
|
### postCssPlugins
|
|
91
99
|
|
|
92
|
-
Type: object[]
|
|
93
|
-
Default: []
|
|
94
|
-
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.
|
|
95
103
|
|
|
96
104
|
### sassOptions
|
|
97
105
|
|
|
98
|
-
Type: object
|
|
99
|
-
Default: {}
|
|
100
|
-
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
|
+
|
|
101
110
|
Example:
|
|
102
111
|
|
|
103
112
|
```js
|
|
104
113
|
// rollup.config.js
|
|
114
|
+
import {libStylePlugin} from "rollup-plugin-lib-style"
|
|
115
|
+
|
|
105
116
|
export default {
|
|
106
117
|
plugins: [
|
|
107
118
|
libStylePlugin({
|
|
@@ -115,17 +126,20 @@ export default {
|
|
|
115
126
|
|
|
116
127
|
### loaders
|
|
117
128
|
|
|
118
|
-
Type: Loader[]
|
|
119
|
-
Default: []
|
|
120
|
-
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
|
+
|
|
121
133
|
Example:
|
|
122
134
|
|
|
123
135
|
```js
|
|
124
136
|
// rollup.config.js
|
|
137
|
+
import {libStylePlugin} from "rollup-plugin-lib-style"
|
|
138
|
+
|
|
125
139
|
const lessLoader = {
|
|
126
|
-
name: "lessLoader"
|
|
127
|
-
regex: /\.less
|
|
128
|
-
process: ({code, filePath}) =>
|
|
140
|
+
name: "lessLoader",
|
|
141
|
+
regex: /\.less$/,
|
|
142
|
+
process: ({code, filePath}) => ({code: compileLess(code)}),
|
|
129
143
|
}
|
|
130
144
|
|
|
131
145
|
export default {
|
|
@@ -133,10 +147,11 @@ export default {
|
|
|
133
147
|
}
|
|
134
148
|
```
|
|
135
149
|
|
|
136
|
-
You can also override the
|
|
150
|
+
You can also override the built-in SCSS loader to customize Sass compilation:
|
|
137
151
|
|
|
138
152
|
```js
|
|
139
153
|
// rollup.config.js
|
|
154
|
+
import {libStylePlugin} from "rollup-plugin-lib-style"
|
|
140
155
|
import sass from "sass"
|
|
141
156
|
|
|
142
157
|
const customSassLoader = {
|
|
@@ -156,44 +171,63 @@ export default {
|
|
|
156
171
|
}
|
|
157
172
|
```
|
|
158
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
|
+
|
|
159
180
|
### exclude
|
|
160
181
|
|
|
161
|
-
Type: Array<string | RegExp> | string | RegExp
|
|
162
|
-
Default: null
|
|
163
|
-
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.
|
|
164
185
|
|
|
165
186
|
### customPath
|
|
166
187
|
|
|
167
|
-
Type: string
|
|
168
|
-
Default: "."
|
|
169
|
-
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.
|
|
170
191
|
|
|
171
192
|
### customCSSPath
|
|
172
193
|
|
|
173
|
-
Type: (id: string) => string
|
|
174
|
-
Default: undefined
|
|
175
|
-
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
|
+
```
|
|
176
209
|
|
|
177
210
|
### customCSSInjectedPath
|
|
178
211
|
|
|
179
|
-
Type: (id: string) => string
|
|
180
|
-
Default: undefined
|
|
181
|
-
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.
|
|
182
215
|
|
|
183
216
|
## Global Styles
|
|
184
217
|
|
|
185
|
-
In some cases,
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
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`
|
|
189
221
|
|
|
190
222
|
```css
|
|
191
|
-
|
|
223
|
+
/* myStyle.global.css */
|
|
192
224
|
.myStyle {
|
|
193
225
|
background-color: red;
|
|
194
226
|
}
|
|
195
227
|
```
|
|
196
228
|
|
|
229
|
+
The generated JavaScript module:
|
|
230
|
+
|
|
197
231
|
```js
|
|
198
232
|
// myStyle.global.css.js
|
|
199
233
|
import "./myComponent/style.css"
|
|
@@ -203,9 +237,9 @@ var style = {myStyle: "myStyle"}
|
|
|
203
237
|
export {style as default}
|
|
204
238
|
```
|
|
205
239
|
|
|
206
|
-
##
|
|
240
|
+
## Suppressing "Unresolved dependencies" Warnings
|
|
207
241
|
|
|
208
|
-
|
|
242
|
+
The plugin uses an internal placeholder path during the build process, which can cause Rollup to emit warnings like:
|
|
209
243
|
|
|
210
244
|
```
|
|
211
245
|
(!) Unresolved dependencies
|
|
@@ -213,7 +247,7 @@ https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
|
|
|
213
247
|
@@_MAGIC_PATH_@@/src/components/Component/style.css (imported by "src/components/Component/style.scss")
|
|
214
248
|
```
|
|
215
249
|
|
|
216
|
-
|
|
250
|
+
To suppress these warnings, use the `onwarn` handler exported by the plugin:
|
|
217
251
|
|
|
218
252
|
```js
|
|
219
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}
|