rollup-plugin-lib-style 1.2.9 → 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 CHANGED
@@ -1,9 +1,10 @@
1
1
  # rollup-plugin-lib-style
2
2
 
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).
3
+ 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
5
4
 
6
5
 
6
+ 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.
7
+
7
8
  ## Why
8
9
 
9
10
  Today there are 2 main ways to bundle and import styles from a library
@@ -11,7 +12,7 @@ Today there are 2 main ways to bundle and import styles from a library
11
12
  - Having a single CSS file for all styles in the library
12
13
  - Using CSS-in-JS (styled-components, emotion, ...)
13
14
 
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
15
+ 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
15
16
 
16
17
  This plugin brings you the ability to consume only the used styles from the library
17
18
 
@@ -75,8 +76,10 @@ Description: prefix for the classnames
75
76
  ### scopedName
76
77
 
77
78
  Type: `string`<br />
78
- Default: "[local]\_[hash:base64:6]"<br />
79
- Description: customize the scoped name of the classname
79
+ Default: "[local]\_[hash:hex:6]"<br />
80
+ Description: customize the scoped name of the classname.
81
+ Available placeholders: [local], [hash], [hash:\<digset>], [hash:\<digset>:\<length>] [hash:\<length>]
82
+ Available digset: "latin1", "hex", "base64"
80
83
 
81
84
  ### postCssPlugins
82
85
 
package/lib/index.es.js CHANGED
@@ -1,11 +1,24 @@
1
1
  import { createFilter } from 'rollup-pluginutils';
2
2
  import postcss from 'postcss';
3
3
  import postcssModules from 'postcss-modules';
4
+ import crypto from 'node:crypto';
4
5
  import fs from 'fs-extra';
5
6
  import sass from 'sass';
6
7
  import glob from 'glob';
7
8
 
8
- const defaultScopedName = "[local]_[hash:base64:6]";
9
+ const hashFormats = ["latin1", "hex", "base64"];
10
+
11
+ const replaceFormat = (formatString, fileName, cssContent) => {
12
+ const hashLengthMatch = formatString.match(/hash:.*:(\d+)/);
13
+ const hashFormatMatch = formatString.match(/hash:([^:]*)[:-]?/);
14
+ const hashFormat = hashFormatMatch && hashFormats.includes(hashFormatMatch[1]) ? hashFormatMatch[1] : "hex";
15
+ const hashLength = hashLengthMatch ? parseInt(hashLengthMatch[1]) : 6;
16
+ const hashString = crypto.createHash("md5").update(cssContent).digest(hashFormat);
17
+ const hashToUse = hashString.length < hashLength ? hashString : hashString.slice(0, hashLength);
18
+ return formatString.replace("[local]", fileName).replace(/\[hash:(.*?)(:\d+)?\]/, hashToUse)
19
+ };
20
+
21
+ const DEFAULT_SCOPED_NAME = "[local]_[hash:hex:6]";
9
22
 
10
23
  /**
11
24
  * @typedef {object} postCssLoaderOptions
@@ -27,7 +40,7 @@ const defaultScopedName = "[local]_[hash:base64:6]";
27
40
  * @returns
28
41
  */
