build-lib.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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__|svelte|companion\//
  14. // Files that should trigger a rebuild of everything on change
  15. const META_FILES = [
  16. 'babel.config.js',
  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. const metaMtime = Math.max(...metaMtimes)
  28. const files = await glob(SOURCE)
  29. for (const file of files) {
  30. if (IGNORE.test(file)) continue
  31. const libFile = file.replace('/src/', '/lib/')
  32. // on a fresh build, rebuild everything.
  33. if (!process.env.FRESH) {
  34. const srcMtime = await lastModified(file)
  35. const libMtime = await lastModified(libFile)
  36. .catch(() => 0) // probably doesn't exist
  37. // Skip files that haven't changed
  38. if (srcMtime < libMtime && metaMtime < libMtime) {
  39. continue
  40. }
  41. }
  42. const { code, map } = await transformFile(file, { sourceMaps: true })
  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. })