Sha256: 3ca5643438d9756d30d5e23e3d585dffe54ec42294c600f46de59e771588d1bc

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

var through = require('through');
var rfile = require('rfile');
var templateSTR = rfile('./template.js');
var uglify = require('uglify-js');
function template(moduleName, cjs) {
  var str = uglify.minify(
    templateSTR
      .replace(/\{\{name\}\}/g, moduleName.toLowerCase())
      .replace(/\{\{pascalcase\}\}/g, pascalCase(moduleName))
      .replace(/\{\{camelcase\}\}/g, camelCase(moduleName)),
    {fromString: true}).code
    .split('source()')
  str[0] = str[0].trim();
  //make sure these are undefined so as to not get confused if modules have inner UMD systems
  str[0] += 'var define,ses,bootstrap,module,exports;';
  if (cjs) str[0] += 'module={exports:(exports={})};';
  str[0] += '\n';
  if (cjs) str[1] = 'return module.exports;' + str[1];
  str[1] = '\n' + str[1];
  return str;
}

exports = module.exports = function (name, cjs, src) {
  if (typeof cjs === 'string') {
    var tmp = cjs;
    cjs = src;
    src = tmp;
  }
  if (src) {
    return exports.prelude(name, cjs) + src + exports.postlude(name, cjs);
  } else {
    var strm = through(write, end);
    var first = true;
    function write(chunk) {
      if (first) strm.queue(exports.prelude(name, cjs));
      first = false;
      strm.queue(chunk);
    }
    function end() {
      if (first) strm.queue(exports.prelude(name, cjs));
      strm.queue(exports.postlude(name, cjs));
      strm.queue(null);
    }
    return strm;
  }
};

exports.prelude = function (moduleName, cjs) {
  return template(moduleName, cjs)[0];
};
exports.postlude = function (moduleName, cjs) {
  return template(moduleName, cjs)[1];
};



function pascalCase(name) {
  return camelCase(name).replace(/^[a-z]/, function (char) { return char.toUpperCase(); });
}
function camelCase(name) {
  name = name.replace(/\-([a-z])/g, function (_, char) { return char.toUpperCase(); });
  return name.replace(/[^a-zA-Z0-9]+/g, '');
}

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
sprockets-browserify-0.2.0 node_modules/browserify/node_modules/umd/index.js
ruby-wisp-source-0.8.0 vendor/node_modules/browserify/node_modules/umd/index.js
ruby-wisp-source-0.7.0 vendor/node_modules/browserify/node_modules/umd/index.js