vite.config.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. '/v/material': 'https://assets.le5lecdn.com/',
  45. },
  46. },
  47. });
  48. function fileList(): Plugin {
  49. return {
  50. name: 'vite-plugin-svg-png-files',
  51. configureServer(server: ViteDevServer) {
  52. server.middlewares.use((req, res, next) => {
  53. const url = req.url as string;
  54. if (
  55. (url.startsWith('/svg/') ||
  56. url.startsWith('/png/') ||
  57. url.startsWith('/material/')) &&
  58. url.endsWith('/')
  59. ) {
  60. const pwd = decodeURI(path.join(__dirname, 'public', url));
  61. const files = fs.readdirSync(pwd, {
  62. withFileTypes: true,
  63. });
  64. const list: {
  65. name: string;
  66. type?: string;
  67. }[] = [];
  68. for (const item of files) {
  69. if (item.isDirectory()) {
  70. list.push({ name: item.name, type: 'directory' });
  71. } else {
  72. list.push({ name: item.name });
  73. }
  74. }
  75. res.end(JSON.stringify(list));
  76. } else if (url === '/img' && req.method === 'POST') {
  77. const form = formidable({
  78. uploadDir: decodeURI(path.join(__dirname, 'public', '/img')),
  79. keepExtensions: true,
  80. });
  81. form.parse(req, (err, fields, files) => {
  82. if (!err) {
  83. res.end(
  84. JSON.stringify({ url: '/img/' + files.file.newFilename })
  85. );
  86. }
  87. });
  88. } else {
  89. next();
  90. }
  91. });
  92. },
  93. };
  94. }