当前位置: 首页 > article >正文

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

参考文档

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

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

安装依赖

pnpm add amfe-flexible
pnpm add postcss-pxtorem -D

引入插件

mian.ts 中引入

import 'amfe-flexible'

配置插件

vite.config.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
            }
          })
        ]
      }
    }
  }
})

http://www.kler.cn/news/305182.html

相关文章:

  • ingress对外服务
  • c# socket通信实例
  • Docker突然宣布:涨价80%
  • 初阶数据结构【TOP】- 11.普通二叉树的介绍 - 1. (细致,保姆~~!)
  • 进阶岛 任务2:Lagent 自定义你的 Agent 智能体
  • sshpass 实现的SSH免交互密码登录和ARM移植
  • Java中List集合去重
  • 【python计算机视觉编程——9.图像分割】
  • 什么是 SMB 服务器以及它如何工作?
  • Nginx引发的惨案
  • 动手学深度学习(pytorch)学习记录28-使用块的网络(VGG)[学习记录]
  • 【计算机网络】HTTPHTTPS
  • vue前端实现下载导入模板文件
  • 1405 问题 E: 世界杯
  • 基于深度学习的信号滤波:创新技术与应用挑战
  • PyTorch 和 TensorFlow
  • 【深度学习】神经网络-怎么分清DNN、CNN、RNN?
  • Anaconda pytorch-gpu CUDA CUDNN 安装指南
  • clickhouse 保证幂等性
  • 前端面试记录
  • mybatis-plu分页出现问题
  • JVM面试真题总结(九)
  • windows检查端口占用并关闭应用
  • git报错,error: bad signature 0x00000000fatal: index file corrupt
  • 3. 进阶指南:自定义 Prompt 提升大模型解题能力
  • 新手教学系列——用Nginx将页面请求分发到不同后端模块
  • 足球大小球及亚盘数据分析与机器学习实战详解:从数据清洗到模型优化
  • vue项目中引入组件时出现的Module is not installed问题
  • 上图为是否色发
  • 15、Python如何获取文件的状态