import { defineConfig, utils } from 'umi';
import { resolve } from 'path';
const fs = require('fs');
const path = require('path');
const lessToJs = require('less-vars-to-js');
const { winPath } = utils;

export default defineConfig({
  //mfsu配置暂时有问题
  // mfsu: {
  //   mfName: 'micro-slave'
  // },
  title: 'hi demo',
  runtimePublicPath: true,
  webpack5: {},
  qiankun: {
    slave: {},
  },
  hash: true,
  history: {
    type: 'hash',
  },
  ignoreMomentLocale: true,
  nodeModulesTransform: {
    type: 'none',
    exclude: [],
  },
  locale: {
    default: 'zh-CN',
    baseNavigator: true,
    antd: true,
  },
  lessLoader: {
    javascriptEnabled: true,
    modifyVars: {
      'root-entry-name': 'default', //解决 Variable @root-entry-name is undefined 错误
    },
  },
  dynamicImport: {
    // loading: '@/lib/PageLoading',
  },
  extraBabelPlugins: [],
  cssLoader: {
    localsConvention: 'camelCase',
    modules: {
      getLocalIdent: (context: { resourcePath: string }, _: string, localName: string) => {
        if (
          context.resourcePath.includes('pages/home') ||
          context.resourcePath.includes('node_modules') ||
          context.resourcePath.includes('ant.[id].pro.less') ||
          context.resourcePath.includes('global.less')
        ) {
          return localName;
        }
        const match = context.resourcePath.match(/src(.*)/);
        if (match && match[1]) {
          let antdProPath = match[1].replace('.less', '');
          antdProPath = antdProPath.replace(/\[/g, '');
          antdProPath = antdProPath.replace(/\]/g, '');
          const arr = winPath(antdProPath)
            .split('/')
            .map((a: string) => a.replace(/([A-Z])/g, '-$1'))
            .map((a: string) => a.toLowerCase());
          return `lys-${arr.join('-')}-${localName}`.replace(/--/g, '-');
        }
        return localName;
      },
    },
  },
  define: {
    _VERSION_: '1.1.0',
  },
  alias: {
    themes: resolve(__dirname, '../src/themes'),
    '@/common': resolve(__dirname, '../src/common/'),
    '@/utils': resolve(__dirname, '../src/utils/'),
    '@/config': resolve(__dirname, '../src/config/'),
    '@/lib': resolve(__dirname, '../src/lib/'),
  },
  dva: {
    disableModelsReExport: true,
    lazyLoad: true,
    immer: {
      enableES5: true,
    },
    hmr: false,
  },
  theme: lessToJs(
    fs.readFileSync(path.join(__dirname, '../src/themes/default.less'), 'utf8'),
  ),
  externals: {
    jquery: 'jQuery',
    $: 'jQuery',
    zTree: 'zTree',
  },
  authorize: [
    {
      guard: ['@/gard/UserGard.tsx'],
      include: /\/.*/,
      exclude: /\/passport\/.*/,
    },
    {
      guard: ['@/gard/AdminGard.tsx'],
      include: /\/manage.*/,
    },
  ],
});
