import { dirname } from 'path' ; import type { IApi } from 'umi' ; import { NProgressPkgName , NProgressStyleSource , PluginKey , RuntimeAPIIe11CjsDirectory , RuntimeAPIIe11EsmDirectory , RuntimeAPIPkgName , RuntimeInjectionFilePath , } from './constants' ; import { UmiPluginNProgressConfig , describeConfig } from './interfaces' ; import { CreateRuntimeProgramOptions , createRuntimeProgram , printSourceFile } from './runtime' ; export { UmiPluginNProgressConfig } ; export default function nprogress ( api ) { let getConfig = ( ) : UmiPluginNProgressConfig => { const config = api . config [ PluginKey ] ; getConfig = ( ) => config ; return config ; } ; api . describe ( { key : PluginKey , config : describeConfig ( ) , } ) ; api . addProjectFirstLibraries ( ( ) => { const RuntimeAPIPkgPath = resolveDependency ( RuntimeAPIPkgName ) ; const NProgressPkgPath = resolveDependency ( NProgressPkgName , RuntimeAPIPkgPath ) ; return [ { name : RuntimeAPIPkgName , path : RuntimeAPIPkgPath } , { name : NProgressPkgName , path : NProgressPkgPath } , ] ; } ) ; api . onGenerateFiles ( ( ) => { const createRuntimeProgramOptions = { ... getConfig ( ) , runtimeAPISource : getRuntimeAPISource ( ) , } ; api . writeTmpFile ( { path : RuntimeInjectionFilePath , content : printSourceFile ( createRuntimeProgram ( createRuntimeProgramOptions ) ) , } ) ; } ) ; api . addUmiExports ( ( ) => { return [ { source : getRuntimeAPISource ( ) , exportAll : true } ] ; } ) ; api . addEntryImports ( ( ) => { return [ { source : NProgressStyleSource } , { source : RuntimeInjectionFilePath } ] ; } ) ; function getRuntimeAPISource ( ) { if ( ! getConfig ( ) . ie11 ) return RuntimeAPIPkgName ; if ( getConfig ( ) . ie11 === 'cjs' ) return `${ RuntimeAPIPkgName }/${ RuntimeAPIIe11CjsDirectory }` ; return `${ RuntimeAPIPkgName }/${ RuntimeAPIIe11EsmDirectory }` ; } function resolveDependency ( id , path = __dirname ) { return dirname ( require . resolve ( `${ id }/package.json` , { paths : [ path ] } ) ) ; } }