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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Daniel Amenou
3
+ Copyright (c) 2026 Daniel Amenou
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,27 +1,36 @@
1
1
  # rollup-plugin-lib-style
2
2
 
3
- [![License](https://img.shields.io/npm/l/rollup-plugin-lib-style.svg)](https://github.com/danielamenou/rollup-plugin-lib-style/blob/main/LICENSE) [![npm downloads](https://img.shields.io/npm/dt/rollup-plugin-lib-style.svg)](https://www.npmjs.com/package/rollup-plugin-lib-style) [![npm version](https://img.shields.io/npm/v/rollup-plugin-lib-style.svg)](https://www.npmjs.com/package/rollup-plugin-lib-style)
3
+ [![License](https://img.shields.io/npm/l/rollup-plugin-lib-style.svg)](https://github.com/danielamenou/rollup-plugin-lib-style/blob/master/LICENSE) [![npm downloads](https://img.shields.io/npm/dt/rollup-plugin-lib-style.svg)](https://www.npmjs.com/package/rollup-plugin-lib-style) [![npm version](https://img.shields.io/npm/v/rollup-plugin-lib-style.svg)](https://www.npmjs.com/package/rollup-plugin-lib-style)
4
4
 
5
- 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
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
- Today there are 2 main ways to bundle and import styles from a library
9
+ There are two common approaches to bundling and distributing styles from a library:
12
10
 
13
- - Having a single CSS file for all styles in the library
14
- - Using CSS-in-JS (styled-components, emotion, ...)
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
- These two ways have some disadvantages when we are having a single CSS file, we are importing styles that probably will not be necessary, and when we are using CSS-in-JS we are increasing the HTML size
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
- This plugin brings you the ability to consume only the used styles from the library
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 we will be able to use CSS, SCSS, and SASS files (and more languages by adding plugins)
39
- 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)).
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
- In the js file that imports style file, the import will be changed in the following way:
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 newly generated file will export the CSS module, but also will import the new CSS file.
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 us the ability to consume only the used style
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<br />
70
- Description: auto import the generated CSS
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: ""<br />
76
- Description: prefix for the classnames
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]\_[hash:hex:6]"<br />
82
- Description: customize the scoped name of the classname.
83
- Available placeholders: [local], [hash], [hash:\<digset>], [hash:\<digset>:\<length>] [hash:\<length>]
84
- Available digset: "latin1", "hex", "base64"
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[]<br />
89
- Default: []<br />
90
- Description: [PostCSS Plugins](https://postcss.org/docs/postcss-plugins)
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<br />
95
- Default: {}<br />
96
- Description: Options passed to the sass compiler. Can be used to set `loadPaths` for global imports/mixins.<br />
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[]<br />
115
- Default: []<br />
116
- Description: loaders for CSS extension languages like Less, Stylus, ...<br />
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}) => less(code)
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 default SCSS loader to customize sass compilation:
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<br />
158
- Default: null<br />
159
- Description: exclude files from load by the loader
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<br />
164
- Default: "."<br />
165
- Description: Change custom path for starting of reference to CSS file, useful for nested component structure
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<br />
170
- Default: undefined<br />
171
- Description: A callback that allows you to transform where to store import the generated CSS file from. For example, `Header.module.scss` transformed to `Header.module.css`, but NextJS treat `.module.scss` as CSS module, so you cannot import it directly. Then you can use `return id.replace(process.cwd(), "").replace(/\\/g, "/").replace('.module', '')` to fix it. This will affect both CSS filename and the `import` statement.
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<br />
176
- Default: undefined<br />
177
- Description: A callback that allows you to transform the injected `import` statement path. For example, if you have deep nested css files like `./components/headers/Header.css` placed along with their corresponding js, this can be transformed to `./Header.css`. This will affect both CSS filename and the `import` statement.
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, we will want to create global class names (without hash)
182
- we can do so by adding ".global" to the style file name.
183
- In this case, the scopedName will be "[local]"
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
- // myStyle.global.css
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
- ## Known Issues
240
+ ## Suppressing "Unresolved dependencies" Warnings
203
241
 
204
- "Unresolved dependencies" warnings
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
- These warnings can be suppressed by using the "onwarn" function
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
@@ -1,4 +1,4 @@
1
- import { createFilter } from 'rollup-pluginutils';
1
+ import { createFilter } from '@rollup/pluginutils';
2
2
  import postcss from 'postcss';
3
3
  import postcssModules from 'postcss-modules';
4
4
  import crypto from 'node:crypto';
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var rollupPluginutils = require('rollup-pluginutils');
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 = rollupPluginutils.createFilter(include, exclude);
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.5",
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.1.0",
64
- "postcss": "8.4.39",
65
- "postcss-modules": "4.0.0",
66
- "rollup-pluginutils": "2.8.2",
67
- "sass": "1.55.0"
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?: any
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: string
17
- process: (arg: ProcessArgs) => string
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}