Vue3+TypeScript+Vite+Less 开发 H5 项目(amfe-flexible + postcss-pxtorem)

参考文档

  • amfe-flexible:将根元素 html 的字体大小 fontSize(1rem) 设为 viewWidth / 10,以适配不同终端

  • postcss-pxtorem:将 px 单位转换为 rem 单位

安装依赖

bash 复制代码
pnpm add amfe-flexible
pnpm add postcss-pxtorem -D

引入插件

在 mian.ts 中引入

ts 复制代码
import 'amfe-flexible'

配置插件

在 vite.config.ts 中添加相关配置

ts 复制代码
import type { ConfigEnv, UserConfig } from 'vite'
import pxtorem from 'postcss-pxtorem'

export default defineConfig(({ command, mode }: ConfigEnv): UserConfig => {
  console.log(command, mode)
  return {
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            themeColor: '#1677ff'
          },
          javascriptEnabled: true,
        }
      },
      postcss: {
        plugins: [
          pxtorem({
            rootValue: 75, // 类型:Number | Function;根元素字体大小,默认 16,一般设置为设计稿尺寸 viewWidth 的 1/10(750 => 75 / 375 => 37.5)
            unitPrecision: 5, // 类型:Number;rem 单位允许的小数位数,默认 5
            propList: ['*'], // 类型:Array,需要将 px 单位转换为 rem 单位的属性列表,默认 ['font', 'font-size', 'line-height', 'letter-spacing']
            selectorBlackList: [] // 类型:Array,需要忽略的选择器列表,不会转换 px 单位,默认 []
            replace: true, // 类型:Boolean,默认 true
            exclude: (file: any) => { // 类型:String | Regexp | Function,要忽略并保持 px 单位的文件路径,默认 /node_modules/i
              if (file.includes('h5'))) {
                // 将所有包含 h5 目录中的文件 px 单位转换为 rem 单位
                return false
              }
              return true
            }
          })
        ]
      }
    }
  }
})
相关推荐
lgbisha1 小时前
银河麒麟入门:日志刷满终端,用 less 定位关键词并看前后几行
java·前端·less·银河麒麟
htzyl2063 小时前
升级到 TypeScript 7:哪些 tsconfig 选项会直接报错
前端·typescript
king王一帅6 小时前
思考了很久,QuireKit 还是支持了 20 年前的 .doc,浏览器/Node 通用,MIT 开源
前端·typescript·开源
Amumu1213816 小时前
TypeScript学习: 基础知识(二)
开发语言·typescript·ecmascript
Amumu1213818 小时前
TypeScript学习: 进阶知识
开发语言·学习·typescript
用户302822530681 天前
AI 视频图片运镜为什么会抖?帧率拉高也救不了多次取整
前端·人工智能·typescript
htzyl2061 天前
TypeScript:tsconfig配置误区及详情介绍,让你清清楚楚
前端·typescript
疯华整猫1 天前
类型全对、tsc 零错误,上线却白屏:AI 时代学 TypeScript,学的是它替不了的那一层
前端·typescript
king王一帅1 天前
啃完几十份国标,QuireKit 实现了像 Word 一样流畅的浏览器 OFD 编辑器,支持完备电子签标准与 OFD-H
前端·typescript·产品
htzyl2062 天前
TypeScript中替代enum的五种方案
前端·typescript