build.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. , 'connection.js'
  42. , 'socket.js'
  43. ];
  44. /**
  45. * @param {Array} transports The transports that needs to be bundled.
  46. * @param {Object} [options] Options to configure the building process.
  47. * @param {Function} callback Last argument should always be the callback
  48. * @callback {String|Boolean} err An optional argument, if it exists than an error
  49. * occurred during the build process.
  50. * @callback {String} result The result of the build process.
  51. * @api public
  52. */
  53. var builder = module.exports = function () {
  54. var options, callback, error = null
  55. , args = Array.prototype.slice.call(arguments, 0)
  56. , settings = {
  57. minify: true
  58. , node: false
  59. , custom: []
  60. };
  61. // Fancy pancy argument support this makes any pattern possible mainly
  62. // because we require only one of each type
  63. args.forEach(function (arg) {
  64. var type = Object.prototype.toString.call(arg)
  65. .replace(/\[object\s(\w+)\]/gi , '$1' ).toLowerCase();
  66. switch (type) {
  67. case 'object':
  68. return options = arg;
  69. case 'function':
  70. return callback = arg;
  71. }
  72. });
  73. // Add defaults
  74. options = options || {};
  75. // Merge the data
  76. for(var option in options) {
  77. settings[option] = options[option];
  78. }
  79. var files = [];
  80. base.forEach(function (file) {
  81. files.push(__dirname + '/../lib/' + file);
  82. });
  83. var results = {};
  84. files.forEach(function (file) {
  85. fs.readFile(file, function (err, content) {
  86. if (err) error = err;
  87. results[file] = content;
  88. // check if we are done yet, or not.. Just by checking the size of the result
  89. // object.
  90. if (Object.keys(results).length !== files.length) return;
  91. // concatinate the file contents in order
  92. var code = development
  93. , ignore = 0;
  94. code += prefix;
  95. files.forEach(function (file) {
  96. code += results[file];
  97. });
  98. // check if we need to add custom code
  99. if (settings.custom.length) {
  100. settings.custom.forEach(function (content) {
  101. code += content;
  102. });
  103. }
  104. if (!settings.node) {
  105. code = code.split('\n').filter(function (line) {
  106. // check if there are tags in here
  107. var start = line.indexOf(starttagIF) >= 0
  108. , end = line.indexOf(endtagIF) >= 0
  109. , ret = ignore;
  110. // ignore the current line
  111. if (start) {
  112. ignore++;
  113. ret = ignore;
  114. }
  115. // stop ignoring the next line
  116. if (end) {
  117. ignore--;
  118. }
  119. return ret == 0;
  120. }).join('\n');
  121. }
  122. code += suffix;
  123. // check if we need to process it any further
  124. if (settings.minify) {
  125. var ast = uglify.parser.parse(code);
  126. ast = uglify.uglify.ast_mangle(ast);
  127. ast = uglify.uglify.ast_squeeze(ast);
  128. code = production + uglify.uglify.gen_code(ast, { ascii_only: true });
  129. }
  130. callback(error, code);
  131. })
  132. })
  133. };
  134. /**
  135. * @type {String}
  136. * @api public
  137. */
  138. builder.version = package.version;
  139. /**
  140. * Command line support, this allows us to generate builds without having
  141. * to load it as module.
  142. */
  143. if (!module.parent){
  144. // the first 2 are `node` and the path to this file, we don't need them
  145. var args = process.argv.slice(2);
  146. // build a development build
  147. builder(args.length ? args : false, { minify:false }, function (err, content) {
  148. if (err) return console.error(err);
  149. console.log(__dirname);
  150. fs.write(
  151. fs.openSync(__dirname + '/../dist/peer.js', 'w')
  152. , content
  153. , 0
  154. , 'utf8'
  155. );
  156. console.log('Successfully generated the development build: peer.js');
  157. });
  158. // and build a production build
  159. builder(args.length ? args : false, function (err, content) {
  160. if (err) return console.error(err);
  161. fs.write(
  162. fs.openSync(__dirname + '/../dist/peer.min.js', 'w')
  163. , content
  164. , 0
  165. , 'utf8'
  166. );
  167. console.log('Successfully generated the production build: peer.min.js');
  168. });
  169. }