'use strict'
const path = require('path')
const ip = '192.168.1.43'
const os = require('os')
const fs = require("fs")
//判断环境
let ifaces = os.networkInterfaces()
let localityIp = '', result = []
for (let dev in ifaces) {
    ifaces[dev].forEach(function (details) {
        if (localityIp === '' && details.family === 'IPv4' && !details.internal) {
            localityIp = details.address
            return;
        }
    })
}
let getFile = localityIp == ip ? false : true
/******** 获取git版本信息 ********/
const gitHEAD = getFile ? fs.readFileSync('.git/HEAD', 'utf-8').trim() : false
const ref = getFile ? gitHEAD.split(': ')[1] : false
const develop = getFile ? gitHEAD.split('/')[2] : false
const gitVersion = getFile ? fs.readFileSync('.git/' + ref, 'utf-8').trim() : false
const buildEdition = develop && gitVersion ? develop + ': ' + gitVersion : '未配置'
const myDate = new Date()
const buildTime = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate() + ' ' + myDate.getHours() + ':' + myDate.getMinutes();
const logsList = getFile ? fs.readFileSync('.git/logs/HEAD', 'utf-8').replace(/\r\n|\r/g, "\n").split("\n") : false
const buildUser = getFile ? logsList[logsList.length - 2].split(" ")[2] : '未配置'

//生成随机版本编码
const revision = generateRandomString()
process.env.VUE_APP_RENEWAL_ENCODING = revision
require('fs').writeFileSync('public/version.txt', revision)

function generateRandomString() {
    let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    let randomString = "";
    for (let i = 0; i < 24; i++) {
        let randomIndex = Math.floor(Math.random() * chars.length);
        randomString += chars.charAt(randomIndex);
    }
    return randomString;
}

const name = '实验室安全智慧化管控系统' // 网页标题
const {defineConfig} = require('@vue/cli-service')
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
const webpack = require('webpack');
module.exports = defineConfig({
    transpileDependencies: true,
    lintOnSave: false,
    publicPath: "./", // 公共路径(必须有的)
    outputDir: "bigData", // 输出文件目录
    assetsDir: "static", //静态资源文件名称
    productionSourceMap: false, //去除打包后js的map文件
    devServer: {
        host: "localhost", // 默认是localhost
        port: 8081, // 前端项目编译后使用的端口号，跟webpack配置的port同理
        open: true,
        proxy: {
            '/api': {
                target: `http://localhost:8081`,  // 实际跨域请求的API地址
                changeOrigin: true,  // 跨域
                // 请求地址重写  http://front-end/api/login ⇒ http://api-url/login
                pathRewrite: {
                    ['^' + process.env.VUE_APP_BASE_API]: ''
                }
            }
        },
    },
    configureWebpack: {
        resolve: {
            fallback: {
            },
        },
        plugins: [
            new NodePolyfillPlugin(),
            new webpack.ProvidePlugin({
                process: 'process/browser',
            }),
        ]
    },
    chainWebpack(config) {
        //标记打包时间与当前git版本信息
        config.plugin('html').tap(args => {
            args[0].buildUser = buildUser;
            args[0].buildTime = buildTime;
            args[0].buildEdition = buildEdition;
            return args
        })
        config
            .when(process.env.VUE_APP_ENV !== 'www.dlc.com',
                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',
                        minSize: 50000, //构建出来的chunk大于30000才会被分割
                        maxSize: 100000, //会尝试根据这个大小进行代码分割

                    })
                }
            )
    },

})


