vite.config.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // const { fileURLToPath, URL } = require('node:url');
  2. const { defineConfig } = require('vite');
  3. // const vue = require('@vitejs/plugin-vue');
  4. const vue = require('@vitejs/plugin-vue');
  5. // const vueDevTools = require('vite-plugin-vue-devtools');
  6. // const { terser } = require('rollup-plugin-terser');
  7. // const commonjs = require('@rollup/plugin-commonjs');
  8. // const {resolve, join} = require("node:path");
  9. const path = require("node:path");
  10. const fs = require("node:fs");
  11. // const viteCompression = require("vite-plugin-compression");
  12. const {visualizer} = require("rollup-plugin-visualizer");
  13. import vitePluginStyleVwLoader from "vite-plugin-style-vw-loader";
  14. // const __PROD__ = process.env.NODE_ENV === 'production';
  15. import postcsspxtoviewport from 'postcss-px-to-viewport-8-plugin'
  16. import {
  17. publicPath,
  18. REGEX_IMG,
  19. REGEX_FONT,
  20. DIR_ASSET,
  21. DIR_IMG,
  22. DIR_FONTS,
  23. } from './build-util';
  24. module.exports = defineConfig({
  25. // root: './public',
  26. // base: publicPath || '/tparking/',
  27. base: '/tparking/',
  28. css: {
  29. preprocessorOptions: {
  30. less: {
  31. additionalData: `@import "@/kui/theme/theme.less"; @import "@/styles/uni.less"; @import "@/styles/common.less";`,
  32. },
  33. scss: {
  34. additionalData: `$injectedColor: orange;` // 如果你需要全局注入一些 SCSS 变量
  35. }
  36. },
  37. },
  38. plugins: [
  39. vitePluginStyleVwLoader({
  40. unitToConvert: "px", // The unit to be converted is "px" by default.
  41. viewportWidth: 750, // The viewport width of the design draft, such as the incoming function, whose parameter is the file path currently processed.
  42. unitPrecision: 5, // Precision retained after unit conversion.
  43. viewportUnit: "vw", // Viewport units you want to use.
  44. fontViewportUnit: "vw", // Viewport units used by fonts.
  45. minPixelValue: 1, // Set the minimum conversion value. If it is 1, only values greater than 1 will be converted.
  46. }),
  47. vue(),
  48. // 开启gzip
  49. // viteCompression(),
  50. visualizer({ open: process.env.NODE_ENV === 'development' }), // 自动开启分析页面
  51. ],
  52. build: {
  53. // sourcemap: false, // 禁用源映射
  54. sourcemap: process.env.NODE_ENV === 'development',
  55. assetsDir: 'static',
  56. assetsInlineLimit: 4096, // 小于4kb的图片会被转为base64,内联在代码中
  57. rollupOptions: {
  58. output: {
  59. chunkFileNames: 'static/js/[name]-[hash].js',
  60. entryFileNames: 'static/js/[name]-[hash].js',
  61. assetFileNames: ({name}) => {
  62. if (name === undefined || name === null) {
  63. name = '';
  64. }
  65. if (REGEX_IMG.test(name)) {
  66. return `${DIR_ASSET}/${DIR_IMG}/[name].[hash][extname]`;
  67. }
  68. if (/\.css$/.test(name)) {
  69. return DIR_ASSET + '/css/[name].[hash][extname]';
  70. }
  71. if (REGEX_FONT.test(name)) {
  72. return `${DIR_ASSET}/${DIR_FONTS}/[name].[hash][extname]`;
  73. }
  74. // 其他文件保持默认的输出路径
  75. return `${DIR_ASSET}/[name]-[hash][extname]`;
  76. }
  77. }
  78. },
  79. // 配置生产环境中的资源引用路径
  80. base: publicPath || '/tparking/'
  81. },
  82. experimental: {
  83. // https://vitejs.cn/vite3-cn/config/shared-options.html#base 控制 html 所在位置
  84. // https://vitejs.cn/vite3-cn/guide/build.html#advanced-base-options 控制 assets 和 public 放在哪里
  85. renderBuiltUrl(filename, options) {
  86. /**
  87. * @param {string} filename
  88. * @param {Object} options
  89. * @param {string} options.hostId
  90. * @param {'js' | 'css' | 'html'} options.hostType
  91. * @param {'public' | 'asset'} options.type
  92. */
  93. const { hostId, hostType, type } = options;
  94. return publicPath + '/' + filename;
  95. // return 'https://aliyun.dc1979.com/img/h5/' + filename
  96. },
  97. },
  98. resolve: {
  99. alias: {
  100. '@': path.resolve(__dirname, './src')
  101. }
  102. },
  103. server: {
  104. port: 8080,
  105. host: '127.0.0.1',
  106. strictPort: true,
  107. watch: {
  108. // 3. tell vite to ignore watching `src-tauri`
  109. ignored: ["**/src-tauri/**", "**/node_modules/**"],
  110. },
  111. },
  112. });