import merge from 'webpack-merge' ; import requireDir from 'require-dir' ; import { RuleSetRule , BalmLoaders } from '@balm-core/index' ; const LOADERS = requireDir ( './rules' ) ; function getLoaders ( customLoaders ) { const enableDefaultLoaders = Object . assign ( { html : true , css : true , js : true , url : true } , BalmJS . config . scripts . defaultLoaders ) ; const useDefaultLoaders = Object . values ( enableDefaultLoaders ) . some ( ( value ) => value ) ; let defaultLoaders = [ ] ; if ( useDefaultLoaders ) { Object . values ( LOADERS ) . forEach ( ( Loader ) => { const DefaultLoader = Loader . default ; const key = DefaultLoader . name . replace ( 'Loader' , '' ) ; if ( enableDefaultLoaders [ key ] ) { const loader = DefaultLoader ( ) ; if ( BalmJS . utils . isArray ( loader ) ) { defaultLoaders = defaultLoaders . concat ( loader ) ; } else { defaultLoaders . push ( loader as RuleSetRule ) ; } } } ) ; } const result = merge ( { module : { rules : defaultLoaders } } , { module : { rules : customLoaders } } ) ; const defaultModule = result . module as any ; BalmJS . logger . debug ( 'webpack loaders' , defaultModule . rules , { pre : true } ) ; return defaultModule . rules ; } export default getLoaders ;