build.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Module dependencies.
  3. */
  4. var fs = require('fs')
  5. , package = JSON.parse(fs.readFileSync(__dirname+ '/../package.json'))
  6. , uglify = require('uglify-js');
  7. /**
  8. * License headers.
  9. *
  10. * @api private
  11. */
  12. var template = '/*! peerjs.%ext% build:' + package.version + ', %type%. Copyright(c) 2013 Michelle Bu <michelle@michellebu.com> */'
  13. , prefix = '\n(function(exports){\n'
  14. , development = template.replace('%type%', 'development').replace('%ext%', 'js')
  15. , production = template.replace('%type%', 'production').replace('%ext%', 'min.js')
  16. , suffix = '\n})(this);\n';
  17. /**
  18. * If statements, these allows you to create serveride & client side compatible
  19. * code using specially designed `if` statements that remove serverside
  20. * designed code from the source files
  21. *
  22. * @api private
  23. */
  24. var starttagIF = '// if node'
  25. , endtagIF = '// end node';
  26. /**
  27. * The modules that are required to create a base build of BinaryJS client.
  28. *
  29. * @const
  30. * @type {Array}
  31. * @api private
  32. */
  33. var base = [
  34. '../deps/js-binarypack/lib/bufferbuilder.js'
  35. , '../deps/js-binarypack/lib/binarypack.js'
  36. , '../deps/EventEmitter/EventEmitter.js'
  37. , 'util.js'
  38. , '../deps/reliable/lib/reliable.js'
  39. , 'adapter.js'
  40. , 'peer.js'
  41. , 'dataconnection.js'
  42. , 'connectionmanager.js'
  43. , 'socket.js'
  44. ];
  45. /**
  46. * @param {Array} transports The transports that needs to be bundled.
  47. * @param {Object} [options] Options to configure the building process.
  48. * @param {Function} callback Last argument should always be the callback
  49. * @callback {String|Boolean} err An optional argument, if it exists than an error
  50. * occurred during the build process.
  51. * @callback {String} result The result of the build process.
  52. * @api public
  53. */
  54. var builder = module.exports = function () {
  55. var options, callback, error = null
  56. , args = Array.prototype.slice.call(arguments, 0)
  57. , settings = {
  58. minify: true
  59. , node: false
  60. , custom: []
  61. };
  62. // Fancy pancy argument support this makes any pattern possible mainly
  63. // because we require only one of each type
  64. args.forEach(function (arg) {
  65. var type = Object.prototype.toString.call(arg)
  66. .replace(/\[object\s(\w+)\]/gi , '$1' ).toLowerCase();
  67. switch (type) {
  68. case 'object':
  69. return options = arg;
  70. case 'function':
  71. return callback = arg;
  72. }
  73. });
  74. // Add defaults
  75. options = options || {};
  76. // Merge the data
  77. for(var option in options) {
  78. settings[option] = options[option];
  79. }
  80. var files = [];
  81. base.forEach(function (file) {
  82. files.push(__dirname + '/../lib/' + file);
  83. });
  84. var results = {};
  85. files.forEach(function (file) {
  86. fs.readFile(file, function (err, content) {
  87. if (err) error = err;
  88. results[file] = content;
  89. // check if we are done yet, or not.. Just by checking the size of the result
  90. // object.
  91. if (Object.keys(results).length !== files.length) return;
  92. // concatinate the file contents in order
  93. var code = development
  94. , ignore = 0;
  95. code += prefix;
  96. files.forEach(function (file) {
  97. code += results[file];
  98. });
  99. // check if we need to add custom code
  100. if (settings.custom.length) {
  101. settings.custom.forEach(function (content) {
  102. code += content;
  103. });
  104. }
  105. if (!settings.node) {
  106. code = code.split('\n').filter(function (line) {
  107. // check if there are tags in here
  108. var start = line.indexOf(starttagIF) >= 0
  109. , end = line.indexOf(endtagIF) >= 0
  110. , ret = ignore;
  111. // ignore the current line
  112. if (start) {
  113. ignore++;
  114. ret = ignore;
  115. }
  116. // stop ignoring the next line
  117. if (end) {
  118. ignore--;
  119. }
  120. return ret == 0;
  121. }).join('\n');
  122. }
  123. code += suffix;
  124. // check if we need to process it any further
  125. if (settings.minify) {
  126. var ast = uglify.parser.parse(code);
  127. ast = uglify.uglify.ast_mangle(ast);
  128. ast = uglify.uglify.ast_squeeze(ast);
  129. code = production + uglify.uglify.gen_code(ast, { ascii_only: true });
  130. }
  131. callback(error, code);
  132. })
  133. })
  134. };
  135. /**
  136. * @type {String}
  137. * @api public
  138. */
  139. builder.version = package.version;
  140. /**
  141. * Command line support, this allows us to generate builds without having
  142. * to load it as module.
  143. */
  144. if (!module.parent){
  145. // the first 2 are `node` and the path to this file, we don't need them
  146. var args = process.argv.slice(2);
  147. // build a development build
  148. builder(args.length ? args : false, { minify:false }, function (err, content) {
  149. if (err) return console.error(err);
  150. console.log(__dirname);
  151. fs.write(
  152. fs.openSync(__dirname + '/../dist/peer.js', 'w')
  153. , content
  154. , 0
  155. , 'utf8'
  156. );
  157. console.log('Successfully generated the development build: peer.js');
  158. });
  159. // and build a production build
  160. builder(args.length ? args : false, function (err, content) {
  161. if (err) return console.error(err);
  162. fs.write(
  163. fs.openSync(__dirname + '/../dist/peer.min.js', 'w')
  164. , content
  165. , 0
  166. , 'utf8'
  167. );
  168. console.log('Successfully generated the production build: peer.min.js');
  169. });
  170. }