187 lines
5.1 KiB
JavaScript
187 lines
5.1 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import react from '@vitejs/plugin-react';
|
|
import {
|
|
defineConfig,
|
|
loadEnv,
|
|
normalizePath,
|
|
transformWithEsbuild,
|
|
} from 'vite';
|
|
import pkg from '@douyinfe/vite-plugin-semi';
|
|
|
|
const { vitePluginSemi } = pkg;
|
|
const projectRoot = path.dirname(fileURLToPath(import.meta.url));
|
|
const sourceRoot = `${normalizePath(path.resolve(projectRoot, 'src'))}/`;
|
|
|
|
const DEFAULT_PROXY_TARGET = 'http://localhost:3000';
|
|
const PROXY_PREFIXES = [
|
|
'/api',
|
|
'/v1',
|
|
'/v1beta',
|
|
'/mj',
|
|
'/pg',
|
|
'/dashboard',
|
|
'/kling',
|
|
'/jimeng',
|
|
'/suno',
|
|
];
|
|
|
|
function createProxy(target) {
|
|
return Object.fromEntries(
|
|
PROXY_PREFIXES.map((prefix) => [
|
|
prefix,
|
|
{
|
|
target,
|
|
changeOrigin: true,
|
|
...(prefix === '/v1' ? { ws: true } : {}),
|
|
},
|
|
]),
|
|
);
|
|
}
|
|
|
|
// Keep matching order: the first matching group owns the module.
|
|
const VENDOR_CHUNKS = [
|
|
['react-core', ['react/', 'react-dom/', 'react-router-dom/', 'scheduler/']],
|
|
['semi-icons', ['@douyinfe/semi-icons']],
|
|
['semi-ui', ['@douyinfe/semi-ui', '@douyinfe/semi-foundation']],
|
|
['vchart', ['@visactor/']],
|
|
['mermaid', ['mermaid/', 'dagre-d3', 'cytoscape', 'elkjs']],
|
|
['markdown', ['react-markdown/', 'remark-', 'rehype-', 'unified/', 'unist-']],
|
|
['katex', ['katex/']],
|
|
['highlight', ['highlight.js/']],
|
|
['lobe-icons', ['@lobehub/icons']],
|
|
['lucide-icons', ['lucide-react/']],
|
|
['i18n', ['i18next', 'react-i18next/']],
|
|
['utils', ['axios/', 'dayjs/', 'clsx/']],
|
|
[
|
|
'react-components',
|
|
[
|
|
'react-dropzone/',
|
|
'react-toastify/',
|
|
'react-telegram-login/',
|
|
'react-turnstile/',
|
|
],
|
|
],
|
|
['qrcode', ['qrcode']],
|
|
];
|
|
|
|
function manualChunks(id) {
|
|
const normalizedId = normalizePath(id);
|
|
return VENDOR_CHUNKS.find(([, packages]) =>
|
|
packages.some((name) => normalizedId.includes(`node_modules/${name}`)),
|
|
)?.[0];
|
|
}
|
|
|
|
function legacyJsxPlugin() {
|
|
return {
|
|
name: 'treat-js-files-as-jsx',
|
|
async transform(code, id) {
|
|
const file = normalizePath(id).split('?')[0];
|
|
if (!file.startsWith(sourceRoot) || !file.endsWith('.js')) return null;
|
|
|
|
return transformWithEsbuild(code, id, {
|
|
loader: 'jsx',
|
|
jsx: 'automatic',
|
|
});
|
|
},
|
|
};
|
|
}
|
|
|
|
function analyticsPlugin(env) {
|
|
return {
|
|
name: 'inject-analytics',
|
|
transformIndexHtml(html) {
|
|
let result = html;
|
|
const umamiId = env.VITE_UMAMI_WEBSITE_ID;
|
|
const umamiUrl =
|
|
env.VITE_UMAMI_SCRIPT_URL || 'https://analytics.umami.is/script.js';
|
|
if (umamiId) {
|
|
result = result.replace(
|
|
'<!--umami-->',
|
|
`<script defer src="${umamiUrl}" data-website-id="${umamiId}"></script>`,
|
|
);
|
|
}
|
|
const gaId = env.VITE_GOOGLE_ANALYTICS_ID;
|
|
if (gaId) {
|
|
result = result.replace(
|
|
'<!--Google Analytics-->',
|
|
`<script async src="https://www.googletagmanager.com/gtag/js?id=${gaId}"></script>` +
|
|
`<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag('js',new Date());gtag('config','${gaId}');</script>`,
|
|
);
|
|
}
|
|
return result;
|
|
},
|
|
};
|
|
}
|
|
|
|
export default defineConfig(({ mode }) => {
|
|
// Load only the settings used here; DEV_PROXY_TARGET stays server-side.
|
|
const env = loadEnv(mode, projectRoot, ['VITE_', 'DEV_PROXY_TARGET']);
|
|
const proxyTarget = env.DEV_PROXY_TARGET?.trim() || DEFAULT_PROXY_TARGET;
|
|
|
|
return {
|
|
resolve: {
|
|
alias: {
|
|
'@': path.resolve(projectRoot, 'src'),
|
|
'~': path.resolve(projectRoot, 'node_modules'),
|
|
'@douyinfe/semi-theme-default': path.resolve(
|
|
projectRoot,
|
|
'node_modules/@douyinfe/semi-theme-default',
|
|
),
|
|
},
|
|
},
|
|
plugins: [
|
|
legacyJsxPlugin(),
|
|
react(),
|
|
vitePluginSemi({ cssLayer: true }),
|
|
analyticsPlugin(env),
|
|
],
|
|
optimizeDeps: {
|
|
esbuildOptions: {
|
|
loader: {
|
|
'.js': 'jsx',
|
|
'.json': 'json',
|
|
},
|
|
},
|
|
},
|
|
build: {
|
|
minify: 'esbuild',
|
|
cssCodeSplit: true,
|
|
chunkSizeWarningLimit: 1000,
|
|
target: 'es2015',
|
|
sourcemap: false,
|
|
rollupOptions: {
|
|
output: {
|
|
chunkFileNames: 'assets/[name]-[hash].js',
|
|
entryFileNames: 'assets/[name]-[hash].js',
|
|
assetFileNames: 'assets/[name]-[hash].[ext]',
|
|
manualChunks,
|
|
},
|
|
},
|
|
},
|
|
server: {
|
|
host: '0.0.0.0',
|
|
proxy: createProxy(proxyTarget),
|
|
},
|
|
};
|
|
});
|