build-lib.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const babel = require('@babel/core')
  2. const t = require('@babel/types')
  3. const { promisify } = require('node:util')
  4. const glob = promisify(require('glob'))
  5. const fs = require('node:fs')
  6. const path = require('node:path')
  7. const { mkdir, stat, writeFile } = fs.promises
  8. const PACKAGE_JSON_IMPORT = /^\..*\/package.json$/
  9. const SOURCE = 'packages/{*,@uppy/*}/src/**/*.{js,ts}?(x)'
  10. // Files not to build (such as tests)
  11. const IGNORE = /\.test\.[jt]s$|__mocks__|svelte|angular|companion\//
  12. // Files that should trigger a rebuild of everything on change
  13. const META_FILES = [
  14. 'babel.config.js',
  15. 'package.json',
  16. 'package-lock.json',
  17. 'yarn.lock',
  18. 'bin/build-lib.js',
  19. ]
  20. function lastModified (file, createParentDir = false) {
  21. return stat(file).then((s) => s.mtime, async (err) => {
  22. if (err.code === 'ENOENT') {
  23. if (createParentDir) {
  24. await mkdir(path.dirname(file), { recursive: true })
  25. }
  26. return 0
  27. }
  28. throw err
  29. })
  30. }
  31. const versionCache = new Map()
  32. async function preparePackage (file) {
  33. const packageFolder = file.slice(0, file.indexOf('/src/'))
  34. if (versionCache.has(packageFolder)) return
  35. // eslint-disable-next-line import/no-dynamic-require, global-require
  36. const { version } = require(path.join(__dirname, '..', packageFolder, 'package.json'))
  37. if (process.env.FRESH) {
  38. // in case it hasn't been done before.
  39. await mkdir(path.join(packageFolder, 'lib'), { recursive: true })
  40. }
  41. versionCache.set(packageFolder, version)
  42. }
  43. const nonJSImport = /^\.\.?\/.+\.([jt]sx|ts)$/
  44. // eslint-disable-next-line no-shadow
  45. function rewriteNonJSImportsToJS (path) {
  46. const match = nonJSImport.exec(path.node.source.value)
  47. if (match) {
  48. // eslint-disable-next-line no-param-reassign
  49. path.node.source.value = `${match[0].slice(0, -match[1].length)}js`
  50. }
  51. }
  52. async function buildLib () {
  53. const metaMtimes = await Promise.all(META_FILES.map((filename) => lastModified(path.join(__dirname, '..', filename))))
  54. const metaMtime = Math.max(...metaMtimes)
  55. const files = await glob(SOURCE)
  56. /* eslint-disable no-continue */
  57. for (const file of files) {
  58. if (IGNORE.test(file)) {
  59. continue
  60. }
  61. await preparePackage(file)
  62. const libFile = file.replace('/src/', '/lib/').replace(/\.[jt]sx?$/, '.js')
  63. // on a fresh build, rebuild everything.
  64. if (!process.env.FRESH) {
  65. const [srcMtime, libMtime] = await Promise.all([
  66. lastModified(file),
  67. lastModified(libFile, true),
  68. ])
  69. // Skip files that haven't changed
  70. if (srcMtime < libMtime && metaMtime < libMtime) {
  71. continue
  72. }
  73. }
  74. const plugins = [{
  75. visitor: {
  76. // eslint-disable-next-line no-shadow
  77. ImportDeclaration (path) {
  78. rewriteNonJSImportsToJS(path)
  79. if (PACKAGE_JSON_IMPORT.test(path.node.source.value)
  80. && path.node.specifiers.length === 1
  81. && path.node.specifiers[0].type === 'ImportDefaultSpecifier') {
  82. // Vendor-in version number from package.json files:
  83. const version = versionCache.get(file.slice(0, file.indexOf('/src/')))
  84. if (version != null) {
  85. const [{ local }] = path.node.specifiers
  86. path.replaceWith(
  87. t.variableDeclaration('const', [t.variableDeclarator(local,
  88. t.objectExpression([
  89. t.objectProperty(t.stringLiteral('version'), t.stringLiteral(version)),
  90. ]))]),
  91. )
  92. }
  93. }
  94. },
  95. ExportAllDeclaration: rewriteNonJSImportsToJS,
  96. // eslint-disable-next-line no-shadow
  97. ExportNamedDeclaration (path) {
  98. if (path.node.source != null) {
  99. rewriteNonJSImportsToJS(path)
  100. }
  101. },
  102. },
  103. }]
  104. const isTSX = file.endsWith('.tsx')
  105. if (isTSX || file.endsWith('.ts')) { plugins.push(['@babel/plugin-transform-typescript', { disallowAmbiguousJSXLike: true, isTSX, jsxPragma: 'h' }]) }
  106. const { code, map } = await babel.transformFileAsync(file, { sourceMaps: true, plugins })
  107. const [{ default: chalk }] = await Promise.all([
  108. import('chalk'),
  109. writeFile(libFile, code),
  110. writeFile(`${libFile}.map`, JSON.stringify(map)),
  111. ])
  112. console.log(chalk.green('Compiled lib:'), chalk.magenta(libFile))
  113. }
  114. /* eslint-enable no-continue */
  115. }
  116. console.log('Using Babel version:', require('@babel/core/package.json').version)
  117. buildLib().catch((err) => {
  118. console.error(err)
  119. process.exit(1)
  120. })