29
42
  const postCssLoader = async ({code, fiePath, options}) => {
30
- const {scopedName = defaultScopedName, postCssPlugins = [], classNamePrefix = ""} = options;
43
+ const {scopedName = DEFAULT_SCOPED_NAME, postCssPlugins = [], classNamePrefix = ""} = options;
31
44
 
32
45
  const modulesExported = {};
33
46
 
@@ -36,7 +49,10 @@ const postCssLoader = async ({code, fiePath, options}) => {
36
49
 
37
50
  const postCssPluginsWithCssModules = [
38
51
  postcssModules({
39
- generateScopedName: isInNodeModules || isGlobalStyle ? "[local]" : classNamePrefix + scopedName,
52
+ generateScopedName: (name, filename, css) => {
53
+ const newClassName = classNamePrefix + ((isInNodeModules || isGlobalStyle) ? name : replaceFormat(scopedName, name, css));
54
+ return newClassName
55
+ },
40
56
  getJSON: (cssFileName, json) => (modulesExported[cssFileName] = json),
41
57
  }),
42
58
  ...postCssPlugins,
package/lib/index.js CHANGED
@@ -3,11 +3,24 @@
3
3
  var rollupPluginutils = require('rollup-pluginutils');
4
4
  var postcss = require('postcss');
5
5
  var postcssModules = require('postcss-modules');
6
+ var crypto = require('node:crypto');
6
7
  var fs = require('fs-extra');
7
8
  var sass = require('sass');
8
9
  var glob = require('glob');
9
10
 
10
- const defaultScopedName = "[local]_[hash:base64:6]";
11
+ const hashFormats = ["latin1", "hex", "base64"];
12
+
13
+ const replaceFormat = (formatString, fileName, cssContent) => {
14
+ const hashLengthMatch = formatString.match(/hash:.*:(\d+)/);
15
+ const hashFormatMatch = formatString.match(/hash:([^:]*)[:-]?/);
16
+ const hashFormat = hashFormatMatch && hashFormats.includes(hashFormatMatch[1]) ? hashFormatMatch[1] : "hex";
17
+ const hashLength = hashLengthMatch ? parseInt(hashLengthMatch[1]) : 6;
18
+ const hashString = crypto.createHash("md5").update(cssContent).digest(hashFormat);
19
+ const hashToUse = hashString.length < hashLength ? hashString : hashString.slice(0, hashLength);
20
+ return formatString.replace("[local]", fileName).replace(/\[hash:(.*?)(:\d+)?\]/, hashToUse)
21
+ };
22
+
23
+ const DEFAULT_SCOPED_NAME = "[local]_[hash:hex:6]";
11
24
 
12
25
  /**
13
26
  * @typedef {object} postCssLoaderOptions
@@ -29,7 +42,7 @@ const defaultScopedName = "[local]_[hash:base64:6]";
29
42
  * @returns
30
43
  */
31
44
  const postCssLoader = async ({code, fiePath, options}) => {
32
- const {scopedName = defaultScopedName, postCssPlugins = [], classNamePrefix = ""} = options;
45
+ const {scopedName = DEFAULT_SCOPED_NAME, postCssPlugins = [], classNamePrefix = ""} = options;
33
46
 
34
47
  const modulesExported = {};
35
48
 
@@ -38,7 +51,10 @@ const postCssLoader = async ({code, fiePath, options}) => {
38
51
 
39
52
  const postCssPluginsWithCssModules = [
40
53
  postcssModules({
41
- generateScopedName: isInNodeModules || isGlobalStyle ? "[local]" : classNamePrefix + scopedName,
54
+ generateScopedName: (name, filename, css) => {
55
+ const newClassName = classNamePrefix + ((isInNodeModules || isGlobalStyle) ? name : replaceFormat(scopedName, name, css));
56
+ return newClassName
57
+ },
42
58
  getJSON: (cssFileName, json) => (modulesExported[cssFileName] = json),
43
59
  }),
44
60
  ...postCssPlugins,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollup-plugin-lib-style",
3
- "version": "1.2.9",
3
+ "version": "2.0.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",
@@ -54,13 +54,13 @@
54
54
  "eslint-plugin-import": "^2.26.0",
55
55
  "eslint-plugin-prettier": "^4.2.1",
56
56
  "eslint-plugin-promise": "^6.1.1",
57
- "fs-extra": "^11.0.0",
58
57
  "jest": "^29.3.1",
59
58
  "jest-environment-node-single-context": "^29.0.0",
60
59
  "prettier": "^2.8.0",
61
60
  "rollup": "^3.5.0"
62
61
  },
63
62
  "dependencies": {
63
+ "fs-extra": "^11.1.0",
64
64
  "postcss": "8.4.17",
65
65
  "postcss-modules": "4.0.0",
66
66
  "rollup-pluginutils": "2.8.2",