vite.config.ts 3.0 KB

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