build-lib.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const chalk = require('chalk')
  2. const babel = require('@babel/core')
  3. const t = require('@babel/types')
  4. const { promisify } = require('util')
  5. const glob = promisify(require('glob'))
  6. const fs = require('fs')
  7. const path = require('path')
  8. const { mkdir, stat, writeFile } = fs.promises
  9. const PACKAGE_JSON_IMPORT = /^\..*\/package.json$/
  10. const SOURCE = 'packages/{*,@uppy/*}/src/**/*.js?(x)'
  11. // Files not to build (such as tests)
  12. const IGNORE = /\.test\.js$|__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 moduleTypeCache = new Map()
  33. const versionCache = new Map()
  34. async function isTypeModule (file) {
  35. const packageFolder = file.slice(0, file.indexOf('/src/'))
  36. const cachedValue = moduleTypeCache.get(packageFolder)
  37. if (cachedValue != null) return cachedValue
  38. // eslint-disable-next-line import/no-dynamic-require, global-require
  39. const { type, version } = require(path.join(__dirname, '..', packageFolder, 'package.json'))
  40. const typeModule = type === 'module'
  41. if (process.env.FRESH) {
  42. // in case it hasn't been done before.
  43. await mkdir(path.join(packageFolder, 'lib'), { recursive: true })
  44. }
  45. if (typeModule) {
  46. await writeFile(path.join(packageFolder, 'lib', 'package.json'), '{"type":"commonjs"}')
  47. }
  48. moduleTypeCache.set(packageFolder, typeModule)
  49. versionCache.set(packageFolder, version)
  50. return typeModule
  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. const libFile = file.replace('/src/', '/lib/').replace(/\.jsx$/, '.js')
  62. // on a fresh build, rebuild everything.
  63. if (!process.env.FRESH) {
  64. const [srcMtime, libMtime] = await Promise.all([
  65. lastModified(file),
  66. lastModified(libFile, true),
  67. ])
  68. // Skip files that haven't changed
  69. if (srcMtime < libMtime && metaMtime < libMtime) {
  70. continue
  71. }
  72. }
  73. const plugins = await isTypeModule(file) ? [['@babel/plugin-transform-modules-commonjs', {
  74. importInterop: 'none',
  75. }], {
  76. visitor: {
  77. // eslint-disable-next-line no-shadow
  78. ImportDeclaration (path) {
  79. const { value } = path.node.source
  80. if (value.endsWith('.jsx') && value.startsWith('./')) {
  81. // Rewrite .jsx imports to .js:
  82. path.node.source.value = value.slice(0, -1) // eslint-disable-line no-param-reassign
  83. } else if (PACKAGE_JSON_IMPORT.test(value)
  84. && path.node.specifiers.length === 1
  85. && path.node.specifiers[0].type === 'ImportDefaultSpecifier') {
  86. // Vendor-in version number from package.json files:
  87. const version = versionCache.get(file.slice(0, file.indexOf('/src/')))
  88. if (version != null) {
  89. const [{ local }] = path.node.specifiers
  90. path.replaceWith(
  91. t.variableDeclaration('const', [t.variableDeclarator(local,
  92. t.objectExpression([
  93. t.objectProperty(t.stringLiteral('version'), t.stringLiteral(version)),
  94. ]))]),
  95. )
  96. }
  97. } else if (!value.startsWith('.')
  98. && path.node.specifiers.length === 1
  99. && path.node.specifiers[0].type === 'ImportDefaultSpecifier'
  100. ) {
  101. // Replace default imports with straight require calls (CommonJS interop):
  102. const [{ local }] = path.node.specifiers
  103. path.replaceWith(
  104. t.variableDeclaration('const', [
  105. t.variableDeclarator(
  106. local,
  107. t.callExpression(t.identifier('require'), [
  108. t.stringLiteral(value),
  109. ]),
  110. ),
  111. ]),
  112. )
  113. }
  114. },
  115. },
  116. }] : undefined
  117. const { code, map } = await babel.transformFileAsync(file, { sourceMaps: true, plugins })
  118. await Promise.all([
  119. writeFile(libFile, code),
  120. writeFile(`${libFile}.map`, JSON.stringify(map)),
  121. ])
  122. console.log(chalk.green('Compiled lib:'), chalk.magenta(libFile))
  123. }
  124. /* eslint-enable no-continue */
  125. }
  126. console.log('Using Babel version:', require('@babel/core/package.json').version)
  127. buildLib().catch((err) => {
  128. console.error(err)
  129. process.exit(1)
  130. })