• vite vite.config.js中的配置


    vite打包依赖于 rollup和esbuild

    rollup中文文档

    esbulid中文文档

    基本配置

    1. import { defineConfig, loadEnv } from "vite";
    2. import vue from "@vitejs/plugin-vue";
    3. import path from "path";
    4. import Components from "unplugin-vue-components/vite";
    5. import AutoImport from "unplugin-auto-import/vite";
    6. import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
    7. //告诉 Vite 打包后的产物哪些 dependencies 需要在页面入口 html 文件中随 public 目录(或 CDN)引入
    8. import htmlConfig from "vite-plugin-html-config";
    9. //告诉vite什么 dependencies 不参与打包
    10. import { viteExternalsPlugin } from "vite-plugin-externals";
    11. // https://vitejs.dev/config/
    12. import { fileURLToPath } from "url";
    13. const filename = fileURLToPath(
    14. import.meta.url);
    15. // 跟目录
    16. const _dirname = path.dirname(filename);
    17. export default ({ mode }) => {
    18. // console.log(loadEnv(mode, process.cwd()).VITE_APP_BASE_API, "api");
    19. return defineConfig({
    20. define: {
    21. "process.env": process.env
    22. },
    23. // 插件配置
    24. plugins: [
    25. vue(),
    26. AutoImport({
    27. resolvers: [
    28. ElementPlusResolver({
    29. importStyle: true,
    30. }),
    31. ],
    32. }),
    33. Components({
    34. resolvers: [
    35. ElementPlusResolver({
    36. importStyle: true,
    37. }),
    38. ],
    39. }),
    40. // 这里表示 xxxx不参与打包
    41. viteExternalsPlugin({
    42. xxxx: "xxxx",
    43. }),
    44. // 引入本地库 xxxx一般放在 public下
    45. htmlConfig({
    46. headScripts: [{
    47. src: "/xxxx/xxxx.js",
    48. },
    49. ],
    50. links: [{
    51. rel: "stylesheet",
    52. href: "/xxxx/xxxx.css",
    53. }, ],
    54. }),
    55. ],
    56. base: "./",
    57. publicDir: "public",
    58. productionSourceMap: false, // 生产环境是否生成 sourceMap 文件
    59. //设置的别名
    60. resolve: {
    61. // 如果报错__dirname找不到,需要安装node,
    62. // 执行npm i @types/node --save-dev
    63. alias: {
    64. "@": path.resolve(_dirname, "./src"),
    65. "@assets": path.resolve(_dirname, "./src/assets"),
    66. "@utils": path.resolve(_dirname, "./src/utils"),
    67. "@components": path.resolve(_dirname, "./src/components"),
    68. },
    69. },
    70. // 服务配置
    71. server: {
    72. port: 3002, // 端口号
    73. open: true, // 自动在浏览器打开
    74. host: "0.0.0.0",
    75. https: false, // 是否开启 https,
    76. proxy: {
    77. "/api": {
    78. target: loadEnv(mode, process.cwd()).VITE_APP_BASE_API,
    79. changeOrigin: true,
    80. rewrite: (path) => path.replace(/^\/api/, ""),
    81. },
    82. },
    83. },
    84. build: {
    85. minify: false,
    86. // 进行压缩计算
    87. brotliSize: false,
    88. //指定输出路径
    89. assetsDir: "./",
    90. // 指定输出文件路径
    91. outDir: "dist",
    92. // 代码压缩配置
    93. terserOptions: {
    94. // 生产环境移除console
    95. compress: {
    96. drop_console: true,
    97. drop_debugger: true,
    98. },
    99. },
    100. // chunkSizeWarningLimit: 1500,大文件报警阈值设置,不建议使用
    101. rollupOptions: {
    102. output: {
    103. //静态资源分类打包
    104. chunkFileNames: "static/js/[name]-[hash].js",
    105. entryFileNames: "static/js/[name]-[hash].js",
    106. // assetFileNames: "static/[ext]/[name]-[hash].[ext]",
    107. assetFileNames(assetInfo) {
    108. // 判断后缀分别放到不用的文件夹中
    109. if (assetInfo.name.endsWith('.css')) {
    110. return "static/css/[name]-[hash].[ext]"
    111. }
    112. if (["png", "jpg", "svg", "PNG"].some(ext => assetInfo.name.endsWith(ext))) {
    113. return "static/img/[name]-[hash].[ext]"
    114. }
    115. if (["ttf", "woff", "woff2"].some(ext => assetInfo.name.endsWith(ext))) {
    116. return "static/fonts/[name]-[hash].[ext]"
    117. }
    118. return "static/css/[name]-[hash].[ext]"
    119. },
    120. manualChunks(id) {
    121. //静态资源分拆打包
    122. if (id.includes("node_modules")) {
    123. return id
    124. .toString()
    125. .split("node_modules/")[1]
    126. .split("/")[0]
    127. .toString();
    128. }
    129. },
    130. },
    131. },
    132. },
    133. });
    134. };

     组件自动引入

    1. import Components from "unplugin-vue-components/vite";
    2. import AutoImport from "unplugin-auto-import/vite";
    3. // 使用
    4. plugins: [
    5. AutoImport({
    6. resolvers: [
    7. ElementPlusResolver({
    8. importStyle: true,
    9. }),
    10. ],
    11. }),
    12. Components({
    13. resolvers: [
    14. ElementPlusResolver({
    15. importStyle: true,
    16. }),
    17. ],
    18. })
    19. ]

    固化版本,cdn引入和不进行打包的插件

    这个插件是告诉 vite,在构建时,告诉 rollup 不要对 elementPlus这个包进行打包,而是在 index.html 中定义一个全局对象 elementPlus,定义到 window 上。

    1. import { viteExternalsPlugin } from "vite-plugin-externals";
    2. // 使用
    3. plugins: [
    4. viteExternalsPlugin({
    5. elementPlus: "elementPlus",
    6. })
    7. ]

    vite-plugin-html-config 这个插件可以在开发时(dev script)和构建时(build script)修改 index.html,注入一些