// https://umijs.org/config/
import { defineConfig } from 'umi';
const assetDir = "static"
export default defineConfig({
  history: { type: 'hash' }, // 默认是 browse
  // devtool: 'source-map',//正式坏境查看报错使用，假如正式版稳定请关闭
  //新增
  nodeModulesTransform: {
    type: 'none',
    exclude: [],
  },
  chunks: ['vendors', 'umi'],
  chainWebpack: function (config, { env, webpack, createCSSRule }) {
    // 修改js，js chunk文件输出目录
    config.output
      .filename(assetDir + "/js/[name].[contenthash: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 + "/img/[name].[hash:8].[ext]",
    //         esModule: false,
    //       },
    //     },
    //   }));

    // // 添加gzip压缩
    // config.when(true, (config) => {
    //   config
    //     .plugin("compression-webpack-plugin")
    //     .use(CompressionWebpackPlugin, [
    //       {
    //         filename: "[path][base].gz",
    //         algorithm: "gzip",
    //         test: new RegExp("\\.(js|css)$"),
    //         threshold: 10240,
    //         minRatio: 0.8,
    //         // deleteOriginalAssets: true//删除源文件
    //       },
    //     ]);
    // });
    //通用插件合并
    config.merge({
      optimization: {
        splitChunks: {
          chunks: 'all',
          minSize: 30000,
          minChunks: 3,
          automaticNameDelimiter: '.',
          cacheGroups: {
            vendor: {
              name: 'vendors',
              test({ resource }: any) {
                return /[\\/]node_modules[\\/]/.test(resource);
              },
              priority: 10,
            },
          },
        },
      }
    });
  },
  // 出去console.log
  terserOptions: {
    compress: {
      drop_console: true,
    },
  },
  // map关闭
  devtool: false,
  //替换压缩器为 esbuild
  esbuild: {},
  // 不打包组件使用cdn
  externals: {
    // react: 'window.React',
    // 'react-dom': 'window.ReactDOM',
    // antd: 'window.antd',
    // echarts: 'window.echarts',
    // moment: 'window.moment' 
  },
  scripts: [
    // 'https://unpkg.com/react@17/umd/react.production.min.js',
    // "https://unpkg.com/react-dom@17/umd/react-dom.production.min.js",
    // 'https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.min.js',
    // 'https://lib.baomitu.com/antd/4.16.2/antd-with-locales.min.js',
    // 'https://lib.baomitu.com/antd/4.16.12/antd.min.js',
    // 'https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.js',
  ],
  extraBabelPlugins:['transform-remove-console']
});
