import mapObj from '@tinkoff/utils/object/map' ; import path from 'path' ; import type Config from 'webpack-chain' ; import browserslist from 'browserslist' ; import type { ConfigManager } from '../../../config/configManager' ; const EXTENDS_REGEXP = /^extends (.+)$/i ; const normalizeQuery = ( rootDir , env , query ) => { const result = [ ] ; for ( const entry of query ) { const match = entry . match ( EXTENDS_REGEXP ) ; if ( match ) { const [ _ , name ] = match ; const externalModule = require ( require . resolve ( name , { paths : [ rootDir ] } ) ) ; result . push ( ... normalizeQuery ( rootDir , env , externalModule [ env ] ?? externalModule . defaults ) ) ; } else { result . push ( entry ) ; } } return result ; } ; export const browserslistConfigResolve = ( configManager ) => ( config ) => { const { rootDir } = configManager ; const browserslistConfig = browserslist . findConfig ( rootDir ) ; if ( ! browserslistConfig ) { return ; } const normalizedConfig = mapObj ( ( query , env ) => normalizeQuery ( rootDir , env , query ) , browserslistConfig ) ; config . module . rule ( 'external-browserslist-normalized-file-config' ) . test ( /[\\/]cli[\\/]lib[\\/]external[\\/]browserslist-normalized-file-config.js$/ ) . use ( 'replace' ) . loader ( path . resolve ( __dirname , '../loaders/replaceContent' ) ) . options ( { code : `
  export default ${ JSON . stringify ( normalizedConfig ) };
` , } ) . end ( ) . enforce ( 'post' ) ; } ;