const Encore = require('@symfony/webpack-encore');
// Manually configure the runtime environment if not already configured yet by the "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}

const debug = false;

const cacheSuffix = require('fs').readFileSync('./.cache_suffix', 'utf8');

Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .addEntry('app.min', './assets/front/js/app.js')
    .addEntry('app_admin.min', './assets/admin/js/app.js')
    .disableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(debug)
    .enableVersioning(false)
    // enables Sass/SCSS support
    .enableSassLoader()
    .configureFilenames({
        js: '[name].js?cs=' + cacheSuffix,
        css: '[name].css?cs=' + cacheSuffix,
        images: 'images/[name].[ext]?cs=' + cacheSuffix,
        fonts: 'fonts/[name].[ext]?cs=' + cacheSuffix
    })
    .copyFiles({
        from: './assets',
        to: '[path][name].[ext]',
        pattern: /\.(png|jpg|jpeg|gif)$/
    })
    // .autoProvidejQuery()
    // auto prefix
    .enablePostCssLoader()
    // js minification
    .configureTerserPlugin(() => {
        return {
            cache: true,
            parallel: true,
            terserOptions: {
                output: {
                    // strip comments
                    comments: false,
                }
            }
        }
    })
    .configureBabel(function (babelConfig) {
        // add additional presets
        //babelConfig.presets.push('@babel/preset-flow');

        // no plugins are added by default, but you can add some
        //babelConfig.plugins.push('styled-jsx/babel');
    }, {
        // node_modules is not processed through Babel by default
        // but you can whitelist specific modules to process
        includeNodeModules: [
            'bootstrap',
            'google-maps',
        ],
    })
;

module.exports = Encore.getWebpackConfig();