vite.config.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { defineConfig, Plugin, ViteDevServer } from 'vite';
  2. import vue from '@vitejs/plugin-vue';
  3. import vueJsx from '@vitejs/plugin-vue-jsx';
  4. import * as path from 'path';
  5. import * as fs from 'fs';
  6. import formidable from 'formidable';
  7. import monacoEditorPlugin from 'vite-plugin-monaco-editor';
  8. import viteCompression from "vite-plugin-compression"
  9. // https://vitejs.dev/config/
  10. export default defineConfig({
  11. // assetsInclude: ['**/*.glb'],
  12. plugins: [
  13. vue(),
  14. vueJsx(),
  15. // fileList(),
  16. monacoEditorPlugin({
  17. customDistPath: () => {
  18. return 'v/monacoeditorwork';
  19. },
  20. // publicPath: 'https://assets.le5lecdn.com/v/monacoeditorwork',
  21. }),
  22. viteCompression({
  23. threshold: 10240, // the unit is Bytes
  24. }),
  25. ],
  26. resolve: {
  27. alias: {
  28. '@': path.resolve(__dirname, './src/'),
  29. '@meta2d/core': path.resolve(__dirname ,'node_modules/@meta2d/core'),
  30. // '@meta2d': path.resolve(__dirname, '../meta2d.js/packages'),
  31. '@2d-components': path.resolve(__dirname, '../2d-components/packages'),
  32. 'dayjs': path.resolve(__dirname ,'node_modules/dayjs'),
  33. // "visio2meta2d": path.resolve(__dirname, '../visio2meta2d'),
  34. // 'dxf': path.resolve(__dirname, '../dxf'),
  35. // '@meta3d': path.resolve(__dirname, '../meta3d.js'),
  36. // "@le5le/auth-token":path.resolve(__dirname, '../auth-token'),
  37. },
  38. },
  39. build: {
  40. outDir: 'v',
  41. rollupOptions: {
  42. output: {
  43. manualChunks: {
  44. monaco: [`monaco-editor`],
  45. },
  46. },
  47. },
  48. },
  49. server: {
  50. proxy: {
  51. // '/image': 'https://v.le5le.com/',
  52. // '/file': 'https://v.le5le.com/',
  53. // '/api': 'http://192.168.110.141:7000/',
  54. // '/file': 'http://192.168.110.141:7000/',
  55. // '/api': 'https://v.le5le.com/',
  56. // '/v/material': 'https://v.le5le.com/',
  57. // '/png': 'https://v.le5le.com/',
  58. // '/svg': 'https://v.le5le.com/',
  59. // // '/view': 'https://v.le5le.com/',
  60. // '/api/tools':'https://v.le5le.com/'
  61. },
  62. },
  63. });
  64. function fileList(): Plugin {
  65. return {
  66. name: 'vite-plugin-svg-png-files',
  67. configureServer(server: ViteDevServer) {
  68. server.middlewares.use((req, res, next) => {
  69. const url = req.url as string;
  70. if (
  71. (url.startsWith('/svg/') ||
  72. url.startsWith('/png/') ||
  73. url.startsWith('/material/')) &&
  74. url.endsWith('/')
  75. ) {
  76. const pwd = decodeURI(path.join(__dirname, 'public', url));
  77. const files = fs.readdirSync(pwd, {
  78. withFileTypes: true,
  79. });
  80. const list: {
  81. name: string;
  82. type?: string;
  83. }[] = [];
  84. for (const item of files) {
  85. if (item.isDirectory()) {
  86. list.push({ name: item.name, type: 'directory' });
  87. } else {
  88. list.push({ name: item.name });
  89. }
  90. }
  91. res.end(JSON.stringify(list));
  92. } else if (url === '/img' && req.method === 'POST') {
  93. const form = formidable({
  94. uploadDir: decodeURI(path.join(__dirname, 'public', '/img')),
  95. keepExtensions: true,
  96. });
  97. form.parse(req, (err, fields, files) => {
  98. if (!err) {
  99. res.end(
  100. JSON.stringify({ url: '/img/' + files.file.newFilename })
  101. );
  102. }
  103. });
  104. } else {
  105. next();
  106. }
  107. });
  108. },
  109. };
  110. }