const fs = require('fs')
var pjson = require('./package.json')
const mix = require('laravel-mix')
const CleanWebpackPlugin = require('clean-webpack-plugin')

//plugin to remove/clean your build folder(s) before building
let pathsToClean = [
    'public'
]

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.setPublicPath('public')

// ============================ //
mix.copyDirectory('resources/img', 'public/img')
/**
 * Example
 */
// mix.sass('resources/sass/app.scss', 'css/app.css')
//     .js('resources/js/app.js', 'js/app.js')
mix.js('resources/js/backend/app.js', 'js/backend/queuesharedhost.js')
// ============================ //
 
// Set module hot reload host & port
mix.options({
    hmrOptions: {
        host: 'localhost',
        port: 9999
    },
    terser: {
        terserOptions: {
            compress: {
                drop_console: false,
                pure_funcs: ['console.log']
            }
        }
    },
    clearConsole: false
})

mix.disableNotifications()
mix.version()

var plugins = {
    plugins: [
        new CleanWebpackPlugin(pathsToClean, {})
    ]
}

// Set webpack comfig
if (mix.inProduction()) {
    mix.webpackConfig(plugins)
}else{
    const SpeedMeasurePlugin = require("speed-measure-webpack-plugin")
    const smp = new SpeedMeasurePlugin()
    const webpackConfig = smp.wrap(plugins)
    const fse = require('fs-extra')

    mix.webpackConfig(webpackConfig)

    // Set browserSync watch files
    mix.browserSync({
        proxy: 'localhost',
        files: [
            './config/**/*',
            './resources/**/*',
            './routes/**/*',
            './src/**/*'
        ]
    })

    // Publish files on develop mode
    mix.then(function () {
        console.log('==> Webpack finishes building.')
        let path = '../../../public/vendor/'+pjson.vendor+'/'+pjson.name
        fse.remove(path, err => {
            if (err) return console.error(err)
            console.log('===> Remove '+path+' Done!')
            fse.copy('public', path, err => {
                if (err) return console.error(err)
                console.log('===> Copy /public to '+path+' Done!')
            })
        })
    })
}

// Manage file .watch สำหรับใช้งานกับ browserSync
mix.then(function () {
    let path = '../../../public/.watch'
    if (JSON.parse(process.env.npm_config_argv).remain[0] == '--watch') {
        if (!fs.existsSync(path)) {
            fs.writeFile(path, '', (err) => {
                if (err) { return console.error(err) }
                console.log("===> Create /public/.watch Done!")
            })
        }
    } else {
        if (fs.existsSync(path)) {
            fs.unlink(path, function(err) {
                if (err) { return console.error(err) }
                console.log('===> Remove /public/.watch Done!')
            })
        }
    }
})