ConceptPortal-public/rsconcept/frontend/vite.config.ts

136 lines
3.1 KiB
TypeScript
Raw Normal View History

import path from 'path';
2025-02-12 21:36:25 +03:00
import tailwindcss from '@tailwindcss/vite';
2025-02-12 21:36:25 +03:00
import react from '@vitejs/plugin-react';
2025-02-22 14:04:01 +03:00
import { defineConfig, loadEnv, type PluginOption } from 'vite';
2023-12-07 23:08:49 +03:00
import { dependencies } from './package.json';
2023-08-13 23:51:48 +03:00
const reactCompilerConfig = {
/* ... */
};
2023-12-13 15:03:50 +03:00
// Packages to include in main app bundle
2025-02-26 23:49:56 +03:00
const inlinePackages = [
'react',
'react-router',
'react-dom',
'react-icons',
'react-hook-form',
'react-tooltip',
'react-toastify',
'global',
'react-scan',
'axios',
'zod',
'zustand',
'@tanstack/react-query',
'@hookform/resolvers'
];
2023-12-13 15:03:50 +03:00
2023-12-30 14:46:43 +03:00
// Rollup warnings that should not be displayed
const warningsToIgnore = [
['SOURCEMAP_ERROR', "Can't resolve original location of error"],
['MODULE_LEVEL_DIRECTIVE', 'Module level directives cause errors when bundled']
];
2023-08-13 23:51:48 +03:00
// https://vitejs.dev/config/
2023-12-30 14:46:43 +03:00
export default ({ mode }: { mode: string }) => {
2023-12-07 23:08:49 +03:00
process.env = {
...process.env,
...loadEnv(mode, process.cwd())
};
return defineConfig({
2023-12-30 14:46:43 +03:00
appType: 'spa',
2025-02-22 14:04:01 +03:00
plugins: [
tailwindcss(),
2025-02-22 14:04:01 +03:00
react({
babel: {
plugins: [['babel-plugin-react-compiler', reactCompilerConfig]]
}
}),
,
muteWarningsPlugin(warningsToIgnore)
],
server: {
2025-03-06 21:34:02 +03:00
port: Number(process.env.VITE_PORTAL_FRONT_PORT),
watch: {
ignored: ['**/tests/**']
}
},
2023-12-30 14:46:43 +03:00
publicDir: 'public',
build: {
chunkSizeWarningLimit: 4000, // KB
sourcemap: false,
rollupOptions: {
output: {
2024-12-05 15:46:23 +03:00
manualChunks: { ...renderChunks(dependencies) }
2023-12-30 14:46:43 +03:00
}
}
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
}
},
optimizeDeps: {
exclude: ['react-scan']
}
});
2023-12-30 14:46:43 +03:00
};
2023-12-13 15:03:50 +03:00
// ======== Internals =======
function renderChunks(deps: Record<string, string>) {
const chunks = {};
2023-12-30 14:46:43 +03:00
Object.keys(deps).forEach(key => {
2023-12-13 15:03:50 +03:00
if (inlinePackages.includes(key)) {
return;
}
chunks[key] = [key];
2023-12-30 14:46:43 +03:00
});
2023-12-13 15:03:50 +03:00
return chunks;
}
function muteWarningsPlugin(warningsToIgnore: string[][]): PluginOption {
const mutedMessages = new Set();
return {
name: 'mute-warnings',
enforce: 'pre',
2023-12-30 14:46:43 +03:00
config: userConfig => ({
2023-12-13 15:03:50 +03:00
build: {
rollupOptions: {
onwarn(warning, defaultHandler) {
if (warning.code) {
const muted = warningsToIgnore.find(
2023-12-30 14:46:43 +03:00
([code, message]) => code == warning.code && warning.message.includes(message)
);
2023-12-13 15:03:50 +03:00
if (muted) {
mutedMessages.add(muted.join());
return;
}
}
if (userConfig.build?.rollupOptions?.onwarn) {
2023-12-30 14:46:43 +03:00
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
2023-12-13 15:03:50 +03:00
} else {
2023-12-30 14:46:43 +03:00
defaultHandler(warning);
2023-12-13 15:03:50 +03:00
}
2023-12-30 14:46:43 +03:00
}
}
}
2023-12-13 15:03:50 +03:00
}),
closeBundle() {
2023-12-30 14:46:43 +03:00
const diff = warningsToIgnore.filter(x => !mutedMessages.has(x.join()));
2023-12-13 15:03:50 +03:00
if (diff.length > 0) {
this.warn('Some of your muted warnings never appeared during the build process:');
2023-12-30 14:46:43 +03:00
diff.forEach(m => this.warn(`- ${m.join(': ')}`));
2023-12-13 15:03:50 +03:00
}
2023-12-30 14:46:43 +03:00
}
};
}