{"id":1397,"date":"2021-11-02T16:43:26","date_gmt":"2021-11-02T08:43:26","guid":{"rendered":"https:\/\/aif.amtbbs.org\/?p=1397"},"modified":"2021-11-02T16:43:26","modified_gmt":"2021-11-02T08:43:26","slug":"%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99%e5%b1%9e%e4%ba%8e%e8%87%aa%e5%b7%b1%e7%9a%84postcss-8%e6%8f%92%e4%bb%b6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/aif.amtbbs.org\/index.php\/2021\/11\/02\/1397\/","title":{"rendered":"\u5982\u4f55\u7f16\u5199\u5c5e\u4e8e\u81ea\u5df1\u7684PostCSS 8\u63d2\u4ef6\uff1f"},"content":{"rendered":"<section><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1398\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/uploads\/2021\/11\/4ffce04d92a4d6cb21c1494cdfcd6dc1-2.png\" width=\"1080\" height=\"720\" alt=\"\u56fe\u7247\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2021\/11\/4ffce04d92a4d6cb21c1494cdfcd6dc1-2.png 1080w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2021\/11\/4ffce04d92a4d6cb21c1494cdfcd6dc1-2-300x200.png 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2021\/11\/4ffce04d92a4d6cb21c1494cdfcd6dc1-2-1024x683.png 1024w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2021\/11\/4ffce04d92a4d6cb21c1494cdfcd6dc1-2-768x512.png 768w\" sizes=\"auto, (max-width: 1080px) 100vw, 1080px\" \/><\/section>\n<section>\u4f5c\u8005 | \u674e\u4f73\u6d69<\/section>\n<p>\u7b14\u8005\u8fd1\u671f\u5728\u5c06\u524d\u7aef\u67b6\u6784 webpack \u5347\u7ea7\u5230 5 \u65f6\uff0c\u4e00\u4e9b\u914d\u5957\u6a21\u5757\u4e5f\u9700\u8981\u8fdb\u884c\u5347\u7ea7\uff0c\u5176\u4e2d\u5305\u62ec\u4e86 css \u5904\u7406\u6a21\u5757 PostCSS\u3002\u65e7\u7248\u672c\u4f7f\u7528\u7684\u662f PostCSS 7\uff0c\u5728\u5347\u7ea7\u81f3 PostCSS 8 \u7684\u8fc7\u7a0b\u4e2d\uff0c\u7b14\u8005\u53d1\u73b0\u90e8\u5206\u63d2\u4ef6\u524d\u7f6e\u4f9d\u8d56\u8fd8\u662f\u505c\u7559\u5728 7 \u7248\u672c\uff0c\u4e14\u5e74\u4e45\u5931\u4fee\uff0c\u5728 PostCSS 8 \u4e2d\u51fa\u73b0\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u65e0\u5948\u53ea\u80fd\u7814\u7a76\u6e90\u7801\uff0c\u5c06\u76ee\u524d\u90e8\u5206\u65e7\u7248\u672c\u63d2\u4ef6\u5347\u7ea7\u81f3\u65b0\u7248\u672c\u3002\u8fd9\u91cc\uff0c\u7b14\u8005\u5c06\u5347\u7ea7\u63d2\u4ef6\u7684\u8fc7\u7a0b\u8fdb\u884c\u7b80\u5316\u548c\u63d0\u70bc\uff0c\u8ba9\u8bfb\u8005\u81ea\u5df1\u4e5f\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a PostCSS 8 \u63d2\u4ef6\u3002<\/p>\n<section>\u63d2\u4ef6\u5de5\u4f5c\u539f\u7406<\/section>\n<p>PostCSS \u662f\u4e00\u4e2a\u5141\u8bb8\u4f7f\u7528 JS \u63d2\u4ef6\u8f6c\u6362\u6837\u5f0f\u7684\u5de5\u5177\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u5b9e\u9645\u9700\u6c42\uff0c\u5728\u7f16\u8bd1\u8fc7\u7a0b\u5c06\u6307\u5b9a css \u6837\u5f0f\u8fdb\u884c\u8f6c\u6362\u548c\u5904\u7406\u3002\u76ee\u524d PostCSS \u5b98\u65b9\u6536\u5f55\u63d2\u4ef6\u6709 200 \u591a\u6b3e\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684<code>Autoprefixer<\/code>\u81ea\u52a8\u8865\u5168 css \u524d\u7f00\u63d2\u4ef6\u3002<\/p>\n<p>PostCSS \u548c\u63d2\u4ef6\u7684\u5de5\u4f5c\u539f\u7406\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u5148\u5c06 css \u6e90\u7801\u8f6c\u6362\u4e3a AST\uff0c\u63d2\u4ef6\u57fa\u4e8e\u8f6c\u6362\u540e AST \u7684\u4fe1\u606f\u8fdb\u884c\u4e2a\u6027\u5316\u5904\u7406\uff0c\u6700\u540e PostCSS \u518d\u5c06\u5904\u7406\u540e\u7684 AST \u4fe1\u606f\u8f6c\u6362\u4e3a css \u6e90\u7801\uff0c\u5b8c\u6210 css \u6837\u5f0f\u8f6c\u6362\uff0c\u5176\u6d41\u7a0b\u53ef\u4ee5\u5f52\u7ed3\u4e3a\u4e0b\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1399\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/uploads\/2021\/11\/4ffce04d92a4d6cb21c1494cdfcd6dc1-3.png\" width=\"512\" height=\"730\" alt=\"\u56fe\u7247\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2021\/11\/4ffce04d92a4d6cb21c1494cdfcd6dc1-3.png 512w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2021\/11\/4ffce04d92a4d6cb21c1494cdfcd6dc1-3-210x300.png 210w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u5b9e\u9645\u4f8b\u5b50\u770b\u770b PostCSS \u4f1a\u5c06 css \u6e90\u7801\u8f6c\u6362\u6210\u7684 AST \u683c\u5f0f\uff1a<\/p>\n<pre><code>const postcss = require('postcss')\r\npostcss().process(`\r\n.demo {\r\n  font-size: 14px; \/*this is a comment*\/\r\n}\r\n`).then(result =&gt; {\r\n  console.log(result)\r\n})\r\n<\/code><\/pre>\n<p>\u4ee3\u7801\u4e2d\u76f4\u63a5\u5f15\u7528 PostCSS\uff0c\u5728\u4e0d\u7ecf\u8fc7\u4efb\u4f55\u63d2\u4ef6\u7684\u60c5\u51b5\u4e0b\u5c06 css \u6e90\u7801\u8fdb\u884c\u8f6c\u6362\uff0cAST \u8f6c\u6362\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>{\r\n  \"processor\": {\r\n    \"version\": \"8.3.6\",\r\n    \"plugins\": []\r\n  },\r\n  \"messages\": [],\r\n  \"root\": {\r\n    \"raws\": {\r\n      \"semicolon\": false,\r\n      \"after\": \"\\n\"\r\n    },\r\n    \"type\": \"root\",\r\n    \/\/ \u2193 nodes \u5b57\u6bb5\u5185\u5bb9\u91cd\u70b9\u5173\u6ce8\r\n    \"nodes\": [\r\n      {\r\n        \"raws\": {\r\n          \"before\": \"\\n\",\r\n          \"between\": \" \",\r\n          \"semicolon\": true,\r\n          \"after\": \"\\n\"\r\n        },\r\n        \"type\": \"rule\",\r\n        \"nodes\": [\r\n          {\r\n            \"raws\": {\r\n              \"before\": \"\\n  \",\r\n              \"between\": \": \"\r\n            },\r\n            \"type\": \"decl\",\r\n            \"source\": {\r\n              \"inputId\": 0,\r\n              \"start\": {\r\n                \"offset\": 11,\r\n                \"line\": 3,\r\n                \"column\": 3\r\n              },\r\n              \"end\": {\r\n                \"offset\": 26,\r\n                \"line\": 3,\r\n                \"column\": 18\r\n              }\r\n            },\r\n            \"prop\": \"font-size\", \/\/ css \u5c5e\u6027\u548c\u503c\r\n            \"value\": \"14px\"\r\n          },\r\n          {\r\n            \"raws\": {\r\n              \"before\": \" \",\r\n              \"left\": \"\",\r\n              \"right\": \"\"\r\n            },\r\n            \"type\": \"comment\", \/\/ \u6ce8\u91ca\u7c7b\r\n            \"source\": {\r\n              \"inputId\": 0,\r\n              \"start\": {\r\n                \"offset\": 28,\r\n                \"line\": 3,\r\n                \"column\": 20\r\n              },\r\n              \"end\": {\r\n                \"offset\": 48,\r\n                \"line\": 3,\r\n                \"column\": 40\r\n              }\r\n            },\r\n            \"text\": \"this is a comment\"\r\n          }\r\n        ],\r\n        \"source\": {\r\n          \"inputId\": 0,\r\n          \"start\": {\r\n            \"offset\": 1,\r\n            \"line\": 2,\r\n            \"column\": 1\r\n          },\r\n          \"end\": {\r\n            \"offset\": 28,\r\n            \"line\": 4,\r\n            \"column\": 1\r\n          }\r\n        },\r\n        \"selector\": \".demo\", \/\/ \u7c7b\u540d\r\n        \"lastEach\": 1,\r\n        \"indexes\": {}\r\n      }\r\n    ],\r\n    \"source\": {\r\n      \"inputId\": 0,\r\n      \"start\": {\r\n        \"offset\": 0,\r\n        \"line\": 1,\r\n        \"column\": 1\r\n      }\r\n    },\r\n    \"lastEach\": 1,\r\n    \"indexes\": {},\r\n    \"inputs\": [\r\n      {\r\n        \"hasBOM\": false,\r\n        \"css\": \"\\n.demo {\\n  font-size: 14px;\\n}\\n\",\r\n        \"id\": \"&lt;input css vi1Oew&gt;\"\r\n      }\r\n    ]\r\n  },\r\n  \"opts\": {},\r\n  \"css\": \"\\n.demo {\\n  font-size: 14px;\\n}\\n\"\r\n}\r\n<\/code><\/pre>\n<p>AST \u5bf9\u8c61\u4e2d nodes \u5b57\u6bb5\u91cc\u7684\u5185\u5bb9\u5c24\u4e3a\u91cd\u8981\uff0c\u5176\u4e2d\u5b58\u50a8\u4e86 css \u6e90\u7801\u7684\u5173\u952e\u5b57\u3001\u6ce8\u91ca\u3001\u6e90\u7801\u7684\u8d77\u59cb\u3001\u7ed3\u675f\u4f4d\u7f6e\u4ee5\u53ca css \u7684\u5c5e\u6027\u548c\u5c5e\u6027\u503c\uff0c\u7c7b\u540d\u4f7f\u7528<code>selector<\/code>\u5b58\u50a8\uff0c\u6bcf\u4e2a\u7c7b\u4e0b\u53c8\u5b58\u50a8\u4e00\u4e2a nodes \u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u4e0b\u5b58\u653e\u7684\u5c31\u662f\u8be5\u7c7b\u7684\u5c5e\u6027 (<code>prop<\/code>) \u548c\u5c5e\u6027\u503c (<code>value<\/code>)\u3002\u90a3\u4e48\u63d2\u4ef6\u5c31\u53ef\u4ee5\u57fa\u4e8e AST \u5b57\u6bb5\u5bf9 css \u5c5e\u6027\u8fdb\u884c\u4fee\u6539\uff0c\u4ece\u800c\u5b9e\u73b0 css \u7684\u8f6c\u6362\u3002<\/p>\n<section>PostCSS \u63d2\u4ef6\u683c\u5f0f\u89c4\u8303\u53ca API<\/section>\n<p>PostCSS \u63d2\u4ef6\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a JS \u5bf9\u8c61\uff0c\u5176\u57fa\u672c\u5f62\u5f0f\u548c\u89e3\u6790\u5982\u4e0b\uff1a<\/p>\n<pre><code>module.exports = (opts = { }) =&gt; {\r\n  \/\/ \u6b64\u5904\u53ef\u5bf9\u63d2\u4ef6\u914d\u7f6e opts \u8fdb\u884c\u5904\u7406\r\n  return {\r\n    postcssPlugin: 'postcss-test', \/\/ \u63d2\u4ef6\u540d\u5b57\uff0c\u4ee5 postcss- \u5f00\u5934\r\n\r\n    Once (root, postcss) {\r\n      \/\/ \u6b64\u5904 root \u5373\u4e3a\u8f6c\u6362\u540e\u7684 AST\uff0c\u6b64\u65b9\u6cd5\u8f6c\u6362\u4e00\u6b21 css \u5c06\u8c03\u7528\u4e00\u6b21\r\n    },\r\n\r\n    Declaration (decl, postcss) {\r\n      \/\/ postcss \u904d\u5386 css \u6837\u5f0f\u65f6\u8c03\u7528\uff0c\u5728\u8fd9\u91cc\u53ef\u4ee5\u5feb\u901f\u83b7\u5f97 type \u4e3a decl \u7684\u8282\u70b9 (\u8bf7\u53c2\u8003\u7b2c\u4e8c\u8282\u7684 AST \u5bf9\u8c61)\r\n    },\r\n\r\n    Declaration: {\r\n      color(decl, postcss) {\r\n        \/\/ \u53ef\u4ee5\u8fdb\u4e00\u6b65\u83b7\u5f97 decl \u8282\u70b9\u6307\u5b9a\u7684\u5c5e\u6027\u503c\uff0c\u8fd9\u91cc\u662f\u83b7\u5f97\u5c5e\u6027\u4e3a color \u7684\u503c\r\n      }\r\n    },\r\n\r\n    Comment (comment, postcss) {\r\n        \/\/ \u53ef\u4ee5\u5feb\u901f\u8bbf\u95ee AST \u6ce8\u91ca\u8282\u70b9\uff08type \u4e3a comment\uff09\r\n    },\r\n\r\n    AtRule(atRule, postcss) {\r\n        \/\/ \u53ef\u4ee5\u5feb\u901f\u8bbf\u95ee css \u5982 @media\uff0c@import \u7b49 @\u5b9a\u4e49\u7684\u8282\u70b9\uff08type \u4e3a atRule\uff09\r\n    }\r\n\r\n  }\r\n}\r\nmodule.exports.postcss = true\r\n<\/code><\/pre>\n<p>\u66f4\u591a\u7684 PostCSS \u63d2\u4ef6 API \u53ef\u4ee5\u8be6\u7ec6\u53c2\u8003\u5b98\u65b9 PostCSS 8 \u6587\u6863 (https:\/\/postcss.org\/api\/)\uff0c\u57fa\u672c\u539f\u7406\u5c31\u662f PostCSS \u4f1a\u904d\u5386\u6bcf\u4e00\u4e2a css \u6837\u5f0f\u5c5e\u6027\u503c\u3001\u6ce8\u91ca\u7b49\u8282\u70b9\uff0c\u4e4b\u540e\u5f00\u53d1\u8005\u5c31\u53ef\u4ee5\u9488\u5bf9\u4e2a\u6027\u9700\u6c42\u5bf9\u8282\u70b9\u8fdb\u884c\u5904\u7406\u5373\u53ef\u3002<\/p>\n<section>\u5b9e\u9645\u5f00\u53d1\u4e00\u4e2a PostCSS 8 \u63d2\u4ef6<\/section>\n<p>\u4e86\u89e3\u4e86 PostCSS \u63d2\u4ef6\u7684\u683c\u5f0f\u548c API\uff0c\u6211\u4eec\u5c06\u6839\u636e\u5b9e\u9645\u9700\u6c42\u6765\u5f00\u53d1\u4e00\u4e2a\u7b80\u6613\u7684\u63d2\u4ef6\uff0c\u6709\u5982\u4e0b css\uff1a<\/p>\n<pre><code>.demo {\r\n  font-size: 14px; \/*this is a comment*\/\r\n  color: #ffffff;\r\n}\r\n<\/code><\/pre>\n<p>\u9700\u6c42\u5982\u4e0b\uff1a<\/p>\n<ol class=\"list-paddingleft-2\">\n<li>\u5220\u9664 css \u5185\u6ce8\u91ca<\/li>\n<li>\u5c06\u6240\u6709\u989c\u8272\u4e3a\u5341\u516d\u8fdb\u5236\u7684<code>#ffffff<\/code>\u8f6c\u4e3a css \u5185\u7f6e\u7684\u989c\u8272\u53d8\u91cf<code>white<\/code><\/li>\n<\/ol>\n<p>\u6839\u636e\u7b2c\u4e09\u8282\u7684\u63d2\u4ef6\u683c\u5f0f\uff0c\u672c\u6b21\u5f00\u53d1\u53ea\u9700\u4f7f\u7528<code>Comment<\/code>\u548c<code>Declaration<\/code>\u63a5\u53e3\u5373\u53ef:<\/p>\n<pre><code>\/\/ plugin.js\r\nmodule.exports = (opts = { }) =&gt; {\r\n\r\n  return {\r\n    postcssPlugin: 'postcss-test',\r\n\r\n    Declaration (decl, postcss) {\r\n        if (decl.value === '#ffffff') {\r\n            decl.value = 'white'\r\n        }\r\n    },\r\n\r\n    Comment(comment) {\r\n      comment.text = ''\r\n    }\r\n\r\n  }\r\n}\r\nmodule.exports.postcss = true\r\n<\/code><\/pre>\n<p>\u5728 PostCSS \u4e2d\u4f7f\u7528\u8be5\u63d2\u4ef6\uff1a<\/p>\n<pre><code>\/\/ index.js\r\nconst plugin = require('.\/plugin.js')\r\n\r\npostcss([plugin]).process(`\r\n.demo {\r\n  font-size: 14px; \/*this is a comment*\/\r\n  color: #ffffff;\r\n}\r\n`).then(result =&gt; {\r\n  console.log(result.css)\r\n})\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>.demo {\r\n  font-size: 14px; \/**\/\r\n  color: white;\r\n}\r\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5b57\u4f53\u989c\u8272\u503c\u5df2\u7ecf\u6210\u529f\u505a\u4e86\u8f6c\u6362\uff0c\u6ce8\u91ca\u5185\u5bb9\u5df2\u7ecf\u5220\u6389\uff0c\u4f46\u6ce8\u91ca\u6807\u8bc6\u7b26\u8fd8\u4f9d\u65e7\u5b58\u5728\uff0c\u8fd9\u662f\u56e0\u4e3a\u6ce8\u91ca\u8282\u70b9\u662f\u5305\u542b<code>\/**\/<\/code>\u5185\u5bb9\u5b58\u5728\u7684\uff0c\u53ea\u8981 AST \u91cc\u6ce8\u91ca\u8282\u70b9\u8fd8\u5b58\u5728\uff0c\u6700\u540e PostCSS \u8fd8\u539f AST \u65f6\u8fd8\u662f\u4f1a\u628a\u8fd9\u6bb5\u5185\u5bb9\u8fd8\u539f\uff0c\u8981\u505a\u5230\u5f7b\u5e95\u5220\u6389\u6ce8\u91ca\uff0c\u9700\u8981\u5bf9 AST \u7684 nodes \u5b57\u6bb5\u8fdb\u884c\u904d\u5386\uff0c\u5c06 type \u4e3a comment \u7684\u8282\u70b9\u8fdb\u884c\u5220\u9664\uff0c\u63d2\u4ef6\u6e90\u7801\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/ plugin.js\r\nmodule.exports = (opts = { }) =&gt; {\r\n\r\n  \/\/ Work with options here\r\n  \/\/ https:\/\/postcss.org\/api\/#plugin\r\n\r\n  return {\r\n    postcssPlugin: 'postcss-test',\r\n\r\n    Once (root, postcss) {\r\n      \/\/ Transform CSS AST here\r\n      root.nodes.forEach(node =&gt; {\r\n        if (node.type === 'rule') {\r\n          node.nodes.forEach((n, i) =&gt; {\r\n            if (n.type === 'comment') {\r\n              node.nodes.splice(i, 1)\r\n            }\r\n          })\r\n        }\r\n      })\r\n    },\r\n\r\n\r\n\r\n    Declaration (decl, postcss) {\r\n      \/\/ The faster way to find Declaration node\r\n      if (decl.value === '#ffffff') {\r\n        decl.value = 'white'\r\n      }\r\n    }\r\n\r\n  }\r\n}\r\nmodule.exports.postcss = true\r\n<\/code><\/pre>\n<p>\u91cd\u65b0\u6267\u884c PostCSS\uff0c\u7ed3\u679c\u5982\u4e0b\uff0c\u7b26\u5408\u9884\u671f\u3002<\/p>\n<pre><code>.demo {\r\n  font-size: 14px;\r\n  color: white;\r\n}\r\n<\/code><\/pre>\n<section>\u63d2\u4ef6\u5f00\u53d1\u6ce8\u610f\u4e8b\u9879<\/section>\n<p>\u901a\u8fc7\u5b9e\u64cd\u5f00\u53d1\u53ef\u4ee5\u770b\u5230\uff0c\u5f00\u53d1\u4e00\u4e2a PostCSS \u63d2\u4ef6\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u4f46\u5728\u5b9e\u9645\u7684\u63d2\u4ef6\u5f00\u53d1\u4e2d\uff0c\u5f00\u53d1\u8005\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e8b\u9879\uff1a<\/p>\n<section>\u00a01. \u5c3d\u91cf\u4f7f\u63d2\u4ef6\u7b80\u5355\uff0c\u4f7f\u7528\u8005\u53ef\u4ee5\u5230\u624b\u5373\u7528<\/section>\n<blockquote><p>Build code that is short, simple, clear, and modular.<\/p><\/blockquote>\n<p>\u5c3d\u91cf\u4f7f\u4f60\u7684\u63d2\u4ef6\u548c\u4f7f\u7528\u8005\u4ee3\u7801\u89e3\u8026\uff0c\u5f00\u653e\u6709\u9650\u7684 API\uff0c\u540c\u65f6\u5f00\u53d1\u8005\u5728\u4f7f\u7528\u4f60\u7684\u63d2\u4ef6\u65f6\u4ece\u540d\u5b57\u5c31\u53ef\u4ee5\u77e5\u9053\u63d2\u4ef6\u7684\u529f\u80fd\u3002\u8fd9\u91cc\u63a8\u8350\u4e00\u4e2a\u7b80\u5355\u800c\u4f18\u96c5\u7684 PostCSS \u63d2\u4ef6 postcss-focus(https:\/\/github.com\/postcss\/postcss-focus)\uff0c\u8bfb\u8005\u53ef\u4ee5\u4ece\u8fd9\u4e2a\u63d2\u4ef6\u7684\u6e90\u7801\u4e2d\u4f53\u4f1a\u8fd9\u4e2a\u8bbe\u8ba1\u7406\u5ff5\u3002<\/p>\n<section>\u00a02. \u5f00\u53d1\u63d2\u4ef6\u524d\u786e\u8ba4\u662f\u5426\u6709\u73b0\u6210\u7684\u8f6e\u5b50<\/section>\n<p>\u5982\u679c\u4f60\u5bf9\u81ea\u5df1\u7684\u9879\u76ee\u6709\u4e2a\u65b0\u70b9\u5b50\uff0c\u60f3\u81ea\u5df1\u5f00\u53d1\u4e00\u4e2a\u63d2\u4ef6\u53bb\u5b9e\u73b0\uff0c\u5728\u5f00\u59cb\u5199\u4ee3\u7801\u524d\uff0c\u53ef\u4ee5\u5148\u5230 PostCSS \u5b98\u65b9\u6ce8\u518c\u7684\u63d2\u4ef6\u5217\u8868 (https:\/\/github.com\/postcss\/postcss\/blob\/main\/docs\/plugins.md) \u4e2d\u67e5\u770b\u662f\u5426\u6709\u7b26\u5408\u81ea\u5df1\u9700\u6c42\u7684\u63d2\u4ef6\uff0c\u907f\u514d\u91cd\u590d\u9020\u8f6e\u5b50\u3002\u4e0d\u8fc7\u622a\u6b62\u76ee\u524d (2021.8)\uff0c\u5927\u90e8\u5206\u63d2\u4ef6\u4f9d\u65e7\u505c\u7559\u5728 PostCSS 8 \u4ee5\u4e0b\uff0c\u867d\u7136 PostCSS 8 \u5df2\u7ecf\u5bf9\u65e7\u7248\u672c\u63d2\u4ef6\u505a\u4e86\u5904\u7406\uff0c\u4f46\u5728 AST \u7684\u89e3\u6790\u5904\u7406\u4e0a\u8fd8\u662f\u6709\u5dee\u5f02\uff0c\u4ece\u5b9e\u9645\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u6211\u5c31\u53d1\u73b0 PostCss8 \u4f7f\u7528\u4f4e\u7248\u672c\u63d2\u4ef6\u4f1a\u5bfc\u81f4 AST \u5185\u7684 source map \u4e22\u5931 (https:\/\/github.com\/leodido\/postcss-clean\/issues\/17)\uff0c\u56e0\u6b64\u76ee\u524d\u800c\u8a00\u5b8c\u5168\u517c\u5bb9 PostCSS 8 \u7684\u63d2\u4ef6\u8fd8\u9700\u5404\u4f4d\u5f00\u53d1\u8005\u53bb\u5347\u7ea7\u3002<\/p>\n<section>\u4ece\u4f4e\u7248\u672c PostCSS \u8fc1\u79fb<\/section>\n<p>\u5347\u7ea7\u4f60\u7684 PostCSS \u63d2\u4ef6\u5177\u4f53\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u7ed9\u51fa\u7684 \u5347\u7ea7\u6307\u5f15\u3002\u8fd9\u91cc\u53ea\u5bf9\u90e8\u5206\u5173\u952e\u90e8\u5206\u505a\u4e0b\u89e3\u91ca\uff1a<\/p>\n<section>\n<section>\u00a01. \u5347\u7ea7 API<\/section>\n<\/section>\n<ul class=\"list-paddingleft-2\">\n<li>\u5c06\u65e7\u7248<code>module.exports = postcss.plugin(name, creator)<\/code>\u66ff\u6362\u4e3a<code>module.exports = creator<\/code>\uff1b<\/li>\n<li>\u65b0\u7248\u63d2\u4ef6\u5c06\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5bf9\u8c61\u5185\u5305\u542b<code>Once<\/code>\u65b9\u6cd5\u56de\u8c03\uff1b<\/li>\n<li>\u5c06\u539f\u63d2\u4ef6\u903b\u8f91\u4ee3\u7801\u8f6c\u79fb\u81f3<code>Once<\/code>\u65b9\u6cd5\u5185\uff1b<\/li>\n<li>\u63d2\u4ef6\u6e90\u7801\u6700\u540e\u52a0\u4e0a<code>module.exports.postcss = true<\/code>\uff1b<\/li>\n<\/ul>\n<p>\u5177\u4f53\u793a\u4f8b\u5982\u4e0b\u3002<\/p>\n<p>\u65e7\u7248\u63d2\u4ef6\uff1a<\/p>\n<pre><code>- module.exports = postcss.plugin('postcss-dark-theme-class', (opts = {}) =&gt; {\r\n-   checkOpts(opts)\r\n-   return (root, result) =&gt; {\r\n      root.walkAtRules(atrule =&gt; { \u2026 })\r\n-   }\r\n- })\r\n<\/code><\/pre>\n<p>\u5347\u7ea7\u540e\u63d2\u4ef6\uff1a<\/p>\n<pre><code>+ module.exports = (opts = {}) =&gt; {\r\n+   checkOpts(opts)\r\n+   return {\r\n+     postcssPlugin: 'postcss-dark-theme-class',\r\n+     Once (root, { result }) {\r\n        root.walkAtRules(atrule =&gt; { \u2026 })\r\n+     }\r\n+   }\r\n+ }\r\n+ module.exports.postcss = true\r\n<\/code><\/pre>\n<section>\n<section>\u00a02. \u63d0\u53d6\u903b\u8f91\u4ee3\u7801\u81f3\u65b0\u7248 API<\/section>\n<\/section>\n<p>\u628a\u903b\u8f91\u4ee3\u7801\u90fd\u653e\u5728<code>Once<\/code>\u56de\u8c03\u5185\u8fd8\u4e0d\u591f\u4f18\u96c5\uff0cPostCSS 8 \u5df2\u7ecf\u5b9e\u73b0\u4e86\u5355\u4e2a css \u7684\u4ee3\u7801\u626b\u63cf\uff0c\u63d0\u4f9b\u4e86<code>Declaration()<\/code>, <code>Rule()<\/code>, <code>AtRule()<\/code>, <code>Comment()<\/code>\u7b49\u65b9\u6cd5\uff0c\u65e7\u7248\u63d2\u4ef6\u7c7b\u4f3c<code>root.walkAtRules<\/code>\u7684\u65b9\u6cd5\u5c31\u53ef\u4ee5\u5206\u522b\u8fdb\u884c\u91cd\u6784\uff0c\u63d2\u4ef6\u6548\u7387\u4e5f\u4f1a\u5f97\u5230\u63d0\u5347:<\/p>\n<pre><code>  module.exports = {\r\n    postcssPlugin: 'postcss-dark-theme-class',\r\n-   Once (root) {\r\n-     root.walkAtRules(atRule =&gt; {\r\n-       \/\/ Slow\r\n-     })\r\n-   }\r\n+   AtRule (atRule) {\r\n+     \/\/ Faster\r\n+   }\r\n  }\r\n  module.exports.postcss = true\r\n<\/code><\/pre>\n<section>\u603b\u00a0 \u7ed3<\/section>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u8bfb\u8005\u53ef\u4ee5\u4e86\u89e3 PostCSS 8 \u5de5\u4f5c\u7684\u57fa\u672c\u539f\u7406\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u5feb\u901f\u5f00\u53d1\u4e00\u4e2a PostCSS 8 \u63d2\u4ef6\uff0c\u5e76\u5728\u6700\u540e\u5f15\u7528\u5b98\u65b9\u793a\u4f8b\u4e2d\u4ecb\u7ecd\u4e86\u5982\u4f55\u5feb\u901f\u5347\u7ea7\u65e7\u7248 PostCSS \u63d2\u4ef6\u3002\u76ee\u524d PostCSS 8 \u8fd8\u6709\u5927\u91cf\u8fd8\u6ca1\u8fdb\u884c\u5347\u7ea7\u517c\u5bb9\u7684 PostCSS \u63d2\u4ef6\uff0c\u5e0c\u671b\u8bfb\u8005\u53ef\u4ee5\u5728\u9605\u8bfb\u672c\u6587\u540e\u53ef\u4ee5\u83b7\u5f97\u542f\u53d1\uff0c\u5bf9 PostCSS 8 \u7684\u63d2\u4ef6\u751f\u6001\u505a\u51fa\u8d21\u732e\u3002<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1397\" class=\"pvc_stats total_only  \" data-element-id=\"1397\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005 | \u674e\u4f73\u6d69 \u7b14\u8005\u8fd1\u671f\u5728\u5c06\u524d\u7aef\u67b6\u6784 webpack \u5347\u7ea7\u5230 5 \u65f6\uff0c\u4e00\u4e9b\u914d\u5957\u6a21\u5757\u4e5f\u9700\u8981\u8fdb\u884c\u5347\u7ea7\uff0c\u5176\u4e2d\u5305\u62ec\u4e86 [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1397\" class=\"pvc_stats total_only  \" data-element-id=\"1397\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":1398,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1397","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai"],"_links":{"self":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/1397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/comments?post=1397"}],"version-history":[{"count":1,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/1397\/revisions"}],"predecessor-version":[{"id":1400,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/1397\/revisions\/1400"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media\/1398"}],"wp:attachment":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media?parent=1397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/categories?post=1397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/tags?post=1397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}