From 1ed5af4681042436f448fe6f18fa78ab9499b6e9 Mon Sep 17 00:00:00 2001 From: "DESKTOP-VMMLSOQ\\wangzg" <1161909281@qq.com> Date: Sat, 2 Mar 2024 22:59:49 +0800 Subject: [PATCH] =?UTF-8?q?px=E8=B0=83=E6=95=B4=E5=80=8D=E6=95=B0=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- buildPlugins/pxresize.js | 37 +++++++++++++++++++------------------ pages/index.vue | 1 + postcss.config.js | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/buildPlugins/pxresize.js b/buildPlugins/pxresize.js index 55b98f4..0324c7c 100644 --- a/buildPlugins/pxresize.js +++ b/buildPlugins/pxresize.js @@ -6,24 +6,25 @@ let pxRegExp = /\b(\d+(\.\d+)?)px\b/; let pxGlobalRegExp = new RegExp(pxRegExp.source, 'g'); module.exports = postcss.plugin('postcss-px2rem', (option) => { - let opt = Object.assign({ - scale: 0.5, - ignore: [] - }, option) + let opt = Object.assign({ + scale: 0.5, + ignore: [] + }, option) - return (root, result) => { - const ignore = opt.ignore || [] - const file = result.opts.from - for (const ignoreElement of ignore) { - if (file.indexOf(ignoreElement) >= 0) return - } - root.walkDecls(function(decl) { - if (/px/.test(decl.value)) { - decl.value = decl.value.replace(pxGlobalRegExp, function(match, p1) { - return p1 * opt.scale + 'px' - }) - } + return (root, result) => { + const file = root.source.input.file + const ignore = opt.ignore || [] + for (const ignoreElement of ignore) { + const regExp = new RegExp(ignoreElement) + if (regExp.test(file)) return + } + root.walkDecls(function (decl) { + if (/px/.test(decl.value)) { + decl.value = decl.value.replace(pxGlobalRegExp, function (match, p1) { + return p1 * opt.scale + 'px' }) - result.root = root - }; + } + }) + result.root = root + }; }); diff --git a/pages/index.vue b/pages/index.vue index a54377a..b2f89ab 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -63,6 +63,7 @@ export default {