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
            }
          })
        ]
      }
    }
  }
})
相关推荐
linweidong3 小时前
前端Three.js面试题及参考答案
前端·javascript·vue.js·typescript·前端框架·three.js·前端面经
程序饲养员16 小时前
注意Tailwind CSS 4.0 自定义颜色方式变更了
前端·css·postcss
热爱编程的小曾17 小时前
sqli-labs靶场 less 7
android·adb·less
EricXJ17 小时前
为什么选择 tsup?
前端·webpack·typescript
EricXJ20 小时前
useMutation Hook 使用指南
前端·react.js·typescript
记得早睡~20 小时前
leetcode78-子集
javascript·算法·leetcode·typescript
逾明1 天前
使用postcss-px-to-viewport-8-plugin将页面转响应式
前端·响应式设计·postcss
活宝小娜2 天前
在Vue 3 + TypeScript + Vite 项目中安装和使用 SCSS
vue.js·typescript·scss
逆袭的小黄鸭3 天前
仿 ElementPlus 组件库( 十一 )—— Form 组件实现
前端·vue.js·typescript
程序员林北北3 天前
深入解析 Vue3 响应式系统:原理、性能优化与应用场景
前端·javascript·vue.js·typescript·前端框架·html·es6