const path = require("path");
const fs = require("fs");
const webpack = require("webpack");
const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
const Visualizer = require("webpack-visualizer-plugin");
const LodashModuleReplacementPlugin = require("lodash-webpack-plugin");

// const defines = {
//     "global.GENTLY": false
// };

const SOURCE = path.resolve(__dirname, "./source");
const DIST = path.resolve(__dirname, "./dist");
const NODE_MODULES = path.resolve(__dirname, "./node_modules");
const BUTTERCUP_CORE = fs.realpathSync(path.resolve(NODE_MODULES, "./buttercup"));
const IOCANE = fs.realpathSync(path.resolve(NODE_MODULES, "./iocane"));
const WEBDAVFS = fs.realpathSync(path.resolve(NODE_MODULES, "./webdav-fs"));

const entry = path.resolve(SOURCE, "./index.js");
const rules = [
    {
        test: /\.json$/i,
        use: "json-loader"
    },
    {
        test: /\.js$/,
        use: "babel-loader",
        include: [
            SOURCE,
            BUTTERCUP_CORE,
            IOCANE,
            WEBDAVFS
        ]
    }
];
const node = {
    crypto: false,
    fs: "empty"
};
const resolve = {
    alias: {
        crypto: require.resolve("crypto-browserify"),
        __buttercup_web: SOURCE
    },
    extensions: [".js"],
    symlinks: false,
    modules: [ NODE_MODULES, BUTTERCUP_CORE, IOCANE, WEBDAVFS ]
};
const stats = { colors: true };
const developmentPlugins = process.env.VIS === "stats" ?
    [ new Visualizer() ] : [];

module.exports = [

    // Raw
    {
        entry,
        module: { rules },
        node,
        output: {
            path: DIST,
            filename: "buttercup.js",
            library: "Buttercup",
            libraryTarget: "umd"
        },
        plugins: [
            // new webpack.DefinePlugin(defines),
            new LodashModuleReplacementPlugin(),
            new webpack.NormalModuleReplacementPlugin(/\/iconv-loader$/, "node-noop"),
            new webpack.IgnorePlugin(/vertx/),
            ...developmentPlugins
        ],
        resolve,
        stats
    },

    // Minified
    {
        entry,
        module: { rules },
        node,
        output: {
            path: DIST,
            filename: "buttercup.min.js",
            library: "Buttercup",
            libraryTarget: "umd"
        },
        plugins: [
            // new webpack.DefinePlugin(defines),
            new LodashModuleReplacementPlugin(),
            new webpack.NormalModuleReplacementPlugin(/\/iconv-loader$/, "node-noop"),
            new webpack.IgnorePlugin(/vertx/),
            new UglifyJSPlugin({
                compress: {
                    warnings: false
                },
                mangle: true,
                comments: false
            })
        ],
        resolve,
        stats
    },

    // Minified + React-Native compat
    {
        entry,
        module: { rules },
        node,
        output: {
            path: DIST,
            filename: "react-native-buttercup.min.js",
            library: "Buttercup",
            libraryTarget: "umd"
        },
        plugins: [
            // new webpack.DefinePlugin(defines),
            new LodashModuleReplacementPlugin(),
            new webpack.NormalModuleReplacementPlugin(/\/iconv-loader$/, "node-noop"),
            new webpack.IgnorePlugin(/vertx/),
            new UglifyJSPlugin({
                compress: {
                    warnings: false
                },
                mangle: true,
                comments: false
            })
        ],
        resolve: Object.assign({
            alias: Object.assign({
                dropbox: "react-native-dropbox-sdk"
            }, resolve.alias)
        }, resolve),
        stats
    }

];

@@ -1332,24 +1332,124 @@
 BDAVFS %5D%0A%7D;%0A
+const resolveRN = Object.assign(%7B%7D, resolve);%0AresolveRN.alias.dropbox = %22react-native-dropbox-sdk%22;%0A
 const stats 
@@ -3631,160 +3631,17 @@
 ve: 
-Object.assign(%7B%0A            alias: Object.assign(%7B%0A                dropbox: %22react-native-dropbox-sdk%22%0A            %7D, resolve.alias)%0A        %7D, resolve)
+resolveRN
 ,%0A  

Fix react-native build`