Vite manual

npm run start

“start”: “vite --host 0.0.0.0 --port 83”,

–port 默認 80

Init file

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})

Vite.config.ts 配置自动导入

Element plus, 其他组件也一样,您需要使用额外的插件来导入要使用的组件。

npm install -D unplugin-vue-components unplugin-auto-import

pnpm add -D unplugin-auto-import

pnpm add -D unplugin-vue-components
AutoImport({
      resolvers: [ElementPlusResolver()],
      eslintrc: {
        enabled: true,
        filepath: './.eslintrc-auto-import.json',
        globalsPropValue: true
      }
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    })

生成两个文件

成功安装后,在项目的根目录下生成两个文件
auto-imports.d.ts

/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// noinspection JSUnusedGlobalSymbols
// Generated by unplugin-auto-import
export {}
declare global {
  const ElMessage: typeof import('element-plus/es')['ElMessage']
}

components.d.ts

/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// Generated by unplugin-vue-components
// Read more: https://github.com/vuejs/core/pull/3399
export {}

declare module 'vue' {
  export interface GlobalComponents {
    ElButton: typeof import('element-plus/es')['ElButton']
    HelloWorld: typeof import('./src/components/HelloWorld.vue')['default']
    IconCommunity: typeof import('./src/components/icons/IconCommunity.vue')['default']
    IconDocumentation: typeof import('./src/components/icons/IconDocumentation.vue')['default']
    IconEcosystem: typeof import('./src/components/icons/IconEcosystem.vue')['default']
    IconSupport: typeof import('./src/components/icons/IconSupport.vue')['default']
    IconTooling: typeof import('./src/components/icons/IconTooling.vue')['default']
    RouterLink: typeof import('vue-router')['RouterLink']
    RouterView: typeof import('vue-router')['RouterView']
    TheWelcome: typeof import('./src/components/TheWelcome.vue')['default']
    WelcomeItem: typeof import('./src/components/WelcomeItem.vue')['default']
  }
}

配置Eslint

  1. 在vite.config.ts导入Eslint文件
eslintrc: {
        enabled: true,
        filepath: './.eslintrc-auto-import.json',
        globalsPropValue: true
      }
  1. 生成.eslintrc-auto-import.json文件
{
  "globals": {
    "ElMessage": true
  }
}
  1. .eslintrc.cjs 包含进来 …eslintrc-auto-import文件,让Eslint可以检测到
extends: [
    './.eslintrc-auto-import.json',
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/eslint-config-typescript',
    '@vue/eslint-config-prettier/skip-formatting'
  ],

vite 使用 svg

安装依赖

npm install vite-plugin-svg-icons -D

配置 vite.config.ts

import { createSvgIconsPlugin } from ‘vite-plugin-svg-icons’

//svg plugin
createSvgIconsPlugin({
  iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
  symbolId: 'icon-[dir]-[name]'
}),

编写 IconSvg.vue

<script lang="ts" setup>
defineProps({
    prefix:{
        type:String,
        default:'#icon-'
    },
    name:String,
    color:{
        type:String,
        default:''
    },
    width:{
        type:String,
        default:'16px'
    },
    height:{
        type:String,
        default:'16px'
    }
})
</script>
<template>
    <svg :style="{width,height}">
        <use :xlink:href="prefix+name" :fill="color"></use>
    </svg>
</template>

使用

