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

105 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-08-09 16:55:53 +03:00
import react from '@vitejs/plugin-react';
import path from 'path';
2023-12-13 15:03:50 +03:00
import { defineConfig, loadEnv,PluginOption } from 'vite';
2023-12-07 23:08:49 +03:00
import { dependencies } from './package.json';
2023-08-13 23:51:48 +03:00
2023-12-13 15:03:50 +03:00
// Packages to include in main app bundle
2023-12-07 23:08:49 +03:00
const inlinePackages = ['react', 'react-router-dom', 'react-dom'];
2023-12-13 15:03:50 +03:00
// Roolup warnings that should not be displayed
const warningsToIgnore = [
['SOURCEMAP_ERROR', "Can't resolve original location of error"]
];
2023-08-13 23:51:48 +03:00
// https://vitejs.dev/config/
export default (({ mode }: { mode: string }) => {
2023-12-07 23:08:49 +03:00
process.env = {
...process.env,
...loadEnv(mode, process.cwd())
};
const enableHttps = process.env.VITE_PORTAL_FRONT_HTTPS === 'true';
return defineConfig({
2023-12-13 15:03:50 +03:00
plugins: [
react(),
muteWarningsPlugin(warningsToIgnore),
],
server: {
port: Number(process.env.VITE_PORTAL_FRONT_PORT),
// NOTE: https is not used for dev builds currently
https: enableHttps,
},
build: {
chunkSizeWarningLimit: 4000, // KB
sourcemap: false,
rollupOptions: {
output: {
manualChunks: {
// Load chunks for dependencies separately
...renderChunks(dependencies),
},
2023-08-13 23:51:48 +03:00
},
},
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
}
}
});
});
2023-12-13 15:03:50 +03:00
// ======== 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(': ')}`));
}
},
}
}