const path = require('path');
const { SRC, DIST, ASSETS, LIB } = require('./paths');
var webpack = require("webpack");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const SymlinkWebpackPlugin = require('symlink-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader')

module.exports = {
	entry: {
		app: path.resolve(SRC, 'scripts', 'app.js')
	},
	output: {
    	path: DIST,
		filename: '[name].js',
    	publicPath: './' //meaning dist/
	},
	devtool: "inline-source-map",
	module: {
		rules: [{
				test: /\.vue$/,
				loader: 'vue-loader'
			}, {
				test: /\.js$/,
				exclude: file => (/node_modules/.test(file) && !/\.vue\.js/.test(file)),
				use: {
					loader: 'babel-loader',
					options: {
						presets: ['@babel/preset-env'],
					}
				},
			}, {
				test: /\.html$/i,
				loader: "html-loader",
			}, {
				test: /\.(woff|woff2|eot|ttf|otf)$/i,
				type: 'asset/resource'
			}, {
				test: /\.s[ac]ss$/i,
				use: [
					'vue-style-loader',
					'css-loader',
					'resolve-url-loader',
					{
						loader: 'sass-loader',
						options: {
							implementation: require.resolve('sass'),
							sourceMap: true
						}
					}
				]
			}, {
				test: /\.css$/i,
				use: ["vue-style-loader", "css-loader"],
			}, {
				test: /\.(png|svg|jpg|jpeg|gif)$/i,
				use: {
					loader: "url-loader",
					options: {
						limit: 1 * 1024,
						//name: "images/[name].[hash:7].[ext]"
						name: "images/[name].[ext]"
					}
				}
				/*type: 'asset',
				parser: {
					dataUrlCondition: {
						maxSize: 8*1024
					}
				}*/
			}
		]
	},
	plugins: [
		new webpack.ProvidePlugin({
			$: require.resolve('jquery'),
			jQuery: require.resolve('jquery')
		}),
		new CopyWebpackPlugin({
			patterns: [/*{
					from: 'geo/',
					to: path.resolve(DIST, 'geo')
				}, {
					from: path.resolve(SRC, 'images/icons'),
					to: 'images/icons'
				}, */{
					from: path.resolve(LIB, 'index.php'),
					to: 'index.php'
				},
				{ from: 'src/images/footprint_mapbox.png', to: 'images' },
				{ from: 'src/images/logo_black.png', to: 'images' },
				{ from: 'src/images/spot-logo-only.svg', to: 'images' }
			],
		}),
		new SymlinkWebpackPlugin([
			{ origin: '../files/', symlink: 'files' },
			{ origin: '../geo/', symlink: 'geo' },
			{ origin: '../src/images/icons/', symlink: 'images/icons' }
		]),
		new CleanWebpackPlugin(),
		new webpack.DefinePlugin({
			__VUE_OPTIONS_API__: 'true',
			__VUE_PROD_DEVTOOLS__: 'false',
			__VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false'
		}),
		new VueLoaderPlugin()
	],
	resolve: {
		extensions: ['', '.js'],
		alias: {
			"@scripts": path.resolve(SRC, "scripts"),
			'load-image': 'blueimp-load-image/js/load-image.js',
			'load-image-meta': 'blueimp-load-image/js/load-image-meta.js',
			'load-image-exif': 'blueimp-load-image/js/load-image-exif.js',
			'canvas-to-blob': 'blueimp-canvas-to-blob/js/canvas-to-blob.js',
			'jquery-ui/ui/widget': 'blueimp-file-upload/js/vendor/jquery.ui.widget.js'
		}
	}
};
