lib/bundler.js
function bundleApp(codeList, ns, domload, compile, before, npmTree, o) {
  var l = []
    , usedBuiltIns = npmTree._builtIns
    , len = 0;
  delete npmTree._builtIns;

  // 1. construct the global namespace object
  l.push("window." + ns + " = {data:{}, path:{}};");

  // 2. attach path code to the namespace object so that require.js can efficiently resolve paths
  l.push('\n// include npm::path');
  var pathCode = read(join(builtInDir, 'path.posix.js'));
  l.push('(function (exports) {\n ' + pathCode + '\n}(window.' + ns + '.path));');

  // 3. pull in serialized data
  Object.keys(o.data).forEach(function (name) {
    return l.push(ns + ".data." + name + " = " + o.data[name] + ";");
  });

  // 4. attach require code
  var config = {
    namespace : ns
  , domains   : Object.keys(o.domains) // TODO: remove npm from here
  , arbiters  : o.arbiters
  , logging   : o.logLevel
  , npmTree   : npmTree
  , builtIns  : builtIns
  , slash     : '/' //TODO: figure out if different types can coexist, if so, determine in resolver, and on client
  };

  l.push(anonWrap(read(join(dir, 'require.js'))
    .replace(/VERSION/, JSON.parse(read(join(dir, '..', 'package.json'))).version)
    .replace(/REQUIRECONFIG/, JSON.stringify(config))
  ));

  // 5. include CommonJS compatible code in the order they have to be defined
  var defineWrap = function (exportName, domain, code) {
    return ns + ".define('" + exportName + "','" + domain + "',function (require, module, exports) {\n" + code + "\n});";
  };

  // 6. harvest function splits code into app and non-app code and defineWraps
  var harvest = function (onlyMain) {
    codeList.forEach(function (pair) {
      var dom = pair[0]
        , file = pair[1]
        , basename = file.split('.')[0];
      if ((dom === 'app') !== onlyMain) {
        return;
      }
      var code = before(compile(join(o.domains[dom], file)));
      l.push(defineWrap(basename, dom, code));
    });
  };

  // 7.a) include required builtIns
  l.push("\n// node builtins\n");
  len = l.length;
  usedBuiltIns.forEach(function (b) {
    if (b === 'path') { // already included
      return;
    }
    l.push(defineWrap(b, 'npm', read(join(builtInDir, b + '.js'))));
  });
  if (l.length === len) {
    l.pop();
  }

  // 7.b) include modules not on the app domain
  l.push("\n// shared code\n");
  len = l.length;
  harvest(false);
  if (l.length === len) {
    l.pop();
  }

  // 7.c) include modules on the app domain, and wait for domloader if set
  l.push("\n// app code - safety wrapped\n\n");
  domload(harvest(true));

  // 8. use a closure to encapsulate the private internal data and APIs
  return anonWrap(l.join('\n'));
}
function bundleApp(codeList, ns, domload, compile, before, npmTree, o) {
  var l = []
    , usedBuiltIns = npmTree._builtIns
    , len = 0;
  delete npmTree._builtIns;

  // 1. construct the global namespace object
  l.push("window." + ns + " = {data:{}, path:{}};");

  // 2. attach path code to the namespace object so that require.js can efficiently resolve paths
  l.push('\n// include npm::path');
  var pathCode = read(join(builtInDir, 'path.posix.js'));
  l.push('(function (exports) {\n ' + pathCode + '\n}(window.' + ns + '.path));');

  // 3. pull in serialized data
  Object.keys(o.data).forEach(function (name) {
    return l.push(ns + ".data." + name + " = " + o.data[name] + ";");
  });

  // 4. attach require code
  var config = {
    namespace : ns
  , domains   : Object.keys(o.domains) // TODO: remove npm from here
  , arbiters  : o.arbiters
  , logging   : o.logLevel
  , npmTree   : npmTree
  , builtIns  : builtIns
  , slash     : '/' //TODO: figure out if different types can coexist, if so, determine in resolver, and on client
  };

  l.push(anonWrap(read(join(dir, 'require.js'))
    .replace(/VERSION/, JSON.parse(read(join(dir, '..', 'package.json'))).version)
    .replace(/REQUIRECONFIG/, JSON.stringify(config))
  ));

  // 5. include CommonJS compatible code in the order they have to be defined
  var defineWrap = function (exportName, domain, code) {
    return ns + ".define('" + exportName + "','" + domain + "',function (require, module, exports) {\n" + code + "\n});";
  };

  // 6. harvest function splits code into app and non-app code and defineWraps
  var harvest = function (onlyMain) {
    codeList.forEach(function (pair) {
      var dom = pair[0]
        , file = pair[1]
        , basename = file.split('.')[0];
      if ((dom === 'app') !== onlyMain) {
        return;
      }
      var code = before(compile(join(o.domains[dom], file)));
      l.push(defineWrap(basename, dom, code));
    });
  };

  // 7.a) include required builtIns
  l.push("\n// node builtins\n");
  len = l.length;
  usedBuiltIns.forEach(function (b) {
    if (b === 'path') { // already included
      return;
    }
    l.push(defineWrap(b, 'npm', read(join(builtInDir, b + '.js'))));
  });
  if (l.length === len) {
    l.pop();
  }

  // 7.b) include modules not on the app domain
  l.push("\n// shared code\n");
  len = l.length;
  harvest(false);
  if (l.length === len) {
    l.pop();
  }

  // 7.c) include modules on the app domain, and wait for domloader if set
  l.push("\n// app code - safety wrapped\n\n");
  domload(harvest(true));

  // 8. use a closure to encapsulate the private internal data and APIs
  return anonWrap(l.join('\n'));
}
function bundleApp ( codeList , ns , domload , compile , before , npmTree , o ) { var l = [ ] , usedBuiltIns = npmTree . _builtIns , len = 0 ; delete npmTree . _builtIns ; // 1. construct the global namespace object l . push ( "window." + ns + " = {data:{}, path:{}};" ) ; // 2. attach path code to the namespace object so that require.js can efficiently resolve paths l . push ( '\n// include npm::path' ) ; var pathCode = read ( join ( builtInDir , 'path.posix.js' ) ) ; l . push ( '(function (exports) {\n ' + pathCode + '\n}(window.' + ns + '.path));' ) ; // 3. pull in serialized data Object . keys ( o . data ) . forEach ( function ( name ) { return l . push ( ns + ".data." + name + " = " + o . data [ name ] + ";" ) ; } ) ; // 4. attach require code var config = { namespace : ns , domains : Object . keys ( o . domains ) // TODO: remove npm from here , arbiters : o . arbiters , logging : o . logLevel , npmTree : npmTree , builtIns : builtIns , slash : '/' //TODO: figure out if different types can coexist, if so, determine in resolver, and on client } ; l . push ( anonWrap ( read ( join ( dir , 'require.js' ) ) . replace ( / VERSION / , JSON . parse ( read ( join ( dir , '..' , 'package.json' ) ) ) . version ) . replace ( / REQUIRECONFIG / , JSON . stringify ( config ) ) ) ) ; // 5. include CommonJS compatible code in the order they have to be defined var defineWrap = function ( exportName , domain , code ) { return ns + ".define('" + exportName + "','" + domain + "',function (require, module, exports) {\n" + code + "\n});" ; } ; // 6. harvest function splits code into app and non-app code and defineWraps var harvest = function ( onlyMain ) { codeList . forEach ( function ( pair ) { var dom = pair [ 0 ] , file = pair [ 1 ] , basename = file . split ( '.' ) [ 0 ] ; if ( ( dom === 'app' ) !== onlyMain ) { return ; } var code = before ( compile ( join ( o . domains [ dom ] , file ) ) ) ; l . push ( defineWrap ( basename , dom , code ) ) ; } ) ; } ; // 7.a) include required builtIns l . push ( "\n// node builtins\n" ) ; len = l . length ; usedBuiltIns . forEach ( function ( b ) { if ( b === 'path' ) { // already included return ; } l . push ( defineWrap ( b , 'npm' , read ( join ( builtInDir , b + '.js' ) ) ) ) ; } ) ; if ( l . length === len ) { l . pop ( ) ; } // 7.b) include modules not on the app domain l . push ( "\n// shared code\n" ) ; len = l . length ; harvest ( false ) ; if ( l . length === len ) { l . pop ( ) ; } // 7.c) include modules on the app domain, and wait for domloader if set l . push ( "\n// app code - safety wrapped\n\n" ) ; domload ( harvest ( true ) ) ; // 8. use a closure to encapsulate the private internal data and APIs return anonWrap ( l . join ( '\n' ) ) ; }
main application packager
main application packager