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