build.js 5.1 KB

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