const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");

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)

    .enableSassLoader()
    .enablePostCssLoader()

    .autoProvidejQuery()

    .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)$/,
    })
    .configureFontRule({
        type: 'asset',
        // inline FA6 subset font files
        maxSize: 4 * 1024,
        filename: 'fonts/[name][ext]?cs=' + cacheSuffix,
    })
    .configureImageRule({
        type: 'asset',
        maxSize: 4 * 1024,
        filename: 'images/[name][ext]?cs=' + cacheSuffix,
    })

    .configureTerserPlugin(() => {
        return {
            terserOptions: {
                format: {
                    comments: false,
                },
            },
            extractComments: false,
        }
    })
    .configureBabelPresetEnv((config) => {
        config.useBuiltIns = 'usage';
        config.corejs = 3;
    })
    .configureBabel(function (babelConfig) {
    }, {
        // node_modules is not processed through Babel by default
        // but you can whitelist specific modules to process
        includeNodeModules: [
            'bootstrap',
            // 'google-maps',
        ],
    })
    .addPlugin(new SpeedMeasurePlugin)
;

module.exports = Encore.getWebpackConfig();
