@csstools/postcss-hwb-function 1.0.2 → 2.0.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changes to PostCSS HWB Function
2
2
 
3
+ ### 2.0.1 (January 28, 2023)
4
+
5
+ - Improve `types` declaration in `package.json`
6
+
7
+ ### 2.0.0 (January 24, 2023)
8
+
9
+ - Updated: Support for Node v14+ (major).
10
+
3
11
  ### 1.0.2 (July 8, 2022)
4
12
 
5
13
  - Fix case insensitive matching.
package/INSTALL.md CHANGED
@@ -2,8 +2,15 @@
2
2
 
3
3
  [PostCSS HWB Function] runs in all Node environments, with special instructions for:
4
4
 
5
- | [Node](#node) | [PostCSS CLI](#postcss-cli) | [Webpack](#webpack) | [Create React App](#create-react-app) | [Gulp](#gulp) | [Grunt](#grunt) |
6
- | --- | --- | --- | --- | --- | --- |
5
+ - [Node](#node)
6
+ - [PostCSS CLI](#postcss-cli)
7
+ - [PostCSS Load Config](#postcss-load-config)
8
+ - [Webpack](#webpack)
9
+ - [Next.js](#nextjs)
10
+ - [Gulp](#gulp)
11
+ - [Grunt](#grunt)
12
+
13
+
7
14
 
8
15
  ## Node
9
16
 
@@ -16,6 +23,7 @@ npm install postcss @csstools/postcss-hwb-function --save-dev
16
23
  Use it as a [PostCSS] plugin:
17
24
 
18
25
  ```js
26
+ // commonjs
19
27
  const postcss = require('postcss');
20
28
  const postcssHWBFunction = require('@csstools/postcss-hwb-function');
21
29
 
@@ -24,6 +32,16 @@ postcss([
24
32
  ]).process(YOUR_CSS /*, processOptions */);
25
33
  ```
26
34
 
35
+ ```js
36
+ // esm
37
+ import postcss from 'postcss';
38
+ import postcssHWBFunction from '@csstools/postcss-hwb-function';
39
+
40
+ postcss([
41
+ postcssHWBFunction(/* pluginOptions */)
42
+ ]).process(YOUR_CSS /*, processOptions */);
43
+ ```
44
+
27
45
  ## PostCSS CLI
28
46
 
29
47
  Add [PostCSS CLI] to your project:
@@ -44,6 +62,38 @@ module.exports = {
44
62
  }
45
63
  ```
46
64
 
65
+ ## PostCSS Load Config
66
+
67
+ If your framework/CLI supports [`postcss-load-config`](https://github.com/postcss/postcss-load-config).
68
+
69
+ ```bash
70
+ npm install @csstools/postcss-hwb-function --save-dev
71
+ ```
72
+
73
+ `package.json`:
74
+
75
+ ```json
76
+ {
77
+ "postcss": {
78
+ "plugins": {
79
+ "@csstools/postcss-hwb-function": {}
80
+ }
81
+ }
82
+ }
83
+ ```
84
+
85
+ `.postcssrc.json`:
86
+
87
+ ```json
88
+ {
89
+ "plugins": {
90
+ "@csstools/postcss-hwb-function": {}
91
+ }
92
+ }
93
+ ```
94
+
95
+ _See the [README of `postcss-load-config`](https://github.com/postcss/postcss-load-config#usage) for more usage options._
96
+
47
97
  ## Webpack
48
98
 
49
99
  _Webpack version 5_
@@ -73,6 +123,7 @@ module.exports = {
73
123
  options: {
74
124
  postcssOptions: {
75
125
  plugins: [
126
+ // Other plugins,
76
127
  [
77
128
  "@csstools/postcss-hwb-function",
78
129
  {
@@ -90,26 +141,35 @@ module.exports = {
90
141
  };
91
142
  ```
92
143
 
93
- ## Create React App
144
+ ## Next.js
94
145
 
95
- Add [React App Rewired] and [React App Rewire PostCSS] to your project:
146
+ Read the instructions on how to [customize the PostCSS configuration in Next.js](https://nextjs.org/docs/advanced-features/customizing-postcss-config)
96
147
 
97
148
  ```bash
98
- npm install react-app-rewired react-app-rewire-postcss @csstools/postcss-hwb-function --save-dev
149
+ npm install @csstools/postcss-hwb-function --save-dev
99
150
  ```
100
151
 
101
- Use [React App Rewire PostCSS] and [PostCSS HWB Function] in your
102
- `config-overrides.js` file:
152
+ Use [PostCSS HWB Function] in your `postcss.config.json` file:
103
153
 
104
- ```js
105
- const reactAppRewirePostcss = require('react-app-rewire-postcss');
106
- const postcssHWBFunction = require('@csstools/postcss-hwb-function');
154
+ ```json
155
+ {
156
+ "plugins": [
157
+ "@csstools/postcss-hwb-function"
158
+ ]
159
+ }
160
+ ```
107
161
 
108
- module.exports = config => reactAppRewirePostcss(config, {
109
- plugins: () => [
110
- postcssHWBFunction(/* pluginOptions */)
162
+ ```json5
163
+ {
164
+ "plugins": [
165
+ [
166
+ "@csstools/postcss-hwb-function",
167
+ {
168
+ // Optionally add plugin options
169
+ }
170
+ ]
111
171
  ]
112
- });
172
+ }
113
173
  ```
114
174
 
115
175
  ## Gulp
@@ -172,5 +232,4 @@ grunt.initConfig({
172
232
  [PostCSS CLI]: https://github.com/postcss/postcss-cli
173
233
  [PostCSS Loader]: https://github.com/postcss/postcss-loader
174
234
  [PostCSS HWB Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-hwb-function
175
- [React App Rewire PostCSS]: https://github.com/csstools/react-app-rewire-postcss
176
- [React App Rewired]: https://github.com/timarney/react-app-rewired
235
+ [Next.js]: https://nextjs.org
package/README.md CHANGED
@@ -45,8 +45,8 @@ postcss([
45
45
  [PostCSS HWB Function] runs in all Node environments, with special
46
46
  instructions for:
47
47
 
48
- | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
49
- | --- | --- | --- | --- | --- | --- |
48
+ | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
49
+ | --- | --- | --- | --- | --- |
50
50
 
51
51
  ## Options
52
52
 
package/dist/hwb.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- declare type color = [number, number, number];
1
+ type color = [number, number, number];
2
2
  export declare function hwbToRgb(hwb: color): color;
3
3
  export {};
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=e(require("postcss-value-parser"));function n(e){const t=e[0];let n=e[1],r=e[2];if(n/=100,r/=100,n+r>=1){const e=n/(n+r);return[e,e,e].map((e=>Math.round(255*e)))}const u=function(e){let t=e[0],n=e[1],r=e[2];t%=360,t<0&&(t+=360);function u(e){const u=(e+t/30)%12,o=n*Math.min(r,1-r);return r-o*Math.max(-1,Math.min(u-3,9-u,1))}return n/=100,r/=100,[u(0),u(8),u(4)]}([t,100,50]);for(let e=0;e<3;e++)u[e]*=1-n-r,u[e]+=n;return u.map((e=>Math.round(255*e)))}function r(e){const r=e.nodes.slice().filter((e=>"comment"!==e.type&&"space"!==e.type)),s=function(e){if(!function(e){if(!e||"word"!==e.type)return!1;if(!i(e))return!1;const n=t.default.unit(e.value);if(!n)return!1;const r=n.unit.toLowerCase();return!!n.number&&("deg"===r||"grad"===r||"rad"===r||"turn"===r||""===r)}(e[0]))return null;if(!u(e[1]))return null;if(!u(e[2]))return null;const n={h:t.default.unit(e[0].value),hNode:e[0],w:t.default.unit(e[1].value),wNode:e[1],b:t.default.unit(e[2].value),bNode:e[2]};if(function(e){switch(e.unit.toLowerCase()){case"deg":return void(e.unit="");case"rad":return e.unit="",void(e.number=(180*parseFloat(e.number)/Math.PI).toString());case"grad":return e.unit="",void(e.number=(.9*parseFloat(e.number)).toString());case"turn":e.unit="",e.number=(360*parseFloat(e.number)).toString()}}(n.h),""!==n.h.unit)return null;a(n.w),a(n.b),function(e){return e&&"div"===e.type&&"/"===e.value}(e[3])&&(n.slash=e[3]);(u(e[4])||function(e){return e&&"function"===e.type&&"calc"===e.value.toLowerCase()}(e[4])||function(e){return e&&"function"===e.type&&"var"===e.value.toLowerCase()}(e[4]))&&(n.alpha=e[4]);return n}(r);if(!s)return;if(r.length>3&&(!s.slash||!s.alpha))return;e.value="rgb",function(e,n,r){if(!n||!r)return;if(e.value="rgba",n.value=",",n.before="",!function(e){if(!e||"word"!==e.type)return!1;if(!i(e))return!1;const n=t.default.unit(e.value);if(!n)return!1;return!!n.number}(r))return;const u=t.default.unit(r.value);if(!u)return;"%"===u.unit&&(u.number=String(parseFloat(u.number)/100),r.value=String(u.number))}(e,s.slash,s.alpha);const[l,c,f]=[(d=s).hNode,d.wNode,d.bNode];var d;const[p,v,b]=function(e){return[e.h,e.w,e.b]}(s),h=n([p.number,v.number,b.number].map((e=>parseFloat(e))));e.nodes.splice(e.nodes.indexOf(l)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),e.nodes.splice(e.nodes.indexOf(c)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),o(e.nodes,l,{...l,value:String(h[0])}),o(e.nodes,c,{...c,value:String(h[1])}),o(e.nodes,f,{...f,value:String(h[2])})}function u(e){if(!e||"word"!==e.type)return!1;if(!i(e))return!1;const n=t.default.unit(e.value);return!!n&&("%"===n.unit||""===n.unit)}function o(e,t,n){const r=e.indexOf(t);e[r]=n}function a(e){if("%"!==e.unit)return e.unit="%",void(e.number=(100*parseFloat(e.number)).toString())}function i(e){if(!e||!e.value)return!1;try{return!1!==t.default.unit(e.value)}catch(e){return!1}}const s=e=>{const n="preserve"in Object(e)&&Boolean(e.preserve);return{postcssPlugin:"postcss-hwb-function",Declaration:(e,{result:u,postcss:o})=>{if(n&&function(e){let t=e.parent;for(;t;)if("atrule"===t.type){if("supports"===t.name.toLowerCase()&&-1!==t.params.toLowerCase().indexOf("(color: hwb(0% 0 0))"))return!0;t=t.parent}else t=t.parent;return!1}(e))return;const a=e.value;if(!a.toLowerCase().includes("hwb"))return;const i=function(e,n,u){let o;try{o=t.default(e)}catch(t){n.warn(u,`Failed to parse value '${e}' as a hwb function. Leaving the original value intact.`)}if(void 0===o)return;o.walk((e=>{e.type&&"function"===e.type&&"hwb"===e.value.toLowerCase()&&r(e)}));const a=String(o);if(a===e)return;return a}(a,e,u);if(void 0!==i)if(e.variable&&n){const t=e.parent,n=o.atRule({name:"supports",params:"(color: hwb(0% 0 0))",source:e.source}),r=t.clone();r.removeAll(),r.append(e.clone()),n.append(r),function(e,t,n){let r=t,u=t.next();for(;r&&u&&"atrule"===u.type&&"supports"===u.name.toLowerCase()&&u.params===n;)r=u,u=u.next();r.after(e)}(n,t,"(color: hwb(0% 0 0))"),e.replaceWith(e.clone({value:i}))}else n?e.cloneBefore({value:i}):e.replaceWith(e.clone({value:i}))}}};s.postcss=!0,module.exports=s;
1
+ "use strict";var e=require("postcss-value-parser");function hasSupportsAtRuleAncestor(e){let n=e.parent;for(;n;)if("atrule"===n.type){if("supports"===n.name.toLowerCase()&&-1!==n.params.toLowerCase().indexOf("(color: hwb(0% 0 0))"))return!0;n=n.parent}else n=n.parent;return!1}function hwbToRgb(e){const n=e[0];let r=e[1],t=e[2];if(r/=100,t/=100,r+t>=1){const e=r/(r+t);return[e,e,e].map((e=>Math.round(255*e)))}const o=hslToRgb([n,100,50]);for(let e=0;e<3;e++)o[e]*=1-r-t,o[e]+=r;return o.map((e=>Math.round(255*e)))}function hslToRgb(e){let n=e[0],r=e[1],t=e[2];function f(e){const o=(e+n/30)%12,u=r*Math.min(t,1-t);return t-u*Math.max(-1,Math.min(o-3,9-o,1))}return n%=360,n<0&&(n+=360),r/=100,t/=100,[f(0),f(8),f(4)]}function onCSSFunctionSRgb(e){const n=e.nodes.slice().filter((e=>"comment"!==e.type&&"space"!==e.type)),r=hwbFunctionContents(n);if(!r)return;if(n.length>3&&(!r.slash||!r.alpha))return;e.value="rgb",transformAlpha(e,r.slash,r.alpha);const[t,o,u]=[(i=r).hNode,i.wNode,i.bNode];var i;const[a,s,c]=channelDimensions(r),l=hwbToRgb([a.number,s.number,c.number].map((e=>parseFloat(e))));e.nodes.splice(e.nodes.indexOf(t)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),e.nodes.splice(e.nodes.indexOf(o)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),replaceWith(e.nodes,t,{...t,value:String(l[0])}),replaceWith(e.nodes,o,{...o,value:String(l[1])}),replaceWith(e.nodes,u,{...u,value:String(l[2])})}function isNumericNode(n){if(!n||"word"!==n.type)return!1;if(!canParseAsUnit(n))return!1;const r=e.unit(n.value);return!!r&&!!r.number}function isNumericNodeHueLike(n){if(!n||"word"!==n.type)return!1;if(!canParseAsUnit(n))return!1;const r=e.unit(n.value);if(!r)return!1;const t=r.unit.toLowerCase();return!!r.number&&("deg"===t||"grad"===t||"rad"===t||"turn"===t||""===t)}function isNumericNodePercentageOrNumber(n){if(!n||"word"!==n.type)return!1;if(!canParseAsUnit(n))return!1;const r=e.unit(n.value);return!!r&&("%"===r.unit||""===r.unit)}function isCalcNode(e){return e&&"function"===e.type&&"calc"===e.value.toLowerCase()}function isVarNode(e){return e&&"function"===e.type&&"var"===e.value.toLowerCase()}function hwbFunctionContents(n){if(!isNumericNodeHueLike(n[0]))return null;if(!isNumericNodePercentageOrNumber(n[1]))return null;if(!isNumericNodePercentageOrNumber(n[2]))return null;const r={h:e.unit(n[0].value),hNode:n[0],w:e.unit(n[1].value),wNode:n[1],b:e.unit(n[2].value),bNode:n[2]};return normalizeHueNode(r.h),""!==r.h.unit?null:(normalizeBlackOrWhiteNode(r.w),normalizeBlackOrWhiteNode(r.b),(t=n[3])&&"div"===t.type&&"/"===t.value&&(r.slash=n[3]),(isNumericNodePercentageOrNumber(n[4])||isCalcNode(n[4])||isVarNode(n[4]))&&(r.alpha=n[4]),r);var t}function channelDimensions(e){return[e.h,e.w,e.b]}function transformAlpha(n,r,t){if(!r||!t)return;if(n.value="rgba",r.value=",",r.before="",!isNumericNode(t))return;const o=e.unit(t.value);o&&"%"===o.unit&&(o.number=String(parseFloat(o.number)/100),t.value=String(o.number))}function replaceWith(e,n,r){const t=e.indexOf(n);e[t]=r}function normalizeHueNode(e){switch(e.unit.toLowerCase()){case"deg":return void(e.unit="");case"rad":return e.unit="",void(e.number=(180*parseFloat(e.number)/Math.PI).toString());case"grad":return e.unit="",void(e.number=(.9*parseFloat(e.number)).toString());case"turn":return e.unit="",void(e.number=(360*parseFloat(e.number)).toString())}}function normalizeBlackOrWhiteNode(e){if("%"!==e.unit)return e.unit="%",void(e.number=(100*parseFloat(e.number)).toString())}function canParseAsUnit(n){if(!n||!n.value)return!1;try{return!1!==e.unit(n.value)}catch(e){return!1}}const n="(color: hwb(0% 0 0))",postcssPlugin=e=>{const r="preserve"in Object(e)&&Boolean(e.preserve);return{postcssPlugin:"postcss-hwb-function",Declaration:(e,{result:t,postcss:o})=>{if(r&&hasSupportsAtRuleAncestor(e))return;const u=e.value;if(!u.toLowerCase().includes("hwb"))return;const i=modifiedValues(u,e,t);if(void 0!==i)if(e.variable&&r){const r=e.parent,t=o.atRule({name:"supports",params:n,source:e.source}),u=r.clone();u.removeAll(),u.append(e.clone()),t.append(u),insertAtSupportsAfterCorrectRule(t,r,n),e.replaceWith(e.clone({value:i}))}else r?e.cloneBefore({value:i}):e.replaceWith(e.clone({value:i}))}}};function modifiedValues(n,r,t){let o;try{o=e(n)}catch(e){r.warn(t,`Failed to parse value '${n}' as a hwb function. Leaving the original value intact.`)}if(void 0===o)return;o.walk((e=>{e.type&&"function"===e.type&&"hwb"===e.value.toLowerCase()&&onCSSFunctionSRgb(e)}));const u=String(o);return u!==n?u:void 0}function insertAtSupportsAfterCorrectRule(e,n,r){let t=n,o=n.next();for(;t&&o&&"atrule"===o.type&&"supports"===o.name.toLowerCase()&&o.params===r;)t=o,o=o.next();t.after(e)}postcssPlugin.postcss=!0,module.exports=postcssPlugin;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import type { PluginCreator } from 'postcss';
2
+ /** postcss-hwb-function plugin options */
3
+ export type pluginOptions = {
4
+ /** Preserve the original notation. default: false */
5
+ preserve?: boolean;
6
+ };
2
7
  /** Transform hwb() functions in CSS. */
3
- declare const postcssPlugin: PluginCreator<{
4
- preserve: boolean;
5
- }>;
8
+ declare const postcssPlugin: PluginCreator<pluginOptions>;
6
9
  export default postcssPlugin;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import e from"postcss-value-parser";function n(e){const n=e[0];let t=e[1],r=e[2];if(t/=100,r/=100,t+r>=1){const e=t/(t+r);return[e,e,e].map((e=>Math.round(255*e)))}const u=function(e){let n=e[0],t=e[1],r=e[2];n%=360,n<0&&(n+=360);function u(e){const u=(e+n/30)%12,o=t*Math.min(r,1-r);return r-o*Math.max(-1,Math.min(u-3,9-u,1))}return t/=100,r/=100,[u(0),u(8),u(4)]}([n,100,50]);for(let e=0;e<3;e++)u[e]*=1-t-r,u[e]+=t;return u.map((e=>Math.round(255*e)))}function t(t){const i=t.nodes.slice().filter((e=>"comment"!==e.type&&"space"!==e.type)),s=function(n){if(!function(n){if(!n||"word"!==n.type)return!1;if(!a(n))return!1;const t=e.unit(n.value);if(!t)return!1;const r=t.unit.toLowerCase();return!!t.number&&("deg"===r||"grad"===r||"rad"===r||"turn"===r||""===r)}(n[0]))return null;if(!r(n[1]))return null;if(!r(n[2]))return null;const t={h:e.unit(n[0].value),hNode:n[0],w:e.unit(n[1].value),wNode:n[1],b:e.unit(n[2].value),bNode:n[2]};if(function(e){switch(e.unit.toLowerCase()){case"deg":return void(e.unit="");case"rad":return e.unit="",void(e.number=(180*parseFloat(e.number)/Math.PI).toString());case"grad":return e.unit="",void(e.number=(.9*parseFloat(e.number)).toString());case"turn":e.unit="",e.number=(360*parseFloat(e.number)).toString()}}(t.h),""!==t.h.unit)return null;o(t.w),o(t.b),function(e){return e&&"div"===e.type&&"/"===e.value}(n[3])&&(t.slash=n[3]);(r(n[4])||function(e){return e&&"function"===e.type&&"calc"===e.value.toLowerCase()}(n[4])||function(e){return e&&"function"===e.type&&"var"===e.value.toLowerCase()}(n[4]))&&(t.alpha=n[4]);return t}(i);if(!s)return;if(i.length>3&&(!s.slash||!s.alpha))return;t.value="rgb",function(n,t,r){if(!t||!r)return;if(n.value="rgba",t.value=",",t.before="",!function(n){if(!n||"word"!==n.type)return!1;if(!a(n))return!1;const t=e.unit(n.value);if(!t)return!1;return!!t.number}(r))return;const u=e.unit(r.value);if(!u)return;"%"===u.unit&&(u.number=String(parseFloat(u.number)/100),r.value=String(u.number))}(t,s.slash,s.alpha);const[l,c,f]=[(p=s).hNode,p.wNode,p.bNode];var p;const[d,v,b]=function(e){return[e.h,e.w,e.b]}(s),m=n([d.number,v.number,b.number].map((e=>parseFloat(e))));t.nodes.splice(t.nodes.indexOf(l)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),t.nodes.splice(t.nodes.indexOf(c)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),u(t.nodes,l,{...l,value:String(m[0])}),u(t.nodes,c,{...c,value:String(m[1])}),u(t.nodes,f,{...f,value:String(m[2])})}function r(n){if(!n||"word"!==n.type)return!1;if(!a(n))return!1;const t=e.unit(n.value);return!!t&&("%"===t.unit||""===t.unit)}function u(e,n,t){const r=e.indexOf(n);e[r]=t}function o(e){if("%"!==e.unit)return e.unit="%",void(e.number=(100*parseFloat(e.number)).toString())}function a(n){if(!n||!n.value)return!1;try{return!1!==e.unit(n.value)}catch(e){return!1}}const i=n=>{const r="preserve"in Object(n)&&Boolean(n.preserve);return{postcssPlugin:"postcss-hwb-function",Declaration:(n,{result:u,postcss:o})=>{if(r&&function(e){let n=e.parent;for(;n;)if("atrule"===n.type){if("supports"===n.name.toLowerCase()&&-1!==n.params.toLowerCase().indexOf("(color: hwb(0% 0 0))"))return!0;n=n.parent}else n=n.parent;return!1}(n))return;const a=n.value;if(!a.toLowerCase().includes("hwb"))return;const i=function(n,r,u){let o;try{o=e(n)}catch(e){r.warn(u,`Failed to parse value '${n}' as a hwb function. Leaving the original value intact.`)}if(void 0===o)return;o.walk((e=>{e.type&&"function"===e.type&&"hwb"===e.value.toLowerCase()&&t(e)}));const a=String(o);if(a===n)return;return a}(a,n,u);if(void 0!==i)if(n.variable&&r){const e=n.parent,t=o.atRule({name:"supports",params:"(color: hwb(0% 0 0))",source:n.source}),r=e.clone();r.removeAll(),r.append(n.clone()),t.append(r),function(e,n,t){let r=n,u=n.next();for(;r&&u&&"atrule"===u.type&&"supports"===u.name.toLowerCase()&&u.params===t;)r=u,u=u.next();r.after(e)}(t,e,"(color: hwb(0% 0 0))"),n.replaceWith(n.clone({value:i}))}else r?n.cloneBefore({value:i}):n.replaceWith(n.clone({value:i}))}}};i.postcss=!0;export{i as default};
1
+ import e from"postcss-value-parser";function hasSupportsAtRuleAncestor(e){let n=e.parent;for(;n;)if("atrule"===n.type){if("supports"===n.name.toLowerCase()&&-1!==n.params.toLowerCase().indexOf("(color: hwb(0% 0 0))"))return!0;n=n.parent}else n=n.parent;return!1}function hwbToRgb(e){const n=e[0];let r=e[1],t=e[2];if(r/=100,t/=100,r+t>=1){const e=r/(r+t);return[e,e,e].map((e=>Math.round(255*e)))}const o=hslToRgb([n,100,50]);for(let e=0;e<3;e++)o[e]*=1-r-t,o[e]+=r;return o.map((e=>Math.round(255*e)))}function hslToRgb(e){let n=e[0],r=e[1],t=e[2];function f(e){const o=(e+n/30)%12,u=r*Math.min(t,1-t);return t-u*Math.max(-1,Math.min(o-3,9-o,1))}return n%=360,n<0&&(n+=360),r/=100,t/=100,[f(0),f(8),f(4)]}function onCSSFunctionSRgb(e){const n=e.nodes.slice().filter((e=>"comment"!==e.type&&"space"!==e.type)),r=hwbFunctionContents(n);if(!r)return;if(n.length>3&&(!r.slash||!r.alpha))return;e.value="rgb",transformAlpha(e,r.slash,r.alpha);const[t,o,u]=[(i=r).hNode,i.wNode,i.bNode];var i;const[a,s,c]=channelDimensions(r),l=hwbToRgb([a.number,s.number,c.number].map((e=>parseFloat(e))));e.nodes.splice(e.nodes.indexOf(t)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),e.nodes.splice(e.nodes.indexOf(o)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),replaceWith(e.nodes,t,{...t,value:String(l[0])}),replaceWith(e.nodes,o,{...o,value:String(l[1])}),replaceWith(e.nodes,u,{...u,value:String(l[2])})}function isNumericNode(n){if(!n||"word"!==n.type)return!1;if(!canParseAsUnit(n))return!1;const r=e.unit(n.value);return!!r&&!!r.number}function isNumericNodeHueLike(n){if(!n||"word"!==n.type)return!1;if(!canParseAsUnit(n))return!1;const r=e.unit(n.value);if(!r)return!1;const t=r.unit.toLowerCase();return!!r.number&&("deg"===t||"grad"===t||"rad"===t||"turn"===t||""===t)}function isNumericNodePercentageOrNumber(n){if(!n||"word"!==n.type)return!1;if(!canParseAsUnit(n))return!1;const r=e.unit(n.value);return!!r&&("%"===r.unit||""===r.unit)}function isCalcNode(e){return e&&"function"===e.type&&"calc"===e.value.toLowerCase()}function isVarNode(e){return e&&"function"===e.type&&"var"===e.value.toLowerCase()}function hwbFunctionContents(n){if(!isNumericNodeHueLike(n[0]))return null;if(!isNumericNodePercentageOrNumber(n[1]))return null;if(!isNumericNodePercentageOrNumber(n[2]))return null;const r={h:e.unit(n[0].value),hNode:n[0],w:e.unit(n[1].value),wNode:n[1],b:e.unit(n[2].value),bNode:n[2]};return normalizeHueNode(r.h),""!==r.h.unit?null:(normalizeBlackOrWhiteNode(r.w),normalizeBlackOrWhiteNode(r.b),(t=n[3])&&"div"===t.type&&"/"===t.value&&(r.slash=n[3]),(isNumericNodePercentageOrNumber(n[4])||isCalcNode(n[4])||isVarNode(n[4]))&&(r.alpha=n[4]),r);var t}function channelDimensions(e){return[e.h,e.w,e.b]}function transformAlpha(n,r,t){if(!r||!t)return;if(n.value="rgba",r.value=",",r.before="",!isNumericNode(t))return;const o=e.unit(t.value);o&&"%"===o.unit&&(o.number=String(parseFloat(o.number)/100),t.value=String(o.number))}function replaceWith(e,n,r){const t=e.indexOf(n);e[t]=r}function normalizeHueNode(e){switch(e.unit.toLowerCase()){case"deg":return void(e.unit="");case"rad":return e.unit="",void(e.number=(180*parseFloat(e.number)/Math.PI).toString());case"grad":return e.unit="",void(e.number=(.9*parseFloat(e.number)).toString());case"turn":return e.unit="",void(e.number=(360*parseFloat(e.number)).toString())}}function normalizeBlackOrWhiteNode(e){if("%"!==e.unit)return e.unit="%",void(e.number=(100*parseFloat(e.number)).toString())}function canParseAsUnit(n){if(!n||!n.value)return!1;try{return!1!==e.unit(n.value)}catch(e){return!1}}const n="(color: hwb(0% 0 0))",postcssPlugin=e=>{const r="preserve"in Object(e)&&Boolean(e.preserve);return{postcssPlugin:"postcss-hwb-function",Declaration:(e,{result:t,postcss:o})=>{if(r&&hasSupportsAtRuleAncestor(e))return;const u=e.value;if(!u.toLowerCase().includes("hwb"))return;const i=modifiedValues(u,e,t);if(void 0!==i)if(e.variable&&r){const r=e.parent,t=o.atRule({name:"supports",params:n,source:e.source}),u=r.clone();u.removeAll(),u.append(e.clone()),t.append(u),insertAtSupportsAfterCorrectRule(t,r,n),e.replaceWith(e.clone({value:i}))}else r?e.cloneBefore({value:i}):e.replaceWith(e.clone({value:i}))}}};function modifiedValues(n,r,t){let o;try{o=e(n)}catch(e){r.warn(t,`Failed to parse value '${n}' as a hwb function. Leaving the original value intact.`)}if(void 0===o)return;o.walk((e=>{e.type&&"function"===e.type&&"hwb"===e.value.toLowerCase()&&onCSSFunctionSRgb(e)}));const u=String(o);return u!==n?u:void 0}function insertAtSupportsAfterCorrectRule(e,n,r){let t=n,o=n.next();for(;t&&o&&"atrule"===o.type&&"supports"===o.name.toLowerCase()&&o.params===r;)t=o,o=o.next();t.after(e)}postcssPlugin.postcss=!0;export{postcssPlugin as default};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@csstools/postcss-hwb-function",
3
3
  "description": "Use hwb() color functions in CSS",
4
- "version": "1.0.2",
4
+ "version": "2.0.1",
5
5
  "author": "Jonathan Neal <jonathantneal@hotmail.com>",
6
6
  "license": "CC0-1.0",
7
7
  "funding": {
@@ -9,13 +9,14 @@
9
9
  "url": "https://opencollective.com/csstools"
10
10
  },
11
11
  "engines": {
12
- "node": "^12 || ^14 || >=16"
12
+ "node": "^14 || ^16 || >=18"
13
13
  },
14
14
  "main": "dist/index.cjs",
15
15
  "module": "dist/index.mjs",
16
16
  "types": "./dist/index.d.ts",
17
17
  "exports": {
18
18
  ".": {
19
+ "types": "./dist/index.d.ts",
19
20
  "import": "./dist/index.mjs",
20
21
  "require": "./dist/index.cjs",
21
22
  "default": "./dist/index.mjs"
@@ -32,11 +33,12 @@
32
33
  "postcss-value-parser": "^4.2.0"
33
34
  },
34
35
  "peerDependencies": {
35
- "postcss": "^8.2"
36
+ "postcss": "^8.4"
36
37
  },
37
38
  "scripts": {
38
- "build": "rollup -c ../../rollup/default.js",
39
- "clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true });\"",
39
+ "prebuild": "npm run clean",
40
+ "build": "rollup -c ../../rollup/default.mjs",
41
+ "clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true }); fs.mkdirSync('./dist');\"",
40
42
  "docs": "node ../../.github/bin/generate-docs/install.mjs",
41
43
  "lint": "npm run lint:eslint && npm run lint:package-json",
42
44
  "lint:eslint": "eslint ./src --ext .js --ext .ts --ext .mjs --no-error-on-unmatched-pattern",
@@ -55,7 +57,6 @@
55
57
  "bugs": "https://github.com/csstools/postcss-plugins/issues",
56
58
  "keywords": [
57
59
  "color",
58
- "colors",
59
60
  "comma",
60
61
  "css",
61
62
  "design",