build-lib.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const chalk = require('chalk')
  2. const babel = require('@babel/core')
  3. const { promisify } = require('util')
  4. const glob = promisify(require('glob'))
  5. const fs = require('fs')
  6. const path = require('path')
  7. const { mkdir, stat, writeFile } = fs.promises
  8. const SOURCE = 'packages/{*,@uppy/*}/src/**/*.js'
  9. // Files not to build (such as tests)
  10. const IGNORE = /\.test\.js$|__mocks__|svelte|angular|companion\//
  11. // Files that should trigger a rebuild of everything on change
  12. const META_FILES = [
  13. 'babel.config.js',
  14. 'package.json',
  15. 'package-lock.json',
  16. 'yarn.lock',
  17. 'bin/build-lib.js',
  18. ]
  19. function lastModified (file) {
  20. return stat(file).then((s) => s.mtime, (err) => {
  21. if (err.code === 'ENOENT') {
  22. return 0
  23. }
  24. throw err
  25. })
  26. }
  27. async function buildLib () {
  28. const metaMtimes = await Promise.all(META_FILES.map((filename) => lastModified(path.join(__dirname, '..', filename))))
  29. const metaMtime = Math.max(...metaMtimes)
  30. const files = await glob(SOURCE)
  31. /* eslint-disable no-continue */
  32. for (const file of files) {
  33. if (IGNORE.test(file)) {
  34. continue
  35. }
  36. const libFile = file.replace('/src/', '/lib/')
  37. // on a fresh build, rebuild everything.
  38. if (!process.env.FRESH) {
  39. const srcMtime = await lastModified(file)
  40. const libMtime = await lastModified(libFile)
  41. .catch(() => 0) // probably doesn't exist
  42. // Skip files that haven't changed
  43. if (srcMtime < libMtime && metaMtime < libMtime) {
  44. continue
  45. }
  46. }
  47. const { code, map } = await babel.transformFileAsync(file, { sourceMaps: true })
  48. await mkdir(path.dirname(libFile), { recursive: true })
  49. await Promise.all([
  50. writeFile(libFile, code),
  51. writeFile(`${libFile}.map`, JSON.stringify(map)),
  52. ])
  53. console.log(chalk.green('Compiled lib:'), chalk.magenta(libFile))
  54. }
  55. /* eslint-enable no-continue */
  56. }
  57. console.log('Using Babel version:', require('@babel/core/package.json').version)
  58. buildLib().catch((err) => {
  59. console.error(err)
  60. process.exit(1)
  61. })