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
            }
          })
        ]
      }
    }
  }
})
相关推荐
GitLqr8 小时前
提升 Web 开发效率:我常用的 10 个 TypeScript 实战技巧
vue.js·react.js·typescript
勇往直前plus10 小时前
Vue3(篇四)单页面应用到路由管理
javascript·typescript·前端框架·vue
勇往直前plus11 小时前
Vue3(篇三) Element Plus
前端·javascript·typescript·vue
退休倒计时12 小时前
【每日一题】LeetCode 207. 课程表 TypeScript
算法·leetcode·typescript
lincats15 小时前
Remotion Skill vs HyperFrames Skill:AI视频圈两大技能包正面交锋,最好的对手,最好的搭档?
typescript·vibe coding·claude code·skills
Web4Browser1 天前
指纹浏览器 API 自动化怎么接:启动 Profile、获取 CDP 端点并连接自动化框架
前端·网络·typescript·自动化
寒水馨1 天前
Linux下载、安装 Bun v1.3.14(附安装包bun-linux-x64.zip)
linux·javascript·typescript·node.js·bun·运行时·包管理器
退休倒计时1 天前
【复习】LeetCode 二叉树DFS TypeScript
算法·leetcode·typescript·深度优先
用户0934077735142 天前
HarmonyOS WPS Open SDK 二开周回顾:从注册到关窗回传怎么串
typescript·harmonyos
啊啊啊迈 旋棍2 天前
【译】TypeScript 7 测试版已在 Visual Studio 2026 18.6 Insiders 3 中默认启用
ubuntu·typescript·visual studio