vue3 + vite 实用依赖与配置

mark一下日常 vue3 + vite 项目配置

实用依赖与配置

      • 1. amfe-flexible
        • (2)安装
        • (3)使用
      • 2. postcss-pxtorem
        • (1)介绍
        • (2)安装
        • (3)使用
      • 3. autoprefixer
        • (1)介绍
        • (2)安装
        • (3)使用
      • 4. rollup-plugin-visualizer
        • (1)介绍
        • (2)安装
        • (3)使用
      • 5. prettier
        • (1)介绍
        • (2)安装
        • (3)使用
      • 6. terser
        • (1)介绍
        • (2)安装
        • (3)使用
      • 7. unplugin-auto-import
        • (1)介绍
        • (2)安装
        • (3)使用
      • 8. unplugin-vue-components
        • (1)介绍
        • (2)安装
        • (3)使用
      • 9. vite-plugin-compression
        • (1)介绍
        • (2)安装
        • (3)使用
      • 10. vite-plugin-imagemin
        • (1)介绍
        • (2)安装
        • (3)使用
      • 11. rollupOptions分包配置
        • (1)介绍
        • (2)使用
      • End. 完整配置文件

1. amfe-flexible

amfe-flexible 是一个用于移动端适配的库,通过动态设置 HTML 根元素的字体大小来实现响应式布局。这使得使用 rem 单位的设计能够根据屏幕尺寸自动调整,从而适应不同的设备。通常搭配 postcss-pxtorem 使用。

(2)安装
yarn add amfe-flexible
(3)使用
// main.ts
import 'amfe-flexible'

2. postcss-pxtorem

(1)介绍

postcss-pxtorem 是一个 PostCSS 插件,用于将 CSS 中的像素(px)单位自动转换为 rem 单位。在现代前端开发中,尤其是在进行响应式设计时,使用 rem 单位能够更好地适应不同的屏幕尺寸和分辨率。

