build-examples.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // eslint-disable-next-line no-underscore-dangle
  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.length > 2) {
  56. [, , srcPattern, dstPattern] = process.argv
  57. }
  58. // Find each app.es6 file with glob.
  59. glob(srcPattern, (err, files) => {
  60. if (err) throw new Error(err)
  61. if (watchifyEnabled) {
  62. console.log('--> Watching examples..')
  63. }
  64. const muted = new Set()
  65. // Create a new watchify instance for each file.
  66. files.forEach((file) => {
  67. const b = browserify(file, {
  68. cache: {},
  69. packageCache: {},
  70. debug: true,
  71. plugin: browserifyPlugins,
  72. })
  73. // Aliasing for using `require('uppy')`, etc.
  74. b
  75. .transform(babelify, {
  76. root: path.join(__dirname, '..'),
  77. })
  78. .transform(aliasify, {
  79. aliases: {
  80. '@uppy': `./${path.relative(process.cwd(), path.join(__dirname, '../packages/@uppy'))}`,
  81. },
  82. })
  83. // Listeners for changes, errors, and completion.
  84. b
  85. .on('update', bundle)
  86. .on('error', onError)
  87. .on('file', (file) => {
  88. // When file completes, unmute it.
  89. muted.delete(file)
  90. })
  91. // Call bundle() manually to start watch processes.
  92. bundle()
  93. /**
  94. * Creates bundle and writes it to static and public folders.
  95. * Changes to
  96. *
  97. * @param {string[]} ids
  98. */
  99. function bundle (ids = []) {
  100. ids.forEach((id) => {
  101. if (!muted.has(id)) {
  102. console.info(chalk.cyan('change:'), path.relative(process.cwd(), id))
  103. muted.add(id)
  104. }
  105. })
  106. const exampleName = path.basename(path.dirname(file))
  107. const output = dstPattern.replace('**', exampleName)
  108. const parentDir = path.dirname(output)
  109. mkdirSync(parentDir, { recursive: true })
  110. console.info(chalk.grey(`⏳ building: ${path.relative(process.cwd(), file)}`))
  111. b
  112. .bundle()
  113. .on('error', onError)
  114. .pipe(createWriteStream(output))
  115. .on('finish', () => {
  116. console.info(chalk.green(`✓ built: ${path.relative(process.cwd(), file)}`))
  117. })
  118. }
  119. })
  120. })
  121. /**
  122. * Logs to console and shows desktop notification on error.
  123. * Calls `this.emit(end)` to stop bundling.
  124. *
  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. }