build-lib.mjs 4.6 KB

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