经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » TypeScript » 查看文章
Vite4+Typescript+Vue3+Pinia 从零搭建(3) - vite配置
来源:cnblogs  作者:唯之为之  时间:2023/11/15 9:23:09  对本文有异议

项目代码同步至码云 weiz-vue3-template
关于vite的详细配置可查看 vite官方文档,本文简单介绍vite的常用配置。

初始内容

项目初建后,vite.config.ts 的默认内容如下:

  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. // https://vitejs.dev/config/
  4. export default defineConfig({
  5. plugins: [vue()],
  6. })

配置别名

1. 安装 @types/node

  1. npm i @types/node -D

image

2. 修改 vite.config.ts

  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import { resolve } from 'path'
  4. // 路径查找
  5. const pathResolve = (dir: string): string => {
  6. return resolve(__dirname, ".", dir);
  7. };
  8. // 设置别名,还可以添加其他路径
  9. const alias: Record<string, string> = {
  10. "@": pathResolve("src"),
  11. "@views": pathResolve("src/views"),
  12. "@store": pathResolve("src/store")
  13. };
  14. // https://vitejs.dev/config/
  15. export default defineConfig({
  16. plugins: [vue()],
  17. resolve: {
  18. alias
  19. },
  20. })

3. 使用

比如,修改 App.vue:

  1. import HelloWorld from '@/components/HelloWorld.vue'

image

配置环境变量

1. 新建env文件

根目录下新建 .env.env.development.env.production 三个文件。
.env 文件内新增内容:

  1. # 本地运行端口号
  2. VITE_PORT = 8686

.env.development 文件内新增内容:

  1. # 本地环境
  2. VITE_USER_NODE_ENV = development
  3. # 公共基础路径
  4. VITE_PUBLIC_PATH = /

.env.production 文件内新增内容:

  1. # 线上环境
  2. VITE_USER_NODE_ENV = production
  3. # 公共基础路径
  4. VITE_PUBLIC_PATH = /

2. 环境变量统一处理

根目录下新建 build 文件夹,其目录下新建 index.ts,内容如下:

  1. // 环境变量处理方法
  2. export function wrapperEnv(envConf: Recordable): ViteEnv {
  3. const ret: any = {};
  4. for (const envName of Object.keys(envConf)) {
  5. let realName = envConf[envName].replace(/\\n/g, "\n");
  6. realName = realName === "true" ? true : realName === "false" ? false : realName;
  7. if (envName === "VITE_PORT") realName = Number(realName);
  8. ret[envName] = realName;
  9. if (typeof realName === "string") {
  10. process.env[envName] = realName;
  11. } else if (typeof realName === "object") {
  12. process.env[envName] = JSON.stringify(realName);
  13. }
  14. }
  15. return ret;
  16. }

3. 环境类型定义

types\index.d.ts 文件里新增对 RecordableViteEnv 的类型定义:

  1. type Recordable<T = any> = Record<string, T>;
  2. interface ViteEnv {
  3. VITE_USER_NODE_ENV: "development" | "production";
  4. VITE_PUBLIC_PATH: string;
  5. VITE_PORT: number;
  6. }

修改 tsconfig.json 文件,将 build 文件夹内的文件包含进去:

  1. "include": [ // 需要检测的文件
  2. "src/**/*.ts",
  3. "build/*.ts",
  4. "src/**/*.d.ts",
  5. "src/**/*.tsx",
  6. "src/**/*.vue",
  7. "mock/*.ts",
  8. "types/*.d.ts",
  9. "vite.config.ts"
  10. ],

同理,修改 tsconfig.node.json 文件:

  1. "include": [
  2. "build/*.ts",
  3. "types/*.d.ts",
  4. "vite.config.ts"
  5. ]

4. 使用

修改 vite.config.ts

  1. import { defineConfig, loadEnv, ConfigEnv, UserConfig } from "vite"
  2. import vue from '@vitejs/plugin-vue'
  3. import { resolve } from 'path'
  4. import { wrapperEnv } from './build'
  5. // 路径查找
  6. const pathResolve = (dir: string): string => {
  7. return resolve(__dirname, ".", dir);
  8. };
  9. // 设置别名,还可以添加其他路径
  10. const alias: Record<string, string> = {
  11. "@": pathResolve("src"),
  12. "@views": pathResolve("src/views"),
  13. "@store": pathResolve("src/store")
  14. };
  15. // https://vitejs.dev/config/
  16. export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  17. const root = process.cwd()
  18. const env = loadEnv(mode, root)
  19. const viteEnv = wrapperEnv(env)
  20. return {
  21. base: viteEnv.VITE_PUBLIC_PATH,
  22. plugins: [vue()],
  23. resolve: {
  24. alias
  25. },
  26. server: {
  27. host: "0.0.0.0",
  28. port: viteEnv.VITE_PORT,
  29. https: false,
  30. open: true,
  31. // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
  32. proxy: {
  33. "^/api": {
  34. target: "http://192.168.1.4:8688",
  35. changeOrigin: true,
  36. rewrite: path => path.replace(/^\/api/, "")
  37. }
  38. }
  39. },
  40. }
  41. })

目录更新

当前项目目录如下:

  1. | .env
  2. | .env.development
  3. | .env.production
  4. | .gitignore
  5. | index.html
  6. | package-lock.json
  7. | package.json
  8. | README.md
  9. | tree.txt
  10. | tsconfig.json
  11. | tsconfig.node.json
  12. | vite.config.ts
  13. |
  14. +---.vscode
  15. | extensions.json
  16. |
  17. +---build
  18. | index.ts
  19. |
  20. +---node_modules
  21. +---public
  22. | vite.svg
  23. |
  24. +---src
  25. | | App.vue
  26. | | main.ts
  27. | | style.css
  28. | | vite-env.d.ts
  29. | |
  30. | +---assets
  31. | | vue.svg
  32. | |
  33. | \---components
  34. | HelloWorld.vue
  35. |
  36. \---types
  37. index.d.ts

原文链接:https://www.cnblogs.com/weizwz/p/17830692.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号