build-lib.js 1.8 KB

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