Portal/rsconcept/frontend/vite.config.ts

93 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
import react from '@vitejs/plugin-react';
import path from 'path';
import { defineConfig, loadEnv, PluginOption } from 'vite';
import { dependencies } from './package.json';
// Packages to include in main app bundle
2024-11-25 19:52:57 +03:00
const inlinePackages = ['react', 'react-router', 'react-dom'];
2024-06-07 20:17:03 +03:00
// Rollup warnings that should not be displayed
const warningsToIgnore = [['SOURCEMAP_ERROR', "Can't resolve original location of error"]];
// https://vitejs.dev/config/
export default ({ mode }: { mode: string }) => {
process.env = {
...process.env,
...loadEnv(mode, process.cwd())
};
return defineConfig({
appType: 'spa',
plugins: [react(), muteWarningsPlugin(warningsToIgnore)],
server: {
2024-07-19 22:30:28 +03:00
port: Number(process.env.VITE_PORTAL_FRONT_PORT)
2024-06-07 20:17:03 +03:00
},
publicDir: 'public',
build: {
chunkSizeWarningLimit: 4000, // KB
sourcemap: false,
rollupOptions: {
output: {
manualChunks: { ...renderChunks(dependencies) }
}
}
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
}
}
});
};
// ======== Internals =======
function renderChunks(deps: Record<string, string>) {
const chunks = {};
Object.keys(deps).forEach(key => {
if (inlinePackages.includes(key)) {
return;
}
chunks[key] = [key];
});
return chunks;
}
function muteWarningsPlugin(warningsToIgnore: string[][]): PluginOption {
const mutedMessages = new Set();
return {
name: 'mute-warnings',
enforce: 'pre',
config: userConfig => ({
build: {
rollupOptions: {
onwarn(warning, defaultHandler) {
if (warning.code) {
const muted = warningsToIgnore.find(
([code, message]) => code == warning.code && warning.message.includes(message)
);
if (muted) {
mutedMessages.add(muted.join());
return;
}
}
if (userConfig.build?.rollupOptions?.onwarn) {
userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
} else {
defaultHandler(warning);
}
}
}
}
}),
closeBundle() {
const diff = warningsToIgnore.filter(x => !mutedMessages.has(x.join()));
if (diff.length > 0) {
this.warn('Some of your muted warnings never appeared during the build process:');
diff.forEach(m => this.warn(`- ${m.join(': ')}`));
}
}
};
}