vite.config.ts 2.8 KB

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