config.ts 5.25 KB
Newer Older
1
// https://umijs.org/config/
愚道's avatar
愚道 committed
2
import os from 'os';
3
import slash from 'slash2';
何乐's avatar
何乐 committed
4
import { IPlugin, IConfig } from 'umi-types';
陈小聪's avatar
陈小聪 committed
5
import defaultSettings from './defaultSettings';
duanledexianxianxian's avatar
duanledexianxianxian committed
6
import webpackPlugin from './plugin.config'; // webpack相关配置
duanledexianxianxian's avatar
duanledexianxianxian committed
7 8 9 10 11
import routes from './routes.config';
import theme from './theme.config';
const path = require('path');

const { pwa } = defaultSettings;
陈帅's avatar
陈帅 committed
12 13

// preview.pro.ant.design only do not use in your production ;
陈帅's avatar
陈帅 committed
14
// preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
Yu's avatar
Yu committed
15

陈帅's avatar
陈帅 committed
16
const { ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION, TEST, NODE_ENV } = process.env;
duanledexianxianxian's avatar
duanledexianxianxian committed
17
// 插件设置
陈小聪's avatar
陈小聪 committed
18
const plugins: IPlugin[] = [
19 20 21 22 23 24 25 26
  [
    'umi-plugin-react',
    {
      antd: true,
      dva: {
        hmr: true,
      },
      locale: {
陈帅's avatar
陈帅 committed
27 28 29 30 31 32
        // default false
        enable: true,
        // default zh-CN
        default: 'zh-CN',
        // default true, when it is true, will use `navigator.language` overwrite default
        baseNavigator: true,
xiaoiver's avatar
xiaoiver committed
33
      },
34 35 36
      dynamicImport: {
        loadingComponent: './components/PageLoading/index',
        webpackChunkName: true,
Yu's avatar
Yu committed
37
        level: 3,
38
      },
陈帅's avatar
陈帅 committed
39 40 41 42 43 44 45
      pwa: pwa
        ? {
            workboxPluginMode: 'InjectManifest',
            workboxOptions: {
              importWorkboxFrom: 'local',
            },
          }
yaphet's avatar
yaphet committed
46
        : false,
47
      ...(!TEST && os.platform() === 'darwin'
陈帅's avatar
陈帅 committed
48 49 50
        ? {
            dll: {
              include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
51
              exclude: ['@babel/runtime', 'netlify-lambda'],
陈帅's avatar
陈帅 committed
52
            },
53
            hardSource: false,
陈帅's avatar
陈帅 committed
54 55
          }
        : {}),
56 57 58 59 60 61 62 63 64 65
    },
  ],
  [
    'umi-plugin-pro-block',
    {
      moveMock: false,
      moveService: false,
      modifyRequest: true,
      autoAddMenu: true,
    },
愚道's avatar
愚道 committed
66
  ],
陈帅's avatar
陈帅 committed
67
]; // 针对 preview.pro.ant.design 的 GA 统计代码
68
// preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
陈帅's avatar
陈帅 committed
69

70
if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
71 72 73 74 75 76 77
  plugins.push([
    'umi-plugin-ga',
    {
      code: 'UA-72788897-6',
    },
  ]);
}
陈帅's avatar
陈帅 committed
78 79 80 81 82 83 84 85 86 87

const uglifyJSOptions =
  NODE_ENV === 'production'
    ? {
        uglifyOptions: {
          // remove console.* except console.error
          compress: {
            drop_console: true,
            pure_funcs: ['console.error'],
          },
信鑫-King's avatar
信鑫-King committed
88
        },
陈帅's avatar
陈帅 committed
89 90
      }
    : {};
duanledexianxianxian's avatar
duanledexianxianxian committed
91

