env.js:
const configs = {
// 测试环境
test: {
API_SERVER: 'your-api-path',
},
// 开发环境
development: {
API_SERVER: 'your-api-path',
},
// 本地
local: {
API_SERVER: 'your-api-path',
},
};
console.log(process.env.API_ENV);
export default configs;
config.js:
import os from 'os';
import pageRoutes from './router.config';
import webpackPlugin from './plugin.config';
import defaultSettings from '../src/defaultSettings';
const plugins = [
[
'umi-plugin-react',
{
antd: true,
dva: {
hmr: true,
},
targets: {
ie: 11,
},
locale: {
enable: true, // default false
default: 'zh-CN', // default zh-CN
baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default
},
dynamicImport: {
loadingComponent: './components/PageLoading/index',
},
pwa: {
workboxPluginMode: 'InjectManifest',
workboxOptions: {
importWorkboxFrom: 'local',
},
},
...(!process.env.TEST && os.platform() === 'darwin'
? {
dll: {
include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
exclude: ['@babel/runtime'],
},
hardSource: true,
}
: {}),
},
],
];
if (process.env.APP_TYPE === 'site') {
plugins.push([
'umi-plugin-ga',
{
code: 'UA-72788897-6',
},
]);
}
export default {
plugins,
history: 'hash',
targets: {
ie: 11,
},
define: {
APP_TYPE: process.env.APP_TYPE || '',
'process.env': {
API_ENV: process.env.API_ENV, // 这里是重点吧,获取配置
},
},
theme: {
'primary-color': defaultSettings.primaryColor,
},
externals: {
'@antv/data-set': 'DataSet',
},
ignoreMomentLocale: true,
lessLoaderOptions: {
javascriptEnabled: true,
},
disableRedirectHoist: true,
cssLoaderOptions: {
modules: true,
getLocalIdent: (context, localIdentName, 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 = antdProPath
.split('/')
.map(a => a.replace(/([A-Z])/g, '-$1'))
.map(a => a.toLowerCase());
return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
}
return localName;
},
},
manifest: {
basePath: '/',
},
chainWebpack: webpackPlugin,
};
package.json:
"start:test": "cross-env APP_TYPE=site API_ENV=test umi dev",
"start:development": "cross-env APP_TYPE=site API_ENV=development umi dev",
"build": "umi build",
"build:test": "cross-env API_ENV=test umi build",
"build:development": "cross-env API_ENV=development umi build",
request.js:
fetch(configs[process.env.API_ENV].API_SERVER + url, newOptions)
相关链接:https://github.com/ant-design/ant-design-pro/issues/1636