一、编写插件
export default (config = {}) => {
return {
transform(content, path) {
if (!/.vue$/.test(path)) {
return;
}
let temTest = /(<template>)([\s\S]+)(<\/template>)/g;
let strTest = /(\d+)(px)/g;
content = content.replace(temTest, (str, p1, p2) => {
p2 = p2.replace(strTest,(str,p1) =>{
let rem = Number(p1 / config.rootValue).toFixed(5);
return `${rem}rem`
});
return `<template>${p2}</template>`
});
return content;
},
};
};
使用
import pluginViteLthStyle from "./plugin/vite-plugin-lth-style.ts";
export default defineConfig({
plugins: [
pluginViteLthStyle({
rootValue: 16,
}),
vue(),
],
})