@elenajs/plugin-rollup-css 0.2.0 → 0.4.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.
Files changed (2) hide show
  1. package/package.json +11 -2
  2. package/src/index.js +26 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elenajs/plugin-rollup-css",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Rollup plugin that minifies individual CSS files and optionally concatenates them into a single bundle.",
5
5
  "author": "Elena <hi@elenajs.com>",
6
6
  "homepage": "https://elenajs.com/",
@@ -15,11 +15,20 @@
15
15
  "files": [
16
16
  "src"
17
17
  ],
18
+ "scripts": {
19
+ "test": "vitest run"
20
+ },
18
21
  "engines": {
19
22
  "node": ">= 20"
20
23
  },
21
24
  "peerDependencies": {
22
25
  "rollup": ">=4.0.0"
23
26
  },
24
- "gitHead": "323fcac2e1d15f81e7b4a039357c1f2e3e8f3030"
27
+ "dependencies": {
28
+ "lightningcss": "^1.31.1"
29
+ },
30
+ "devDependencies": {
31
+ "vitest": "4.0.18"
32
+ },
33
+ "gitHead": "81fe2fc78111f605854c07df2001f746231d9dfc"
25
34
  }
package/src/index.js CHANGED
@@ -1,20 +1,35 @@
1
+ /**
2
+ * ██████████ ████
3
+ * ░░███░░░░░█░░███
4
+ * ░███ █ ░ ███ ██████ ████████ ██████
5
+ * ░██████ ███ ███░░███░░███░░███ ░░░░░███
6
+ * ░███░░█ ███ ░███████ ░███ ░███ ███████
7
+ * ░███ ░ █ ███ ░███░░░ ░███ ░███ ███░░███
8
+ * ██████████ █████░░██████ ████ █████░░████████
9
+ * ░░░░░░░░░░ ░░░░░ ░░░░░░ ░░░░ ░░░░░ ░░░░░░░░
10
+ *
11
+ * Elena Rollup CSS Plugin
12
+ * https://elenajs.com
13
+ */
14
+
1
15
  import { basename } from "path";
2
16
  import { readFileSync, readdirSync } from "fs";
17
+ import { transform } from "lightningcss";
3
18
 
4
19
  /**
5
- * Minifies a CSS string by stripping comments, collapsing whitespace, removing
6
- * spaces around structural characters, and dropping trailing semicolons.
20
+ * Minifies a CSS string using Lightning CSS.
7
21
  *
8
22
  * @param {string} css
23
+ * @param {string} [filename]
9
24
  * @returns {string}
10
25
  */
11
- function minifyCss(css) {
12
- return css
13
- .replace(/\/\*[\s\S]*?\*\//g, "")
14
- .replace(/\s+/g, " ")
15
- .replace(/\s*([{}:;,>~+])\s*/g, "$1")
16
- .replace(/;}/g, "}")
17
- .trim();
26
+ function minifyCss(css, filename = "style.css") {
27
+ const { code } = transform({
28
+ filename,
29
+ code: Buffer.from(css),
30
+ minify: true,
31
+ });
32
+ return code.toString();
18
33
  }
19
34
 
20
35
  /**
@@ -33,7 +48,7 @@ export function cssPlugin(srcDir) {
33
48
  .map(f => `${srcDir}/${f}`);
34
49
 
35
50
  for (const file of cssFiles) {
36
- const source = minifyCss(readFileSync(file, "utf8"));
51
+ const source = minifyCss(readFileSync(file, "utf8"), basename(file));
37
52
  this.emitFile({ type: "asset", fileName: basename(file), source });
38
53
  }
39
54
  },
@@ -56,7 +71,7 @@ export function cssBundlePlugin(srcDir, fileName) {
56
71
  .filter(f => f.endsWith(".css"))
57
72
  .map(f => `${srcDir}/${f}`);
58
73
 
59
- const source = minifyCss(cssFiles.map(f => readFileSync(f, "utf8")).join("\n"));
74
+ const source = minifyCss(cssFiles.map(f => readFileSync(f, "utf8")).join("\n"), fileName);
60
75
 
61
76
  this.emitFile({ type: "asset", fileName, source });
62
77
  },