vite.config.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // "visio2meta2d": path.resolve(__dirname, '../visio2meta2d'),
  33. // 'dxf': path.resolve(__dirname, '../dxf'),
  34. // '@meta3d': path.resolve(__dirname, '../meta3d.js'),
  35. // "@le5le/auth-token":path.resolve(__dirname, '../auth-token'),
  36. },
  37. },
  38. build: {
  39. outDir: 'v',
  40. rollupOptions: {
  41. output: {
  42. manualChunks: {
  43. monaco: [`monaco-editor`],
  44. },
  45. },
  46. },
  47. },
  48. server: {
  49. proxy: {
  50. // '/image': 'https://v.le5le.com/',
  51. // '/file': 'https://v.le5le.com/',
  52. // '/api': 'http://192.168.110.141:7000/',
  53. // '/file': 'http://192.168.110.141:7000/',
  54. // '/api': 'https://v.le5le.com/',
  55. // '/v/material': 'https://v.le5le.com/',
  56. // '/png': 'https://v.le5le.com/',
  57. // '/svg': 'https://v.le5le.com/',
  58. // // '/view': 'https://v.le5le.com/',
  59. // '/api/tools':'https://v.le5le.com/'
  60. },
  61. },
  62. });
  63. function fileList(): Plugin {
  64. return {
  65. name: 'vite-plugin-svg-png-files',
  66. configureServer(server: ViteDevServer) {
  67. server.middlewares.use((req, res, next) => {
  68. const url = req.url as string;
  69. if (
  70. (url.startsWith('/svg/') ||
  71. url.startsWith('/png/') ||
  72. url.startsWith('/material/')) &&
  73. url.endsWith('/')
  74. ) {
  75. const pwd = decodeURI(path.join(__dirname, 'public', url));
  76. const files = fs.readdirSync(pwd, {
  77. withFileTypes: true,
  78. });
  79. const list: {
  80. name: string;
  81. type?: string;
  82. }[] = [];
  83. for (const item of files) {
  84. if (item.isDirectory()) {
  85. list.push({ name: item.name, type: 'directory' });
  86. } else {
  87. list.push({ name: item.name });
  88. }
  89. }
  90. res.end(JSON.stringify(list));
  91. } else if (url === '/img' && req.method === 'POST') {
  92. const form = formidable({
  93. uploadDir: decodeURI(path.join(__dirname, 'public', '/img')),
  94. keepExtensions: true,
  95. });
  96. form.parse(req, (err, fields, files) => {
  97. if (!err) {
  98. res.end(
  99. JSON.stringify({ url: '/img/' + files.file.newFilename })
  100. );
  101. }
  102. });
  103. } else {
  104. next();
  105. }
  106. });
  107. },
  108. };
  109. }