<commit_before>import { dest } from 'gulp';
import { buildExternalHelpers } from 'babel-core';
import babelify from 'babelify';
import browserify from 'browserify';
import buffer from 'gulp-buffer';
import collapse from 'bundle-collapser/plugin';
import envify from 'envify/custom';
import { transform as insert } from 'gulp-insert';
import source from 'vinyl-source-stream';
import uglify from 'gulp-uglify';
import when from 'gulp-if';

export default function browserifyTask({ minify = false }) {
  const b = browserify({
    debug: !minify,
    entries: './src/js/app.js'
  });

  b.transform(babelify, { stage: 0, externalHelpers: true });
  b.transform(envify({
    _: 'purge',
    NODE_ENV: minify ? 'production' : 'development'
  }), { global: true });

  if (minify) {
    b.plugin(collapse);
  }

  const helpers = {};
  function maybeAddExternalHelpersHandler(tr) {
    if (tr instanceof babelify) {
      tr.once('babelify', ({ metadata }) => {
        metadata.usedHelpers.forEach(helper => {
          helpers[helper] = true;
        });
      });
    }
  }

  // add a "babelHelpers =" variable that can be mangled by uglifyjs
  function getHelpers() {
    return `
      ${buildExternalHelpers(Object.keys(helpers))}
      var babelHelpers = (typeof global === 'undefined' ? self : global).babelHelpers;
    `;
  }

  return b
    .on('transform', maybeAddExternalHelpersHandler)
    .bundle()
    .pipe(source('out.js'))
    .pipe(buffer())
    .pipe(insert(contents => `${getHelpers()}; \n ${contents}`))
    .pipe(when(minify, uglify({
      compress: { screw_ie8: true },
      output: { screw_ie8: true },
      mangle: { toplevel: true }
    })))
    .pipe(dest('lib/'));
}
<commit_msg>build: simplify babel external helpers<commit_after>import { dest } from 'gulp';
import { buildExternalHelpers } from 'babel-core';
import babelify from 'babelify';
import browserify from 'browserify';
import buffer from 'gulp-buffer';
import collapse from 'bundle-collapser/plugin';
import envify from 'envify/custom';
import { transform as insert } from 'gulp-insert';
import source from 'vinyl-source-stream';
import uglify from 'gulp-uglify';
import when from 'gulp-if';

export default function browserifyTask({ minify = false }) {
  const b = browserify({
    debug: !minify,
    entries: './src/js/app.js'
  });

  b.transform(babelify, { stage: 0, externalHelpers: true });
  b.transform(envify({
    _: 'purge',
    NODE_ENV: minify ? 'production' : 'development'
  }), { global: true });

  if (minify) {
    b.plugin(collapse);
  }

  const helpers = {};
  function maybeAddExternalHelpersHandler(tr) {
    if (tr instanceof babelify) {
      tr.once('babelify', ({ metadata }) => {
        metadata.usedHelpers.forEach(helper => {
          helpers[helper] = true;
        });
      });
    }
  }

  function getHelpers() {
    return `${buildExternalHelpers(Object.keys(helpers), 'var')}`;
  }

  return b
    .on('transform', maybeAddExternalHelpersHandler)
    .bundle()
    .pipe(source('out.js'))
    .pipe(buffer())
    .pipe(insert(contents => `${getHelpers()}; \n ${contents}`))
    .pipe(when(minify, uglify({
      compress: { screw_ie8: true },
      output: { screw_ie8: true },
      mangle: { toplevel: true }
    })))
    .pipe(dest('lib/'));
}

{'fraction_non_alphanumeric': 0.15640243902439024, 'fraction_numerical': 0.001829268292682927, 'mean_word_length': 3.40994623655914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2182735', 'n_tokens_mistral': 1067, 'n_tokens_neox': 1062, 'n_words': 252}