build-examples.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * build-examples.js
  3. * --------
  4. * Searches for each example's `src/js/app.js` file.
  5. * Creates a new watchify instance for each `app.js`.
  6. * Changes to Uppy's source will trigger rebundling.
  7. *
  8. * Note:
  9. * Since each example is dependent on Uppy's source,
  10. * changing one source file causes the 'file changed'
  11. * notification to fire multiple times. To stop this,
  12. * files are added to a 'muted' array that is checked
  13. * before announcing a changed file. It's removed from
  14. * the array when it has been bundled.
  15. */
  16. var createStream = require('fs').createWriteStream;
  17. var glob = require('multi-glob').glob;
  18. var chalk = require('chalk');
  19. var path = require('path');
  20. var notifier = require('node-notifier');
  21. var babelify = require('babelify');
  22. var browserify = require('browserify');
  23. var watchify = require('watchify');
  24. var webRoot = __dirname;
  25. var uppyRoot = path.dirname(webRoot);
  26. var srcPattern = webRoot + '/src/examples/**/js/app.js';
  27. var dstPattern = webRoot + '/public/examples/**/js/app.js';
  28. var watchifyEnabled = process.argv[2] === 'watch';
  29. var browserifyPlugins = [];
  30. if (watchifyEnabled) {
  31. browserifyPlugins.push(watchify);
  32. }
  33. // Find each app.js file with glob.
  34. glob(srcPattern, function(err, files) {
  35. if (err) throw new Error(err);
  36. console.log('--> Watching examples..');
  37. console.log('--> Pre-building ' + files.length + ' files..')
  38. var muted = [];
  39. // Create a new watchify instance for each file.
  40. files.forEach(function(file) {
  41. var browseFy = browserify(file, {
  42. cache : {},
  43. packageCache: {},
  44. plugin : browserifyPlugins
  45. })
  46. // Aliasing for using `require('uppy')`, etc.
  47. browseFy
  48. .require(uppyRoot + '/src/index.js', { expose: 'uppy' })
  49. .require(uppyRoot + '/src/core/index.js', { expose: 'uppy/core' })
  50. .require(uppyRoot + '/src/plugins/index.js', { expose: 'uppy/plugins' })
  51. .transform(babelify);
  52. // Listeners for changes, errors, and completion.
  53. browseFy
  54. .on('update', bundle)
  55. .on('error', onError)
  56. .on('log', function(msg) {
  57. })
  58. .on('file', function(file, id, parent) {
  59. // When file completes, unmute it.
  60. muted = muted.filter(function(mutedId) {
  61. return id !== mutedId;
  62. });
  63. });
  64. // Call bundle() manually to start watch processes.
  65. bundle();
  66. /**
  67. * Creates bundle and writes it to static and public folders.
  68. * Changes to
  69. * @param {[type]} ids [description]
  70. * @return {[type]} [description]
  71. */
  72. function bundle(ids) {
  73. ids = ids || [];
  74. ids.forEach(function(id) {
  75. if (!isMuted(id, muted)) {
  76. console.info(chalk.cyan('change:'), id);
  77. muted.push(id);
  78. }
  79. });
  80. var exampleName = path.dirname(path.dirname(file));
  81. var output = file.replace('**', exampleName);
  82. console.log('output: '+output);
  83. var bundle = browseFy.bundle()
  84. .on('error', onError)
  85. .on('file', onFile)
  86. bundle.pipe(createStream(output));
  87. // bundle.pipe(createStream(output.replace('src', 'public')));
  88. }
  89. });
  90. });
  91. /**
  92. * Logs to console and shows desktop notification on error.
  93. * Calls `this.emit(end)` to stop bundling.
  94. * @param {object} err Error object
  95. */
  96. function onError(err) {
  97. console.error(chalk.red('✗ error:'), chalk.red(err.message));
  98. notifier.notify({
  99. 'title': 'Build failed:',
  100. 'message': err.message
  101. })
  102. this.emit('end');
  103. }
  104. function onFile(file, id, parent) {
  105. var msg = id + '-' + parent;
  106. console.info(chalk.green('✓ done:'), chalk.green(file), chalk.gray.dim('(' + msg + ')'));
  107. }
  108. /**
  109. * Checks if a file has been added to muted list.
  110. * This stops single changes from logging multiple times.
  111. * @param {string} id Name of changed file
  112. * @param {Array<string>} list Muted files array
  113. * @return {Boolean} True if file is muted
  114. */
  115. function isMuted(id, list) {
  116. return list.reduce(function(prev, curr) {
  117. return prev || (curr === id);
  118. }, false);
  119. }