const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const CssMinimizerPlugin = require( 'css-minimizer-webpack-plugin' );
const path = require( 'path' );

module.exports = {
	...defaultConfig,
	optimization: {
		...( defaultConfig.optimization || {} ),
		minimizer: [
			...( defaultConfig.optimization?.minimizer || [] ),
			new CssMinimizerPlugin( {
				minimizerOptions: {
					preset: [
						'default',
						{
							discardComments: { removeAll: true },
							/* Disable all transforms except comment removal */
							normalizeWhitespace: false,
							normalizeUrl: false,
							normalizeUnicode: false,
							normalizeString: false,
							normalizePositions: false,
							normalizeTimingFunctions: false,
							normalizeDisplayValues: false,
							normalizeRepeatStyle: false,
							orderedValues: false,
							minifyFontValues: false,
							minifyGradients: false,
							minifyParams: false,
							minifySelectors: false,
							mergeLonghand: false,
							mergeRules: false,
							cssDeclarationSorter: false,
							calc: false,
							colormin: false,
							convertValues: false,
							discardDuplicates: false,
							discardEmpty: false,
							discardOverridden: false,
							reduceInitial: false,
							reduceTransforms: false,
							svgo: false,
							uniqueSelectors: false,
						},
					],
				},
			} ),
		],
	},
	resolve: {
		...( defaultConfig.resolve || {} ),
		extensions: [
			'.ts',
			'.tsx',
			'.js',
			'.jsx',
			'.json',
		],
	},
	module: {
		...( defaultConfig.module || {} ),
		rules: [
			...( defaultConfig.module?.rules || [] ),
			{
				test: /\.tsx?$/,
				exclude: /node_modules/,
				use: {
					loader: require.resolve( 'babel-loader' ),
					options: {
						cacheDirectory: true,
						presets: [
							require.resolve( '@wordpress/babel-preset-default' ),
							require.resolve( '@babel/preset-typescript' ),
						],
					},
				},
			},
		],
	},
	entry: {
		...( typeof defaultConfig.entry === 'function'
			? defaultConfig.entry()
			: defaultConfig.entry ),
		'currency-converter-frontend': path.resolve(
			process.cwd(),
			'src',
			'currency-converter-frontend.ts'
		),
		'ticker-frontend': path.resolve(
			process.cwd(),
			'src',
			'ticker-frontend.ts'
		),
		'rate-card-frontend': path.resolve(
			process.cwd(),
			'src',
			'rate-card-frontend.ts'
		),
		'price-badge-frontend': path.resolve(
			process.cwd(),
			'src',
			'price-badge-frontend.ts'
		),
	},
};
