let vueConfig = require("sasp-base/src/assets/js/bin/vueConfig.js");
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const { VueLoaderPlugin } = require('vue-loader')
const requireContext = require('require-context');
const FileManagerPlugin = require('filemanager-webpack-plugin');
const { name } = require('./package');

function resolve(dir) {
    return path.join(__dirname, dir)
}
// 字体文件class名称数组
let fontPaths = [
    "./node_modules/sasp-form-render/src/assets/font/iconfont.css",
    "./node_modules/element-ui/packages/theme-chalk/lib/icon.css",
    // "./src/assets/font/iconfont.css"
];

// 自动将组件信息写入JSON
// 后续可能会利用自定义块解决组件信息
// if (process.env.NODE_ENV === 'production') {
//     let components = [];
//     const requireComps = requireContext(resolve("src/views/components/custom"), true, /\index.vue$/);
//     requireComps.keys().forEach(item => {
//         let matchArr = JSON.stringify(fs.readFileSync(requireComps.resolve(item), 'utf-8')).match(/customCompKey:(.*?)customCompSign/g);
//         let analysisStr = `return {${matchArr[0].split('customCompSign')[0].replace(/(\\r\\n)/g,'')}}`;
//         let infos = new Function(analysisStr)();
//         components.push({
//             key: infos.customCompKey,
//             name: infos.customCompName,
//             type: infos.customCompType
//         })
//     });
//     fs.writeFile(resolve("src/views/compInfo.json"), JSON.stringify(components, null, "\t"), err => {
//         if (!!err) {
//             return console.log(chalk.red.bold(
//                 `\n\0\0 Building for production 组件信息写入JSON失败...`
//             ));
//         }
//         console.log(chalk.green.bold(
//             `\n\0\0 Building for production 组件信息写入JSON成功...`
//         ));
//     });
// }

module.exports = {
    publicPath: "./", // 只有普通build的时候才需要./,其他模式都不需要
    assetsDir: "static", // 编译后静态文件夹名称
    productionSourceMap: false, // 是否在构建生产包时生成 sourceMap 文件，false将提高构建速度
    filenameHashing: true, // 生成的静态资源在它们的文件名中包含了 hash 以便更好的控制缓存
    lintOnSave: false, // eslint-loader 是否在保存的时候检查
    devServer: {
        open: true, // 启动成功自动打开浏览器
        port: 8999, // 启动端口
        headers: {
            'Access-Control-Allow-Origin': '*',
        }
    },
    chainWebpack: config => {
       // 移除 prefetch 插件
        config.plugins.delete('prefetch');
        // 移除 preload 插件
        config.plugins.delete('preload');

        config.resolve.alias
            .set('@', resolve('src'))
            .set('utils', resolve('src/utils'))
            .set('packages', resolve('packages'))
    },
    runtimeCompiler: true,
    configureWebpack: function (config) {
        if (process.env.ANALAYZ) {
            config
                .plugin('webpack-bundle-analyzer')
                .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin);
        }

       /* // 用于提供给qiankun识别
        Object.assign(config.output, {
            library: `${name}-[name]`,
            libraryTarget: 'umd', // 把微应用打包成 umd 库格式
            jsonpFunction: `webpackJsonp_${name}`,
        });*/

        vueConfig.init(config, {
            fontPaths
        });
        config.plugins.push(new FileManagerPlugin({
            events: (() => {
                if (process.env.NODE_ENV === 'production') {
                    return {
                        onEnd: {
                            move: [
                                {source: './src/views/compInfo.json', destination: './package/static/compInfo.json'}
                            ],
                            delete: [
                                './src/views/compInfo.json'
                            ]
                        }
                    }
                } else {
                    return {};
                }
            })()
        }))
    },
    css: {
        extract: false,
        sourceMap: false
    }
};
