javascript - PostCSS 插件:如何获取 CSS4 变量的值
问题描述
我将svg-transform-loader与postcss-move-props-to-bg-image-query一起使用,以便通过 postCSS 修改 SVG 属性。
我不使用postcss-css-variables
插件,因此我的 CSS 变量不会转换为纯字符串 - 它们保持原样保留在代码中。
它需要是这样的,因为我不想通过整个 CSS 代码postcss-css-variables
,但我想用作一个独立的工具来解析PostCSS 插件回调中的postcss-css-variables
一些特定变量。transform
postcss-move-props-to-bg-image-query
我希望能够做到:
:root {
--my-css-variable: blue;
}
.img {
background-image: url('./img.svg');
-svg-mixer-fill: var(--my-css-variable); // I need `var(--my-css-variable)` to be converted to `blue`
}
当我通过postcss-move-props-to-bg-image-query
插件传递该代码时,var(--my-css-variable)
不会对其进行评估-我想使用postcss-css-variables/lib/resolve-value
模块来获取此变量的值。
我已经postcss-move-props-to-bg-image-query
以一种在transform
回调中可以访问root
PostCSSdeclaration
对象的方式分叉了。
const resolveValue = require('postcss-css-variables/lib/resolve-value');
const moveProps = require('postcss-move-props-to-bg-image-query');
module.exports = {
plugins: [
moveProps({
transform: ({ name, value }, root, declaration) => {
// `name` is '-svg-mixer-fill'
// `value` is `var(--my-css-variable)`. I'd like to convert it to `blue`
const map = {
// I don't know how to use `name` and `value` to create the properly structured object.
};
const evaluatedCssVariable = resolveValue(declaration, map); // I'd like it to return `blue`
return {
name: name.replace(/^-svg-mixer-/, ''),
value: evaluatedCssVariable,
};
},
}),
],
};
现在我被困住了。我不知道如何使用该resolveValue()
模块。我知道它declaration
作为第一个参数和map
对象作为第二个参数,但我不知道如何构建一个合适的map
对象。
我不一定需要使用postcss-css-variables/lib/resolve-value
- 我只是认为我可以使用它来提取 CSS4 变量的值,因为这就是这个插件在内部所做的。我对任何替代方案持开放态度。
解决方案
推荐阅读
- python - 更改图像的原点位于极坐标图上
- python - 当使用 eval() 从 php 脚本调用脚本以及从 bash 脚本调用相同的函数时,python 如何使用路径?
- java - 混淆功能文档和合同?
- python - python pygame中的分数没有变化
- java - 如何使用 Selenium + Java 在 ChromeDriver 中执行代理身份验证?
- spring-boot - 在 Spring application.yaml 中读取 docker env 变量
- javascript - 我可以在 Javascript 中检测到物理键盘的存在吗?
- c++ - 如何从 OpenCV c++ 中的矩阵向量创建 4D 矩阵
- python - 禁用 OpenCV 缩放窗口上的平移运动
- javascript - 未捕获的 SyntaxError:particle.js 的输入意外结束