const HtmlWebPackPlugin = require("html-webpack-plugin"); //打包html
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); //css抽取独立文件分开打包压缩
const {CleanWebpackPlugin} = require('clean-webpack-plugin'); //清除文件夹
const CopyWebpackPlugin = require('copy-webpack-plugin'); //复制文件
const WebpackSftpClient = require('webpack-sftp-client'); //自动上传

const path = require("path");

module.exports = (env, options) => {
    let outPath = "dist",
        plugins = [
            new CleanWebpackPlugin(),
            new CopyWebpackPlugin([
                {
                    from: 'src/assets',
                    to: 'assets'
                }
            ]),
            new MiniCssExtractPlugin({filename: "[name][hash].css", chunkFilename: "[id][hash].css"}),
            new HtmlWebPackPlugin({template: './src/index.html', title: '竹香米业数字农业精准管理图', chunksSortMode: "none", inject: true})
        ];
    if (options.mode === 'development') {
        outPath = 'dev'
    } else {
        /*plugins.push(new WebpackSftpClient({
            port: '22', //服务器端口
            host: '172.16.90.253', //服务器地址
            username: 'root', //用户名
            password: 'hywa$123', //密码
            path: path.resolve(__dirname, outPath), //本地路径
            remotePath: '/www/演示项目/湄潭一期/tpl-H5', //服务器上的路径
            verbose: true
        }))*/
    }
    return {
        entry: {
            index: "./src/index.js"
        },
        output: {
            filename: `js/[hash]${parseInt(Math.random()*10000)}.js`,
            path: path.resolve(__dirname, outPath),
            publicPath: ''
        },
        plugins,
        module: {
            rules: [
                {
                    test: /\.(js|jsx)$/,
                    exclude: /node_modules/,
                    include: path.resolve(__dirname, "src"),
                    use: [
                        {
                            loader: "babel-loader"
                        }
                    ]
                }, {
                    test: /\.(sa|sc|)ss$/,
                    use: [
                        {
                            loader: MiniCssExtractPlugin.loader,
                            options: {
                                hmr: options.mode === 'development'
                            }
                        }, {
                            loader: "css-loader",
                            options: {
                                modules: false, //css modules
                            }
                        },
                        'sass-loader'
                    ]
                }, {
                    test: /\.css$/,
                    use: [
                        {
                            loader: MiniCssExtractPlugin.loader,
                            options: {
                                hmr: options.mode === 'development'
                            }
                        }, {
                            loader: "css-loader",
                            options: {
                                modules: true, //css modules
                            }
                        }
                    ]
                }, {
                    test: /\.(jpe?g|png|gif|webp)$/,
                    use: [
                        {
                            loader: "url-loader",
                            options: {
                                // Inline files smaller than 10 kB (10240 bytes)
                                limit: 10 * 1024
                            }
                        }
                    ]
                }, {
                    test: /\.(wsv|ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
                    use: [
                        {
                            loader: "file-loader",
                            options: {
                                name: "build/[name].[ext]"
                            }
                        }
                    ]
                }
            ]
        },
        externals: [(context, request, callback) => {
                if (/pe-wasm$/.test(request)) {
                    return callback(null, "amd " + request);
                }
                callback();
            }
        ],
        resolve: {
            modules: [
                path.resolve(__dirname, "/src"),
                "node_modules/"
            ],
            extensions: [".js", ".scss"]
        },
        node: {
            process: false,
            global: false,
            fs: "empty"
        },
        devServer: {
            disableHostCheck: true,
            historyApiFallback: true,
            host: '0.0.0.0',
            port: 9008,
            proxy: {
                '/screen':{
                    target: 'http://127.0.0.1:9008',
                    pathRewrite: {
                        '^/screen': ''
                    },
                    changeOrigin: true
                },
                '/htdata': {
                    // target: 'http://47.111.224.169:8310',
                    target: 'http://lyfbht.hw.hongweisoft.com',
                    // pathRewrite: {
                    //     '^/htdata': ''
                    // },
                    changeOrigin: true
                },
                '/product': {
                    target: 'http://ghost.nat300.top',
                    pathRewrite: {
                        '^/product': ''
                    },
                    changeOrigin: true
                },
                '/forest-admin':{
                    target: 'http://129.28.174.150:7050',
                    changeOrigin: true
                }
            }
        }
    }
}
