// Imports
require('dotenv').config()
const mix            = require('laravel-mix').mix
const ImageminPlugin = require('imagemin-webpack-plugin').default
const packageJson    = require('./package.json')

// Project info
const project = {
	theme : process.env.WP_THEME,
	url   : process.env.WP_DOMAIN,
	vendor: Object.keys(packageJson.dependencies)
}

// Project paths
const paths = {
	theme : `public/app/themes/${project.theme}`,
	assets: {
		resources: `public/app/themes/${project.theme}/resources/assets`,
		public   : `public/app/themes/${project.theme}/static`
	}
}

// Set public path
mix.setPublicPath(path.normalize(paths.assets.public))

// Version files in production
// Use source maps in development
if (mix.inProduction()) {
    mix.version()
} else {
	mix.sourceMaps()
}

// Mix options
mix.options({
	processCssUrls: false
})

// Build scripts
mix.js(`${paths.assets.resources}/scripts/app.js`, 'scripts')

// Build styles
mix.sass(`${paths.assets.resources}/styles/app.scss`, 'styles')

// Extract project dependencies to vendor file
mix.extract(project.vendor)

// Autoload modules
mix.autoload({
	jquery: ['$', 'jQuery', 'window.$', 'window.jQuery']
})

// Copy images and optimize them when in production
mix.copy(`${paths.assets.resources}/images/`, `${paths.assets.public}/images/`)

// Copy fonts
mix.copy(`${paths.assets.resources}/fonts/`, `${paths.assets.public}/fonts/`)

// Webpack plugins
mix.webpackConfig({
	plugins: [
		new ImageminPlugin({
			disable : process.env.NODE_ENV !== 'production',
			pngquant: {
				quality: '95-100'
			},
			jpegtran: {
				progressive: true
			}
		})
	]
})

// Browser sync config
mix.browserSync({
	proxy: project.url,
	files: [
		`${paths.assets.public}/scripts/app.js`,
		`${paths.assets.public}/styles/app.css`,
		`${paths.theme}/**/*.php`
	]
})
