import checkConfig from './check_config' ; import { ASSETS_KEYS } from '../config/constants' ; import { LooseObject , DeepPartial , BalmConfig , BalmPath , BalmAssetsPath } from '@balm-core/index' ; function createQuickPath ( config , rootKey ) { const result = { } ; const rootValue = ( config . roots as LooseObject ) [ rootKey ] ; for ( const pathKey of Object . keys ( ( config . paths as LooseObject ) [ rootKey ] ) ) { const pathValue = ( config . paths as LooseObject ) [ rootKey ] [ pathKey ] ; result [ pathKey ] = rootKey === 'target' && ASSETS_KEYS . includes ( pathKey ) ? path . join ( rootValue , BalmJS . config . assets . virtualDir , BalmJS . file . assetsSuffixPath , pathValue ) : path . join ( rootValue , pathValue ) ; } return rootKey === 'source' ? ( result as BalmPath ) : ( result as BalmAssetsPath ) ; } function ready ( config ) { config . src = createQuickPath ( config , 'source' ) ; config . dest = createQuickPath ( config , config . env . isProd || ! config . inFrontend ? 'target' : 'tmp' ) as BalmAssetsPath ; config . dest . static = path . join ( config . dest . base , BalmJS . config . assets . virtualDir , BalmJS . file . assetsSuffixPath ) ; config . assets . static = path . join ( config . assets . root , config . assets . mainDir , BalmJS . file . assetsSuffixPath ) ; for ( const assetKey of ASSETS_KEYS ) { ( config . assets as LooseObject ) [ assetKey ] = path . join ( config . assets . static , ( config . paths . target as LooseObject ) [ assetKey ] ) ; } const HMR_ENV = config . useDefaults && config . env . isDev && ! config . env . inSSR && ! config . env . isMP ; const HMR_Enabling = config . scripts . bundler === BalmJS . Bundler . webpack && ! config . scripts . ie8 ; config . server . useHMR = HMR_ENV && HMR_Enabling ; config . inDesktopApp = /^electron-.*/ . test ( config . scripts . target as string ) ; config . scripts . useCache = config . env . isProd && config . assets . cache && config . scripts . injectHtml ; return config ; } function resetConfig ( ) { BalmJS . config . roots . target = 'dist' ; BalmJS . config . roots . tmp = '.tmp' ; return BalmJS . config ; } function setConfig ( customConfig ) { const defaultConfig = resetConfig ( ) ; const newConfig = BalmJS . utils . deepMerge ( defaultConfig , customConfig ) as BalmConfig ; checkConfig ( ) ; let config = BalmJS . utils . deepMerge ( newConfig , { paths : { tmp : newConfig . paths . target } } ) as BalmConfig ; if ( ! config . inFrontend ) { if ( config . roots . target === 'dist' ) { config . roots . target = config . assets . mainDir ; } config . roots . tmp = config . roots . target ; } config = ready ( config ) ; BalmJS . logger . success ( 'balm configuration' , config , { pre : true } ) ; return config ; } function setTask ( name ) { let result = undefined ; if ( BalmJS . utils . isFunction ( name ) ) { result = name as Function ; } else if ( BalmJS . utils . isString ( name ) ) { if ( ( gulp . tree ( ) . nodes as string [ ] ) . includes ( name as string ) ) { result = name as string ; } else { BalmJS . logger . error ( 'before/end task' , 'Invalid gulp task name' ) ; } } else { BalmJS . logger . error ( 'before/end task' , 'Task must be a string (gulp task name) or function' ) ; } return result ; } export { setConfig , setTask } ;