vite.config.ts 2.9 KB

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