# all files / keystone/admin/server/middleware/ browserify.js

24.44% Statements 22/90
0% Branches 0/26
0% Functions 0/15
25.29% Lines 22/87

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    1281×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     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({
    			plugins: [require('babel-plugin-transform-object-rest-spread'), require('babel-plugin-transform-object-assign')],
    			presets: [require('babel-preset-es2015'), require('babel-preset-react')],
    		}));
    		b.exclude('FieldTypes');
    		packages.forEach(function (i) {
    			b.exclude(i);
    		});
    		if (devMode) {
    			b = watchify(b, { poll: 500 });
    		}
    		b.bundle(function (err, buff) {
    			if (err) return logError(file, err);
    			src = buff;
    			ready = true;
    			queue.forEach(function (reqres) {
    				send.apply(null, reqres);
    			});
    			writeBundle(buff);
    		});
    		b.on('update', function () {
    			b.bundle(function (err, buff) {
    				if (err) return logError(file, err);
    				else logRebuild(file);
    				src = buff;
    				writeBundle(buff);
    			});
    		});
    	}
    	function serve (req, res) {
    		if (!ready) {
    			build();
    			queue.push([req, res]);
    			return;
    		}
    		send(req, res);
    	}
    	function send (req, res) {
    		res.setHeader('Content-Type', 'application/javascript');
    		var etag = crypto.createHash('md5').update(src).digest('hex').slice(0, 6);
    		if (req.get && (etag === req.get('If-None-Match'))) {
    			res.status(304);
    			res.end();
    		}
    		else {
    			res.setHeader('ETag', etag);
    			res.setHeader('Vary', 'Accept-Encoding');
    			res.send(src);
    		}
    	}
    	return {
    		serve: serve,
    		build: build,
    	};
    };