(2)安装
yarn add postcss-pxtorem
(3)使用
// vite.config.ts
import postCssPxToRem from 'postcss-pxtorem'
export default defineConfig({...,css:{...,postcss: {plugins: [postCssPxToRem({rootValue: 192, // 设计稿宽度/10propList: ['*'], // 全部css属性都转换remselectorBlackList: [],exclude: '/node_modules', // 忽略包文件转换rem}),],},}
})

3. autoprefixer

(1)介绍

autoprefixer 是一个 PostCSS 插件,用于自动为 CSS 属性添加不同浏览器的前缀。它基于 Can I Use 数据库,确保生成的 CSS 代码能够在目标浏览器中兼容和正常工作,从而提高代码的兼容性和稳定性。

(2)安装
yarn add autoprefixer
(3)使用
// vite.config.ts
import autoprefixer from 'autoprefixer'
export default defineConfig({...,css:{...,postcss: {plugins: [autoprefixer({overrideBrowserslist: ['> 1%','last 2 versions','Firefox ESR','not dead',],flexbox: 'no-2009',}),],},}
})

4. rollup-plugin-visualizer

(1)介绍

rollup-plugin-visualizer 是一个 Rollup 插件,用于生成打包后文件的可视化分析图。它帮助开发者了解每个模块在最终构建中的大小,识别和优化较大的依赖,从而减少打包体积,提升性能。

(2)安装
yarn add rollup-plugin-visualizer -D
(3)使用
// vite.config.ts
import { visualizer } from 'rollup-plugin-visualizer'
export default defineConfig({...,plugins: [...,visualizer(), // 可传入配置]
})

5. prettier

(1)介绍

prettier 是一个代码格式化工具,支持多种编程语言。它通过统一的规则自动格式化代码,确保代码风格的一致性,提高代码的可读性和可维护性。Prettier 可以与多种编辑器和版本控制系统集成。

(2)安装
yarn add prettier -D
(3)使用

vscode安装 Prettier - Code formatter 拓展 + 配置Prettier默认格式化

根目录新建文件 .prettierrc

# 忽略目录或文件
build
dist
coverage
node_modules
public
release
.vscode# 忽略所有 html 文件
*.html

根目录新建 .prettierignore

{"arrowParens": "always","bracketSameLine": false,"bracketSpacing": true,"embeddedLanguageFormatting": "auto","htmlWhitespaceSensitivity": "css","insertPragma": false,"jsxSingleQuote": false,"printWidth": 80,"proseWrap": "preserve","quoteProps": "as-needed","requirePragma": false,"semi": false,"singleQuote": true,"tabWidth": 2,"trailingComma": "es5","useTabs": false,"vueIndentScriptAndStyle": false
}

6. terser

(1)介绍

terser 是一个 JavaScript 压缩工具,用于优化和缩小 JavaScript 文件体积。它能够删除多余的代码、注释和空白,进行代码混淆,从而提高加载速度和性能。Terser 是 UglifyJS 的分支,具有更好的 ES6+ 支持。

(2)安装
yarn add terser -D
(3)使用
 // vite.config.ts
export default defineConfig({...,build:{...,minify: 'terser',terserOptions: {compress: {drop_console: true, // 去除logdrop_debugger: true, // 去除debugger},format: {comments: false, // 去除注释},},}
})

7. unplugin-auto-import

(1)介绍

unplugin-auto-import 是一个 Vite 插件,用于自动导入项目中的常用依赖和函数。它简化了代码中的手动导入操作,减少了重复代码,提高了开发效率。插件会根据配置文件自动生成类型定义文件,确保类型安全。

(2)安装
yarn add unplugin-auto-import -D
(3)使用
 // vite.config.ts
import AutoImport from 'unplugin-auto-import/vite'
export default defineConfig({...,plugins:[...,AutoImport({imports: ['vue', 'vue-router', 'pinia'],  // 配置自动导入的apidts: 'src/auto-imports.d.ts',}),]
})

8. unplugin-vue-components

(1)介绍

unplugin-vue-components 是一个 Vite 插件,用于自动导入和注册 Vue 组件。它通过扫描项目中的组件文件,自动生成组件的导入语句,避免了手动注册组件的繁琐操作,从而提升开发效率和代码可读性。

(2)安装
yarn add unplugin-vue-components -D
(3)使用

需要配置需要导入的ui库组件,具体查看各ui库文档

 // vite.config.ts
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
export default defineConfig({...,plugins:[...,Components({resolvers: [NaiveUiResolver()],  // 自动导入NaiveUi的组件dts: 'src/components.d.ts',}),]
})

9. vite-plugin-compression

(1)介绍

vite-plugin-compression 是一个 Vite 插件,用于在构建过程中压缩生成的文件。它支持多种压缩算法,如 gzip 和 brotli,能够减少文件体积,提高传输效率,从而加快页面加载速度。

(2)安装
yarn add vite-plugin-compression -D
(3)使用
 // vite.config.ts
import ViteCompression from 'vite-plugin-compression'
export default defineConfig({...,plugins:[...,ViteCompression({verbose: true, // 是否输出压缩过程logdisable: false, // 启用或禁用压缩功能threshold: 10240, // 触发压缩的文件大小阈值algorithm: 'gzip', // Gzip 压缩算法ext: '.gz', // 压缩后文件的扩展名}),]
})

10. vite-plugin-imagemin

(1)介绍

vite-plugin-imagemin 是一个 Vite 插件,用于优化和压缩图片资源。它支持多种图片格式的优化,如 JPEG、PNG、GIF 和 SVG,能够显著减少图片体积,提高页面加载速度和性能。插件提供了多种配置选项,允许开发者根据需求调整优化参数。

(2)安装
yarn add vite-plugin-imagemin -D
(3)使用
 // vite.config.ts
import ViteImagemin from 'vite-plugin-imagemin'
export default defineConfig({...,plugins:[...,ViteImagemin({gifsicle: {optimizationLevel: 7,interlaced: false,},optipng: {optimizationLevel: 7,},mozjpeg: {quality: 80,},pngquant: {quality: [0.8, 0.9],speed: 4,},svgo: {plugins: [{name: 'removeViewBox',},{name: 'removeEmptyAttrs',active: false,},],},}),]
})

11. rollupOptions分包配置

(1)介绍

rollupOptions 是vite用于配置 Rollup 构建工具的选项

(2)使用
 // vite.config.ts
export default defineConfig({...,build:{...,rollupOptions: {output: {chunkFileNames: 'static/js/[name]-[hash].js',entryFileNames: 'static/js/[name]-[hash].js',assetFileNames: 'static/[ext]/[name]-[hash].[ext]',manualChunks(id) {if (id.includes('node_modules')) {return id.toString().split('node_modules/')[1].split('/')[0].toString()}},},},}
})

End. 完整配置文件

vite.config.ts

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import ViteCompression from 'vite-plugin-compression'
import ViteImagemin from 'vite-plugin-imagemin'
import { visualizer } from 'rollup-plugin-visualizer'
import postCssPxToRem from 'postcss-pxtorem'
import autoprefixer from 'autoprefixer'
import path from 'path'
import { resolve } from 'path'export default defineConfig({base: './',plugins: [vue(),visualizer(),AutoImport({imports: ['vue', 'vue-router', 'pinia'],dts: 'src/auto-imports.d.ts',}),Components({resolvers: [NaiveUiResolver()],dts: 'src/components.d.ts',}),ViteCompression({verbose: true,disable: false,threshold: 10240,algorithm: 'gzip',ext: '.gz',}),ViteImagemin({gifsicle: {optimizationLevel: 7,interlaced: false,},optipng: {optimizationLevel: 7,},mozjpeg: {quality: 80,},pngquant: {quality: [0.8, 0.9],speed: 4,},svgo: {plugins: [{name: 'removeViewBox',},{name: 'removeEmptyAttrs',},],},}),],build: {minify: 'terser',terserOptions: {compress: {drop_console: true,drop_debugger: true,},format: {comments: false,},},rollupOptions: {output: {chunkFileNames: 'static/js/[name]-[hash].js',entryFileNames: 'static/js/[name]-[hash].js',assetFileNames: 'static/[ext]/[name]-[hash].[ext]',manualChunks(id) {if (id.includes('node_modules')) {return id.toString().split('node_modules/')[1].split('/')[0].toString()}},},},},server: {port: 5260,host: '0.0.0.0',},css: {preprocessorOptions: {scss: {additionalData: `@import "@/style/public.scss";`,includePaths: [path.resolve(__dirname, './src/styles')],},},postcss: {plugins: [postCssPxToRem({rootValue: 247.8,propList: ['*'],selectorBlackList: [],exclude: '/node_modules',}),autoprefixer({overrideBrowserslist: ['> 1%','last 2 versions','Firefox ESR','not dead',],flexbox: 'no-2009',}),],},},resolve: {alias: [{find: '@',replacement: resolve(__dirname, './src'),},],},
})

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/14750.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Convolutional Occupancy Networks【ECCV2020】

论文:https://arxiv.org/pdf/2003.04618 代码:GitHub - autonomousvision/convolutional_occupancy_networks: [ECCV20] Convolutional Occupancy Networks 图 1:卷积占据网络。传统的隐式模型 (a) 由于其全连接网络结构,表现能力…

继承初级入门复习

注意:保护和私有在类中没有区别,但是在继承中有区别,private在继承的子类不可见,protect在继承的子类可见 记忆方法:先看基类的修饰符是private,那都是不可见的。如果不是,那就用继承的修饰和基…

对this对象的理解

This 是指函数被调用是的上下文,它指向最后一次调用这个方法的对象。this的值并不是在函数定义时确定的,而是在函数被调用时确定的。在实际开发中,this 的指向一般可以通过四种调用模式来判断。 1、函数调用 当一个函数不是一个对象的属性时…

使用keepalived实现mysql主从复制的自动切换

使用Keepalived实现MySQL主从复制的自动切换通常涉及配置一个虚拟IP(VIP)作为MySQL服务器对客户端的访问点。Keepalived会监控MySQL主服务器的健康状况,如果主服务器宕机,Keepalived会自动将虚拟IP移至备用服务器,从而…

鸿蒙实战开发:网络层的艺术——优雅封装与搭建指南(上)

在鸿蒙的广袤开发世界中,网络层作为信息交换的桥梁,其重要性不言而喻。今天,我将带领大家一同探索如何以艺术般的手法,优雅地封装鸿蒙官方的网络库,为我们的应用搭建一个高效、灵活的网络层。我们在下一篇章中,将深入阐述如何利用这一封装完善的网络库,轻松驾驭网络层的…

gstreamer Windows常见问题汇总

需要先安装gstreamer , 再编译opencv。https://gstreamer.freedesktop.org/download/,都需要安装。 OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK 2.x or Carbon support. If you are on Ubuntu or D…

肌肤暗沉与胶原蛋白:解锁透亮肌肤的秘密

🌸亲爱的小仙女们,今天我们来聊聊肌肤暗沉与胶原蛋白之间的神秘联系。你是不是也曾为肌肤的暗沉而烦恼?其实,很多时候,肌肤的暗沉不仅仅是外部因素造成的,更与肌肤内部的胶原蛋白含量密切相关。&#x1f31…

C++ 变量类型与转换

C 变量类型与转换 文章目录 C 变量类型与转换变量int_tsize_t与ssize_tpid_ttime_t typenametypeid关键字类型转换编译期类型转换std::static_cast注意事项运行时类型转换std::dynamic_cast 变量 int_t 它是通过typedef定义的,而不是一种新的数据类型。 - int8_t…

SpringBoot之@Builder 注解

(1)Builder 生成的构造器不是完美的,如果没有提供必须的参数,构造器可能会创建出不完整或者不合法的对象,导致代码报错。 Builder 注解产生的 Builder 类的构造方法默认并不能限定必传参数。 (2&#xff…

系统架构师-考试-基础题-错题集锦1

系统架构师-考试-基础题-错题集锦 1.当一台服务器出现故障时将业务迁移到另外一台物理服务器上,保障了业务的连续性。 2.面向对象: 实体类,边界类,控制类 3.RUP:UP,统一过程,以架构为中心&am…

LeetCode700二叉搜索树中的搜索

题目描述 给定二叉搜索树(BST)的根节点 root 和一个整数值 val。你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。 解析 最基本的二叉搜索树的应用,递归或者while循环都可以…

分布式数据库HBase入门指南

目录 概述 HBase 的主要特点包括: HBase 的典型应用场景包括: 访问接口 1. Java API: 2. REST API: 3. Thrift API: 4. 其他访问接口: HBase 数据模型 概述 该模型具有以下特点: 1. 面向列: 2. 多维: 3. 稀疏: 数据存储: 数据访问: HBase 的数据模型…

炼丹学习笔记2---ubuntu2004运行3D Gaussian Splatting记录

前言 主要想看看前沿效果,看看跟激光slam出来效果差多少。折腾过程中,务必 根据本地的cuda版本号,安装对应的torch相关东西。 1、拉仓库 git clone https://github.com/graphdeco-inria/gaussian-splatting.git2、创建环境并激活 conda c…

MySQL详细安装、配置过程,多图,详解

本文适合centos7环境下安装mysql,在安装和卸载过程中,都在root用户下完成。文章目录 清理环境获取mysql官方yum源安装mysql yum源安装mysql服务安装报错解决办法验证是否安装完成启动mysql服务登录服务方法一:方法二:方法三&#…

数据意外删除?安卓手机数据恢复教程来帮你解救

手机不仅仅是一个通讯工具,更是我们记录生活、工作、学习等各种信息的重要载体,无论是拍照、录音、录像,还是文字记录,手机都能轻松完成。可有时候我们会不小心删除一些重要的数据,这时候我们该怎么办呢?别…

Modbus-RTU/TCP规约 | 报文解析 | 组织报文与解析报文(C++)

文章目录 一、MODBUS规约1.MODBUS-RTU规约2.MODBUS-TCP规约 二、报文解析1.MODBUS-RTU报文帧解析2.MODBUS-TCP报文帧解析 三、C代码实现组织报文与解析报文 一、MODBUS规约 Modbus规约是一种广泛使用的串行通信协议(应用层报文传输协议),用于…

鲲泰新闻丨第七届数字中国建设峰会正式启幕,神州鲲泰携手天翼云共筑智算云生态

2024年5月23日,由国家发展改革委、国家数据局、国家网信办、科技部、国务院国资委、福建省人民政府共同主办的“第七届数字中国建设峰会”在福建省福州市海峡国际会展中心盛大开幕。 数字中国建设峰会是展示数字中国建设成就的盛会,本次峰会以“释放数据…

【MYSQL】分数排名

表: Scores ---------------------- | Column Name | Type | ---------------------- | id | int | | score | decimal | ---------------------- id 是该表的主键(有不同值的列)。 该表的每一行都包含了一场比赛的分数。Score 是…

草图大师2024怎么保存低版本呢?插件怎么写?

草图大师是一款流行的绘图和设计软件,为了向后兼容,保存低版本文件时,可以采取以下步骤: su模型库 1.另存为旧版本格式: 在保存文件时,草图大师通常会提供一个选项,让你选择要保存的文件格式和…

智简云携手云器Lakehouse打造一体化大数据平台,释放数据价值

导读 本篇分享的是智简云使用云器Lakehouse升级数据平台的实践总结。 智简云,是一家拥有十余年历史的科技公司,专注于企业服务领域,开发了两款核心产品:基于PASS平台的客户关系管理(CRM)系统和为中小型用…