'use strict'; // eslint-disable-line

const { default: ImageminPlugin } = require('imagemin-webpack-plugin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const glob = require('glob-all');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const PurgecssPlugin = require('purgecss-webpack-plugin');
const purgecssWordpress = require('./purgeCssWordpress');
const whitelister = require('purgecss-whitelister');

const config = require('./config');

class TailwindExtractor {
  static extract(content) {
    return content.match(/[A-z0-9-:/]+/g) || [];
  }
}


module.exports = {
  plugins: [
    new ImageminPlugin({
      optipng: { optimizationLevel: 2 },
      gifsicle: { optimizationLevel: 3 },
      maxConcurrency: Infinity,
      pngquant: { quality: '65-90', speed: 4 },
      svgo: {
        plugins: [
          { removeUnknownsAndDefaults: false },
          { cleanupIDs: false },
          { removeViewBox: false },
        ],
      },
      plugins: [imageminMozjpeg({ quality: 75 })],
      disable: (config.enabled.watcher),
    }),
    new PurgecssPlugin({
      paths: glob.sync([
        'app/**/*.php',
        'resources/views/**/*.php',
        'resources/assets/**/*.js',
        'resources/assets/**/*.js',
        'resources/assets/**/*.scss',
        'resources/assets/**/*.sass',
        'resources/assets/**/*.css',
        'resources/assets/**/*.vue',
      ]),
      whitelistPatterns: purgecssWordpress.whitelistPatterns,
      whitelist: whitelister([
        'resources/assets/**/*.scss',
      ]),
      extractors: [
        {
          extractor: TailwindExtractor,
          extensions: ['js', 'php', 'scss', 'sass', 'css'],
        },
      ],
      only: ['main'],
    }),
    new UglifyJsPlugin({
      uglifyOptions: {
        ecma: 5,
        compress: {
          warnings: true,
          drop_console: true,
        },
      },
    }),
  ],
};
