vite.config.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // https://vitejs.dev/config/
  9. export default defineConfig({
  10. // assetsInclude: ['**/*.glb'],
  11. plugins: [
  12. vue(),
  13. vueJsx(),
  14. fileList(),
  15. monacoEditorPlugin({
  16. customDistPath: () => {
  17. return 'v/monacoeditorwork';
  18. },
  19. }),
  20. ],
  21. resolve: {
  22. alias: {
  23. '@': path.resolve(__dirname, './src/'),
  24. '@meta2d': path.resolve(__dirname, '../meta2d.js/packages'),
  25. '@2d-components': path.resolve(__dirname, '../2d-components/packages'),
  26. // '@meta3d': path.resolve(__dirname, '../meta3d.js'),
  27. },
  28. },
  29. build: {
  30. outDir: 'v',
  31. rollupOptions: {
  32. output: {
  33. manualChunks: {
  34. monaco: [`monaco-editor`],
  35. },
  36. },
  37. },
  38. },
  39. server: {
  40. proxy: {
  41. '/image': 'https://v.le5le.com/',
  42. '/file': 'https://v.le5le.com/',
  43. '/api': 'https://v.le5le.com/',
  44. },
  45. },
  46. });
  47. function fileList(): Plugin {
  48. return {
  49. name: 'vite-plugin-svg-png-files',
  50. configureServer(server: ViteDevServer) {
  51. server.middlewares.use((req, res, next) => {
  52. const url = req.url as string;
  53. if (
  54. (url.startsWith('/svg/') ||
  55. url.startsWith('/png/') ||
  56. url.startsWith('/material/')) &&
  57. url.endsWith('/')
  58. ) {
  59. const pwd = decodeURI(path.join(__dirname, 'public', url));
  60. const files = fs.readdirSync(pwd, {
  61. withFileTypes: true,
  62. });
  63. const list: {
  64. name: string;
  65. type?: string;
  66. }[] = [];
  67. for (const item of files) {
  68. if (item.isDirectory()) {
  69. list.push({ name: item.name, type: 'directory' });
  70. } else {
  71. list.push({ name: item.name });
  72. }
  73. }
  74. res.end(JSON.stringify(list));
  75. } else if (url === '/img' && req.method === 'POST') {
  76. const form = formidable({
  77. uploadDir: decodeURI(path.join(__dirname, 'public', '/img')),
  78. keepExtensions: true,
  79. });
  80. form.parse(req, (err, fields, files) => {
  81. if (!err) {
  82. res.end(
  83. JSON.stringify({ url: '/img/' + files.file.newFilename })
  84. );
  85. }
  86. });
  87. } else {
  88. next();
  89. }
  90. });
  91. },
  92. };
  93. }