build-examples.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 { createWriteStream, mkdirSync } = require('fs')
  24. const { glob } = require('multi-glob')
  25. const chalk = require('chalk')
  26. const path = require('path')
  27. const notifier = require('node-notifier')
  28. const babelify = require('babelify')
  29. const aliasify = require('aliasify')
  30. const browserify = require('browserify')
  31. const watchify = require('watchify')
  32. const bresolve = require('browser-resolve')
  33. function useSourcePackages (b) {
  34. b._bresolve = (id, opts, cb) => {
  35. bresolve(id, opts, (err, result, pkg) => {
  36. if (err) return cb(err)
  37. if (/packages\/@uppy\/[^/]+?\/lib\//.test(result)) {
  38. result = result.replace(/packages\/@uppy\/([^/]+?)\/lib\//, 'packages/@uppy/$1/src/')
  39. }
  40. cb(err, result, pkg)
  41. })
  42. }
  43. }
  44. const webRoot = __dirname
  45. let srcPattern = `${webRoot}/src/examples/**/app.es6`
  46. let dstPattern = `${webRoot}/public/examples/**/app.js`
  47. const watchifyEnabled = process.argv[2] === 'watch'
  48. const browserifyPlugins = [useSourcePackages]
  49. if (watchifyEnabled) {
  50. browserifyPlugins.push(watchify)
  51. }
  52. // Instead of 'watch', build-examples.js can also take a path as cli argument.
  53. // In this case we'll only bundle the specified path/pattern
  54. if (!watchifyEnabled && process.argv[2]) {
  55. srcPattern = process.argv[2]
  56. if (process.argv[3]) {
  57. dstPattern = process.argv[3]
  58. }
  59. }
  60. // Find each app.es6 file with glob.
  61. glob(srcPattern, (err, files) => {
  62. if (err) throw new Error(err)
  63. if (watchifyEnabled) {
  64. console.log('--> Watching examples..')
  65. }
  66. const muted = new Set()
  67. // Create a new watchify instance for each file.
  68. files.forEach((file) => {
  69. const b = browserify(file, {
  70. cache: {},
  71. packageCache: {},
  72. debug: true,
  73. plugin: browserifyPlugins,
  74. })
  75. // Aliasing for using `require('uppy')`, etc.
  76. b
  77. .transform(babelify, {
  78. root: path.join(__dirname, '..'),
  79. })
  80. .transform(aliasify, {
  81. aliases: {
  82. '@uppy': `./${path.relative(process.cwd(), path.join(__dirname, '../packages/@uppy'))}`,
  83. },
  84. })
  85. // Listeners for changes, errors, and completion.
  86. b
  87. .on('update', bundle)
  88. .on('error', onError)
  89. .on('file', (file) => {
  90. // When file completes, unmute it.
  91. muted.delete(file)
  92. })
  93. // Call bundle() manually to start watch processes.
  94. bundle()
  95. /**
  96. * Creates bundle and writes it to static and public folders.
  97. * Changes to
  98. *
  99. * @param {string[]} ids
  100. */
  101. function bundle (ids = []) {
  102. ids.forEach((id) => {
  103. if (!muted.has(id)) {
  104. console.info(chalk.cyan('change:'), path.relative(process.cwd(), id))
  105. muted.add(id)
  106. }
  107. })
  108. const exampleName = path.basename(path.dirname(file))
  109. const output = dstPattern.replace('**', exampleName)
  110. const parentDir = path.dirname(output)
  111. mkdirSync(parentDir, { recursive: true })
  112. console.info(chalk.grey(`⏳ building: ${path.relative(process.cwd(), file)}`))
  113. b
  114. .bundle()
  115. .on('error', onError)
  116. .pipe(createWriteStream(output))
  117. .on('finish', () => {
  118. console.info(chalk.green(`✓ built: ${path.relative(process.cwd(), file)}`))
  119. })
  120. }
  121. })
  122. })
  123. /**
  124. * Logs to console and shows desktop notification on error.
  125. * Calls `this.emit(end)` to stop bundling.
  126. *
  127. * @param {object} err Error object
  128. */
  129. function onError (err) {
  130. console.error(chalk.red('✗ error:'), chalk.red(err.message))
  131. notifier.notify({
  132. title: 'Build failed:',
  133. message: err.message,
  134. })
  135. this.emit('end')
  136. // When running without watch, process.exit(1) on error
  137. if (!watchifyEnabled) {
  138. process.exit(1)
  139. }
  140. }