const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const publicPath = require('./src/config/isc-publicPath.js')
// const baseData = require('./src/config/isc-baseData-local.js')
const baseData = require('./src/config/isc-baseData.js')
let moment = require("moment")

process.env.VUE_APP_BASETHEME = 'purple';
if (process.env.NODE_ENV === 'production') {
	process.env.VUE_APP_VERSION = moment(new Date()).format('YYYYMMDD HHmmss')
}
const WebpackBundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css']

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

module.exports = {
	publicPath: publicPath + '/',
	outputDir: 'sstf',
	assetsDir: 'static',
	productionSourceMap: false,
	runtimeCompiler: true,
	devServer: {
		inline: true,
		disableHostCheck: true,
		port: 8088,
		//proxy: baseData.schemes + '://' +baseData.host,
		proxy: {
			'/webapi/funds': {
				target: baseData.schemes + '://' + baseData.hostFunds,
				changeOrigin: false,
				pathRewrite: {
					'^/webapi/funds': '/funds'
				}
			},
			'/webapi/gjzf/': {
				target: baseData.schemes + '://' + baseData.hostGjzf,
				changeOrigin: false,
				pathRewrite: {
					'^/webapi/gjzf': '/gjzf'
				}
			},	
			'/webapi/manager/': {
				target: baseData.schemes + '://' + baseData.host,
				changeOrigin: false,
				pathRewrite: {
					'^/webapi/manager': '/manager'
				}
			},
			'/webapi/admin/': {
				target: baseData.schemes + '://' + baseData.hostAdmin,
				changeOrigin: false,
				pathRewrite: {
					'^/webapi/admin': '/admin'
				}
			},
			'/webapi/public/': {
				target: baseData.schemes + '://' + baseData.hostPublic,
				changeOrigin: true,
				pathRewrite: {
					'^/webapi/public': '/public'
				}
			},
			'/webapi/business/': {
				target: baseData.schemes + '://' + baseData.hostBusiness,
				changeOrigin: true,
				pathRewrite: {
					'^/webapi/business': '/business'
				}
			},
			'/webapi/Financing/': {
						target: baseData.schemes + '://' + baseData.hostFinance,
						changeOrigin: true,
						pathRewrite: {
							'^/webapi/Financing': '/financing'
						}
			},
			'/webapi/Remittance/': {
				target: baseData.schemes + '://' + baseData.hostRemittance,
				changeOrigin: true,
				pathRewrite: {
					'^/webapi/Remittance': '/remit'
				}
			},
			'/webapi/Lc/': {
						target: baseData.schemes + '://' + baseData.hostLc,
						changeOrigin: true,
						pathRewrite: {
							'^/webapi/Lc': '/lc'
						}
			},
			'/webapi/Domlc/': {
						target: baseData.schemes + '://' + baseData.hostDomlc,
						changeOrigin: true,
						pathRewrite: {
							'^/webapi/Domlc': '/domlc'
						}
			},
			'/webapi/Collection/': {
						target: baseData.schemes + '://' + baseData.hostCollection,
						changeOrigin: true,
						pathRewrite: {
							'^/webapi/Collection': '/collection'
						}
			},
			'/webapi/Derivative/': {
						target: baseData.schemes + '://' + baseData.hostDerivative,
						changeOrigin: true,
						pathRewrite: {
							'^/webapi/Derivative': '/derivative'
						}
			},
			'/webapi/report/': {
				target: baseData.schemes + '://' + baseData.hostReport,
				changeOrigin: true,
				pathRewrite: {
					'^/webapi/report': '/report'
				}
			},
			'': {
				target: baseData.schemes + '://' + baseData.host,
				changeOrigin: true
			}
		},
		historyApiFallback: {
			verbose: true,
			rewrites: [{
					from: /^((?!\/webscan).)+$/,
					to: "/index.html"
				},
				{
					from: /^\/webscan.*$/,
					to: "/scanuse.html"
				},
			]
		}
	},
	chainWebpack: config => {
		config.resolve.alias
      .set('~', resolve('src'))
    config.externals ({'BopRules': 'BopRules','RcpmisRules':'RcpmisRules','CFARules':'CFARules','JSHRules':'JSHRules'})
	},
	configureWebpack: config => {
		if (process.env.NODE_ENV !== 'production') {
			// 开发环境下的source map配置
			config.devtool = 'source-map';
		}
		if (process.env.NODE_ENV === 'production') {
			// 将每个依赖包打包成单独的js文件
			let optimization = {
				runtimeChunk: 'single',
				splitChunks: {
					// chunks: 'all', 
					maxInitialRequests: Infinity,
					minSize: 20000, // 依赖包超过20000bit将被单独打包
					cacheGroups: {
						//官方名称
						vendors: {
							test: /[\\/]node_modules[\\/]/,
							//enforce: true,
							name(module) {
								// or node_modules/packageName
								const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
								// npm package names are URL-safe, but some servers don't like @ symbols
								return `npm.${packageName.replace('@', '')}`
							}
						},
					}
				}
			}
			Object.assign(config, {
				optimization
			})
			//放开下行注释可以分析打包体积分布
			//config.plugins.push(new WebpackBundleAnalyzerPlugin())
			//开启gizp压缩
			config.plugins.push(new CompressionWebpackPlugin({
				algorithm: 'gzip',
				test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
				threshold: 10240,
				minRatio: 0.8
			}))
		}

		config.plugins.push(new CopyWebpackPlugin([{
			from: path.resolve(__dirname + '/static'),
			to: path.resolve(__dirname + '/sstf/static'),
			toType: 'dir'
		},  
		{
			from: path.resolve(__dirname + '/testf5.html'),
			to: path.resolve(__dirname + '/sstf/testf5.html')
    }]))
	},
	pages: {
		index: {
			entry: "src/main.js",
			template: "index.html",
      filename: "index.html",
      chunks: 'all'
		}
	// 	,
	// 	scanuse: {
	// 		entry: "src/scanuse.js",
	// 		template: "scanuse.html",
    //   filename: "scanuse.html",
    //   chunks: ["chunk-vendors","chunk-common","scanuse","runtime"]
	// 	}
	},
	transpileDependencies: ['sock-js', 'sockjs-client']
}