vite.config.ts 3.3 KB

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