import { join } from 'path'; import { existsSync } from 'fs'; import { chokidar } from '@umijs/utils'; import { IApi } from '@umijs/types'; interface APIConf { baseURL: string; } export default function(api: IApi) { let watcher: chokidar.FSWatcher; const localFile = join(api.cwd, '.local.config.ts'); function getLocalApiUrl(filePath: string): APIConf { let localServerConfig = { baseURL: '', }; if (existsSync(filePath)) { localServerConfig = require(filePath); } return { baseURL: localServerConfig.baseURL, }; } function getApiUrl(): APIConf { // 获取CI设置的环境变量 let env = process.env.CONFIG_ENV; if (env && env.startsWith('dev')) { env = 'dev1'; } if (env && env.startsWith('test')) { env = 'test1'; } if (env === 'prod' || env === 'pre') { return { baseURL: 'https://lins-api.sensoro.com', }; } return { baseURL: `https://lins-${env}-api.sensoro.com`, }; } // 开发服务启动之后 监听配置文件 api.onDevCompileDone(({ isFirstCompile }) => { if (isFirstCompile) { watcher = chokidar.watch(localFile).on('all', event => { if (event === 'change') { api.restartServer(); } }); } }); api.onExit(() => { // 微信产品吱声的功能及服务 watcher && watcher.close(); }); const apiConf = api.env === 'development' ? getLocalApiUrl(localFile) : getApiUrl(); api.addHTMLHeadScripts(() => { return [ { content: ` window.BASE_URL = "${apiConf.baseURL}" `, }, ]; }); }