UMI自定义插件并使用
自定义插件文件getBranchName.js(这个例子是获取当前git branch分支得)
const { execSync } = require('child_process');
function getBranchName(){
try{
const res = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
console.log(res, 'branch name')
return res
}catch(err){
return ''
}
}
export default (api, opts) => {
api.modifyConfig((config) => {
// config.define.BRANCH_NAME = '123'
// console.log(config, 'config')
// console.log(process.env, 'env1111')
const branchName = getBranchName()
config.define = {
...config.define,
BRANCH_NAME: branchName
}
if(process.env.UMI_ENV === 'preview'){
config.extraBabelPlugins = []
}
return config
})
};
在config.ts中引入自定义插件
import { defineConfig } from "umi";
export default defineConfig({
...
plugins: [
require.resolve('./getBranchName.js')
],
...
})
如果需要在不同环境下配置 要在config目录下加入config.dev.ts
在package.json得script
script:{
...
"build:preview": "cross-env REACT_APP_ENV=preview UMI_ENV=preview umi build",
...
}
umi官网自定义插件链接
链接: https://umijs.org/docs/api/plugin-api#modifydefaultconfig