build.js 5.1 KB

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