postcss-lab-function 4.0.0 → 4.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/dist/index.cjs.js +17 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +17 -2
- package/dist/index.esm.js.map +1 -1
- package/package.json +9 -9
package/dist/index.cjs.js
CHANGED
|
@@ -148,13 +148,28 @@ var options = {
|
|
|
148
148
|
|
|
149
149
|
/** @type {(decl: CSSDeclaration) => void} Transform lab() and lch() functions in CSS Declarations. */
|
|
150
150
|
|
|
151
|
-
const onCSSDeclaration = decl
|
|
151
|
+
const onCSSDeclaration = (decl, {
|
|
152
|
+
result
|
|
153
|
+
}) => {
|
|
152
154
|
const {
|
|
153
155
|
value: originalValue
|
|
154
156
|
} = decl;
|
|
155
157
|
|
|
156
158
|
if (hasAnyColorFunction(originalValue)) {
|
|
157
|
-
|
|
159
|
+
let valueAST;
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
valueAST = postcssValuesParser.parse(originalValue, {
|
|
163
|
+
ignoreUnknownWords: true
|
|
164
|
+
});
|
|
165
|
+
} catch (error) {
|
|
166
|
+
decl.warn(result, `Failed to parse value '${originalValue}' as a lab or hcl function. Leaving the original value intact.`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (typeof valueAST === 'undefined') {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
158
173
|
valueAST.walkType('func', onCSSFunction);
|
|
159
174
|
const modifiedValue = String(valueAST);
|
|
160
175
|
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/onCSSFunction.js","../src/options.js","../src/onCSSDeclaration.js","../src/index.js"],"sourcesContent":["import { lab2rgb, lch2rgb } from '@csstools/convert-colors'\nimport { parse } from 'postcss-values-parser'\n\n/** @type {(decl: CSSFunction) => void} Transform lab() and lch() functions. */\nconst onCSSFunction = node => {\n\t/** @type {{ name: string, nodes: CSSNode[] }} */\n\tconst { name, nodes } = node\n\n\tif (isColorFunctionName(name)) {\n\t\tif (isLabFunctionContents(nodes) || isLchFunctionContents(nodes)) {\n\t\t\t// rename the Color function to `rgb`\n\t\t\tObject.assign(node, { name: 'rgb' })\n\n\t\t\t/** @type {CSSPunctuation} Slash punctuation before the Alpha channel. */\n\t\t\tconst slashNode = nodes[3]\n\n\t\t\t/** @type {CSSNumber} Alpha channel. */\n\t\t\tconst alphaNode = nodes[4]\n\n\t\t\tif (alphaNode) {\n\t\t\t\tif (isPercentage(alphaNode) && !isCalc(alphaNode)) {\n\t\t\t\t\t// transform the Alpha channel from a Percentage to (0-1) Number\n\t\t\t\t\tObject.assign(alphaNode, { value: String(alphaNode.value / 100), unit: '' })\n\t\t\t\t}\n\n\t\t\t\t// if the color is fully opaque (i.e. the Alpha channel is `1`)\n\t\t\t\tif (alphaNode.value === '1') {\n\t\t\t\t\t// remove the Slash and Alpha channel\n\t\t\t\t\tslashNode.remove()\n\t\t\t\t\talphaNode.remove()\n\t\t\t\t} else {\n\t\t\t\t\t// otherwise, rename the Color function to `rgba`\n\t\t\t\t\tObject.assign(node, { name: 'rgba' })\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// replace a remaining Slash with a Comma\n\t\t\tif (slashNode && isSlash(slashNode)) {\n\t\t\t\tslashNode.replaceWith(commaNode.clone())\n\t\t\t}\n\n\t\t\t/** Extracted Color channels. */\n\t\t\tconst [channelNode1, channelNode2, channelNode3] = nodes\n\n\t\t\t/** Corresponding Color transformer. */\n\t\t\tconst toRGB = isLabColorFunctionName(name) ? lab2rgb : lch2rgb\n\n\t\t\t/** RGB channels from the source color. */\n\t\t\tconst rgbValues = toRGB(\n\t\t\t\t...[\n\t\t\t\t\tchannelNode1.value,\n\t\t\t\t\tchannelNode2.value,\n\t\t\t\t\tchannelNode3.value\n\t\t\t\t].map(\n\t\t\t\t\tchannelNumber => parseFloat(channelNumber)\n\t\t\t\t)\n\t\t\t).map(\n\t\t\t\tchannelValue => Math.max(Math.min(parseInt(channelValue * 2.55), 255), 0)\n\t\t\t)\n\n\t\t\tchannelNode3.replaceWith(\n\t\t\t\tchannelNode3.clone({ value: String(rgbValues[2]) })\n\t\t\t)\n\n\t\t\tchannelNode2.replaceWith(\n\t\t\t\tchannelNode2.clone({ value: String(rgbValues[1]) }),\n\t\t\t\tcommaNode.clone()\n\t\t\t)\n\n\t\t\tchannelNode1.replaceWith(\n\t\t\t\tchannelNode1.clone({ value: String(rgbValues[0]), unit: '' }),\n\t\t\t\tcommaNode.clone()\n\t\t\t)\n\t\t}\n\t}\n}\n\nexport default onCSSFunction\n\nconst commaNode = parse(',').first\n\n/** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */\nconst createRegExpTest = Function.bind.bind(RegExp.prototype.test)\n\n/** Return whether the function name is `lab` or `lch`. */\nconst isColorFunctionName = createRegExpTest(/^(lab|lch)$/i)\n\n/** Return whether the function name is `calc`. */\nconst isCalcFunctionName = createRegExpTest(/^calc$/i)\n\n/** Return whether the function name is `lab`. */\nconst isLabColorFunctionName = createRegExpTest(/^lab$/i)\n\n/** Return whether the unit is alpha-like. */\nconst isAlphaLikeUnit = createRegExpTest(/^%?$/i)\n\n/** Return whether the unit is hue-like. */\nconst isHueLikeUnit = createRegExpTest(/^(deg|grad|rad|turn)?$/i)\n\n/** @type {(node: CSSFunction) => boolean} Return whether the node is an Alpha-like unit. */\nconst isAlphaValue = node => isCalc(node) || node.type === 'numeric' && isAlphaLikeUnit(node.unit)\n\n/** @type {(node: CSSFunction) => boolean} Return whether the node is a calc() function. */\nconst isCalc = node => node.type === 'func' && isCalcFunctionName(node.name)\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Hue-like unit. */\nconst isHue = node => isCalc(node) || node.type === 'numeric' && isHueLikeUnit(node.unit)\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Number unit. */\nconst isNumber = node => isCalc(node) || node.type === 'numeric' && node.unit === ''\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Percentage unit. */\nconst isPercentage = node => isCalc(node) || node.type === 'numeric' && node.unit === '%'\n\n/** @type {(node: CSSOperator) => boolean} Return whether the node is a Slash delimiter. */\nconst isSlash = node => node.type === 'operator' && node.value === '/'\n\n/** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lab() function. */\nconst isLabFunctionContents = nodes => nodes.every(\n\t(node, index) => typeof labFunctionContents[index] === 'function' && labFunctionContents[index](node)\n)\n\n/** Set of nodes in a lab() function. */\nconst labFunctionContents = [isPercentage, isNumber, isNumber, isSlash, isAlphaValue]\n\n/** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lch() function. */\nconst isLchFunctionContents = nodes => nodes.every(\n\t(node, index) => typeof lchFunctionContents[index] === 'function' && lchFunctionContents[index](node)\n)\n\n/** Set of nodes in a lch() function. */\nconst lchFunctionContents = [isPercentage, isNumber, isHue, isSlash, isAlphaValue]\n\n/** @typedef {import('postcss-values-parser').Func} CSSFunction */\n/** @typedef {import('postcss-values-parser').Node} CSSNode */\n/** @typedef {import('postcss-values-parser').Numeric} CSSNumber */\n/** @typedef {import('postcss-values-parser').Operator} CSSOperator */\n/** @typedef {import('postcss-values-parser').Punctuation} CSSPunctuation */\n","export default {\n\t/** Whether to preserve the original functional color declaration. */\n\tpreserve: false\n}\n","import { parse } from 'postcss-values-parser'\nimport onCSSFunction from './onCSSFunction'\nimport options from './options'\n\n/** @type {(decl: CSSDeclaration) => void} Transform lab() and lch() functions in CSS Declarations. */\nconst onCSSDeclaration = decl => {\n\tconst { value: originalValue } = decl\n\n\tif (hasAnyColorFunction(originalValue)) {\n\t\tconst valueAST = parse(originalValue)\n\n\t\tvalueAST.walkType('func', onCSSFunction)\n\n\t\tconst modifiedValue = String(valueAST)\n\n\t\tif (modifiedValue !== originalValue) {\n\t\t\tif (options.preserve) decl.cloneBefore({ value: modifiedValue })\n\t\t\telse decl.value = modifiedValue\n\t\t}\n\t}\n}\n\nexport default onCSSDeclaration\n\n/** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */\nconst createRegExpTest = Function.bind.bind(RegExp.prototype.test)\n\n/** Return whether the value has a lab() or lch() function. */\nconst hasAnyColorFunction = createRegExpTest(/(^|[^\\w-])(lab|lch)\\(/i)\n\n/** @typedef {import('postcss').Declaration} CSSDeclaration */\n","import onCSSDeclaration from './onCSSDeclaration'\nimport options from './options'\n\n/** Transform lab() and lch() functions in CSS. */\nconst postcssPlugin = /** @type {PostCSSPluginInitializer} */ opts => {\n\toptions.preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false\n\n\treturn {\n\t\tpostcssPlugin: 'postcss-lab-function',\n\t\tDeclaration: onCSSDeclaration,\n\t}\n}\n\npostcssPlugin.postcss = true\n\nexport default postcssPlugin\n\n/** @typedef {import('postcss').Plugin} PostCSSPlugin */\n/** @typedef {(opts: options) => PostCSSPlugin} PostCSSPluginInitializer */\n"],"names":["onCSSFunction","node","name","nodes","isColorFunctionName","isLabFunctionContents","isLchFunctionContents","Object","assign","slashNode","alphaNode","isPercentage","isCalc","value","String","unit","remove","isSlash","replaceWith","commaNode","clone","channelNode1","channelNode2","channelNode3","toRGB","isLabColorFunctionName","lab2rgb","lch2rgb","rgbValues","map","channelNumber","parseFloat","channelValue","Math","max","min","parseInt","parse","first","createRegExpTest","Function","bind","RegExp","prototype","test","isCalcFunctionName","isAlphaLikeUnit","isHueLikeUnit","isAlphaValue","type","isHue","isNumber","every","index","labFunctionContents","lchFunctionContents","preserve","onCSSDeclaration","decl","originalValue","hasAnyColorFunction","valueAST","walkType","modifiedValue","options","cloneBefore","postcssPlugin","opts","Boolean","Declaration","postcss"],"mappings":";;;;;AAGA;;AACA,MAAMA,aAAa,GAAGC,IAAI,IAAI;AAC7B;AACA,QAAM;AAAEC,IAAAA,IAAF;AAAQC,IAAAA;AAAR,MAAkBF,IAAxB;;AAEA,MAAIG,mBAAm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C,IAApC,CAAzB;AAEA;;AACA,MAAMgB,mBAAmB,GAAGrB,gBAAgB,CAAC,wBAAD,CAA5C;AAEA;;AC3BA;;MACM2B,aAAa;AAAG;AAAwCC,IAAI,IAAI;AACrEH,EAAAA,OAAO,CAACR,QAAR,GAAmB,cAAcjD,MAAM,CAAC4D,IAAD,CAApB,GAA6BC,OAAO,CAACD,IAAI,CAACX,QAAN,CAApC,GAAsD,KAAzE;AAEA,SAAO;AACNU,IAAAA,aAAa,EAAE,sBADT;AAENG,IAAAA,WAAW,EAAEZ;AAFP,GAAP;AAIA;;AAEDS,aAAa,CAACI,OAAd,GAAwB,IAAxB;AAIA;;AACA;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/onCSSFunction.js","../src/options.js","../src/onCSSDeclaration.js","../src/index.js"],"sourcesContent":["import { lab2rgb, lch2rgb } from '@csstools/convert-colors'\nimport { parse } from 'postcss-values-parser'\n\n/** @type {(decl: CSSFunction) => void} Transform lab() and lch() functions. */\nconst onCSSFunction = node => {\n\t/** @type {{ name: string, nodes: CSSNode[] }} */\n\tconst { name, nodes } = node\n\n\tif (isColorFunctionName(name)) {\n\t\tif (isLabFunctionContents(nodes) || isLchFunctionContents(nodes)) {\n\t\t\t// rename the Color function to `rgb`\n\t\t\tObject.assign(node, { name: 'rgb' })\n\n\t\t\t/** @type {CSSPunctuation} Slash punctuation before the Alpha channel. */\n\t\t\tconst slashNode = nodes[3]\n\n\t\t\t/** @type {CSSNumber} Alpha channel. */\n\t\t\tconst alphaNode = nodes[4]\n\n\t\t\tif (alphaNode) {\n\t\t\t\tif (isPercentage(alphaNode) && !isCalc(alphaNode)) {\n\t\t\t\t\t// transform the Alpha channel from a Percentage to (0-1) Number\n\t\t\t\t\tObject.assign(alphaNode, { value: String(alphaNode.value / 100), unit: '' })\n\t\t\t\t}\n\n\t\t\t\t// if the color is fully opaque (i.e. the Alpha channel is `1`)\n\t\t\t\tif (alphaNode.value === '1') {\n\t\t\t\t\t// remove the Slash and Alpha channel\n\t\t\t\t\tslashNode.remove()\n\t\t\t\t\talphaNode.remove()\n\t\t\t\t} else {\n\t\t\t\t\t// otherwise, rename the Color function to `rgba`\n\t\t\t\t\tObject.assign(node, { name: 'rgba' })\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// replace a remaining Slash with a Comma\n\t\t\tif (slashNode && isSlash(slashNode)) {\n\t\t\t\tslashNode.replaceWith(commaNode.clone())\n\t\t\t}\n\n\t\t\t/** Extracted Color channels. */\n\t\t\tconst [channelNode1, channelNode2, channelNode3] = nodes\n\n\t\t\t/** Corresponding Color transformer. */\n\t\t\tconst toRGB = isLabColorFunctionName(name) ? lab2rgb : lch2rgb\n\n\t\t\t/** RGB channels from the source color. */\n\t\t\tconst rgbValues = toRGB(\n\t\t\t\t...[\n\t\t\t\t\tchannelNode1.value,\n\t\t\t\t\tchannelNode2.value,\n\t\t\t\t\tchannelNode3.value\n\t\t\t\t].map(\n\t\t\t\t\tchannelNumber => parseFloat(channelNumber)\n\t\t\t\t)\n\t\t\t).map(\n\t\t\t\tchannelValue => Math.max(Math.min(parseInt(channelValue * 2.55), 255), 0)\n\t\t\t)\n\n\t\t\tchannelNode3.replaceWith(\n\t\t\t\tchannelNode3.clone({ value: String(rgbValues[2]) })\n\t\t\t)\n\n\t\t\tchannelNode2.replaceWith(\n\t\t\t\tchannelNode2.clone({ value: String(rgbValues[1]) }),\n\t\t\t\tcommaNode.clone()\n\t\t\t)\n\n\t\t\tchannelNode1.replaceWith(\n\t\t\t\tchannelNode1.clone({ value: String(rgbValues[0]), unit: '' }),\n\t\t\t\tcommaNode.clone()\n\t\t\t)\n\t\t}\n\t}\n}\n\nexport default onCSSFunction\n\nconst commaNode = parse(',').first\n\n/** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */\nconst createRegExpTest = Function.bind.bind(RegExp.prototype.test)\n\n/** Return whether the function name is `lab` or `lch`. */\nconst isColorFunctionName = createRegExpTest(/^(lab|lch)$/i)\n\n/** Return whether the function name is `calc`. */\nconst isCalcFunctionName = createRegExpTest(/^calc$/i)\n\n/** Return whether the function name is `lab`. */\nconst isLabColorFunctionName = createRegExpTest(/^lab$/i)\n\n/** Return whether the unit is alpha-like. */\nconst isAlphaLikeUnit = createRegExpTest(/^%?$/i)\n\n/** Return whether the unit is hue-like. */\nconst isHueLikeUnit = createRegExpTest(/^(deg|grad|rad|turn)?$/i)\n\n/** @type {(node: CSSFunction) => boolean} Return whether the node is an Alpha-like unit. */\nconst isAlphaValue = node => isCalc(node) || node.type === 'numeric' && isAlphaLikeUnit(node.unit)\n\n/** @type {(node: CSSFunction) => boolean} Return whether the node is a calc() function. */\nconst isCalc = node => node.type === 'func' && isCalcFunctionName(node.name)\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Hue-like unit. */\nconst isHue = node => isCalc(node) || node.type === 'numeric' && isHueLikeUnit(node.unit)\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Number unit. */\nconst isNumber = node => isCalc(node) || node.type === 'numeric' && node.unit === ''\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Percentage unit. */\nconst isPercentage = node => isCalc(node) || node.type === 'numeric' && node.unit === '%'\n\n/** @type {(node: CSSOperator) => boolean} Return whether the node is a Slash delimiter. */\nconst isSlash = node => node.type === 'operator' && node.value === '/'\n\n/** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lab() function. */\nconst isLabFunctionContents = nodes => nodes.every(\n\t(node, index) => typeof labFunctionContents[index] === 'function' && labFunctionContents[index](node)\n)\n\n/** Set of nodes in a lab() function. */\nconst labFunctionContents = [isPercentage, isNumber, isNumber, isSlash, isAlphaValue]\n\n/** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lch() function. */\nconst isLchFunctionContents = nodes => nodes.every(\n\t(node, index) => typeof lchFunctionContents[index] === 'function' && lchFunctionContents[index](node)\n)\n\n/** Set of nodes in a lch() function. */\nconst lchFunctionContents = [isPercentage, isNumber, isHue, isSlash, isAlphaValue]\n\n/** @typedef {import('postcss-values-parser').Func} CSSFunction */\n/** @typedef {import('postcss-values-parser').Node} CSSNode */\n/** @typedef {import('postcss-values-parser').Numeric} CSSNumber */\n/** @typedef {import('postcss-values-parser').Operator} CSSOperator */\n/** @typedef {import('postcss-values-parser').Punctuation} CSSPunctuation */\n","export default {\n\t/** Whether to preserve the original functional color declaration. */\n\tpreserve: false\n}\n","import { parse } from 'postcss-values-parser'\nimport onCSSFunction from './onCSSFunction'\nimport options from './options'\n\n/** @type {(decl: CSSDeclaration) => void} Transform lab() and lch() functions in CSS Declarations. */\nconst onCSSDeclaration = (decl, { result }) => {\n\tconst { value: originalValue } = decl\n\n\tif (hasAnyColorFunction(originalValue)) {\n\t\tlet valueAST\n\n\t\ttry {\n\t\t\tvalueAST = parse(originalValue, { ignoreUnknownWords: true })\n\t\t} catch (error) {\n\t\t\tdecl.warn(\n\t\t\t\tresult,\n\t\t\t\t`Failed to parse value '${originalValue}' as a lab or hcl function. Leaving the original value intact.`\n\t\t\t)\n\t\t}\n\n\t\tif (typeof valueAST === 'undefined') {\n\t\t\treturn\n\t\t}\n\n\t\tvalueAST.walkType('func', onCSSFunction)\n\n\t\tconst modifiedValue = String(valueAST)\n\n\t\tif (modifiedValue !== originalValue) {\n\t\t\tif (options.preserve) decl.cloneBefore({ value: modifiedValue })\n\t\t\telse decl.value = modifiedValue\n\t\t}\n\t}\n}\n\nexport default onCSSDeclaration\n\n/** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */\nconst createRegExpTest = Function.bind.bind(RegExp.prototype.test)\n\n/** Return whether the value has a lab() or lch() function. */\nconst hasAnyColorFunction = createRegExpTest(/(^|[^\\w-])(lab|lch)\\(/i)\n\n/** @typedef {import('postcss').Declaration} CSSDeclaration */\n","import onCSSDeclaration from './onCSSDeclaration'\nimport options from './options'\n\n/** Transform lab() and lch() functions in CSS. */\nconst postcssPlugin = /** @type {PostCSSPluginInitializer} */ opts => {\n\toptions.preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false\n\n\treturn {\n\t\tpostcssPlugin: 'postcss-lab-function',\n\t\tDeclaration: onCSSDeclaration,\n\t}\n}\n\npostcssPlugin.postcss = true\n\nexport default postcssPlugin\n\n/** @typedef {import('postcss').Plugin} PostCSSPlugin */\n/** @typedef {(opts: options) => PostCSSPlugin} PostCSSPluginInitializer */\n"],"names":["onCSSFunction","node","name","nodes","isColorFunctionName","isLabFunctionContents","isLchFunctionContents","Object","assign","slashNode","alphaNode","isPercentage","isCalc","value","String","unit","remove","isSlash","replaceWith","commaNode","clone","channelNode1","channelNode2","channelNode3","toRGB","isLabColorFunctionName","lab2rgb","lch2rgb","rgbValues","map","channelNumber","parseFloat","channelValue","Math","max","min","parseInt","parse","first","createRegExpTest","Function","bind","RegExp","prototype","test","isCalcFunctionName","isAlphaLikeUnit","isHueLikeUnit","isAlphaValue","type","isHue","isNumber","every","index","labFunctionContents","lchFunctionContents","preserve","onCSSDeclaration","decl","result","originalValue","hasAnyColorFunction","valueAST","ignoreUnknownWords","error","warn","walkType","modifiedValue","options","cloneBefore","postcssPlugin","opts","Boolean","Declaration","postcss"],"mappings":";;;;;AAGA;;AACA,MAAMA,aAAa,GAAGC,IAAI,IAAI;AAC7B;AACA,QAAM;AAAEC,IAAAA,IAAF;AAAQC,IAAAA;AAAR,MAAkBF,IAAxB;;AAEA,MAAIG,mBAAmB,CAACF,IAAD,CAAvB,EAA+B;AAC9B,QAAIG,qBAAqB,CAACF,KAAD,CAArB,IAAgCG,qBAAqB,CAACH,KAAD,CAAzD,EAAkE;AACjE;AACAI,MAAAA,MAAM,CAACC,MAAP,CAAcP,IAAd,EAAoB;AAAEC,QAAAA,IAAI,EAAE;AAAR,OAApB;AAEA;;AACA,YAAMO,SAAS,GAAGN,KAAK,CAAC,CAAD,CAAvB;AAEA;;AACA,YAAMO,SAAS,GAAGP,KAAK,CAAC,CAAD,CAAvB;;AAEA,UAAIO,SAAJ,EAAe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gBAAgB,CAAC,wBAAD,CAA5C;AAEA;;ACxCA;;MACM+B,aAAa;AAAG;AAAwCC,IAAI,IAAI;AACrEH,EAAAA,OAAO,CAACZ,QAAR,GAAmB,cAAcjD,MAAM,CAACgE,IAAD,CAApB,GAA6BC,OAAO,CAACD,IAAI,CAACf,QAAN,CAApC,GAAsD,KAAzE;AAEA,SAAO;AACNc,IAAAA,aAAa,EAAE,sBADT;AAENG,IAAAA,WAAW,EAAEhB;AAFP,GAAP;AAIA;;AAEDa,aAAa,CAACI,OAAd,GAAwB,IAAxB;AAIA;;AACA;;;;"}
|
package/dist/index.esm.js
CHANGED
|
@@ -146,13 +146,28 @@ var options = {
|
|
|
146
146
|
|
|
147
147
|
/** @type {(decl: CSSDeclaration) => void} Transform lab() and lch() functions in CSS Declarations. */
|
|
148
148
|
|
|
149
|
-
const onCSSDeclaration = decl
|
|
149
|
+
const onCSSDeclaration = (decl, {
|
|
150
|
+
result
|
|
151
|
+
}) => {
|
|
150
152
|
const {
|
|
151
153
|
value: originalValue
|
|
152
154
|
} = decl;
|
|
153
155
|
|
|
154
156
|
if (hasAnyColorFunction(originalValue)) {
|
|
155
|
-
|
|
157
|
+
let valueAST;
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
valueAST = parse(originalValue, {
|
|
161
|
+
ignoreUnknownWords: true
|
|
162
|
+
});
|
|
163
|
+
} catch (error) {
|
|
164
|
+
decl.warn(result, `Failed to parse value '${originalValue}' as a lab or hcl function. Leaving the original value intact.`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (typeof valueAST === 'undefined') {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
156
171
|
valueAST.walkType('func', onCSSFunction);
|
|
157
172
|
const modifiedValue = String(valueAST);
|
|
158
173
|
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../src/onCSSFunction.js","../src/options.js","../src/onCSSDeclaration.js","../src/index.js"],"sourcesContent":["import { lab2rgb, lch2rgb } from '@csstools/convert-colors'\nimport { parse } from 'postcss-values-parser'\n\n/** @type {(decl: CSSFunction) => void} Transform lab() and lch() functions. */\nconst onCSSFunction = node => {\n\t/** @type {{ name: string, nodes: CSSNode[] }} */\n\tconst { name, nodes } = node\n\n\tif (isColorFunctionName(name)) {\n\t\tif (isLabFunctionContents(nodes) || isLchFunctionContents(nodes)) {\n\t\t\t// rename the Color function to `rgb`\n\t\t\tObject.assign(node, { name: 'rgb' })\n\n\t\t\t/** @type {CSSPunctuation} Slash punctuation before the Alpha channel. */\n\t\t\tconst slashNode = nodes[3]\n\n\t\t\t/** @type {CSSNumber} Alpha channel. */\n\t\t\tconst alphaNode = nodes[4]\n\n\t\t\tif (alphaNode) {\n\t\t\t\tif (isPercentage(alphaNode) && !isCalc(alphaNode)) {\n\t\t\t\t\t// transform the Alpha channel from a Percentage to (0-1) Number\n\t\t\t\t\tObject.assign(alphaNode, { value: String(alphaNode.value / 100), unit: '' })\n\t\t\t\t}\n\n\t\t\t\t// if the color is fully opaque (i.e. the Alpha channel is `1`)\n\t\t\t\tif (alphaNode.value === '1') {\n\t\t\t\t\t// remove the Slash and Alpha channel\n\t\t\t\t\tslashNode.remove()\n\t\t\t\t\talphaNode.remove()\n\t\t\t\t} else {\n\t\t\t\t\t// otherwise, rename the Color function to `rgba`\n\t\t\t\t\tObject.assign(node, { name: 'rgba' })\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// replace a remaining Slash with a Comma\n\t\t\tif (slashNode && isSlash(slashNode)) {\n\t\t\t\tslashNode.replaceWith(commaNode.clone())\n\t\t\t}\n\n\t\t\t/** Extracted Color channels. */\n\t\t\tconst [channelNode1, channelNode2, channelNode3] = nodes\n\n\t\t\t/** Corresponding Color transformer. */\n\t\t\tconst toRGB = isLabColorFunctionName(name) ? lab2rgb : lch2rgb\n\n\t\t\t/** RGB channels from the source color. */\n\t\t\tconst rgbValues = toRGB(\n\t\t\t\t...[\n\t\t\t\t\tchannelNode1.value,\n\t\t\t\t\tchannelNode2.value,\n\t\t\t\t\tchannelNode3.value\n\t\t\t\t].map(\n\t\t\t\t\tchannelNumber => parseFloat(channelNumber)\n\t\t\t\t)\n\t\t\t).map(\n\t\t\t\tchannelValue => Math.max(Math.min(parseInt(channelValue * 2.55), 255), 0)\n\t\t\t)\n\n\t\t\tchannelNode3.replaceWith(\n\t\t\t\tchannelNode3.clone({ value: String(rgbValues[2]) })\n\t\t\t)\n\n\t\t\tchannelNode2.replaceWith(\n\t\t\t\tchannelNode2.clone({ value: String(rgbValues[1]) }),\n\t\t\t\tcommaNode.clone()\n\t\t\t)\n\n\t\t\tchannelNode1.replaceWith(\n\t\t\t\tchannelNode1.clone({ value: String(rgbValues[0]), unit: '' }),\n\t\t\t\tcommaNode.clone()\n\t\t\t)\n\t\t}\n\t}\n}\n\nexport default onCSSFunction\n\nconst commaNode = parse(',').first\n\n/** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */\nconst createRegExpTest = Function.bind.bind(RegExp.prototype.test)\n\n/** Return whether the function name is `lab` or `lch`. */\nconst isColorFunctionName = createRegExpTest(/^(lab|lch)$/i)\n\n/** Return whether the function name is `calc`. */\nconst isCalcFunctionName = createRegExpTest(/^calc$/i)\n\n/** Return whether the function name is `lab`. */\nconst isLabColorFunctionName = createRegExpTest(/^lab$/i)\n\n/** Return whether the unit is alpha-like. */\nconst isAlphaLikeUnit = createRegExpTest(/^%?$/i)\n\n/** Return whether the unit is hue-like. */\nconst isHueLikeUnit = createRegExpTest(/^(deg|grad|rad|turn)?$/i)\n\n/** @type {(node: CSSFunction) => boolean} Return whether the node is an Alpha-like unit. */\nconst isAlphaValue = node => isCalc(node) || node.type === 'numeric' && isAlphaLikeUnit(node.unit)\n\n/** @type {(node: CSSFunction) => boolean} Return whether the node is a calc() function. */\nconst isCalc = node => node.type === 'func' && isCalcFunctionName(node.name)\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Hue-like unit. */\nconst isHue = node => isCalc(node) || node.type === 'numeric' && isHueLikeUnit(node.unit)\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Number unit. */\nconst isNumber = node => isCalc(node) || node.type === 'numeric' && node.unit === ''\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Percentage unit. */\nconst isPercentage = node => isCalc(node) || node.type === 'numeric' && node.unit === '%'\n\n/** @type {(node: CSSOperator) => boolean} Return whether the node is a Slash delimiter. */\nconst isSlash = node => node.type === 'operator' && node.value === '/'\n\n/** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lab() function. */\nconst isLabFunctionContents = nodes => nodes.every(\n\t(node, index) => typeof labFunctionContents[index] === 'function' && labFunctionContents[index](node)\n)\n\n/** Set of nodes in a lab() function. */\nconst labFunctionContents = [isPercentage, isNumber, isNumber, isSlash, isAlphaValue]\n\n/** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lch() function. */\nconst isLchFunctionContents = nodes => nodes.every(\n\t(node, index) => typeof lchFunctionContents[index] === 'function' && lchFunctionContents[index](node)\n)\n\n/** Set of nodes in a lch() function. */\nconst lchFunctionContents = [isPercentage, isNumber, isHue, isSlash, isAlphaValue]\n\n/** @typedef {import('postcss-values-parser').Func} CSSFunction */\n/** @typedef {import('postcss-values-parser').Node} CSSNode */\n/** @typedef {import('postcss-values-parser').Numeric} CSSNumber */\n/** @typedef {import('postcss-values-parser').Operator} CSSOperator */\n/** @typedef {import('postcss-values-parser').Punctuation} CSSPunctuation */\n","export default {\n\t/** Whether to preserve the original functional color declaration. */\n\tpreserve: false\n}\n","import { parse } from 'postcss-values-parser'\nimport onCSSFunction from './onCSSFunction'\nimport options from './options'\n\n/** @type {(decl: CSSDeclaration) => void} Transform lab() and lch() functions in CSS Declarations. */\nconst onCSSDeclaration = decl => {\n\tconst { value: originalValue } = decl\n\n\tif (hasAnyColorFunction(originalValue)) {\n\t\tconst valueAST = parse(originalValue)\n\n\t\tvalueAST.walkType('func', onCSSFunction)\n\n\t\tconst modifiedValue = String(valueAST)\n\n\t\tif (modifiedValue !== originalValue) {\n\t\t\tif (options.preserve) decl.cloneBefore({ value: modifiedValue })\n\t\t\telse decl.value = modifiedValue\n\t\t}\n\t}\n}\n\nexport default onCSSDeclaration\n\n/** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */\nconst createRegExpTest = Function.bind.bind(RegExp.prototype.test)\n\n/** Return whether the value has a lab() or lch() function. */\nconst hasAnyColorFunction = createRegExpTest(/(^|[^\\w-])(lab|lch)\\(/i)\n\n/** @typedef {import('postcss').Declaration} CSSDeclaration */\n","import onCSSDeclaration from './onCSSDeclaration'\nimport options from './options'\n\n/** Transform lab() and lch() functions in CSS. */\nconst postcssPlugin = /** @type {PostCSSPluginInitializer} */ opts => {\n\toptions.preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false\n\n\treturn {\n\t\tpostcssPlugin: 'postcss-lab-function',\n\t\tDeclaration: onCSSDeclaration,\n\t}\n}\n\npostcssPlugin.postcss = true\n\nexport default postcssPlugin\n\n/** @typedef {import('postcss').Plugin} PostCSSPlugin */\n/** @typedef {(opts: options) => PostCSSPlugin} PostCSSPluginInitializer */\n"],"names":["onCSSFunction","node","name","nodes","isColorFunctionName","isLabFunctionContents","isLchFunctionContents","Object","assign","slashNode","alphaNode","isPercentage","isCalc","value","String","unit","remove","isSlash","replaceWith","commaNode","clone","channelNode1","channelNode2","channelNode3","toRGB","isLabColorFunctionName","lab2rgb","lch2rgb","rgbValues","map","channelNumber","parseFloat","channelValue","Math","max","min","parseInt","parse","first","createRegExpTest","Function","bind","RegExp","prototype","test","isCalcFunctionName","isAlphaLikeUnit","isHueLikeUnit","isAlphaValue","type","isHue","isNumber","every","index","labFunctionContents","lchFunctionContents","preserve","onCSSDeclaration","decl","originalValue","hasAnyColorFunction","valueAST","walkType","modifiedValue","options","cloneBefore","postcssPlugin","opts","Boolean","Declaration","postcss"],"mappings":";;;AAGA;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aAAb;AACL;AACD;AACD,CAfD;AAmBA;;AACA,MAAMxB,gBAAgB,GAAGC,QAAQ,CAACC,IAAT,CAAcA,IAAd,CAAmBC,MAAM,CAACC,SAAP,CAAiBC,IAApC,CAAzB;AAEA;;AACA,MAAMgB,mBAAmB,GAAGrB,gBAAgB,CAAC,wBAAD,CAA5C;AAEA;;AC3BA;;MACM2B,aAAa;AAAG;AAAwCC,IAAI,IAAI;AACrEH,EAAAA,OAAO,CAACR,QAAR,GAAmB,cAAcjD,MAAM,CAAC4D,IAAD,CAApB,GAA6BC,OAAO,CAACD,IAAI,CAACX,QAAN,CAApC,GAAsD,KAAzE;AAEA,SAAO;AACNU,IAAAA,aAAa,EAAE,sBADT;AAENG,IAAAA,WAAW,EAAEZ;AAFP,GAAP;AAIA;;AAEDS,aAAa,CAACI,OAAd,GAAwB,IAAxB;AAIA;;AACA;;;;"}
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/onCSSFunction.js","../src/options.js","../src/onCSSDeclaration.js","../src/index.js"],"sourcesContent":["import { lab2rgb, lch2rgb } from '@csstools/convert-colors'\nimport { parse } from 'postcss-values-parser'\n\n/** @type {(decl: CSSFunction) => void} Transform lab() and lch() functions. */\nconst onCSSFunction = node => {\n\t/** @type {{ name: string, nodes: CSSNode[] }} */\n\tconst { name, nodes } = node\n\n\tif (isColorFunctionName(name)) {\n\t\tif (isLabFunctionContents(nodes) || isLchFunctionContents(nodes)) {\n\t\t\t// rename the Color function to `rgb`\n\t\t\tObject.assign(node, { name: 'rgb' })\n\n\t\t\t/** @type {CSSPunctuation} Slash punctuation before the Alpha channel. */\n\t\t\tconst slashNode = nodes[3]\n\n\t\t\t/** @type {CSSNumber} Alpha channel. */\n\t\t\tconst alphaNode = nodes[4]\n\n\t\t\tif (alphaNode) {\n\t\t\t\tif (isPercentage(alphaNode) && !isCalc(alphaNode)) {\n\t\t\t\t\t// transform the Alpha channel from a Percentage to (0-1) Number\n\t\t\t\t\tObject.assign(alphaNode, { value: String(alphaNode.value / 100), unit: '' })\n\t\t\t\t}\n\n\t\t\t\t// if the color is fully opaque (i.e. the Alpha channel is `1`)\n\t\t\t\tif (alphaNode.value === '1') {\n\t\t\t\t\t// remove the Slash and Alpha channel\n\t\t\t\t\tslashNode.remove()\n\t\t\t\t\talphaNode.remove()\n\t\t\t\t} else {\n\t\t\t\t\t// otherwise, rename the Color function to `rgba`\n\t\t\t\t\tObject.assign(node, { name: 'rgba' })\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// replace a remaining Slash with a Comma\n\t\t\tif (slashNode && isSlash(slashNode)) {\n\t\t\t\tslashNode.replaceWith(commaNode.clone())\n\t\t\t}\n\n\t\t\t/** Extracted Color channels. */\n\t\t\tconst [channelNode1, channelNode2, channelNode3] = nodes\n\n\t\t\t/** Corresponding Color transformer. */\n\t\t\tconst toRGB = isLabColorFunctionName(name) ? lab2rgb : lch2rgb\n\n\t\t\t/** RGB channels from the source color. */\n\t\t\tconst rgbValues = toRGB(\n\t\t\t\t...[\n\t\t\t\t\tchannelNode1.value,\n\t\t\t\t\tchannelNode2.value,\n\t\t\t\t\tchannelNode3.value\n\t\t\t\t].map(\n\t\t\t\t\tchannelNumber => parseFloat(channelNumber)\n\t\t\t\t)\n\t\t\t).map(\n\t\t\t\tchannelValue => Math.max(Math.min(parseInt(channelValue * 2.55), 255), 0)\n\t\t\t)\n\n\t\t\tchannelNode3.replaceWith(\n\t\t\t\tchannelNode3.clone({ value: String(rgbValues[2]) })\n\t\t\t)\n\n\t\t\tchannelNode2.replaceWith(\n\t\t\t\tchannelNode2.clone({ value: String(rgbValues[1]) }),\n\t\t\t\tcommaNode.clone()\n\t\t\t)\n\n\t\t\tchannelNode1.replaceWith(\n\t\t\t\tchannelNode1.clone({ value: String(rgbValues[0]), unit: '' }),\n\t\t\t\tcommaNode.clone()\n\t\t\t)\n\t\t}\n\t}\n}\n\nexport default onCSSFunction\n\nconst commaNode = parse(',').first\n\n/** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */\nconst createRegExpTest = Function.bind.bind(RegExp.prototype.test)\n\n/** Return whether the function name is `lab` or `lch`. */\nconst isColorFunctionName = createRegExpTest(/^(lab|lch)$/i)\n\n/** Return whether the function name is `calc`. */\nconst isCalcFunctionName = createRegExpTest(/^calc$/i)\n\n/** Return whether the function name is `lab`. */\nconst isLabColorFunctionName = createRegExpTest(/^lab$/i)\n\n/** Return whether the unit is alpha-like. */\nconst isAlphaLikeUnit = createRegExpTest(/^%?$/i)\n\n/** Return whether the unit is hue-like. */\nconst isHueLikeUnit = createRegExpTest(/^(deg|grad|rad|turn)?$/i)\n\n/** @type {(node: CSSFunction) => boolean} Return whether the node is an Alpha-like unit. */\nconst isAlphaValue = node => isCalc(node) || node.type === 'numeric' && isAlphaLikeUnit(node.unit)\n\n/** @type {(node: CSSFunction) => boolean} Return whether the node is a calc() function. */\nconst isCalc = node => node.type === 'func' && isCalcFunctionName(node.name)\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Hue-like unit. */\nconst isHue = node => isCalc(node) || node.type === 'numeric' && isHueLikeUnit(node.unit)\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Number unit. */\nconst isNumber = node => isCalc(node) || node.type === 'numeric' && node.unit === ''\n\n/** @type {(node: CSSNumber) => boolean} Return whether the node is a Percentage unit. */\nconst isPercentage = node => isCalc(node) || node.type === 'numeric' && node.unit === '%'\n\n/** @type {(node: CSSOperator) => boolean} Return whether the node is a Slash delimiter. */\nconst isSlash = node => node.type === 'operator' && node.value === '/'\n\n/** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lab() function. */\nconst isLabFunctionContents = nodes => nodes.every(\n\t(node, index) => typeof labFunctionContents[index] === 'function' && labFunctionContents[index](node)\n)\n\n/** Set of nodes in a lab() function. */\nconst labFunctionContents = [isPercentage, isNumber, isNumber, isSlash, isAlphaValue]\n\n/** @type {(nodes: Node[]) => boolean} Return whether a set of nodes is a lch() function. */\nconst isLchFunctionContents = nodes => nodes.every(\n\t(node, index) => typeof lchFunctionContents[index] === 'function' && lchFunctionContents[index](node)\n)\n\n/** Set of nodes in a lch() function. */\nconst lchFunctionContents = [isPercentage, isNumber, isHue, isSlash, isAlphaValue]\n\n/** @typedef {import('postcss-values-parser').Func} CSSFunction */\n/** @typedef {import('postcss-values-parser').Node} CSSNode */\n/** @typedef {import('postcss-values-parser').Numeric} CSSNumber */\n/** @typedef {import('postcss-values-parser').Operator} CSSOperator */\n/** @typedef {import('postcss-values-parser').Punctuation} CSSPunctuation */\n","export default {\n\t/** Whether to preserve the original functional color declaration. */\n\tpreserve: false\n}\n","import { parse } from 'postcss-values-parser'\nimport onCSSFunction from './onCSSFunction'\nimport options from './options'\n\n/** @type {(decl: CSSDeclaration) => void} Transform lab() and lch() functions in CSS Declarations. */\nconst onCSSDeclaration = (decl, { result }) => {\n\tconst { value: originalValue } = decl\n\n\tif (hasAnyColorFunction(originalValue)) {\n\t\tlet valueAST\n\n\t\ttry {\n\t\t\tvalueAST = parse(originalValue, { ignoreUnknownWords: true })\n\t\t} catch (error) {\n\t\t\tdecl.warn(\n\t\t\t\tresult,\n\t\t\t\t`Failed to parse value '${originalValue}' as a lab or hcl function. Leaving the original value intact.`\n\t\t\t)\n\t\t}\n\n\t\tif (typeof valueAST === 'undefined') {\n\t\t\treturn\n\t\t}\n\n\t\tvalueAST.walkType('func', onCSSFunction)\n\n\t\tconst modifiedValue = String(valueAST)\n\n\t\tif (modifiedValue !== originalValue) {\n\t\t\tif (options.preserve) decl.cloneBefore({ value: modifiedValue })\n\t\t\telse decl.value = modifiedValue\n\t\t}\n\t}\n}\n\nexport default onCSSDeclaration\n\n/** @type {(value: RegExp) => (value: string) => boolean} Return a function that checks whether the expression exists in a value. */\nconst createRegExpTest = Function.bind.bind(RegExp.prototype.test)\n\n/** Return whether the value has a lab() or lch() function. */\nconst hasAnyColorFunction = createRegExpTest(/(^|[^\\w-])(lab|lch)\\(/i)\n\n/** @typedef {import('postcss').Declaration} CSSDeclaration */\n","import onCSSDeclaration from './onCSSDeclaration'\nimport options from './options'\n\n/** Transform lab() and lch() functions in CSS. */\nconst postcssPlugin = /** @type {PostCSSPluginInitializer} */ opts => {\n\toptions.preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false\n\n\treturn {\n\t\tpostcssPlugin: 'postcss-lab-function',\n\t\tDeclaration: onCSSDeclaration,\n\t}\n}\n\npostcssPlugin.postcss = true\n\nexport default postcssPlugin\n\n/** @typedef {import('postcss').Plugin} PostCSSPlugin */\n/** @typedef {(opts: options) => PostCSSPlugin} PostCSSPluginInitializer */\n"],"names":["onCSSFunction","node","name","nodes","isColorFunctionName","isLabFunctionContents","isLchFunctionContents","Object","assign","slashNode","alphaNode","isPercentage","isCalc","value","String","unit","remove","isSlash","replaceWith","commaNode","clone","channelNode1","channelNode2","channelNode3","toRGB","isLabColorFunctionName","lab2rgb","lch2rgb","rgbValues","map","channelNumber","parseFloat","channelValue","Math","max","min","parseInt","parse","first","createRegExpTest","Function","bind","RegExp","prototype","test","isCalcFunctionName","isAlphaLikeUnit","isHueLikeUnit","isAlphaValue","type","isHue","isNumber","every","index","labFunctionContents","lchFunctionContents","preserve","onCSSDeclaration","decl","result","originalValue","hasAnyColorFunction","valueAST","ignoreUnknownWords","error","warn","walkType","modifiedValue","options","cloneBefore","postcssPlugin","opts","Boolean","Declaration","postcss"],"mappings":";;;AAGA;;AACA,MAAMA,aAAa,GAAGC,IAAI,IAAI;AAC7B;AACA,QAAM;AAAEC,IAAAA,IAAF;AAAQC,IAAAA;AAAR,MAAkBF,IAAxB;;AAEA,MAAIG,mBAAmB,CAACF,IAAD,CAAvB,EAA+B;AAC9B,QAAIG,qBAAqB,CAACF,KAAD,CAArB,IAAgCG,qBAAqB,CAACH,KAAD,CAAzD,EAAkE;AACjE;AACAI,MAAAA,MAAM,CAACC,MAAP,CAAcP,IAAd,EAAoB;AAAEC,QAAAA,IAAI,EAAE;AAAR,OAApB;AAEA;;AACA,YAAM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C,QAAQ,CAACC,IAAT,CAAcA,IAAd,CAAmBC,MAAM,CAACC,SAAP,CAAiBC,IAApC,CAAzB;AAEA;;AACA,MAAMiB,mBAAmB,GAAGtB,gBAAgB,CAAC,wBAAD,CAA5C;AAEA;;ACxCA;;MACM+B,aAAa;AAAG;AAAwCC,IAAI,IAAI;AACrEH,EAAAA,OAAO,CAACZ,QAAR,GAAmB,cAAcjD,MAAM,CAACgE,IAAD,CAApB,GAA6BC,OAAO,CAACD,IAAI,CAACf,QAAN,CAApC,GAAsD,KAAzE;AAEA,SAAO;AACNc,IAAAA,aAAa,EAAE,sBADT;AAENG,IAAAA,WAAW,EAAEhB;AAFP,GAAP;AAIA;;AAEDa,aAAa,CAACI,OAAd,GAAwB,IAAxB;AAIA;;AACA;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-lab-function",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "Use lab() and lch() color functions in CSS",
|
|
5
5
|
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
|
|
6
6
|
"license": "CC0-1.0",
|
|
@@ -27,19 +27,19 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@csstools/convert-colors": "2.0.0",
|
|
30
|
-
"postcss-values-parser": "6.0.
|
|
30
|
+
"postcss-values-parser": "6.0.1"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"postcss": "^8.3"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@babel/core": "7.
|
|
37
|
-
"@babel/preset-env": "7.
|
|
38
|
-
"@rollup/plugin-babel": "5.3.0",
|
|
39
|
-
"eslint": "
|
|
40
|
-
"postcss": "8.3.
|
|
41
|
-
"postcss-tape": "6.0.1",
|
|
42
|
-
"rollup": "2.
|
|
36
|
+
"@babel/core": "^7.16.0",
|
|
37
|
+
"@babel/preset-env": "^7.16.4",
|
|
38
|
+
"@rollup/plugin-babel": "^5.3.0",
|
|
39
|
+
"eslint": "^8.2.0",
|
|
40
|
+
"postcss": "^8.3.11",
|
|
41
|
+
"postcss-tape": "^6.0.1",
|
|
42
|
+
"rollup": "^2.60.0"
|
|
43
43
|
},
|
|
44
44
|
"babel": {
|
|
45
45
|
"presets": [
|