92 93 94 95
export default {
  // add for transfer to umi
  plugins,
  define: {
duanledexianxianxian's avatar
duanledexianxianxian committed
96
    // 'process.env.APP_NAME': 'http://platform.kuopu.net:9008',
97 98
    ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION:
      ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION || '', // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
99
  },
duanledexianxianxian's avatar
duanledexianxianxian committed
100
  // 配置是否开启 treeShaking,默认关闭。
101
  treeShaking: true,
duanledexianxianxian's avatar
duanledexianxianxian committed
102
  // 配置浏览器最低版本,会自动引入 polyfill 和做语法转换,配置的 targets 会和合并到默认值,所以不需要重复配置。
103 104 105
  targets: {
    ie: 11,
  },
duanledexianxianxian's avatar
duanledexianxianxian committed
106 107
  // 打包后源码 umi已经根据不同环境有默认的源码生成规则
  devtool: false,
108
  // 路由配置
duanledexianxianxian's avatar
duanledexianxianxian committed
109
  routes,
afc163's avatar
afc163 committed
110 111
  // Theme for antd
  // https://ant.design/docs/react/customize-theme-cn
duanledexianxianxian's avatar
duanledexianxianxian committed
112 113
  theme,
  //忽略 moment 的 locale 文件,用于减少尺寸。
愚道's avatar
愚道 committed
114
  ignoreMomentLocale: true,
duanledexianxianxian's avatar
duanledexianxianxian committed
115
  //给 less-loader 的额外配置项
愚道's avatar
愚道 committed
116
  lessLoaderOptions: {
duanledexianxianxian's avatar
duanledexianxianxian committed
117
    // 开启此项,less中能够使用函数 https://github.com/webpack-contrib/less-loader/issues/249
愚道's avatar
愚道 committed
118 119
    javascriptEnabled: true,
  },
duanledexianxianxian's avatar
duanledexianxianxian committed
120
  // 禁用 redirect 上提
121
  disableRedirectHoist: true,
duanledexianxianxian's avatar
duanledexianxianxian committed
122
  // 给 css-loader 的额外配置项
123
  cssLoaderOptions: {
duanledexianxianxian's avatar
duanledexianxianxian committed
124
    // 开启css modules
125
    modules: true,
duanledexianxianxian's avatar
duanledexianxianxian committed
126
    // 生成classname的规则
陈帅's avatar
陈帅 committed
127 128 129 130 131 132 133
    getLocalIdent: (
      context: {
        resourcePath: string;
      },
      localIdentName: string,
      localName: string,
    ) => {
134 135 136 137 138 139 140
      if (
        context.resourcePath.includes('node_modules') ||
        context.resourcePath.includes('ant.design.pro.less') ||
        context.resourcePath.includes('global.less')
      ) {
        return localName;
      }
陈帅's avatar
陈帅 committed
141

142
      const match = context.resourcePath.match(/src(.*)/);
陈帅's avatar
陈帅 committed
143

144 145 146 147
      if (match && match[1]) {
        const antdProPath = match[1].replace('.less', '');
        const arr = slash(antdProPath)
          .split('/')
陈帅's avatar
陈帅 committed
148 149
          .map((a: string) => a.replace(/([A-Z])/g, '-$1'))
          .map((a: string) => a.toLowerCase());
150 151
        return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
      }
陈帅's avatar
陈帅 committed
152

153 154 155
      return localName;
    },
  },
duanledexianxianxian's avatar
duanledexianxianxian committed
156
  // 配置后会生成 asset-manifest.json,option 传给 https://www.npmjs.com/package/webpack-manifest-plugin
157 158 159
  manifest: {
    basePath: '/',
  },
duanledexianxianxian's avatar
duanledexianxianxian committed
160
  // 配置传给 uglifyjs-webpack-plugin@1.x 的配置项。
信鑫-King's avatar
信鑫-King committed
161
  uglifyJSOptions,
duanledexianxianxian's avatar
duanledexianxianxian committed
162
  // 通过 webpack-chain 的 API 扩展或修改 webpack 配置。
163
  chainWebpack: webpackPlugin,
duanledexianxianxian's avatar
duanledexianxianxian committed
164 165 166 167 168 169 170 171 172 173
  // 配置 webpack 的 resolve.alias 属性 默认src=>@
  alias: {
    src: path.join(__dirname, '../src'),
    components: path.join(__dirname, '../src', 'components'),
    utils: path.join(__dirname, '../src', 'utils'),
    assets: path.join(__dirname, '../src', 'assets'),
    themes: path.join(__dirname, '../src', 'themes'),
    config: path.join(__dirname, '../src', 'config'),
    public: path.join(__dirname, '../public'),
  },
何乐's avatar
何乐 committed
174
} as IConfig;