import merge from 'webpack-merge' ; import getCommonConfig from './common' ; import { ASYNC_SCRIPTS , HASH_NAME_PROD } from '../../../config/constants' ; import { Configuration , MinifyOptions , BalmScripts } from '@balm-core/index' ; const bundleAnalyzerReport = process . env . npm_config_report || false ; const getCssFilename = ( ) : string => `${ BalmJS . config . paths . target . css }/${ ASYNC_SCRIPTS }/[name].${ HASH_NAME_PROD }.css` ; function getProdConfig ( webpack , scripts ) { const TerserPlugin = require ( 'terser-webpack-plugin' ) ; const OptimizeCSSAssetsPlugin = require ( 'optimize-css-assets-webpack-plugin' ) ; const safePostCssParser = require ( 'postcss-safe-parser' ) ; const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ; const webpackBundleAnalyzer = require ( 'webpack-bundle-analyzer' ) ; const shouldUseSourceMap = scripts . sourceMap as boolean ; const terserOptions = scripts . minifyOptions ; if ( scripts . ie8 ) { terserOptions . ie8 = true ; } return merge ( getCommonConfig ( webpack , scripts ) , { mode : 'production' , optimization : { minimizer : [ new TerserPlugin ( { terserOptions , extractComments : false } ) , new OptimizeCSSAssetsPlugin ( { cssProcessorOptions : { parser : safePostCssParser , map : shouldUseSourceMap ? { inline : false , annotation : true } : false } , cssProcessorPluginOptions : { preset : [ 'default' , { minifyFontValues : { removeQuotes : false } } ] } } ) ] } , plugins : [ new webpack . HashedModuleIdsPlugin ( ) , ... ( scripts . extractCss ? [ new MiniCssExtractPlugin ( { filename : BalmJS . file . assetsPath ( getCssFilename ( ) ) , chunkFilename : BalmJS . file . assetsPath ( getCssFilename ( ) ) } ) ] : [ ] ) , ... ( bundleAnalyzerReport ? [ new webpackBundleAnalyzer . BundleAnalyzerPlugin ( ) ] : [ ] ) ] , devtool : shouldUseSourceMap ? 'source-map' : false } ) ; } export default getProdConfig ;