vite.config.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import { createHtmlPlugin } from 'vite-plugin-html';
  4. import { resolve } from 'path'
  5. import vueSetupExtend from 'unplugin-vue-setup-extend-plus/vite'
  6. import path from 'path';
  7. import fs from 'fs';
  8. try {
  9. const vue_bundler_file = path.resolve(
  10. __dirname,
  11. "node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js"
  12. );
  13. //使用同步读取文件
  14. let data = fs.readFileSync(vue_bundler_file, "utf8");
  15. //如果未添加过
  16. if (data.indexOf("//__v_cache") < 0) {
  17. console.log("正在修改源码文件:", vue_bundler_file);
  18. //先找到__v_cache变量的位置
  19. let index = data.indexOf("__v_cache");
  20. if (index >= 0) {
  21. // 继续往前找if关键字
  22. index = data.lastIndexOf("if ", index);
  23. if (index >= 0) {
  24. //从上一个位置开始
  25. index -= 1;
  26. //然后放一个注释
  27. const comment = " //__v_cache ";
  28. //然后拼接
  29. data = data.substring(0, index) + comment + data.substring(index);
  30. //继续往后找下一个大括号 }
  31. index = data.indexOf("}", index);
  32. if (index >= 0) {
  33. //从上一个位置开始
  34. index -= 1;
  35. //然后拼接
  36. data = data.substring(0, index) + comment + data.substring(index);
  37. }
  38. fs.writeFileSync(vue_bundler_file, data, "utf8");
  39. }
  40. }
  41. }
  42. } catch (er) {
  43. console.error(er.message);
  44. }
  45. // https://vitejs.dev/config/
  46. export default defineConfig({
  47. base: "./",
  48. plugins: [
  49. vue(),
  50. createHtmlPlugin({
  51. minify: true
  52. }),
  53. vueSetupExtend({
  54. })
  55. ],
  56. server: {
  57. host: '0.0.0.0', //解决“vite use `--host` to expose”
  58. port: 8080,
  59. open: true,
  60. hmr: true
  61. },
  62. resolve: {
  63. preserveSymlinks: true,
  64. alias: [
  65. {
  66. find: '@',
  67. replacement: resolve(__dirname, 'src')
  68. }
  69. ]
  70. },
  71. css: {
  72. preprocessorOptions: {
  73. scss: {
  74. additionalData: '@import "./src/style.scss";', //导入全局样式
  75. javascriptEnabled: true
  76. }
  77. }
  78. },
  79. build: {
  80. minify: "terser",
  81. terserOptions: {
  82. compress: {
  83. //生产环境时移除console
  84. drop_console: true
  85. }
  86. }
  87. },
  88. esbuild: {
  89. jsxFactory: 'h',
  90. jsxFragment: 'Fragment',
  91. jsxInject: "import { h } from 'vue';"
  92. }
  93. })