• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var path = require('path')
2var execSync = require('child_process').execSync
3var resolveBin = require('resolve-bin')
4
5function replaceRequire(code) {
6  var count = 0
7  var hasRequire = false
8  code = code.replace(/require/g, function ($0) {
9    hasRequire = true
10    return count++ < 5 ? 'browserifyRequire' : $0
11  })
12  if (hasRequire) {
13    code = 'var ' + code
14  }
15  return code
16}
17
18function getBundle(requires) {
19  var cmd = '"' + resolveBin.sync('browserify') + '"'
20  Object.keys(requires).forEach(function (key) {
21    cmd += ' -r ' + '"' + requires[key] + ':' + key + '"'
22  })
23  var bundleCode = execSync(cmd)
24  bundleCode = replaceRequire(bundleCode.toString())
25  return bundleCode
26}
27
28exports.getBundle = getBundle
29