build.js 4.9 KB

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