import applyOrReturn from '@tinkoff/utils/function/applyOrReturn' ; import path from 'path' ; import type Config from 'webpack-chain' ; import ExtractCssChunks from 'mini-css-extract-plugin' ; import { createGenerator } from '@tinkoff/minicss-class-generator' ; import { safeRequire } from '../../../utils/safeRequire' ; import type { ConfigManager } from '../../../config/configManager' ; const cssLocalIdentNameDevDefault = '[name]__[local]_[minicss]' ; const cssLocalIdentNameProdDefault = '[minicss]' ; interface Options { localIdentName ? : string ; } export default ( configManager , options = { } ) => ( config ) => { const { postcss : { config : postcssConfig = '' , cssLocalIdentName = null , cssLocalIdentNameDev = cssLocalIdentName ?? cssLocalIdentNameDevDefault , cssLocalIdentNameProd = cssLocalIdentName ?? cssLocalIdentNameProdDefault , } = { } , } = configManager . build . configurations ; const { env , sourceMap } = configManager ; const localIdentName = options . localIdentName ?? ( env === 'production' ? cssLocalIdentNameProd : cssLocalIdentNameDev ) ; const configCssLoader = ( cfg ) => { cfg . use ( 'extract-css' ) . loader ( ExtractCssChunks . loader ) . options ( { esModule : false } ) ; const cssModulesOptions = { localIdentName , } ; if ( /\[minicss]/ . test ( localIdentName ) ) { cssModulesOptions . getLocalIdent = createGenerator ( ) ; } cfg . use ( 'css' ) . loader ( 'css-loader' ) . options ( { modules : cssModulesOptions , sourceMap , importLoaders : 1 , esModule : false , } ) ; } ; const postcssCfg = postcssConfig ? safeRequire ( path . resolve ( configManager . rootDir , postcssConfig ) , true ) : { } ; config . module . rule ( 'css' ) . test ( /\.css$/ ) . batch ( configCssLoader ) . use ( 'postcss' ) . loader ( 'postcss-loader' ) . options ( { sourceMap , postcssOptions : ( ... args ) => ( { ... postcssCfg , plugins : [ require ( 'postcss-modules-tilda' ) , require ( 'postcss-modules-values-replace' ) ( { importsAsModuleRequests : true } ) , ... ( applyOrReturn ( args , postcssCfg . plugins ) || [ ] ) , ] , } ) , } ) ; } ;