build-examples.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * build-examples.js
  3. * --------
  4. * Searches for each example's `js/app.es6` file.
  5. * Creates a new watchify instance for each `app.es6`.
  6. * Changes to Uppy's source will trigger rebundling.
  7. *
  8. * Run as:
  9. *
  10. * build-examples.js # to build all examples one-off
  11. * build-examples.js watch # to keep rebuilding examples with an internal watchify
  12. * build-examples.js <path> # to build just one example app.es6
  13. * build-examples.js <path> <path> # to build just one example app.es6 to a specific location
  14. *
  15. * Note:
  16. * Since each example is dependent on Uppy's source,
  17. * changing one source file causes the 'file changed'
  18. * notification to fire multiple times. To stop this,
  19. * files are added to a 'muted' Set that is checked
  20. * before announcing a changed file. It's removed from
  21. * the Set when it has been bundled.
  22. */
  23. const createStream = require('fs').createWriteStream
  24. const glob = require('multi-glob').glob
  25. const chalk = require('chalk')
  26. const path = require('path')
  27. const mkdirp = require('mkdirp')
  28. const notifier = require('node-notifier')
  29. const babelify = require('babelify')
  30. const aliasify = require('aliasify')
  31. const browserify = require('browserify')
  32. const watchify = require('watchify')
  33. const bresolve = require('browser-resolve')
  34. function useSourcePackages (b) {
  35. b._bresolve = (id, opts, cb) => {
  36. bresolve(id, opts, (err, result, pkg) => {
  37. if (err) return cb(err)
  38. if (/packages\/@uppy\/.*?\/lib\//.test(result)) {
  39. result = result.replace(/packages\/@uppy\/(.*?)\/lib\//, 'packages/@uppy/$1/src/')
  40. }
  41. cb(err, result, pkg)
  42. })
  43. }
  44. }
  45. const webRoot = __dirname
  46. let srcPattern = `${webRoot}/src/examples/**/app.es6`
  47. let dstPattern = `${webRoot}/public/examples/**/app.js`
  48. const watchifyEnabled = process.argv[2] === 'watch'
  49. const browserifyPlugins = [useSourcePackages]
  50. if (watchifyEnabled) {
  51. browserifyPlugins.push(watchify)
  52. }
  53. // Instead of 'watch', build-examples.js can also take a path as cli argument.
  54. // In this case we'll only bundle the specified path/pattern
  55. if (!watchifyEnabled && process.argv[2]) {
  56. srcPattern = process.argv[2]
  57. if (process.argv[3]) {
  58. dstPattern = process.argv[3]
  59. }
  60. }
  61. // Find each app.es6 file with glob.
  62. glob(srcPattern, (err, files) => {
  63. if (err) throw new Error(err)
  64. if (watchifyEnabled) {
  65. console.log('--> Watching examples..')
  66. }
  67. const muted = new Set()
  68. // Create a new watchify instance for each file.
  69. files.forEach((file) => {
  70. const b = browserify(file, {
  71. cache: {},
  72. packageCache: {},
  73. debug: true,
  74. plugin: browserifyPlugins
  75. })
  76. // Aliasing for using `require('uppy')`, etc.
  77. b
  78. .transform(babelify)
  79. .transform(aliasify, {
  80. aliases: {
  81. '@uppy': `./${path.relative(process.cwd(), path.join(__dirname, '../packages/@uppy'))}`
  82. }
  83. })
  84. // Listeners for changes, errors, and completion.
  85. b
  86. .on('update', bundle)
  87. .on('error', onError)
  88. .on('file', (file) => {
  89. // When file completes, unmute it.
  90. muted.delete(file)
  91. })
  92. // Call bundle() manually to start watch processes.
  93. bundle()
  94. /**
  95. * Creates bundle and writes it to static and public folders.
  96. * Changes to
  97. * @param {[type]} ids [description]
  98. * @return {[type]} [description]
  99. */
  100. function bundle (ids = []) {
  101. ids.forEach((id) => {
  102. if (!muted.has(id)) {
  103. console.info(chalk.cyan('change:'), path.relative(process.cwd(), id))
  104. muted.add(id)
  105. }
  106. })
  107. const exampleName = path.basename(path.dirname(file))
  108. const output = dstPattern.replace('**', exampleName)
  109. const parentDir = path.dirname(output)
  110. mkdirp.sync(parentDir)
  111. console.info(chalk.grey(`⏳ building: ${path.relative(process.cwd(), file)}`))
  112. b
  113. .bundle()
  114. .on('error', onError)
  115. .pipe(createStream(output))
  116. .on('finish', () => {
  117. console.info(chalk.green(`✓ built: ${path.relative(process.cwd(), file)}`))
  118. })
  119. }
  120. })
  121. })
  122. /**
  123. * Logs to console and shows desktop notification on error.
  124. * Calls `this.emit(end)` to stop bundling.
  125. * @param {object} err Error object
  126. */
  127. function onError (err) {
  128. console.error(chalk.red('✗ error:'), chalk.red(err.message))
  129. notifier.notify({
  130. title: 'Build failed:',
  131. message: err.message
  132. })
  133. this.emit('end')
  134. // When running without watch, process.exit(1) on error
  135. if (!watchifyEnabled) {
  136. process.exit(1)
  137. }
  138. }