format import { IApi } from "umi" ; import { join } from "path" ; import serveStatic from "serve-static" ; import rimraf from "rimraf" ; import { existsSync , mkdirSync } from "fs" ; import defaultTheme from "./defaultTheme" ; const buildCss = require ( "antd-pro-merge-less" ) ; const winPath = require ( "slash2" ) ; interface themeConfig { theme ? : string ; fileName : string ; key : string ; modifyVars ? : { [ key ] : string ; } ; } export default function ( api ) { api . modifyDefaultConfig ( ( config ) => { config . cssLoader = { modules : { getLocalIdent : ( context , _ , localName ) => { if ( context . resourcePath . includes ( "node_modules" ) || context . resourcePath . includes ( "ant.design.pro.less" ) || context . resourcePath . includes ( "global.less" ) ) { return localName ; } const match = context . resourcePath . match ( /src(.*)/ ) ; if ( match && match [ 1 ] ) { const antdProPath = match [ 1 ] . replace ( ".less" , "" ) ; const arr = winPath ( antdProPath ) . split ( "/" ) . map ( ( a ) => a . replace ( /([A-Z])/g , "-$1" ) ) . map ( ( a ) => a . toLowerCase ( ) ) ; return `antd-pro${ arr . join ( "-" ) }-${ localName }` . replace ( /--/g , "-" ) ; } return localName ; } , } , } ; return config ; } ) ; let options = defaultTheme ; const themeConfigPath = winPath ( join ( api . paths . cwd , "config/theme.config.json" ) ) ; if ( existsSync ( themeConfigPath ) ) { options = require ( themeConfigPath ) ; } const { cwd , absOutputPath , absNodeModulesPath } = api . paths ; const outputPath = absOutputPath ; let themeTemp = winPath ( join ( __dirname , "../../.plugin-theme" ) ) ; if ( existsSync ( winPath ( join ( absNodeModulesPath , ".plugin-theme" ) ) ) ) { themeTemp = winPath ( join ( absNodeModulesPath , ".plugin-theme" ) ) ; } api . addMiddewares ( ( ) => { return serveStatic ( themeTemp ) ; } ) ; api . addHTMLHeadScripts ( ( ) => [ { content : `window.umi_plugin_ant_themeVar = ${ JSON . stringify ( options . theme ) }` , } , ] ) ; api . onBuildComplete ( ( { err } ) => { if ( err ) { return ; } api . logger . info ( "💄  build theme" ) ; try { if ( existsSync ( winPath ( join ( outputPath , "theme" ) ) ) ) { rimraf . sync ( winPath ( join ( outputPath , "theme" ) ) ) ; } mkdirSync ( winPath ( join ( outputPath , "theme" ) ) ) ; } catch ( error ) { } buildCss ( cwd , options . theme . map ( ( theme ) => ( { ... theme , fileName : winPath ( join ( outputPath , "theme" , theme . fileName ) ) , } ) ) , { min : true , ... options , } ) . then ( ( ) => { api . logger . log ( "🎊  build theme success" ) ; } ) . catch ( ( e ) => { console . log ( e ) ; } ) ; } ) ; api . onDevCompileDone ( ( ) => { api . logger . info ( "cache in :" + themeTemp ) ; api . logger . info ( "💄  build theme" ) ; try { if ( existsSync ( themeTemp ) ) { rimraf . sync ( themeTemp ) ; } if ( existsSync ( winPath ( join ( themeTemp , "theme" ) ) ) ) { rimraf . sync ( winPath ( join ( themeTemp , "theme" ) ) ) ; } mkdirSync ( themeTemp ) ; mkdirSync ( winPath ( join ( themeTemp , "theme" ) ) ) ; } catch ( error ) { } buildCss ( cwd , options . theme . map ( ( theme ) => ( { ... theme , fileName : winPath ( join ( themeTemp , "theme" , theme . fileName ) ) , } ) ) , { ... options , } ) . then ( ( ) => { api . logger . log ( "🎊  build theme success" ) ; } ) . catch ( ( e ) => { console . log ( e ) ; } ) ; } ) ; }