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. ))
  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. .catch(() => 0) // probably doesn't exist
  38. // Skip files that haven't changed
  39. if (srcMtime < libMtime && metaMtime < libMtime) {
  40. continue
  41. }
  42. }
  43. const { code, map } = await transformFile(file, { sourceMaps: true })
  44. await mkdirp(path.dirname(libFile))
  45. await Promise.all([
  46. writeFile(libFile, code),
  47. writeFile(libFile + '.map', JSON.stringify(map))
  48. ])
  49. console.log(chalk.green('Compiled lib:'), chalk.magenta(libFile))
  50. }
  51. }
  52. console.log('Using Babel version:', require('@babel/core/package.json').version)
  53. buildLib().catch((err) => {
  54. console.error(err.stack)
  55. process.exit(1)
  56. })