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