config.ts 7.56 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

duanledexianxianxian's avatar
duanledexianxianxian committed
8 9 10
import routes from './routes.config';
import theme from './theme.config';

duanledexianxianxian's avatar
duanledexianxianxian committed
11
const path = require('path');
陈帅's avatar
陈帅 committed
12

duanledexianxianxian's avatar
duanledexianxianxian committed
13
const { pwa } = defaultSettings; // 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

duanledexianxianxian's avatar
duanledexianxianxian committed
16 17
const { ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION, TEST, NODE_ENV } = process.env; // 插件设置

陈小聪'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
      }
    : {};
91 92 93 94
export default {
  // add for transfer to umi
  plugins,
  define: {
duanledexianxianxian's avatar
duanledexianxianxian committed
95
    // 'process.env.APP_NAME': 'http://platform.kuopu.net:9008',
96 97
    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 专用环境变量,请不要在你的项目中使用它。
98
  },
duanledexianxianxian's avatar
duanledexianxianxian committed
99
  // 配置是否开启 treeShaking,默认关闭。
100
  treeShaking: true,
duanledexianxianxian's avatar
duanledexianxianxian committed
101
  // 配置浏览器最低版本,会自动引入 polyfill 和做语法转换,配置的 targets 会和合并到默认值,所以不需要重复配置。
102 103 104
  targets: {
    ie: 11,
  },
duanledexianxianxian's avatar
duanledexianxianxian committed
105 106
  // 打包后源码 umi已经根据不同环境有默认的源码生成规则
  devtool: false,
107
  // 路由配置
duanledexianxianxian's avatar
duanledexianxianxian committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  routes: [
    {
      path: '/',
      component: '../layouts/BlankLayout',
      routes: [
        {
          path: '/user',
          component: '../layouts/UserLayout',
          routes: [
            {
              path: '/user',
              redirect: '/user/login',
            },
            {
              name: 'login',
              path: '/user/login',
              component: './user/login',
            },
          ],
        },
        {
          path: '/',
          component: '../layouts/BasicLayout',
          Routes: ['src/pages/Authorized'],
          authority: ['admin', 'user'],
          routes: [
            {
              path: '/dashboard',
              name: 'dashboard',
              icon: 'dashboard',
              routes: [],
            },
            {
              path: '/form',
              icon: 'form',
              name: 'form',
              routes: [],
            },
            {
              path: '/list',
              icon: 'table',
              name: 'list',
              routes: [
                {
                  path: '/list/search',
                  name: 'search-list',
                  component: './list/search',
                  routes: [
                    {
                      path: '/list/search',
                      redirect: '/list/search/articles',
                    },
                  ],
                },
              ],
            },
            {
              path: '/profile',
              name: 'profile',
              icon: 'profile',
              routes: [],
            },
            {
              name: 'result',
              icon: 'check-circle-o',
              path: '/result',
              routes: [],
            },
            {
              name: 'exception',
              icon: 'warning',
              path: '/exception',
              routes: [],
            },
            {
              name: 'account',
              icon: 'user',
              path: '/account',
              routes: [],
            },
            {
              name: 'editor',
              icon: 'highlight',
              path: '/editor',
              routes: [],
            },
            {
              path: '/',
              redirect: '/dashboard/analysis',
              authority: ['admin', 'user'],
            },
          ],
        },
      ],
    },
  ],
afc163's avatar
afc163 committed
204 205
  // Theme for antd
  // https://ant.design/docs/react/customize-theme-cn
duanledexianxianxian's avatar
duanledexianxianxian committed
206 207
  theme,
  //忽略 moment 的 locale 文件,用于减少尺寸。
愚道's avatar
愚道 committed
208
  ignoreMomentLocale: true,
duanledexianxianxian's avatar
duanledexianxianxian committed
209
  //给 less-loader 的额外配置项
愚道's avatar
愚道 committed
210
  lessLoaderOptions: {
duanledexianxianxian's avatar
duanledexianxianxian committed
211
    // 开启此项,less中能够使用函数 https://github.com/webpack-contrib/less-loader/issues/249
愚道's avatar
愚道 committed
212 213
    javascriptEnabled: true,
  },
duanledexianxianxian's avatar
duanledexianxianxian committed
214
  // 禁用 redirect 上提
215
  disableRedirectHoist: true,
duanledexianxianxian's avatar
duanledexianxianxian committed
216
  // 给 css-loader 的额外配置项
217
  cssLoaderOptions: {
duanledexianxianxian's avatar
duanledexianxianxian committed
218
    // 开启css modules
219
    modules: true,
duanledexianxianxian's avatar
duanledexianxianxian committed
220
    // 生成classname的规则
陈帅's avatar
陈帅 committed
221 222 223 224 225
    getLocalIdent: (
      context: {
        resourcePath: string;
      },
      localIdentName: string,
duanledexianxianxian's avatar
duanledexianxianxian committed
226
      localName: string
陈帅's avatar
陈帅 committed
227
    ) => {
228 229 230 231 232 233 234
      if (
        context.resourcePath.includes('node_modules') ||
        context.resourcePath.includes('ant.design.pro.less') ||
        context.resourcePath.includes('global.less')
      ) {
        return localName;
      }
陈帅's avatar
陈帅 committed
235

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

238 239 240 241
      if (match && match[1]) {
        const antdProPath = match[1].replace('.less', '');
        const arr = slash(antdProPath)
          .split('/')
陈帅's avatar
陈帅 committed
242 243
          .map((a: string) => a.replace(/([A-Z])/g, '-$1'))
          .map((a: string) => a.toLowerCase());
244 245
        return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
      }
陈帅's avatar
陈帅 committed
246

247 248 249
      return localName;
    },
  },
duanledexianxianxian's avatar
duanledexianxianxian committed
250
  // 配置后会生成 asset-manifest.json,option 传给 https://www.npmjs.com/package/webpack-manifest-plugin
251 252 253
  manifest: {
    basePath: '/',
  },
duanledexianxianxian's avatar
duanledexianxianxian committed
254
  // 配置传给 uglifyjs-webpack-plugin@1.x 的配置项。
信鑫-King's avatar
信鑫-King committed
255
  uglifyJSOptions,
duanledexianxianxian's avatar
duanledexianxianxian committed
256
  // 通过 webpack-chain 的 API 扩展或修改 webpack 配置。
257
  chainWebpack: webpackPlugin,
duanledexianxianxian's avatar
duanledexianxianxian committed
258 259 260 261 262 263 264 265 266 267
  // 配置 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
268
} as IConfig;