vite.config.ts 2.9 KB

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