| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- // https://umijs.org/config/
- // import { cleanupSVG, isEmptyColor, parseColors, runSVGO, SVG } from '@iconify/tools';
- import { defineConfig } from '@umijs/max';
- import { glob } from 'glob';
- import path, { join } from 'path';
- // import { FileSystemIconLoader } from 'unplugin-icons/loaders';
- // import Icons from 'unplugin-icons/webpack';
- import dayjs from 'dayjs';
- import fs from 'fs';
- import defaultSettings from './defaultSettings';
- import proxy from './proxy';
- import routes from './routes';
- // 导入所有环境配置
- const allConfigs = glob.sync('*/**.ts', { cwd: path.join(__dirname, 'env') }).reduce(
- (configs, file) => {
- const [productId, envFile] = file.split(path.sep);
- const envName = path.basename(envFile, '.ts');
- if (!configs[productId]) {
- configs[productId] = {};
- }
- configs[productId][envName] = require(`./env/${file}`).default;
- return configs;
- },
- {} as Record<string, Record<string, NodeJS.ProcessEnv>>,
- );
- const { REACT_APP_ENV = 'prod', PROD_ID = 'go-pmp' } = process.env;
- const envConfig = Object.assign(
- allConfigs[PROD_ID]?.['default'] ?? {},
- allConfigs[PROD_ID]?.[REACT_APP_ENV as any] ?? {},
- );
- const buildTime = dayjs();
- envConfig.REACT_APP_VERSION = `${envConfig.REACT_APP_VERSION} build:${buildTime
- .unix()
- .toString(36)
- .toUpperCase()}`;
- const defSettings = {
- ...defaultSettings,
- title: process.env.REACT_APP_NAME,
- };
- // 路由过滤函数
- function filterRoutesByProd(routes: any[], prodId: string): any[] {
- return routes
- .map((route) => {
- // 检查是否应该显示该路由
- const shouldShow = (() => {
- // 如果定义了 includeProds,只在指定产品中显示
- if (route.includeProds?.length > 0) {
- return route.includeProds.includes(prodId);
- }
- // 如果定义了 excludeProds,在非指定产品中显示
- if (route.excludeProds?.length > 0) {
- return !route.excludeProds.includes(prodId);
- }
- // 默认显示
- return true;
- })();
- if (prodId !== 'go-pmp' && !shouldShow) {
- return null;
- }
- // 递归处理子路由
- if (route.routes) {
- const filteredChildren = filterRoutesByProd(route.routes, prodId);
- // 如果子路由全部被过滤掉了,且当前路由没有 component,则过滤掉当前路由
- if (filteredChildren.length === 0 && !route.component) {
- return null;
- }
- return {
- ...route,
- routes: filteredChildren,
- };
- }
- return route;
- })
- .filter(Boolean); // 过滤掉 null 值
- }
- export default defineConfig({
- /**
- * 注入环境变量
- */
- define: {
- 'process.env': {
- REACT_APP_ENV: REACT_APP_ENV,
- REACT_APP_ID: envConfig.REACT_APP_ID,
- REACT_APP_NAME: envConfig.REACT_APP_NAME,
- REACT_APP_VERSION: envConfig.REACT_APP_VERSION,
- REACT_APP_BUILD_TIME: buildTime.unix(),
- STORAGE_NAME_SPACE: envConfig.STORAGE_NAME_SPACE,
- ENABLE_REQUEST_ENCRYPTION: envConfig.ENABLE_REQUEST_ENCRYPTION,
- REQUEST_ENCRYPTION_KEY: envConfig.REQUEST_ENCRYPTION_KEY,
- ENABLE_STORAGE_ENCRYPTION: envConfig.ENABLE_STORAGE_ENCRYPTION,
- STORAGE_ENCRYPTION_KEY: envConfig.STORAGE_ENCRYPTION_KEY,
- REACT_APP_API_URL: envConfig.REACT_APP_API_URL,
- REACT_APP_STAT_API_URL: envConfig.REACT_APP_STAT_API_URL,
- REACT_APP_RES_SERVER: envConfig.REACT_APP_RES_SERVER,
- },
- },
- links: [
- {
- rel: 'icon',
- href: `/favicon-${envConfig.REACT_APP_ID}.ico`,
- },
- ],
- // /** unplugin-icons 加载本地图标 */
- // alias: {
- // '~icons': '.icons',
- // },
- chainWebpack(memo) {
- // 在生产环境构建时生成 version.json 文件
- if (REACT_APP_ENV === 'prod' || REACT_APP_ENV === 'test' || REACT_APP_ENV === 'dev') {
- memo.plugin('generate-version-file').use(
- class GenerateVersionFilePlugin {
- apply(compiler: any) {
- compiler.hooks.beforeCompile.tapAsync(
- 'GenerateVersionFilePlugin',
- (params: any, callback: any) => {
- try {
- const publicDir = path.join(process.cwd(), 'public');
- if (!fs.existsSync(publicDir)) {
- fs.mkdirSync(publicDir, { recursive: true });
- }
- // 生成版本信息
- const versionInfo = {
- productId: envConfig.REACT_APP_ID,
- version: envConfig.REACT_APP_VERSION,
- buildTime: buildTime.format('YYYY-MM-DD HH:mm:ss'),
- buildTimestamp: buildTime.unix(),
- environment: REACT_APP_ENV,
- };
- // 写入 version.json 文件
- const versionFilePath = path.join(publicDir, 'version.json');
- fs.writeFileSync(versionFilePath, JSON.stringify(versionInfo, null, 2), 'utf8');
- console.log(`✅ 已生成 version.json 文件: ${versionFilePath}`);
- console.log('版本信息:', versionInfo);
- } catch (error) {
- console.error('❌ 生成 version.json 文件失败:', error);
- }
- callback();
- },
- );
- }
- },
- );
- }
- // chainWebpack(memo) {
- // memo.plugin('unplugin-icons').use(
- // Icons({
- // compiler: 'jsx',
- // jsx: 'react',
- // autoInstall: true,
- // scale: 1,
- // defaultClass: 'iconify-icon',
- // customCollections: {
- // // 自定义单色图标集合
- // 'custom-sc': FileSystemIconLoader(
- // // 单色 svg 文件目录
- // './svgs/single-color',
- // (svg) => {
- // // 将 SVG 字符串转换为 SVG 实例
- // const svgObj = new SVG(svg);
- // cleanupSVG(svgObj);
- // parseColors(svgObj, {
- // defaultColor: 'currentColor',
- // callback: (attr, colorStr, color) => {
- // return !color || isEmptyColor(color) ? colorStr : 'currentColor';
- // },
- // });
- // runSVGO(svgObj);
- // return svgObj.toMinifiedString();
- // },
- // ),
- // // 自定义多色图标集合
- // 'custom-mc': FileSystemIconLoader('./svgs/multi-color', (svg) => {
- // // 将 SVG 字符串转换为 SVG 实例
- // const svgObj = new SVG(svg);
- // cleanupSVG(svgObj);
- // runSVGO(svgObj);
- // return svgObj.toMinifiedString();
- // }),
- // },
- // }),
- // );
- // return memo;
- // },
- return memo;
- },
- /**
- * @name 开启 hash 模式
- * @description 让 build 之后的产物包含 hash 后缀。通常用于增量发布和避免浏览器加载缓存。
- * @doc https://umijs.org/docs/api/config#hash
- */
- hash: true,
- /**
- * @name 兼容性设置
- * @description 设置 ie11 不一定完美兼容,需要检查自己使用的所有依赖
- * @doc https://umijs.org/docs/api/config#targets
- */
- // targets: {
- // ie: 11,
- // },
- /**
- * @name 路由的配置,不在路由中引入的文件不会编译
- * @description 只支持 path,component,routes,redirect,wrappers,title 的配置
- * @doc https://umijs.org/docs/guides/routes
- */
- // umi routes: https://umijs.org/docs/routing
- routes: filterRoutesByProd(routes, PROD_ID),
- /**
- * @name 主题的配置
- * @description 虽然叫主题,但是其实只是 less 的变量设置
- * @doc antd的主题设置 https://ant.design/docs/react/customize-theme-cn
- * @doc umi 的theme 配置 https://umijs.org/docs/api/config#theme
- */
- theme: {
- // 如果不想要 configProvide 动态设置主题需要把这个设置为 default
- // 只有设置为 variable, 才能使用 configProvide 动态设置主色调
- 'root-entry-name': 'variable',
- },
- /**
- * @name moment 的国际化配置
- * @description 如果对国际化没有要求,打开之后能减少js的包大小
- * @doc https://umijs.org/docs/api/config#ignoremomentlocale
- */
- ignoreMomentLocale: true,
- /**
- * @name 代理配置
- * @description 可以让你的本地服务器代理到你的服务器上,这样你就可以访问服务器的数据了
- * @see 要注意以下 代理只能在本地开发时使用,build 之后就无法使用了。
- * @doc 代理介绍 https://umijs.org/docs/guides/proxy
- * @doc 代理配置 https://umijs.org/docs/api/config#proxy
- */
- proxy: proxy(envConfig) as any,
- /**
- * @name 快速热更新配置
- * @description 一个不错的热更新组件,更新时可以保留 state
- */
- fastRefresh: true,
- //============== 以下都是max的插件配置 ===============
- /**
- * @name 数据流插件
- * @@doc https://umijs.org/docs/max/data-flow
- */
- model: {},
- /**
- * 一个全局的初始数据流,可以用它在插件之间共享数据
- * @description 可以用来存放一些全局的数据,比如用户信息,或者一些全局的状态,全局初始状态在整个 Umi 项目的最开始创建。
- * @doc https://umijs.org/docs/max/data-flow#%E5%85%A8%E5%B1%80%E5%88%9D%E5%A7%8B%E7%8A%B6%E6%80%81
- */
- initialState: {},
- /**
- * @name layout 插件
- * @doc https://umijs.org/docs/max/layout-menu
- */
- title: 'Go PMP',
- layout: {
- locale: true,
- ...defSettings,
- },
- /**
- * @name moment2dayjs 插件
- * @description 将项目中的 moment 替换为 dayjs
- * @doc https://umijs.org/docs/max/moment2dayjs
- */
- moment2dayjs: {
- preset: 'antd',
- plugins: ['duration'],
- },
- /**
- * @name 国际化插件
- * @doc https://umijs.org/docs/max/i18n
- */
- locale: {
- // default zh-CN
- default: 'zh-CN',
- antd: true,
- // default true, when it is true, will use `navigator.language` overwrite default
- baseNavigator: true,
- },
- /**
- * @name antd 插件
- * @description 内置了 babel import 插件
- * @doc https://umijs.org/docs/max/antd#antd
- */
- antd: {},
- /**
- * @name 网络请求配置
- * @description 它基于 axios 和 ahooks 的 useRequest 提供了一套统一的网络请求和错误处理方案。
- * @doc https://umijs.org/docs/max/request
- */
- request: {},
- /**
- * @name 权限插件
- * @description 基于 initialState 的权限插件,必须先打开 initialState
- * @doc https://umijs.org/docs/max/access
- */
- access: {},
- /**
- * @name <head> 中额外的 script
- * @description 配置 <head> 中额外的 script
- */
- headScripts: [
- // 解决首次加载时白屏的问题
- { src: '/scripts/loading.js', async: true },
- ],
- //================ pro 插件配置 =================
- presets: ['umi-presets-pro'],
- /**
- * @name openAPI 插件的配置
- * @description 基于 openapi 的规范生成serve 和mock,能减少很多样板代码
- * @doc https://pro.ant.design/zh-cn/docs/openapi/
- */
- openAPI: [
- {
- requestLibPath: "import { request } from '@umijs/max'",
- // 或者使用在线的版本
- // schemaPath: "https://gw.alipayobjects.com/os/antfincdn/M%24jrzTTYJN/oneapi.json"
- schemaPath: join(__dirname, 'oneapi.json'),
- mock: false,
- },
- {
- requestLibPath: "import { request } from '@umijs/max'",
- schemaPath: 'https://gw.alipayobjects.com/os/antfincdn/CA1dOm%2631B/openapi.json',
- projectName: 'swagger',
- },
- ],
- mock: {
- include: ['mock/**/*', 'src/pages/**/_mock.ts'],
- },
- mfsu: {
- strategy: 'normal',
- shared: {
- 'lodash-es': { singleton: true, eager: true, requiredVersion: false },
- ramda: { singleton: true, eager: true, requiredVersion: false },
- },
- },
- esbuildMinifyIIFE: true,
- /**
- * @name Babel 插件配置
- * @description 使用 Babel 插件移除 console 语句
- */
- extraBabelPlugins: [
- REACT_APP_ENV === 'prod' ? 'babel-plugin-transform-remove-console' : '',
- ].filter(Boolean),
- requestRecord: {},
- tailwindcss: {
- timeout: 30000,
- },
- icons: { autoInstall: {} },
- }) as any;
|