Code coverage report for browserify-middleware/lib/compile.js

Statements: 71.64% (48 / 67)      Branches: 73.91% (34 / 46)      Functions: 80% (4 / 5)      Lines: 73.77% (45 / 61)      Ignored: none     

All files » browserify-middleware/lib/ » compile.js
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    1 1 1 1 1 1   1   1   1 10 10 10     1 75 75 75 61 61 61 28 33 5       47                         47             47 6 6                     6       41   47 33   47     47     47                 47 47 44 44 44 10 10 10   10   44       1  
'use strict';
 
var browserify = require('browserify');
var uglify = require('uglify-js');
var moldSourceMap = require('mold-source-map');
var relative = require('path').relative;
var fs = require('fs');
var guard = require('once');
 
var DynamicCache = require('./dynamic-cache.js');
//START Compile
var dynamicCache = {};
 
function minify(str, options) {
  if (!options || typeof options !== 'object') options = {};
  options.fromString = true;
  return uglify.minify(str, options);
}
 
function compile(path, options, cb, cacheUpdated) {
  cb = guard(cb);
  var cache;
  if (options.cache === 'dynamic') {
    var cacheName = path + ':' + options.external + ':' + options.debug;
    cache = dynamicCache[cacheName];
    if (cache && !cacheUpdated) {
      return cache.update(function () { compile(path, options, cb, true) });
    } else if (!cache) {
      cache = (dynamicCache[cacheName] = new DynamicCache());
    }
  }
 
  var bundle = browserify({
    noParse: options.noParse,
    extensions: options.extensions,
    resolve: options.resolve,
    insertGlobals: options.insertGlobals,
    detectGlobals: options.detectGlobals,
    ignoreMissing: options.ignoreMissing,
    basedir: options.basedir,
    debug: options.debug,
    standalone: options.standalone || false,
    cache: cache ? cache.getCache() : undefined,
    fullPaths: cache ? true : false
  });
  Iif (options.plugins) {
    var plugins = options.plugins; // in the format options.plugins = [{plugin: plugin, options: options}, {plugin: plugin, options: options}, ... ]
    for(var i = 0; i < plugins.length; i++) {
      var obj = plugins[i];
      bundle.plugin(obj.plugin, obj.options);
    }
  }
  if (Array.isArray(path)) {
    for (var i = 0; i < path.length; i++) {
      Iif (typeof path[i] === 'object') { // obj spec support; i.e. {"jquery": {options...}}
        var spec = path[i];
        var keys = Object.keys(spec);
        keys.forEach(function (key) {
          if (spec[key].run) {
            bundle.add(key, spec[key]);
          } else {
            bundle.require(key, spec[key]);
          }
        })
      } else {
        bundle.require(path[i]);
      }
    }
  } else {
    bundle.add(path);
  }
  if (cache) {
    cache.populate(bundle);
  }
  for (var i = 0; i < (options.external || []).length; i++) {
    bundle.external(options.external[i]);
  }
  for (var i = 0; i < (options.ignore || []).length; i++) {
    bundle.ignore(options.ignore[i]);
  }
  for (var i = 0; i < (options.transform || []).length; i++) {
    var transform = options.transform[i];
 
    if (Array.isArray(transform)) {
      bundle.transform(transform[1], transform[0]);
    } else {
      bundle.transform(transform);
    }
  }
  bundle.bundle(function (err, src) {
    if (err) return cb(err);
    src = src.toString();
    Iif (options.postcompile) src = options.postcompile(src);
    if (options.minify) {
      Iif (options.preminify) src = options.preminify(src);
      try {
        src = minify(src, options.minify).code;
      } catch (ex) { } //better to just let the client fail to parse
      Iif (options.postminify) src = options.postminify(src);
    }
    cb(null, src);
  });
}
 
module.exports = compile