rollup-plugin-lib-style 1.0.3 → 1.2.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 +36 -8
- package/lib/index.es.js +9 -3
- package/lib/index.js +9 -3
- package/package.json +1 -1
- package/types/index.d.ts +4 -3
package/README.md
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
# rollup-plugin-lib-style
|
|
2
2
|
|
|
3
|
-
A Rollup plugin that converts CSS and
|
|
4
|
-
|
|
3
|
+
A Rollup plugin that converts CSS and CSS extension languages into CSS modules and imports the generated CSS files.
|
|
4
|
+
This plugin gives you the ability to build a library that imports its styles (under the assumption the library will be bundled with a bundler like webpack or rollup).
|
|
5
|
+
|
|
5
6
|
|
|
6
7
|
## Why
|
|
7
8
|
|
|
8
|
-
Today there are 2 main ways to bundle and import
|
|
9
|
+
Today there are 2 main ways to bundle and import styles from a library
|
|
9
10
|
|
|
10
|
-
-
|
|
11
|
-
-
|
|
11
|
+
- Having a single CSS file for all styles in the library
|
|
12
|
+
- Using CSS-in-JS (styled-components, emotion, ...)
|
|
12
13
|
|
|
13
|
-
These two ways have some disadvantages, when we are
|
|
14
|
+
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
|
|
14
15
|
|
|
15
16
|
This plugin brings you the ability to consume only the used styles from the library
|
|
16
17
|
|
|
@@ -56,10 +57,15 @@ var style = {test: "test_cPySKa"}
|
|
|
56
57
|
export {style as default}
|
|
57
58
|
```
|
|
58
59
|
|
|
59
|
-
This gives us the ability to consume only the used style
|
|
60
|
+
This gives us the ability to consume only the used style
|
|
60
61
|
|
|
61
62
|
## Options
|
|
62
63
|
|
|
64
|
+
### importCSS
|
|
65
|
+
Type: `boolean`<br />
|
|
66
|
+
Default: true<br />
|
|
67
|
+
Description: auto import the generated CSS
|
|
68
|
+
|
|
63
69
|
### classNamePrefix
|
|
64
70
|
|
|
65
71
|
Type: `string`<br />
|
|
@@ -82,7 +88,7 @@ Description: [PostCSS Plugins](https://postcss.org/docs/postcss-plugins)
|
|
|
82
88
|
|
|
83
89
|
Type: Loader[]<br />
|
|
84
90
|
Default: []<br />
|
|
85
|
-
Description: loaders for CSS
|
|
91
|
+
Description: loaders for CSS extension languages like Less, Stylus, ...<br />
|
|
86
92
|
Example:
|
|
87
93
|
|
|
88
94
|
```js
|
|
@@ -104,6 +110,28 @@ Default: null<br />
|
|
|
104
110
|
Description: exclude files from load by the loader
|
|
105
111
|
|
|
106
112
|
|
|
113
|
+
## Global Styles
|
|
114
|
+
In some cases, we will want to create global class names (without hash)
|
|
115
|
+
we can do so by adding ".global" to the style file name.
|
|
116
|
+
In this case, the scopedName will be "[local]"
|
|
117
|
+
Example: myStyle.global.css, mySecondStyle.global.scss
|
|
118
|
+
|
|
119
|
+
```css
|
|
120
|
+
// myStyle.global.css
|
|
121
|
+
.myStyle {
|
|
122
|
+
background-color: red;
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
// myStyle.global.css.js
|
|
128
|
+
import "./myComponent/style.css"
|
|
129
|
+
|
|
130
|
+
var style = {myStyle: "myStyle"}
|
|
131
|
+
|
|
132
|
+
export {style as default}
|
|
133
|
+
```
|
|
134
|
+
|
|
107
135
|
## Known Issues
|
|
108
136
|
"Unresolved dependencies" warnings
|
|
109
137
|
```
|
package/lib/index.es.js
CHANGED
|
@@ -32,9 +32,11 @@ const postCssLoader = async ({code, fiePath, options}) => {
|
|
|
32
32
|
|
|
33
33
|
const modulesExported = {};
|
|
34
34
|
|
|
35
|
+
const isGlobalStyle = /\.global.(css|scss|sass|less|stylus)$/.test(fiePath);
|
|
36
|
+
|
|
35
37
|
postCssPlugins.unshift(
|
|
36
38
|
postcssModules({
|
|
37
|
-
generateScopedName: classNamePrefix + scopedName,
|
|
39
|
+
generateScopedName: isGlobalStyle ? "[local]" : classNamePrefix + scopedName,
|
|
38
40
|
getJSON: (cssFileName, json) => (modulesExported[cssFileName] = json),
|
|
39
41
|
})
|
|
40
42
|
);
|
|
@@ -94,7 +96,7 @@ const defaultLoaders = [
|
|
|
94
96
|
const replaceMagicPath = (fileContent) => fileContent.replace(MAGIC_PATH_REGEX, ".");
|
|
95
97
|
|
|
96
98
|
const libStylePlugin = (options = {}) => {
|
|
97
|
-
const {loaders, include, exclude, ...postCssOptions} = options;
|
|
99
|
+
const {loaders, include, exclude, importCSS = true, ...postCssOptions} = options;
|
|
98
100
|
const allLoaders = [...(loaders || []), ...defaultLoaders];
|
|
99
101
|
const filter = createFilter(include, exclude);
|
|
100
102
|
const getLoader = (filepath) => allLoaders.find((loader) => loader.regex.test(filepath));
|
|
@@ -128,7 +130,7 @@ const libStylePlugin = (options = {}) => {
|
|
|
128
130
|
source: postCssResult.extracted.code,
|
|
129
131
|
});
|
|
130
132
|
|
|
131
|
-
const importStr = `import "${MAGIC_PATH}${cssFilePath.replace(loader.regex, ".css")}";\n
|
|
133
|
+
const importStr = importCSS ? `import "${MAGIC_PATH}${cssFilePath.replace(loader.regex, ".css")}";\n` : "";
|
|
132
134
|
|
|
133
135
|
// create a new js file with css module
|
|
134
136
|
return {
|
|
@@ -138,9 +140,12 @@ const libStylePlugin = (options = {}) => {
|
|
|
138
140
|
},
|
|
139
141
|
|
|
140
142
|
async closeBundle() {
|
|
143
|
+
if (!importCSS) return
|
|
144
|
+
|
|
141
145
|
const importers = [];
|
|
142
146
|
modulesIds.forEach((id) => this.getModuleInfo(id).importers.forEach((importer) => importers.push(path.parse(importer).name + ".js"))); // TODO - add number pattern for duplicate name files
|
|
143
147
|
|
|
148
|
+
// get all the modules that import CSS files
|
|
144
149
|
const importersPaths = outputPaths
|
|
145
150
|
.reduce((result, currentPath) => {
|
|
146
151
|
result.push(glob.sync(`${currentPath}/**/*(${importers.join("|")})`));
|
|
@@ -148,6 +153,7 @@ const libStylePlugin = (options = {}) => {
|
|
|
148
153
|
}, [])
|
|
149
154
|
.flat();
|
|
150
155
|
|
|
156
|
+
// replace magic path with relative path
|
|
151
157
|
await Promise.all(
|
|
152
158
|
importersPaths.map((currentPath) =>
|
|
153
159
|
fs
|
package/lib/index.js
CHANGED
|
@@ -45,9 +45,11 @@ const postCssLoader = async ({code, fiePath, options}) => {
|
|
|
45
45
|
|
|
46
46
|
const modulesExported = {};
|
|
47
47
|
|
|
48
|
+
const isGlobalStyle = /\.global.(css|scss|sass|less|stylus)$/.test(fiePath);
|
|
49
|
+
|
|
48
50
|
postCssPlugins.unshift(
|
|
49
51
|
postcssModules__default["default"]({
|
|
50
|
-
generateScopedName: classNamePrefix + scopedName,
|
|
52
|
+
generateScopedName: isGlobalStyle ? "[local]" : classNamePrefix + scopedName,
|
|
51
53
|
getJSON: (cssFileName, json) => (modulesExported[cssFileName] = json),
|
|
52
54
|
})
|
|
53
55
|
);
|
|
@@ -107,7 +109,7 @@ const defaultLoaders = [
|
|
|
107
109
|
const replaceMagicPath = (fileContent) => fileContent.replace(MAGIC_PATH_REGEX, ".");
|
|
108
110
|
|
|
109
111
|
const libStylePlugin = (options = {}) => {
|
|
110
|
-
const {loaders, include, exclude, ...postCssOptions} = options;
|
|
112
|
+
const {loaders, include, exclude, importCSS = true, ...postCssOptions} = options;
|
|
111
113
|
const allLoaders = [...(loaders || []), ...defaultLoaders];
|
|
112
114
|
const filter = rollupPluginutils.createFilter(include, exclude);
|
|
113
115
|
const getLoader = (filepath) => allLoaders.find((loader) => loader.regex.test(filepath));
|
|
@@ -141,7 +143,7 @@ const libStylePlugin = (options = {}) => {
|
|
|
141
143
|
source: postCssResult.extracted.code,
|
|
142
144
|
});
|
|
143
145
|
|
|
144
|
-
const importStr = `import "${MAGIC_PATH}${cssFilePath.replace(loader.regex, ".css")}";\n
|
|
146
|
+
const importStr = importCSS ? `import "${MAGIC_PATH}${cssFilePath.replace(loader.regex, ".css")}";\n` : "";
|
|
145
147
|
|
|
146
148
|
// create a new js file with css module
|
|
147
149
|
return {
|
|
@@ -151,9 +153,12 @@ const libStylePlugin = (options = {}) => {
|
|
|
151
153
|
},
|
|
152
154
|
|
|
153
155
|
async closeBundle() {
|
|
156
|
+
if (!importCSS) return
|
|
157
|
+
|
|
154
158
|
const importers = [];
|
|
155
159
|
modulesIds.forEach((id) => this.getModuleInfo(id).importers.forEach((importer) => importers.push(path__default["default"].parse(importer).name + ".js"))); // TODO - add number pattern for duplicate name files
|
|
156
160
|
|
|
161
|
+
// get all the modules that import CSS files
|
|
157
162
|
const importersPaths = outputPaths
|
|
158
163
|
.reduce((result, currentPath) => {
|
|
159
164
|
result.push(glob__default["default"].sync(`${currentPath}/**/*(${importers.join("|")})`));
|
|
@@ -161,6 +166,7 @@ const libStylePlugin = (options = {}) => {
|
|
|
161
166
|
}, [])
|
|
162
167
|
.flat();
|
|
163
168
|
|
|
169
|
+
// replace magic path with relative path
|
|
164
170
|
await Promise.all(
|
|
165
171
|
importersPaths.map((currentPath) =>
|
|
166
172
|
fs__default["default"]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rollup-plugin-lib-style",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
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",
|
package/types/index.d.ts
CHANGED
|
@@ -15,9 +15,10 @@ declare interface Options {
|
|
|
15
15
|
include?: string | string[]
|
|
16
16
|
exclude?: string | string[]
|
|
17
17
|
loaders?: Loader[]
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
importCSS?: boolean
|
|
19
|
+
postCssPlugins?: object[]
|
|
20
|
+
classNamePrefix?: string
|
|
21
|
+
scopedName?: string
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
type onwarn = (warning: RollupWarning, defaultHandler: (warning: string | RollupWarning) => void) => void
|