"use strict";
const path = require("path");
const defaultSettings = require("./src/settings.js");

function resolve(dir) {
  return path.join(__dirname, dir);
}
const name = defaultSettings.title || "vue Element Admin"; //页面标题

//如果端口设置为80，
//使用管理员权限执行命令行。
//例如，Mac:sudo npm run
//您可以通过以下方法更改端口：
//端口=9527 npm运行开发或npm运行开发--端口=9527
const port = process.env.port || process.env.npm_config_port || 9528; // 开发端口

// dev portal配置项说明可在中找到https://cli.vuejs.org/config/
module.exports = {
  assetsDir: 'static',
  lintOnSave: false,
  productionSourceMap: false,
  //开启运行时编译模式
  runtimeCompiler: true,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
   
  },
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        "@": resolve("src")
      }
    }
  },

  chainWebpack(config) {
    config.plugin("preload").tap(() => [{
      rel: "preload",
      fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
      include: "initial"
    }]);
    config.plugins.delete("prefetch");
    // 设置svg精灵加载程序
    config.module
      .rule("svg")
      .exclude.add(resolve("src/icons"))
      .end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]"
      })
      .end();

    config.when(process.env.NODE_ENV !== "development", config => {
      config
        .plugin("ScriptExtHtmlWebpackPlugin")
        .after("html")
        .use("script-ext-html-webpack-plugin", [{
          // `runtime`必须与runtimeChunk名称相同。默认值为“运行时”`
          inline: /runtime\..*\.js$/
        }])
        .end();
      config.optimization.splitChunks({
        chunks: "all",
        cacheGroups: {
          libs: {
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: "initial" //仅限于最初依赖的第三方
          },
          elementUI: {
            name: "chunk-elementUI", //将elementUI拆分为单个包
            priority: 20, // 重量需要大于libs和app，否则将打包到libs或app中
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/ //为了适应cnpm
          },
          commons: {
            name: "chunk-commons",
            test: resolve("src/components"), // 您可以自定义您的规则
            minChunks: 3, // 最小公共数
            priority: 5,
            reuseExistingChunk: true
          }
        }
      });
      config.optimization.runtimeChunk("single");
    });
  },

  css: {
    loaderOptions: {
      sass: {
        prependData: '\n        @import "@/styles/variables.scss";\n        '
      }
    }
  },

  publicPath: './'
};
