const path = require("path");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const FileManagerPlugin = require('filemanager-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TypedocWebpackPlugin = require('typedoc-webpack-plugin');
const webpack = require('webpack');

let confBundles = {
    entry: {
        "easyquery.ui-jquery": "./src/public_api.ts",
        "easyquery.ui-jquery.min": "./src/public_api.ts"
    },
    devtool: 'source-map',
    stats: {warnings:false},
    output: {
        library: "easyqueryUiJQuery",
        path: path.resolve(__dirname, 'dist/bundles'),
        filename: "[name].js",
        umdNamedDefine: true,
        libraryTarget: 'umd'
    },
    externals: {
        "@easyquery/core": "easyqueryCore",
        "@easyquery/broker-eqs": "easyqueryBrokerEqs",
        "@easyquery/ui": "easyqueryUi"
    },  
    resolve: {
        extensions: [".js", ".ts"]
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: "ts-loader"
            }
        ]
    },
    optimization: {
        minimizer: [new UglifyJsPlugin({
            include: /\.min\.js$/,
            sourceMap: true
        })]
    },
	plugins: [
		new FileManagerPlugin({
			onEnd: {
				copy: [
				  { source: './assets/**/*', destination: './dist/assets' }
				]
			}
		}),
		new TypedocWebpackPlugin({
            mode: 'file',
			json: '../../../../docs/ui-jquery.json',
			includeDeclarations: false,
			ignoreCompilerErrors: true
        })
	]
};

let confBrowser = {
    entry: {
        "eq.all": [
			"./styles-eq-core.js",
			"./api-eq-all.js"
		],
        "eq.all.min": [
			"./styles-eq-core.js",
			"./api-eq-all.js"
		],
        "eq.view": [
			"./styles-eq-view.js"
		],
        "eq.view.min": [
			"./styles-eq-view.js"
		]
    },
    stats: {warnings:false},
	devtool: "source-map",
	resolve: {
		extensions: [".js", ".css"]
	},
    output: {
		library: "easyquery",
        path: path.resolve(__dirname, 'dist/browser'),
        filename: "[name].js",
		libraryTarget: 'window',
        libraryTarget: 'umd'
    },
    module: {
        rules: [
			{
				test: /\.css$/,
				loader: process.env.NODE_ENV !== 'production' ? 'style-loader' : MiniCssExtractPlugin.loader
			},
			{
				test: /\.css$/,
				loader: 'css-loader',
				options: {
				  url: false,
				}
			}
        ]
    },
    optimization: {
        minimizer: [new UglifyJsPlugin({
            include: /\.min\.js$/,
            sourceMap: true,
			extractComments: true,
        })]
    },
	plugins: [
		new FileManagerPlugin({
			onEnd: {
				delete: [
					'./dist/browser/eq.view.js',
					'./dist/browser/eq.view.min.js',
					'./dist/browser/eq.view.js.map',
					'./dist/browser/eq.view.min.js.map'
				],
				move: [
					{ source: './dist/browser/eq.all.css', destination: './dist/browser/eq.core.css' },
					{ source: './dist/browser/eq.all.min.css', destination: './dist/browser/eq.core.min.css' }
				]
			}
		}),
		new MiniCssExtractPlugin({
			filename: "[name].css"
		}),
		new OptimizeCssAssetsPlugin({
		  assetNameRegExp: /\.min.css$/g
		}),
		new webpack.ProvidePlugin({
		  Promise: 'es6-promise'
		})
	]
}


module.exports = [confBundles, confBrowser];