You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB
JavaScript

2 months ago
import autoprefixer from 'autoprefixer';
import postcssResponsiveType from 'postcss-responsive-type';
import pxToViewport from 'postcss-px-to-viewport';
2 months ago
import tailwindcss from 'tailwindcss';
// 使用新的PostCSS 8 API创建自定义的px-to-viewport插件
const customPxToViewportPlugin = (options) => {
return {
postcssPlugin: 'custom-px-to-viewport',
Once(root, { result }) {
const file = result.opts.from;
let viewportWidth = '';
if (file?.includes('vant')) {
viewportWidth = 375;
}
if (file.includes('src/views')) {
viewportWidth = 1920;
}
const pxToViewportInstance = pxToViewport({
...options,
viewportWidth: viewportWidth,
});
if (file) {
pxToViewportInstance(root, result);
}
}
};
};
2 months ago
2 months ago
// 定义postcss插件名称
customPxToViewportPlugin.postcss = true;
2 months ago
export default {
plugins: [
2 months ago
tailwindcss(),
2 months ago
autoprefixer(), // 自动添加浏览器前缀
postcssResponsiveType(), // 自动调整文本大小
2 months ago
customPxToViewportPlugin({
2 months ago
unitPrecision: 10, // 保留的小数位数
2 months ago
selectorBlackList: [/^\.van/], // 以 .van 开头的类名不转换
2 months ago
minPixelValue: 1, // 小于或等于 1px 不转换
mediaQuery: false, // 允许在媒体查询中转换 px
2 months ago
viewportUnit: "vw", // 转换后的单位
fontViewportUnit: "vw", // 字体单位
unitToConvert: "px" // 需要转换的单位
2 months ago
}),
],
};