build-lib.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // Rollup uses get-form-data's ES modules build, and rollup-plugin-commonjs automatically resolves `.default`.
  22. // So, if we are being built using rollup, this require() won't have a `.default` property.
  23. const esPackagesThatNeedSpecialTreatmentForRollupInterop = [
  24. 'get-form-data',
  25. ]
  26. function lastModified (file, createParentDir = false) {
  27. return stat(file).then((s) => s.mtime, async (err) => {
  28. if (err.code === 'ENOENT') {
  29. if (createParentDir) {
  30. await mkdir(path.dirname(file), { recursive: true })
  31. }
  32. return 0
  33. }
  34. throw err
  35. })
  36. }
  37. const moduleTypeCache = new Map()
  38. const versionCache = new Map()
  39. async function isTypeModule (file) {
  40. const packageFolder = file.slice(0, file.indexOf('/src/'))
  41. const cachedValue = moduleTypeCache.get(packageFolder)
  42. if (cachedValue != null) return cachedValue
  43. // eslint-disable-next-line import/no-dynamic-require, global-require
  44. const { type, version } = require(path.join(__dirname, '..', packageFolder, 'package.json'))
  45. const typeModule = type === 'module'
  46. if (process.env.FRESH) {
  47. // in case it hasn't been done before.
  48. await mkdir(path.join(packageFolder, 'lib'), { recursive: true })
  49. }
  50. if (typeModule) {
  51. await writeFile(path.join(packageFolder, 'lib', 'package.json'), '{"type":"commonjs"}')
  52. }
  53. moduleTypeCache.set(packageFolder, typeModule)
  54. versionCache.set(packageFolder, version)
  55. return typeModule
  56. }
  57. // eslint-disable-next-line no-shadow
  58. function transformExportDeclarations (path) {
  59. const { value } = path.node.source
  60. if (value.endsWith('.jsx') && value.startsWith('./')) {
  61. // Rewrite .jsx imports to .js:
  62. path.node.source.value = value.slice(0, -1) // eslint-disable-line no-param-reassign
  63. }
  64. path.replaceWith(
  65. t.assignmentExpression(
  66. '=',
  67. t.memberExpression(t.identifier('module'), t.identifier('exports')),
  68. t.callExpression(t.identifier('require'), [path.node.source]),
  69. ),
  70. )
  71. }
  72. async function buildLib () {
  73. const metaMtimes = await Promise.all(META_FILES.map((filename) => lastModified(path.join(__dirname, '..', filename))))
  74. const metaMtime = Math.max(...metaMtimes)
  75. const files = await glob(SOURCE)
  76. /* eslint-disable no-continue */
  77. for (const file of files) {
  78. if (IGNORE.test(file)) {
  79. continue
  80. }
  81. const libFile = file.replace('/src/', '/lib/').replace(/\.jsx$/, '.js')
  82. // on a fresh build, rebuild everything.
  83. if (!process.env.FRESH) {
  84. const [srcMtime, libMtime] = await Promise.all([
  85. lastModified(file),
  86. lastModified(libFile, true),
  87. ])
  88. // Skip files that haven't changed
  89. if (srcMtime < libMtime && metaMtime < libMtime) {
  90. continue
  91. }
  92. }
  93. const plugins = await isTypeModule(file) ? [['@babel/plugin-transform-modules-commonjs', {
  94. importInterop: 'none',
  95. }], {
  96. visitor: {
  97. // eslint-disable-next-line no-shadow
  98. ImportDeclaration (path) {
  99. let { value } = path.node.source
  100. if (value.endsWith('.jsx') && value.startsWith('./')) {
  101. // Rewrite .jsx imports to .js:
  102. value = path.node.source.value = value.slice(0, -1) // eslint-disable-line no-param-reassign,no-multi-assign
  103. }
  104. if (PACKAGE_JSON_IMPORT.test(value)
  105. && path.node.specifiers.length === 1
  106. && path.node.specifiers[0].type === 'ImportDefaultSpecifier') {
  107. // Vendor-in version number from package.json files:
  108. const version = versionCache.get(file.slice(0, file.indexOf('/src/')))
  109. if (version != null) {
  110. const [{ local }] = path.node.specifiers
  111. path.replaceWith(
  112. t.variableDeclaration('const', [t.variableDeclarator(local,
  113. t.objectExpression([
  114. t.objectProperty(t.stringLiteral('version'), t.stringLiteral(version)),
  115. ]))]),
  116. )
  117. }
  118. } else if (path.node.specifiers[0].type === 'ImportDefaultSpecifier') {
  119. const [{ local }, ...otherSpecifiers] = path.node.specifiers
  120. if (otherSpecifiers.length === 1 && otherSpecifiers[0].type === 'ImportNamespaceSpecifier') {
  121. // import defaultVal, * as namespaceImport from '@uppy/package'
  122. // is transformed into:
  123. // const defaultVal = require('@uppy/package'); const namespaceImport = defaultVal
  124. path.insertAfter(
  125. t.variableDeclaration('const', [
  126. t.variableDeclarator(
  127. otherSpecifiers[0].local,
  128. local,
  129. ),
  130. ]),
  131. )
  132. } else if (otherSpecifiers.length !== 0) {
  133. // import defaultVal, { exportedVal as importedName, other } from '@uppy/package'
  134. // is transformed into:
  135. // const defaultVal = require('@uppy/package'); const { exportedVal: importedName, other } = defaultVal
  136. path.insertAfter(t.variableDeclaration('const', [t.variableDeclarator(
  137. t.objectPattern(
  138. otherSpecifiers.map(specifier => t.objectProperty(
  139. t.identifier(specifier.imported.name),
  140. specifier.local,
  141. )),
  142. ),
  143. local,
  144. )]))
  145. }
  146. let requireCall = t.callExpression(t.identifier('require'), [
  147. t.stringLiteral(value),
  148. ])
  149. if (esPackagesThatNeedSpecialTreatmentForRollupInterop.includes(value)) {
  150. requireCall = t.logicalExpression('||', t.memberExpression(requireCall, t.identifier('default')), requireCall)
  151. }
  152. path.replaceWith(
  153. t.variableDeclaration('const', [
  154. t.variableDeclarator(
  155. local,
  156. requireCall,
  157. ),
  158. ]),
  159. )
  160. }
  161. },
  162. ExportAllDeclaration: transformExportDeclarations,
  163. // eslint-disable-next-line no-shadow,consistent-return
  164. ExportNamedDeclaration (path) {
  165. if (path.node.source != null) return transformExportDeclarations(path)
  166. },
  167. // eslint-disable-next-line no-shadow
  168. ExportDefaultDeclaration (path) {
  169. const moduleExports = t.memberExpression(t.identifier('module'), t.identifier('exports'))
  170. if (!t.isDeclaration(path.node.declaration)) {
  171. path.replaceWith(
  172. t.assignmentExpression('=', moduleExports, path.node.declaration),
  173. )
  174. } else if (path.node.declaration.id != null) {
  175. const { id } = path.node.declaration
  176. path.insertBefore(path.node.declaration)
  177. path.replaceWith(
  178. t.assignmentExpression('=', moduleExports, id),
  179. )
  180. } else {
  181. const id = t.identifier('_default')
  182. path.node.declaration.id = id // eslint-disable-line no-param-reassign
  183. path.insertBefore(path.node.declaration)
  184. path.replaceWith(
  185. t.assignmentExpression('=', moduleExports, id),
  186. )
  187. }
  188. },
  189. },
  190. }] : undefined
  191. const { code, map } = await babel.transformFileAsync(file, { sourceMaps: true, plugins })
  192. await Promise.all([
  193. writeFile(libFile, code),
  194. writeFile(`${libFile}.map`, JSON.stringify(map)),
  195. ])
  196. console.log(chalk.green('Compiled lib:'), chalk.magenta(libFile))
  197. }
  198. /* eslint-enable no-continue */
  199. }
  200. console.log('Using Babel version:', require('@babel/core/package.json').version)
  201. buildLib().catch((err) => {
  202. console.error(err)
  203. process.exit(1)
  204. })