vite.config.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { defineConfig, loadEnv, ConfigEnv, UserConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import vueJsx from '@vitejs/plugin-vue-jsx'
  4. import svgLoader from 'vite-svg-loader'
  5. // import eslintPlugin from 'vite-plugin-eslint'
  6. import { viteCommonjs } from '@originjs/vite-plugin-commonjs'
  7. import { createHtmlPlugin } from 'vite-plugin-html'
  8. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  9. import vueSetupExtend from 'unplugin-vue-setup-extend-plus/vite'
  10. import path from 'path'
  11. import pkg from './package.json'
  12. import dayjs from 'dayjs'
  13. import { wrapperEnv } from './build/getEnv'
  14. const isProduction = process.env.NODE_ENV === 'production'
  15. const { dependencies, devDependencies, name, version } = pkg
  16. const __APP_INFO__ = {
  17. pkg: { dependencies, devDependencies, name, version },
  18. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  19. }
  20. const examplePlugin = () => {
  21. let config
  22. return {
  23. name: 'custom-vuedraggableAndSortable',
  24. transform(code, id) {
  25. /*eslint-disable*/
  26. if (isProduction) {
  27. if (/vuedraggable\.js/.test(id)) {
  28. return code.replace('this._sortable = new Sortable(targetDomElement, sortableOptions);', (...e) => {
  29. return `Sortable.mount($attrs.plugins || []);
  30. ${e[0]}`
  31. })
  32. }
  33. if (/sortablejs/.test(id)) {
  34. return code.replace(` plugins.forEach(function (p) {
  35. if (p.pluginName === plugin.pluginName) {
  36. throw "Sortable: Cannot mount plugin ".concat(plugin.pluginName, " more than once");
  37. }
  38. });
  39. plugins.push(plugin);`, (...e) => {
  40. return `if (!plugins.filter(e => e.pluginName === plugin.pluginName).length) {
  41. window.plugins = plugins;
  42. plugins.push(plugin);}`
  43. })
  44. }
  45. } else {
  46. if (/vuedraggable/.test(id)) {
  47. let result = code.replace('this._sortable = new external_commonjs_sortablejs_commonjs2_sortablejs_amd_sortablejs_root_Sortable_default.a(targetDomElement, sortableOptions);', (...e) => {
  48. return `external_commonjs_sortablejs_commonjs2_sortablejs_amd_sortablejs_root_Sortable_default.a.mount($attrs.plugins || []);
  49. ${e[0]}`
  50. })
  51. result = result.replace(`plugins.forEach(function(p) {
  52. if (p.pluginName === plugin.pluginName) {
  53. throw "Sortable: Cannot mount plugin ".concat(plugin.pluginName, " more than once");
  54. }
  55. });
  56. plugins.push(plugin);`, (...e) => {
  57. return `if (!plugins.filter(e => e.pluginName === plugin.pluginName).length) {
  58. window.plugins = plugins;
  59. plugins.push(plugin);}`
  60. })
  61. return result
  62. }
  63. }
  64. /*eslint-disable*/
  65. }
  66. }
  67. }
  68. // https://vitejs.dev/config/
  69. export default defineConfig(({ mode /*command,*/ }: ConfigEnv): UserConfig => {
  70. // if (command === 'serve') {
  71. // return {
  72. // // serve 独有配置
  73. // }
  74. // } else {
  75. // return {
  76. // // build 独有配置
  77. // }
  78. // }
  79. const process_cwd = process.cwd()
  80. // 获取 .env 环境配置文件
  81. const env = loadEnv(mode, process_cwd)
  82. const viteEnv = wrapperEnv(env)
  83. return {
  84. base: viteEnv.VITE_PUBLIC_PATH,
  85. resolve: {
  86. // Vite路径别名配置
  87. alias: {
  88. '@': path.resolve(__dirname, './src'),
  89. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
  90. '@ER': path.resolve(__dirname, './src/components/packages'),
  91. vuedraggable: isProduction ? 'vuedraggable/src/vuedraggable' : 'vuedraggable'
  92. },
  93. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.vue', '.json'] // 默认 ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json']
  94. },
  95. define: {
  96. __APP_INFO__: JSON.stringify(__APP_INFO__)
  97. },
  98. css: {
  99. devSourcemap: true,
  100. preprocessorOptions: {
  101. scss: {
  102. additionalData: `@import "@/styles/variables.scss";`
  103. }
  104. }
  105. },
  106. server: {
  107. host: '0.0.0.0',
  108. port: viteEnv.VITE_PORT,
  109. open: viteEnv.VITE_OPEN,
  110. cors: true,
  111. // Load proxy configuration from .env.development
  112. proxy: {
  113. '/sssss': {
  114. target: 'https://api.everright.site', // 目标接口的域名
  115. changeOrigin: true, // 是否改变源地址
  116. rewrite: (path) => path.replace(/^\/sssss/, ''), // 重写路径
  117. },
  118. },
  119. },
  120. plugins: [
  121. examplePlugin(),
  122. // vue支持
  123. vue(),
  124. // JSX支持
  125. vueJsx({
  126. // 默认支持 .[jt]sx .vue 和 .js 需要支持 jsx 写法 需要 配置
  127. include: /\.[jt]sx$/ // |\.vue$
  128. }),
  129. // // esLint 报错信息显示在浏览器界面上
  130. // eslintPlugin(),
  131. // name 可以写在 script 标签上
  132. vueSetupExtend({}),
  133. // 注入变量到 html 文件
  134. createHtmlPlugin({
  135. inject: {
  136. data: { title: viteEnv.VITE_APP_TITLE }
  137. }
  138. }),
  139. // 使用 svg 图标
  140. createSvgIconsPlugin({
  141. // 指定需要缓存的图标文件夹
  142. iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
  143. // 指定symbolId格式
  144. symbolId: 'icon-[dir]-[name]'
  145. }),
  146. svgLoader()
  147. // viteCommonjs()
  148. ]
  149. }
  150. })