<IconSvg name="icon-vend-48" width="24px" height="24px"/>
03-08
<think>嗯,用户让我介绍一下Vite,我需要先回忆一下Vite的相关知识。Vite是一个前端构建工具,对吧?它是由Vue.js的作者尤雨溪开发的,主要特点是速度快,尤其是在开发服务器启动和热更新方面。我记得它利用了浏览器原生ES模块导入的特性,所以不需要打包,直接按需加载,这样开发环境下的启动速度特别快。 接下来要组织一下回答的结构。用户可能需要知道Vite是什么,核心优势,使用场景,基本用法,还有可能和Webpack这样的传统工具对比。不过用户没有明确说要对比,可能先重点介绍Vite本身。 用户可能对Vite的优势比较感兴趣,比如快速的冷启动、即时热模块替换(HMR)、支持多种框架如Vue、React、Svelte等。另外,生产环境构建方面,Vite使用Rollup,这也是一个优势,因为Rollup在打包方面效率很高。 可能需要提到Vite的插件生态系统,虽然相比Webpack可能还不够丰富,但正在快速发展。还有Vite的配置相对简单,对新手友好,这也是一个卖点。 然后,用户可能想知道如何开始使用Vite,所以需要给出一个简单的示例,比如创建项目的命令,以及目录结构和基本配置。可能还要提到vite.config.js这个配置文件,以及如何通过插件扩展功能。 还要注意用户可能的背景,比如是否是前端开发者,是否熟悉现有的构建工具。如果用户之前用过Webpack,可能会关心迁移到Vite的难易程度,或者Vite解决了哪些痛点。不过用户的问题只是介绍,所以可能不需要深入比较,但简要提一下差异还是有帮助的。 另外,用户可能关心Vite适合什么样的项目,比如单页应用(SPA)、库开发,或者是否需要特定的框架支持。可以提到Vite默认支持TypeScript、CSS预处理器等,这些都是现代前端开发的常见需求。 需要确认一下最新的Vite版本是否有新特性,比如是否支持SSR(服务器端渲染)或者其他的优化。不过作为一般介绍,可能不需要太深入,保持基础信息准确即可。 最后,可能需要给出一些资源链接,比如官方文档,或者社区推荐的插件,但用户的问题里没有要求,所以可能不需要,但可以建议查阅官方文档获取更多信息。 总结一下,回答的结构应该是:定义、核心优势、适用场景、基本用法、简单示例,可能的话,加上生态系统和配置说明。确保用中文,口语化,避免技术术语堆砌,但必要的术语需要解释清楚。</think>Vite 是一款**现代化前端构建工具**,由 Vue.js 作者尤雨溪主导开发,专注于提升开发体验和构建效率。以下是其核心特性和使用场景的详细介绍: --- ### **一、核心优势** 1. **闪电般的启动速度** - 利用浏览器原生支持的 **ES Modules**(ESM),开发服务器无需打包整个项目,实现**按需编译**。 - 冷启动时间通常**秒级完成**,尤其适合大型项目。 2. **即时热更新(HMR)** - 模块热替换速度极快,修改代码后几乎**无感知刷新**,保持应用状态不丢失。 3. **开箱即用的功能** - 内置对 **TypeScript、CSS 预处理器(Sass/Less)、PostCSS** 的支持。 - 默认支持 **JSX、Vue、React、Svelte** 等多种框架。 4. **高效的生产构建** - 生产环境使用 **Rollup** 进行打包,自动优化代码(如 Tree-shaking、代码分割)。 --- ### **二、适用场景** - **单页应用(SPA)**:快速原型开发或企业级项目。 - **库开发**:轻量级构建,生成多种格式的模块(ESM、CommonJS)。 - **多框架支持**:Vue、React、Lit 等均可无缝集成。 --- ### **三、基础使用** 1. **创建项目** ```bash # 使用 npm npm create vite@latest # 选择框架(如 Vue/React)和模板 ``` 2. **目录结构** ``` my-project/ ├─ src/ │ ├─ main.js # 入口文件 ├─ public/ # 静态资源 ├─ vite.config.js # 配置文件 ``` 3. **常用命令** ```bash npm run dev # 启动开发服务器 npm run build # 构建生产版本 npm run preview # 本地预览生产构建 ``` --- ### **四、配置示例(`vite.config.js`)** ```javascript import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' export default defineConfig({ plugins: [vue()], server: { port: 3000, // 自定义端口 open: true // 自动打开浏览器 }, build: { minify: 'terser' // 代码压缩方式 } }) ``` --- ### **五、与传统工具(如 Webpack)对比** | **特性** | Vite | Webpack | |----------------|--------------------------|-----------------------| | **启动速度** | 极快(按需编译) | 较慢(全量打包) | | **HMR 速度** | 毫秒级 | 依赖项目规模 | | **配置复杂度** | 简单,约定大于配置 | 复杂,需手动配置 | | **生产构建** | Rollup(高效但插件较少) | 自研生态(插件丰富) | --- ### **六、生态系统** - **官方插件**:支持 Vue、React、Lit 等框架。 - **社区插件**:如 `vite-plugin-pwa`(PWA 支持)、`unplugin`(统一插件格式)。 --- 如果你需要进一步探索,可以参考 [Vite 官方文档](https://vitejs.dev/),其设计哲学是**“为开发者提供更愉悦的体验”** 🚀。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值