import baseConfig from './vite.config.mjs';
import { defineConfig } from 'vite';
import { sentryVitePlugin } from '@sentry/vite-plugin';

const rollupExternalModules = {
  tests: (id) => /.*\.test\.(ts|tsx)$/.test(id) || /.*\.spec\.(ts|tsx)$/.test(id),
};

const rollupExternal = (id) =>
  rollupExternalModules.tests(id) || 
  rollupExternalModules.stories(id) || 
  rollupExternalModules.cypress(id);

export default ({ mode }) => {
	const base = baseConfig({ mode }); 

	return defineConfig({
		...base,
		build: {
			...base.build,
			rollupOptions: {
				...base.build.rollupOptions,
				external: rollupExternal,
			},
		},
		esBuild: {
			exclude: ['**/*.test.ts', '**/*.epsc.ts', '**/*.cy.ts'],
			drop: ['console'],
		},
		plugins: [
			...base.plugins,
			sentryVitePlugin({
				org: 'medea-cc',
				project: 'javascript-react',
				authToken: process.env.SENTRY_AUTH_TOKEN,
				sourcemaps: {
					filesToDeleteAfterUpload: '**/*.js.map',
				},
				bundleSizeOptimizations: {
					excludeDebugStatement: true,
					// Only relvant if you added `browserTracingIntegration`
					excludePerformanceMonitoring: true,
					// Only relevant if you added `replatIntegration`
					excludeReplayIframe: true,
					excludeReplayShadowDom: true,
					excludeReplayWorker: true,
				},
			}),
		],
	}); 
};