const path = require('path')
const webpack = require('webpack')
const createThemeColorReplacerPlugin = require('./config/plugin.config')
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
var baseURL = process.env.VUE_APP_API_CTX_PATH
//生产环境下打包保留debugger
//const TerserPlugin = require('terser-webpack-plugin')
//生产环境下组件获取路径 __file ;
const VueFilenameInjector = require('@d2-projects/vue-filename-injector')

function resolve (dir) {
    return path.join(__dirname, dir)
}

// webpack build externals
const prodExternals = {
    // key表示包名(import foo from 'xx' 里的xx)
    // value表示window下的全局变量名(库暴露出来的namespace,可查lib对应的webpack配置里的library字段)
    'vue': 'Vue',
    'axios': 'axios',
    'vue-router': 'VueRouter',
    'vuex': 'Vuex',
    /*'jpaas-common-lib': 'jpaas-commons',*/
    'moment': 'moment',
    '@antv/g2': 'G2',
    '@antv/data-set': 'DataSet',
    'ant-design-vue': 'antd'
}

// publicPath: "/jpaas",
// vue.config.js
const vueConfig = {
    publicPath: baseURL,
    configureWebpack: {
        externals: prodExternals,
        plugins: [
            // Ignore all locale files of moment.js
            new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
            new webpack.IgnorePlugin(/moment\//),
            new webpack.ProvidePlugin({
                'window.Quill': 'quill/dist/quill.js',
                'Quill': 'quill/dist/quill.js'
            })
            // new BundleAnalyzerPlugin()
        ]
    },
    assetsDir: 'static',
    chainWebpack: config => {
        // config.entry('polyfill').add('@babel/polyfill');
        config.resolve.alias.set('@$', resolve('src'))
            .set('@static', resolve('static'))
            .set('@img', resolve('static/img'))
        const svgRule = config.module.rule('svg')
        svgRule.uses.clear()
        svgRule
            .oneOf('inline')
            .resourceQuery(/inline/)
            .use('vue-svg-icon-loader')
            .loader('vue-svg-icon-loader')
            .end()
            .end()
            .oneOf('external')
            .use('file-loader')
            .loader('file-loader')
            .options({
                name: 'assets/[name].[hash:8].[ext]'
            })
        /*config.optimization.minimizer([//生产环境下保留debugger
                new TerserPlugin({
                    terserOptions: {
                        mangle: true,
                        compress: {
                            drop_debugger: false
                        }
                    }
                })
            ]
        )*/


        //公共代码抽离
        //   config.optimization.splitChunks({
        //     cacheGroups: {
        //         // vendor: {
        //         //     chunks: 'initial',
        //         //     test: /node_modules/,
        //         //     name: 'vendor',
        //         //     minChunks: 1,
        //         //     maxInitialRequests: 5,
        //         //     minSize: 5*1024,
        //         //     priority: 5
        //         // },
        //         // common: {
        //         //     chunks: 'initial',
        //         //     test: /[\\/]src[\\/]js[\\/]/,
        //         //     name: 'common',
        //         //     minChunks: 2,
        //         //     maxInitialRequests: 5,
        //         //     minSize: 5*1024,
        //         //     priority: 1
        //         // },
        //       jpaasCommonLib: {
        //         chunks: 'initial',
        //         test: /[\\/]node_modules[\\/]jpaas-common-lib[\\/]/,
        //         name: 'jpaas-common-lib',
        //         maxInitialRequests: 5,
        //         priority: 2
        //       },
        //       jpaasFormComponent: {
        //         chunks: 'initial',
        //         test: /[\\/]node_modules[\\/]jpaas-form-component[\\/]/,
        //         name: 'jpaas-form-component',
        //         maxInitialRequests: 5,
        //         priority: 3
        //       },
        //       rxKFormDesign: {
        //         chunks: 'initial',
        //         test: /[\\/]node_modules[\\/]rx-k-form-design[\\/]/,
        //         name: 'rx-k-form-design',
        //         maxInitialRequests: 5,
        //         priority: 4
        //       },
        //         // styles: {
        //         //     name: 'styles',
        //         //     test: /\.(sa|sc|c)ss$/,
        //         //     chunks: 'all',
        //         //     enforce: true
        //         // },
        //         // runtimeChunk: {
        //         //     name: 'manifest'
        //         // }
        //     }
        // })

        VueFilenameInjector(config, {
            propName: '__source' // default
        })
    },

    css: {
        loaderOptions: {
            less: {
                lessOptions: {
                    javascriptEnabled: true
                }

            }
        }
    },

  devServer: {
    // development server port 8000
    port: 8003,
    disableHostCheck: true,
    proxy: {
        '/api-knowledge': {
            // target: 'http://10.5.100.64:5208',
            target: 'http://10.5.100.230:9009',//服务器
            // target: 'http://www.hkcqjy.com.cn:8088',
            // target: 'http://192.168.0.185:7206',    // 张哲
            // target: 'http://192.168.0.205:7206',//柳哥
            // target: 'http://10.5.0.233:9900',
            // target: 'http://10.5.100.64:9900',
            pathRewrite: { '^/api': '' },
            ws: false,
            changeOrigin: true
        },
      '/api': {
        // target: 'http://localhost:9900',
        // target: 'http://10.5.0.233:9900',
        //target: 'http://10.5.100.64:9900',
        target: 'http://www.hkcqjy.com.cn:8088',
        //target: 'http://10.5.100.230:9009',
        //target: 'http://10.5.100.64:5208',
        //target: 'http://192.168.0.176:7206',//柳哥
        //target: 'http://192.168.0.109:7206',//白哥
        pathRewrite: { '^api': '' },
        ws: false,
        changeOrigin: true
      }
    }
  },

    // disable source map in production
    productionSourceMap: false,
    lintOnSave: undefined,
    // babel-loader no-ignore node_modules/*
    transpileDependencies: []
}

// 如果你不想在生产环境开启换肤功能,请打开下面注释
// if (process.env.VUE_APP_PREVIEW === 'true') {
// add `ThemeColorReplacer` plugin to webpack plugins
vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
vueConfig.configureWebpack.plugins.push(new MonacoWebpackPlugin())
// }

module.exports = vueConfig