const path = require('path')

/* Cesium */
const CopyPlugin = require("copy-webpack-plugin")
const HtmlPlugin = require("html-webpack-plugin")
const HtmlTagsPlugin = require('html-webpack-tags-plugin');
const HtmlIncludeAssetsPlugin = require("html-webpack-include-assets-plugin")
const webpack = require("webpack")

module.exports = {
    entry: './chroma-react-ui.js',
    mode: 'none',
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        "presets" : ["@babel/preset-env","@babel/preset-react"],
                        "plugins": [
                            '@babel/transform-runtime'
                        ]
                    },
                }
            },
            {
                test: /\.css$/i,
                use: ['style-loader', 'css-loader'],
            },
        ]
    },
    "plugins": [
        // new CopyWebpackPlugin({
        //     patterns: [
        //         {
        //           from: "node_modules/cesium/Build/Cesium",
        //           to: "cesium",
        //         }
        //     ]
        // }),
        // new HtmlWebpackPlugin(),
        // new HtmlWebpackTagsPlugin({
        //     append: false,
        //     assets: ["cesium/Widgets/widgets.css", "cesium/Cesium.js"],
        // }),
        // new webpack.DefinePlugin({
        //     CESIUM_BASE_URL: JSON.stringify("/cesium"),
        // }),
        new webpack.DefinePlugin({
            CESIUM_BASE_URL: JSON.stringify("dist/cesium")
        }),
        // new CopyPlugin({
        //     patterns: [{
        //         from: "node_modules/cesium/Build/Cesium",
        //         to: "cesium",
        //     }]}),
        new CopyPlugin({
            patterns: [{
                from: "./node_modules/cesium/Build/Cesium",
                to: "cesium"
            }]
        }),
        new HtmlPlugin({
            template: 'dist/index.html'
        }),
        // new HtmlPlugin({
        //     filepath: path.resolve(__dirname, 'dist/index.html'),
        //     template: 'dist/index.html',
        //     scripts: ['cesium/Cesium.js']
        // }),
        new HtmlIncludeAssetsPlugin({
            append: false,
            assets: ["cesium/Widgets/widgets.css", "cesium/Cesium.js"],
        }),
        // new HtmlTagsPlugin({
        //     append: false,
        //     tags: ["cesium/Widgets/widgets.css", "cesium/Cesium.js"],
        // })
    ],
    output: {
        filename: 'chroma-react-ui.js',
        path: path.resolve(__dirname, "dist"),
        publicPath: "/dist/",
        libraryTarget: "umd",
        library: ["chroma-react-ui"],
        umdNamedDefine: true
    },
    externals: {
        /* Cesium */
        cesium: "Cesium",
        // Use parents react/react-dom
        react: {          
            commonjs: "react",          
            commonjs2: "react",          
            amd: "React",          
            root: "React"      
        },      
        "react-dom": {          
            commonjs: "react-dom",          
            commonjs2: "react-dom",          
            amd: "ReactDOM",          
            root: "ReactDOM"      
        }  
    },
    resolve: {
        alias: {
          cesium$: "cesium/Cesium",
          cesium: "cesium/Source"
        }
    }
}