vite.config.ts 4.4 KB

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