vite.config.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. template: {
  49. compilerOptions: {
  50. isCustomElement: tag => tag === 'wx-open-launch-weapp'
  51. }
  52. }
  53. }),
  54. // 开启gzip
  55. // viteCompression(),
  56. visualizer({ open: process.env.NODE_ENV === 'development' }), // 自动开启分析页面
  57. ],
  58. build: {
  59. // sourcemap: false, // 禁用源映射
  60. sourcemap: process.env.NODE_ENV === 'development',
  61. assetsDir: 'static',
  62. assetsInlineLimit: 4096, // 小于4kb的图片会被转为base64,内联在代码中
  63. rollupOptions: {
  64. output: {
  65. chunkFileNames: 'static/js/[name]-[hash].js',
  66. entryFileNames: 'static/js/[name]-[hash].js',
  67. assetFileNames: ({name}) => {
  68. if (name === undefined || name === null) {
  69. name = '';
  70. }
  71. if (REGEX_IMG.test(name)) {
  72. return `${DIR_ASSET}/${DIR_IMG}/[name].[hash][extname]`;
  73. }
  74. if (/\.css$/.test(name)) {
  75. return DIR_ASSET + '/css/[name].[hash][extname]';
  76. }
  77. if (REGEX_FONT.test(name)) {
  78. return `${DIR_ASSET}/${DIR_FONTS}/[name].[hash][extname]`;
  79. }
  80. // 其他文件保持默认的输出路径
  81. return `${DIR_ASSET}/[name]-[hash][extname]`;
  82. }
  83. }
  84. },
  85. // 配置生产环境中的资源引用路径
  86. base: publicPath || '/tparking/'
  87. },
  88. experimental: {
  89. // https://vitejs.cn/vite3-cn/config/shared-options.html#base 控制 html 所在位置
  90. // https://vitejs.cn/vite3-cn/guide/build.html#advanced-base-options 控制 assets 和 public 放在哪里
  91. renderBuiltUrl(filename, options) {
  92. /**
  93. * @param {string} filename
  94. * @param {Object} options
  95. * @param {string} options.hostId
  96. * @param {'js' | 'css' | 'html'} options.hostType
  97. * @param {'public' | 'asset'} options.type
  98. */
  99. const { hostId, hostType, type } = options;
  100. return publicPath + '/' + filename;
  101. // return 'https://aliyun.dc1979.com/img/h5/' + filename
  102. },
  103. },
  104. resolve: {
  105. alias: {
  106. '@': path.resolve(__dirname, './src')
  107. }
  108. },
  109. server: {
  110. port: 8080,
  111. host: '127.0.0.1',
  112. strictPort: true,
  113. watch: {
  114. // 3. tell vite to ignore watching `src-tauri`
  115. ignored: ["**/src-tauri/**", "**/node_modules/**"],
  116. },
  117. },
  118. });