<commit_before>var babelify = require('babelify');
var browserify = require('browserify');
var chalk = require('chalk');
var crypto = require('crypto');
var fs = require('fs-extra');
var moment = require('moment');
var packages = require('../../client/packages');
var path = require('path');
var watchify = require('watchify');

var basedir = path.resolve(__dirname + '/../../client/');
var devMode = process.env.KEYSTONE_DEV === 'true';
var devWriteBundles = process.env.KEYSTONE_WRITE_BUNDLES === 'true';
var devWriteDisc = process.env.KEYSTONE_WRITE_DISC === 'true';

function ts () {
	return chalk.gray(moment().format('YYYY-MM-DD HH:MM:SS '));
}

function logInit (file) {
	console.log(chalk.grey('Watching ') + chalk.underline('keystone/admin/src/' + file) + chalk.grey(' for changes...'));
}

function logRebuild (file) {
	console.log(ts() + chalk.green('rebuilt ' + chalk.underline(file)));
}

function logError (file, err) {
	console.log(ts() + chalk.red('error building ' + chalk.underline(file) + ':') + '\n' + err.message);
}

module.exports = function (file, name) {
	var b;
	var building = false;
	var queue = [];
	var ready;
	var src;
	function writeBundle (buff) {
		if (devWriteBundles) {
			fs.outputFile(path.resolve(path.join(__dirname, '../../bundles/js', file)), buff, 'utf8');
		}
		if (devWriteDisc) {
			var discFile = file.replace('.js', '.html');
			require('disc').bundle(buff, function (err, html) {
				if (err) {
					logError(discFile, err);
				} else {
					fs.outputFile(path.resolve(path.join(__dirname, '../../bundles/disc', discFile)), html, 'utf8');
					console.log(ts() + chalk.green('wrote disc for ' + chalk.underline(file)));
				}
			});
		}
	}
	function build () {
		if (building) return;
		building = true;
		var opts = { basedir: basedir };
		if (devMode) {
			logInit(file);
			opts.debug = true;
			opts.cache = {};
			opts.packageCache = {};
		}
		if (devWriteDisc) {
			opts.fullPaths = true;
		}
		if (name) {
			b = browserify(opts);
			b.require('./' + file, { expose: name });
		} else {
			b = browserify('./' + file, opts);
		}
		b.transform(babelify.configure({<commit_msg>Defer loading babelify/browserify packages until build is requested<commit_after>var chalk = require('chalk');
var crypto = require('crypto');
var fs = require('fs-extra');
var moment = require('moment');
var packages = require('../../client/packages');
var path = require('path');

var basedir = path.resolve(__dirname + '/../../client/');
var devMode = process.env.KEYSTONE_DEV === 'true';
var devWriteBundles = process.env.KEYSTONE_WRITE_BUNDLES === 'true';
var devWriteDisc = process.env.KEYSTONE_WRITE_DISC === 'true';

function ts () {
	return chalk.gray(moment().format('YYYY-MM-DD HH:MM:SS '));
}

function logInit (file) {
	console.log(chalk.grey('Watching ') + chalk.underline('keystone/admin/src/' + file) + chalk.grey(' for changes...'));
}

function logRebuild (file) {
	console.log(ts() + chalk.green('rebuilt ' + chalk.underline(file)));
}

function logError (file, err) {
	console.log(ts() + chalk.red('error building ' + chalk.underline(file) + ':') + '\n' + err.message);
}

module.exports = function (file, name) {
	var b;
	var building = false;
	var queue = [];
	var ready;
	var src;
	function writeBundle (buff) {
		if (devWriteBundles) {
			fs.outputFile(path.resolve(path.join(__dirname, '../../bundles/js', file)), buff, 'utf8');
		}
		if (devWriteDisc) {
			var discFile = file.replace('.js', '.html');
			require('disc').bundle(buff, function (err, html) {
				if (err) {
					logError(discFile, err);
				} else {
					fs.outputFile(path.resolve(path.join(__dirname, '../../bundles/disc', discFile)), html, 'utf8');
					console.log(ts() + chalk.green('wrote disc for ' + chalk.underline(file)));
				}
			});
		}
	}
	function build () {
		if (building) return;
		building = true;
		var babelify = require('babelify');
		var browserify = require('browserify');
		var watchify = require('watchify');
		var opts = { basedir: basedir };
		if (devMode) {
			logInit(file);
			opts.debug = true;
			opts.cache = {};
			opts.packageCache = {};
		}
		if (devWriteDisc) {
			opts.fullPaths = true;
		}
		if (name) {
			b = browserify(opts);
			b.require('./' + file, { expose: name });
		} else {
			b = browserify('./' + file, opts);
		}
		b.transform(babelify.configure({
{'fraction_non_alphanumeric': 0.19310823311748382, 'fraction_numerical': 0.0009250693802035153, 'mean_word_length': 3.821627647714604, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2540167', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1464, 'n_words': 324}