vite.config.ts 3.1 KB

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