text: 'use strict'
  const path = require('path')
  // 项目的配置文件
  const defaultSettings = require('./src/settings.js')
  
  function resolve (dir) {
    return path.join(__dirname, dir)
  }
  
  // 设置开发服务器端口
  // console.log('看看：', process.env)
  /**
   * process.env 环境变量：1. development（开发环境）2.production（生产环境）3.staging（灰度环境）
   * 1. npm run dev => 开发环境 => 通过读取.env.development文件获取环境变量
   * 2. npm run build:prod => 生产环境 =》通过读取.env.production文件获取环境变量
   */
  const name = defaultSettings.title || 'vue Admin Template' // page title
  let externals = {}
  let cdn = { css: [], js: [] }
  const isProduction = process.env.NODE_ENV === 'production' // 判断是否是生产环境
  if (isProduction) {
    // 排除大文件
    externals = {
      'vue': 'Vue',
      'element-ui': 'ELEMENT',
      'xlsx': 'XLSX'
    }
    // cdn引入
    cdn = {
      css: [
        // element-ui css 样式表
        'https://cdn.jsdelivr.net/npm/element-ui@2.13.2/lib/theme-chalk/index.css'
      ],
      js: [
        // vue must at first!
        'https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js',
        // element-ui js
        'https://cdn.jsdelivr.net/npm/element-ui@2.13.2/lib/index.js',
        // xlsx
        'https://cdn.jsdelivr.net/npm/xlsx@0.16.6/dist/xlsx.full.min.js'
      ]
    }
  }
  
  // If your port is set to 80,
  // use administrator privileges to execute the command line.
  // For example, Mac: sudo npm run
  // You can change the port by the following methods:
  // port = 9528 npm run dev OR npm run dev --port = 9528
  // process.env 环境变量: 开发环境 、生产环境、灰度环境(最后一次开发无限接近于生产环境)
  /**
   *
   */
  const port = process.env.port || process.env.npm_config_port || 9528 // dev port
  
  // All configuration item explanations can be find in https://cli.vuejs.org/config/
  module.exports = {
    /**
     * You will need to set publicPath if you plan to deploy your site under a sub path,
     * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
     * then publicPath should be set to "/bar/".
     * In most cases please use '/' !!!
     * Detail: https://cli.vuejs.org/config/#publicpath
     */
    publicPath: '/',
    outputDir: 'dist',
    assetsDir: 'static',
    lintOnSave: process.env.NODE_ENV === 'development',
    productionSourceMap: false,
    devServer: {
      port: port,
      open: true,
      overlay: {
        warnings: false,
        errors: true
      },
      // 配置proxy代理服务器
      proxy: {
        // 表示如果我们的请求地址有/api的时候,就出触发代理机制
        '/api': {
          // 删除数据太少用线上的，老师的太少 但是线上的登录不上
          target: 'http://8.142.65.7:3000', // 跨域请求的地址=>告诉代理服务器去哪里请求数据
          // target: 'http://ihrm-java.itheima.net',
          changeOrigin: true // 是否跨域 需要设置此值为true 才可以让本地服务代理我们发出请求 默认是true可以省略
          // 重写路径----------在后台没有统一前缀情况下使用，用于去除前端临时添加的/api前缀
          // pathRewrite: {
          //   '^/api': ''
          // }
        }
      }
      // before: require('./mock/mock-server.js')
    },
    configureWebpack: {
      // provide the app's title in webpack's name field, so that
      // it can be accessed in index.html to inject the correct title.
      name: name,
      externals: externals,
      resolve: {
        alias: {
          '@': resolve('src')
        }
      }
    },
    chainWebpack (config) {
      // it can improve the speed of the first screen, it is recommended to turn on preload
      config.plugin('preload').tap(() => [
        {
          rel: 'preload',
          // to ignore runtime.js
          // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
          fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
          include: 'initial'
        }
      ])
      // 注入cdn变量 (打包时会执行)
      config.plugin('html').tap(args => {
        args[0].cdn = cdn // 配置cdn给插件
        return args
      })
  
      // when there are many pages, it will cause too many meaningless requests
      config.plugins.delete('prefetch')
  
      // set svg-sprite-loader
      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` must same as runtimeChunk name. default is `runtime`
                inline: /runtime\..*\.js$/
              }])
              .end()
            config
              .optimization.splitChunks({
                chunks: 'all',
                cacheGroups: {
                  libs: {
                    name: 'chunk-libs',
                    test: /[\\/]node_modules[\\/]/,
                    priority: 10,
                    chunks: 'initial' // only package third parties that are initially dependent
                  },
                  elementUI: {
                    name: 'chunk-elementUI', // split elementUI into a single package
                    priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                    test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                  },
                  commons: {
                    name: 'chunk-commons',
                    test: resolve('src/components'), // can customize your rules
                    minChunks: 3, //  minimum common number
                    priority: 5,
                    reuseExistingChunk: true
                  }
                }
              })
            // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
            config.optimization.runtimeChunk('single')
          }
        )
    }
  }
  