umi4 config文档 https://umijs.org/docs/api/config
image.png

config.ts

  1. import { defineConfig } from '@umijs/max';
  2. import { join } from 'path';
  3. import defaultSettings from './defaultSettings';
  4. import proxy from './proxy';
  5. import routes from './routes';
  6. const { REACT_APP_ENV, NODE_ENV } = process.env;
  7. const isEnvProduction = NODE_ENV === "production";
  8. export default defineConfig({
  9. title: defaultSettings.title,
  10. outputPath: 'build',
  11. publicPath: './',
  12. hash: false,
  13. history: {type: 'hash'}, //
  14. targets: {
  15. ie: 11,
  16. },
  17. // umi routes: https://umijs.org/docs/routing
  18. routes,
  19. theme: {
  20. 'root-entry-name': 'variable',
  21. },
  22. ignoreMomentLocale: true,
  23. proxy: proxy[REACT_APP_ENV || 'dev'],
  24. manifest: {
  25. basePath: '/',
  26. },
  27. fastRefresh: true,
  28. model: {},
  29. initialState: {},
  30. layout: {
  31. locale: true,
  32. siderWidth: 200,
  33. ...defaultSettings,
  34. },
  35. locale: {
  36. default: 'zh-CN',
  37. antd: true,
  38. // default true, when it is true, will use `navigator.language` overwrite default
  39. baseNavigator: true,
  40. },
  41. antd: {},
  42. // 生产环境去除 console日志打印
  43. terserOptions: {
  44. compress: {
  45. drop_console: isEnvProduction,
  46. },
  47. },
  48. nodeModulesTransform: {
  49. // build 先留着 all,避免线上依赖代码里没有编译 es5
  50. type: process.env.NODE_ENV === 'development' ? 'none' : 'all',
  51. },
  52. // dynamicImport: {
  53. // loading: "@/components/PageLoading/index",
  54. // },
  55. // devServer: {
  56. // open: true,
  57. // port: 8367,
  58. // },
  59. // useRequest https://umijs.org/docs/max/request
  60. request: {},
  61. access: {},
  62. presets: ['umi-presets-pro'],
  63. // 基于 openapi 的规范生成serve 和mock,能减少很多样板代码
  64. openAPI: [
  65. {
  66. requestLibPath: "import { request } from '@umijs/max'",
  67. // 或者使用在线的版本
  68. // schemaPath: "https://gw.alipayobjects.com/os/antfincdn/M%24jrzTTYJN/oneapi.json"
  69. schemaPath: join(__dirname, 'oneapi.json'),
  70. mock: false,
  71. },
  72. {
  73. requestLibPath: "import { request } from '@umijs/max'",
  74. schemaPath: 'https://gw.alipayobjects.com/os/antfincdn/CA1dOm%2631B/openapi.json',
  75. projectName: 'swagger',
  76. },
  77. ],
  78. });

umijs .env

项目根目录,新建 .env,写入键值对配置
config.ts 设置 process.env,不然 .env 文件无效

  1. export default defineConfig({
  2. 'process.env': process.env,
  3. 'process.env.UMI_ENV': 'dev',
  4. })

defualtSetting.json

chainWebpack

扩展 umi 内置的 webpack 配置,基于 webpack-chain

  • config, 现有 webpack 配置
  • args 一些额外信息和辅助对象,目前有 env 和 webpack
    • env 为当前环境,值为 development 或 production
    • webpack 为 webpack 对象,可从中获取 webpack 内置插件

https://github.com/neutrinojs/webpack-chain
https://umijs.org/docs/api/config#chainwebpack

  • 修改 js文件输出目录
  • 修改 图片输出目录
  • 添加 gzip压缩 ```javascript import CompressionWebpackPlugin from ‘compression-webpack-plugin’

const assetDir = “static”; const isEnvProduction = process.env.NODE_ENV === “production”;

function chainWebpack(config, args) { const { env, webpack, createCSSRule } = args; // 修改 js chunk文件输出目录 config .output .filename(assetDir + ‘/js/[name].[hash:8].js’) .chunkFilename(assetDir + ‘/js/[name].[contenthash:8].chunk.js’);

// 修改css输出目录 config .plugin(“extract-css”) .tap(() => [ { filename: ${assetDir}/css/[name].[contenthash:8].css, chunkFilename: ${assetDir}/css/[name].[contenthash:8].chunk.css, ignoreOrder: true, }, ]);

// 修改图片输出目录 config .module .rule(“images”) .test(/.(png|jpe?g|gif|webp|ico)(\?.*)?$/) .use(“url-loader”) .loader(require.resolve(“url-loader”)) .tap((options) => { const newOptions = { …options, name: assetDir + “/img/[name].[hash:8].[ext]”, fallback: { …options.fallback, options: { name: assetDir + “/img/[name].[hash:8].[ext]”, esModule: false, }, }, }; return newOptions; });

// 修改svg输出目录 config .module .rule(“svg”) .test(/.(svg)(\?.*)?$/) .use(“file-loader”) .loader(require.resolve(“file-loader”)) .tap((options) => ({ …options, name: assetDir + “/img/[name].[hash:8].[ext]”, }));

// 修改fonts输出目录 config .module .rule(“fonts”) .test(/.(eot|woff|woff2|ttf)(\?.*)?$/) .use(“file-loader”) .loader(require.resolve(“file-loader”)) .tap((options) => ({ …options, name: assetDir + “/fonts/[name].[hash:8].[ext]”, fallback: { …options.fallback, options: { name: assetDir + “/fonts/[name].[hash:8].[ext]”, esModule: false, }, }, }));

// 添加gzip压缩 config .when(isEnvProduction, (conf) => { conf .plugin(“compression-webpack-plugin”) .use(CompressionWebpackPlugin, [ { filename: “[path].gz[query]”, algorithm: “gzip”, test: new RegExp(“\.(js|css)$”), threshold: 10240, minRatio: 0.8, }, ]); }); }

export default chainWebpack ```