djsun-postcss-px2rem-plugin 1.0.3
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/__tests__/index.js +27 -0
- package/index.js +19 -0
- package/package.json +20 -0
- package/readme.md +25 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// test.js
|
|
2
|
+
const postcss = require('postcss')
|
|
3
|
+
const myPlugin = require('./index.js') // 引入自己的插件
|
|
4
|
+
|
|
5
|
+
// 待测试的CSS代码
|
|
6
|
+
const testCss = `
|
|
7
|
+
body {
|
|
8
|
+
color: red;
|
|
9
|
+
font-size: 16px;
|
|
10
|
+
margin: 20px 0;
|
|
11
|
+
}
|
|
12
|
+
.box {
|
|
13
|
+
color: blue;
|
|
14
|
+
width: 320px;
|
|
15
|
+
}
|
|
16
|
+
`
|
|
17
|
+
|
|
18
|
+
// 执行插件处理
|
|
19
|
+
postcss([myPlugin({ rootValue: 16 })]) // 传入插件配置
|
|
20
|
+
.process(testCss, { from: undefined })
|
|
21
|
+
.then(result => {
|
|
22
|
+
console.log('处理后的CSS:\n', result.css)
|
|
23
|
+
// 打印错误信息(如有)
|
|
24
|
+
if (result.warnings().length) {
|
|
25
|
+
result.warnings().forEach(warn => console.warn(warn.toString()))
|
|
26
|
+
}
|
|
27
|
+
})
|
package/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// index.js 完整插件示例:可配置的 px 转 rem 插件
|
|
2
|
+
module.exports = (options = {}) => {
|
|
3
|
+
// 配置项默认值
|
|
4
|
+
const defaultOptions = { rootValue: 16, unitPrecision: 2 }
|
|
5
|
+
const opts = { ...defaultOptions, ...options }
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
postcssPlugin: 'djsun-postcss-px2rem-plugin',
|
|
9
|
+
Declaration(decl) {
|
|
10
|
+
// 业务逻辑:匹配 px 单位,转换为 rem
|
|
11
|
+
if (decl.value.includes('px') && !decl.value.includes('rem')) {
|
|
12
|
+
decl.value = decl.value.replace(/(\d+)px/g, (_, num) => {
|
|
13
|
+
return (num / opts.rootValue).toFixed(opts.unitPrecision) + 'rem'
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
module.exports.postcss = true
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "djsun-postcss-px2rem-plugin",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "这是我的第一个PostCSS自定义插件,用于单位转换功能",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"postcss",
|
|
8
|
+
"postcss-plugin",
|
|
9
|
+
"css",
|
|
10
|
+
"px2rem"
|
|
11
|
+
],
|
|
12
|
+
"author": "djsun",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"postcss": "^8.0.0"
|
|
16
|
+
},
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=14.0.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# postcss自定义插件
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
# djsun-postcss-px2rem-plugin
|
|
6
|
+
一个自定义的PostCSS插件
|
|
7
|
+
## 功能
|
|
8
|
+
单位转化功能 px=>rem
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install djsun-postcss-px2rem-plugin --save-dev
|
|
13
|
+
|
|
14
|
+
## 使用
|
|
15
|
+
在 postcss.config.js 中配置:
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
module.exports = {
|
|
19
|
+
plugins: [
|
|
20
|
+
require('djsun-postcss-px2rem-plugin')({
|
|
21
|
+
rootValue: 16, // 配置项1
|
|
22
|
+
unitPrecision: 2 // 配置项2
|
|
23
|
+
})
|
|
24
|
+
]
|
|
25
|
+
}
|