build.js 5.1 KB

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