<commit_before>'use strict';
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = class WebpackOptimize {
  constructor(ctx) {
    this.ctx = ctx;
    this.optimization = ctx.utils.cloneDeep(ctx.config.optimization || {});
  }

  getCommonsChunk() {
    const commonsChunks = [];
    const optimization = this.getWebOptimization();
    const { runtimeChunk = {}, splitChunks = {} } = optimization;
    const { cacheGroups = {} } = splitChunks;
    if (runtimeChunk.name) {
      commonsChunks.push(runtimeChunk.name);
    }
    Object.keys(cacheGroups).forEach(key => {
      const group = cacheGroups[key];
      const name = group.name || key;
      if (!commonsChunks.includes(name)) {
        commonsChunks.push(name);
      }
    });
    return commonsChunks;
  }

  getCacheVendors() {
    const { lib } = this.ctx.config;
    const strRegex = Array.isArray(lib) ? lib.join('|') : '.*';
    const test = new RegExp(`node_modules/(${strRegex})\\.js`);
    return {
      name: 'common',
      chunks: 'initial',
      minChunks: 2,
      test
    };
  }

  getCacheStyles() {
    return {
      name: 'common',
      chunks: 'initial',
      minChunks: 2,
      test: /\.(css|less|scss|stylus)$/
    };
  }

  normalizeMinimizer() {
    if (!this.ctx.prod && this.optimization.minimizer) {
      delete this.optimization.minimizer;
    }
    // compatible old config
    const { plugins = {} } = this.ctx.config;
    if (this.ctx.prod && !this.optimization.minimizer && this.ctx.utils.isObject(plugins.uglifyJs)) {
      const args = plugins.uglifyJs.args || plugins.uglifyJs;
      const options = this.ctx.merge({
        cache: true,
        parallel: 2,
        sourceMap: false,
        uglifyOptions: {
          warnings: false,
          compress: {
            dead_code: true,
            drop_console: true,
            drop_debugger: true
          },
          output: {
            comments: false
          }
        }
      }, args);
      this.optimization.minimizer = [ new UglifyJsPlugin(options) ];
    }
    return this.optimization;
  }

  getOptimization() {
    const { runtimeChunk, splitChunks } = this.optimization;
    if (runtimeChunk) {
      delete this.optimization.runtimeChunk;
    }
    if (splitChunks) {
      delete this.optimization.splitChunks;
    }
    return this.normalizeMinimizer();
  }

  getWebOptimization() {
    const optimization = this.normalizeMinimizer();
    return this.ctx.merge({
      runtimeChunk: {
        name: 'runtime'
      },
      splitChunks: {
        chunks: 'initial',
        minSize: 30000,
        maxAsyncRequests: 5,
        maxInitialRequests: 3,
        name: false,
        cacheGroups: {
          vendors: this.getCacheVendors(),<commit_msg>fix: split for lib<commit_after>'use strict';
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = class WebpackOptimize {
  constructor(ctx) {
    this.ctx = ctx;
    this.optimization = ctx.utils.cloneDeep(ctx.config.optimization || {});
  }

  getCommonsChunk() {
    const commonsChunks = [];
    const optimization = this.getWebOptimization();
    const { runtimeChunk = {}, splitChunks = {} } = optimization;
    const { cacheGroups = {} } = splitChunks;
    if (runtimeChunk.name) {
      commonsChunks.push(runtimeChunk.name);
    }
    Object.keys(cacheGroups).forEach(key => {
      const group = cacheGroups[key];
      const name = group.name || key;
      if (!commonsChunks.includes(name)) {
        commonsChunks.push(name);
      }
    });
    return commonsChunks;
  }

  getCacheVendors() {
    const { lib } = this.ctx.config;
    const strRegex = Array.isArray(lib) ? lib.join('|') : '.*';
    const test = new RegExp(`node_modules/_?(${strRegex})(@|/)`);
    const options = lib ? {
      chunks: 'all',
      minChunks: 1
    } : {
      chunks: 'initial',
      minChunks: 2
    };
    return this.ctx.merge(options, {
      name: 'common',
      test
    });
  }

  getCacheStyles() {
    return {
      name: 'common',
      chunks: 'initial',
      minChunks: 2,
      test: /\.(css|less|scss|stylus)$/,
      enforce: true,
      priority: 50
    };
  }

  normalizeMinimizer() {
    if (!this.ctx.prod && this.optimization.minimizer) {
      delete this.optimization.minimizer;
    }
    // compatible old config
    const { plugins = {} } = this.ctx.config;
    if (this.ctx.prod && !this.optimization.minimizer && this.ctx.utils.isObject(plugins.uglifyJs)) {
      const args = plugins.uglifyJs.args || plugins.uglifyJs;
      const options = this.ctx.merge({
        cache: true,
        parallel: 2,
        sourceMap: false,
        uglifyOptions: {
          warnings: false,
          compress: {
            dead_code: true,
            drop_console: true,
            drop_debugger: true
          },
          output: {
            comments: false
          }
        }
      }, args);
      this.optimization.minimizer = [ new UglifyJsPlugin(options) ];
    }
    return this.optimization;
  }

  getOptimization() {
    const { runtimeChunk, splitChunks } = this.optimization;
    if (runtimeChunk) {
      delete this.optimization.runtimeChunk;
    }
    if (splitChunks) {
      delete this.optimization.splitChunks;
    }
    return this.normalizeMinimizer();
  }

  getWebOptimization() {
    const optimization = this.normalizeMinimizer();
    return this.ctx.merge({
      runtimeChunk: {
        name: 'runtime'
      },
      splitChunks: {
        chunks: 'initial',
        minSize: 30000,
        minChunks: 1,
        name: false,
        cacheGroups: {
          default: false,
          vendors: this.getCacheVendors(),
{'fraction_non_alphanumeric': 0.13188174898212074, 'fraction_numerical': 0.0038944946008143033, 'mean_word_length': 2.271569195136074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2305787', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1772, 'n_words': 330}