Repository: jsdom/cssstyle
Pull request #110 (2020-01-04 14:24:40)

Ignore everything except lib
Hi!

When looking at the global size of jsdom, I noticed that cssstyle was packaging a lot of unnecessary things. For instance, the generated coverage (1.2Mb) is included in the package.

Before (v2.0.0):
The generated tgz is 149kb.
The unpacked package is 1.43Mb.

Estimated now:
The generated tgz is 32kb.
The unpackage package is 190Kb.




Patch:
diff --git a/.npmignore b/.npmignore
index 2d51347..4c9a487 100644
--- a/.npmignore
+++ b/.npmignore
@@ -1,1 +1,3 @@
-node_module
+/*
+!lib/
+!LICENSE
diff --git a/lib/parsers.js b/lib/parsers.js
index 5bc92cd..3d8d620 100644
--- a/lib/parsers.js
+++ b/lib/parsers.js
@@ -5,7 +5,6 @@
 'use strict';
 
 const namedColors = require('./named_colors.json');
-const { hslToRgb } = require('./utils/colorSpace');
 
 exports.TYPES = {
   INTEGER: 1,
@@ -93,7 +92,7 @@ exports.valueType = function valueType(val) {
     }
     if (
       parts.slice(0, 3).every(percentRegEx.test.bind(percentRegEx)) ||
-      parts.slice(0, 3).every(integerRegEx.test.bind(integerRegEx))
+      parts.every(integerRegEx.test.bind(integerRegEx))
     ) {
       if (numberRegEx.test(parts[3])) {
         return exports.TYPES.COLOR;
@@ -379,12 +378,10 @@ exports.parseColor = function parseColor(val) {
     if (_alpha && numberRegEx.test(_alpha)) {
       alpha = parseFloat(_alpha);
     }
-
-    const [r, g, b] = hslToRgb(hue, saturation / 100, lightness / 100);
     if (!_alphaString || alpha === 1) {
-      return 'rgb(' + r + ', ' + g + ', ' + b + ')';
+      return 'hsl(' + hue + ', ' + saturation + '%, ' + lightness + '%)';
     }
-    return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + alpha + ')';
+    return 'hsla(' + hue + ', ' + saturation + '%, ' + lightness + '%, ' + alpha + ')';
   }
 
   if (type === exports.TYPES.COLOR) {
diff --git a/lib/utils/colorSpace.js b/lib/utils/colorSpace.js
deleted file mode 100644
index 92ca7bd..0000000
--- a/lib/utils/colorSpace.js
+++ /dev/null
@@ -1,21 +0,0 @@
-'use strict';
-
-const hueToRgb = (t1, t2, hue) => {
-  if (hue < 0) hue += 6;
-  if (hue >= 6) hue -= 6;
-
-  if (hue < 1) return (t2 - t1) * hue + t1;
-  else if (hue < 3) return t2;
-  else if (hue < 4) return (t2 - t1) * (4 - hue) + t1;
-  else return t1;
-};
-
-// https://www.w3.org/TR/css-color-4/#hsl-to-rgb
-exports.hslToRgb = (hue, sat, light) => {
-  const t2 = light <= 0.5 ? light * (sat + 1) : light + sat - light * sat;
-  const t1 = light * 2 - t2;
-  const r = hueToRgb(t1, t2, hue + 2);
-  const g = hueToRgb(t1, t2, hue);
-  const b = hueToRgb(t1, t2, hue - 2);
-  return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
-};


Test patch:
diff --git a/lib/CSSStyleDeclaration.test.js b/lib/CSSStyleDeclaration.test.js
index a246036..f8903c7 100644
--- a/lib/CSSStyleDeclaration.test.js
+++ b/lib/CSSStyleDeclaration.test.js
@@ -184,9 +184,9 @@ describe('CSSStyleDeclaration', () => {
     style.color = 'rgba(300, 200, 100, 1.5)';
     expect(style.color).toEqual('rgb(255, 200, 100)');
     style.color = 'hsla(0, 1%, 2%, 0.5)';
-    expect(style.color).toEqual('rgba(5, 5, 5, 0.5)');
+    expect(style.color).toEqual('hsla(0, 1%, 2%, 0.5)');
     style.color = 'hsl(0, 1%, 2%)';
-    expect(style.color).toEqual('rgb(5, 5, 5)');
+    expect(style.color).toEqual('hsl(0, 1%, 2%)');
     style.color = 'rebeccapurple';
     expect(style.color).toEqual('rebeccapurple');
     style.color = 'transparent';
diff --git a/lib/parsers.test.js b/lib/parsers.test.js
index 1f57dfa..d7db528 100644
--- a/lib/parsers.test.js
+++ b/lib/parsers.test.js
@@ -38,13 +38,6 @@ describe('valueType', () => {
     expect(output).toEqual(parsers.TYPES.COLOR);
   });
 
-  it('returns color for rgba(n, n, n, n) with decimal alpha', () => {
-    let input = 'rgba(10, 10, 10, 0.5)';
-    let output = parsers.valueType(input);
-
-    expect(output).toEqual(parsers.TYPES.COLOR);
-  });
-
   it('returns color for rgba(p, p, p, n)', () => {
     let input = 'rgba(10%, 10%, 10%, 1)';
     let output = parsers.valueType(input);
@@ -52,13 +45,6 @@ describe('valueType', () => {
     expect(output).toEqual(parsers.TYPES.COLOR);
   });
 
-  it('returns color for rgba(p, p, p, n) with decimal alpha', () => {
-    let input = 'rgba(10%, 10%, 10%, 0.5)';
-    let output = parsers.valueType(input);
-
-    expect(output).toEqual(parsers.TYPES.COLOR);
-  });
-
   it('returns length for 100ch', () => {
     let input = '100ch';
     let output = parsers.valueType(input);
@@ -88,20 +74,7 @@ describe('parseString', () => {
   it.todo('test');
 });
 describe('parseColor', () => {
-  it('should convert hsl to rgb values', () => {
-    let input = 'hsla(0, 1%, 2%)';
-    let output = parsers.parseColor(input);
-
-    expect(output).toEqual('rgb(5, 5, 5)');
-  });
-  it('should convert hsla to rgba values', () => {
-    let input = 'hsla(0, 1%, 2%, 0.5)';
-    let output = parsers.parseColor(input);
-
-    expect(output).toEqual('rgba(5, 5, 5, 0.5)');
-  });
-
-  it.todo('Add more tests');
+  it.todo('test');
 });
 describe('parseAngle', () => {
   it.todo('test');