build-lib.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. 'cropperjs',
  26. ]
  27. function lastModified (file, createParentDir = false) {
  28. return stat(file).then((s) => s.mtime, async (err) => {
  29. if (err.code === 'ENOENT') {
  30. if (createParentDir) {
  31. await mkdir(path.dirname(file), { recursive: true })
  32. }
  33. return 0
  34. }
  35. throw err
  36. })
  37. }
  38. const moduleTypeCache = new Map()
  39. const versionCache = new Map()
  40. async function isTypeModule (file) {
  41. const packageFolder = file.slice(0, file.indexOf('/src/'))
  42. const cachedValue = moduleTypeCache.get(packageFolder)
  43. if (cachedValue != null) return cachedValue
  44. // eslint-disable-next-line import/no-dynamic-require, global-require
  45. const { type, version } = require(path.join(__dirname, '..', packageFolder, 'package.json'))
  46. const typeModule = type === 'module'
  47. if (process.env.FRESH) {
  48. // in case it hasn't been done before.
  49. await mkdir(path.join(packageFolder, 'lib'), { recursive: true })
  50. }
  51. if (typeModule) {
  52. await writeFile(path.join(packageFolder, 'lib', 'package.json'), '{"type":"commonjs"}')
  53. }
  54. moduleTypeCache.set(packageFolder, typeModule)
  55. versionCache.set(packageFolder, version)
  56. return typeModule
  57. }
  58. // eslint-disable-next-line no-shadow
  59. function ExportAllDeclaration (path) {
  60. const { value } = path.node.source
  61. if (value.endsWith('.jsx') && (value.startsWith('./') || value.startsWith('../'))) {
  62. // Rewrite .jsx imports to .js:
  63. path.node.source.value = value.slice(0, -1) // eslint-disable-line no-param-reassign
  64. }
  65. path.replaceWith(
  66. t.assignmentExpression(
  67. '=',
  68. t.memberExpression(t.identifier('module'), t.identifier('exports')),
  69. t.callExpression(t.identifier('require'), [path.node.source]),
  70. ),
  71. )
  72. }
  73. async function buildLib () {
  74. const metaMtimes = await Promise.all(META_FILES.map((filename) => lastModified(path.join(__dirname, '..', filename))))
  75. const metaMtime = Math.max(...metaMtimes)
  76. const files = await glob(SOURCE)
  77. /* eslint-disable no-continue */
  78. for (const file of files) {
  79. if (IGNORE.test(file)) {
  80. continue
  81. }
  82. const libFile = file.replace('/src/', '/lib/').replace(/\.jsx$/, '.js')
  83. // on a fresh build, rebuild everything.
  84. if (!process.env.FRESH) {
  85. const [srcMtime, libMtime] = await Promise.all([
  86. lastModified(file),
  87. lastModified(libFile, true),
  88. ])
  89. // Skip files that haven't changed
  90. if (srcMtime < libMtime && metaMtime < libMtime) {
  91. continue
  92. }
  93. }
  94. let idCounter = 0 // counter to ensure uniqueness of identifiers created by the build script.
  95. const plugins = await isTypeModule(file) ? [['@babel/plugin-transform-modules-commonjs', {
  96. importInterop: 'none',
  97. }], {
  98. visitor: {
  99. // eslint-disable-next-line no-shadow
  100. ImportDeclaration (path) {
  101. let { value } = path.node.source
  102. if (value.endsWith('.jsx') && (value.startsWith('./') || value.startsWith('../'))) {
  103. // Rewrite .jsx imports to .js:
  104. value = path.node.source.value = value.slice(0, -1) // eslint-disable-line no-param-reassign,no-multi-assign
  105. }
  106. if (PACKAGE_JSON_IMPORT.test(value)
  107. && path.node.specifiers.length === 1
  108. && path.node.specifiers[0].type === 'ImportDefaultSpecifier') {
  109. // Vendor-in version number from package.json files:
  110. const version = versionCache.get(file.slice(0, file.indexOf('/src/')))
  111. if (version != null) {
  112. const [{ local }] = path.node.specifiers
  113. path.replaceWith(
  114. t.variableDeclaration('const', [t.variableDeclarator(local,
  115. t.objectExpression([
  116. t.objectProperty(t.stringLiteral('version'), t.stringLiteral(version)),
  117. ]))]),
  118. )
  119. }
  120. } else if (path.node.specifiers[0].type === 'ImportDefaultSpecifier') {
  121. const [{ local }, ...otherSpecifiers] = path.node.specifiers
  122. if (otherSpecifiers.length === 1 && otherSpecifiers[0].type === 'ImportNamespaceSpecifier') {
  123. // import defaultVal, * as namespaceImport from '@uppy/package'
  124. // is transformed into:
  125. // const defaultVal = require('@uppy/package'); const namespaceImport = defaultVal
  126. path.insertAfter(
  127. t.variableDeclaration('const', [
  128. t.variableDeclarator(
  129. otherSpecifiers[0].local,
  130. local,
  131. ),
  132. ]),
  133. )
  134. } else if (otherSpecifiers.length !== 0) {
  135. // import defaultVal, { exportedVal as importedName, other } from '@uppy/package'
  136. // is transformed into:
  137. // const defaultVal = require('@uppy/package'); const { exportedVal: importedName, other } = defaultVal
  138. path.insertAfter(t.variableDeclaration('const', [t.variableDeclarator(
  139. t.objectPattern(
  140. otherSpecifiers.map(specifier => t.objectProperty(
  141. t.identifier(specifier.imported.name),
  142. specifier.local,
  143. )),
  144. ),
  145. local,
  146. )]))
  147. }
  148. let requireCall = t.callExpression(t.identifier('require'), [
  149. t.stringLiteral(value),
  150. ])
  151. if (esPackagesThatNeedSpecialTreatmentForRollupInterop.includes(value)) {
  152. requireCall = t.logicalExpression('||', t.memberExpression(requireCall, t.identifier('default')), requireCall)
  153. }
  154. path.replaceWith(
  155. t.variableDeclaration('const', [
  156. t.variableDeclarator(
  157. local,
  158. requireCall,
  159. ),
  160. ]),
  161. )
  162. }
  163. },
  164. ExportAllDeclaration,
  165. // eslint-disable-next-line no-shadow,consistent-return
  166. ExportNamedDeclaration (path) {
  167. if (path.node.source != null) {
  168. if (path.node.specifiers.length === 1
  169. && path.node.specifiers[0].local.name === 'default'
  170. && path.node.specifiers[0].exported.name === 'default') return ExportAllDeclaration(path)
  171. if (path.node.specifiers.some(spec => spec.exported.name === 'default')) {
  172. throw new Error('unsupported mix of named and default re-exports')
  173. }
  174. let { value } = path.node.source
  175. if (value.endsWith('.jsx') && (value.startsWith('./') || value.startsWith('../'))) {
  176. // Rewrite .jsx imports to .js:
  177. value = path.node.source.value = value.slice(0, -1) // eslint-disable-line no-param-reassign,no-multi-assign
  178. }
  179. // If there are no default export/import involved, Babel can handle it with no problem.
  180. if (path.node.specifiers.every(spec => spec.local.name !== 'default' && spec.exported.name !== 'default')) return undefined
  181. let requireCall = t.callExpression(t.identifier('require'), [
  182. t.stringLiteral(value),
  183. ])
  184. if (esPackagesThatNeedSpecialTreatmentForRollupInterop.includes(value)) {
  185. requireCall = t.logicalExpression('||', t.memberExpression(requireCall, t.identifier('default')), requireCall)
  186. }
  187. const requireCallIdentifier = t.identifier(`_${idCounter++}`)
  188. const namedExportIdentifiers = path.node.specifiers
  189. .filter(spec => spec.local.name !== 'default')
  190. .map(spec => [
  191. t.identifier(requireCallIdentifier.name + spec.local.name),
  192. t.memberExpression(requireCallIdentifier, spec.local),
  193. spec,
  194. ])
  195. path.insertBefore(
  196. t.variableDeclaration('const', [
  197. t.variableDeclarator(
  198. requireCallIdentifier,
  199. requireCall,
  200. ),
  201. ...namedExportIdentifiers.map(([id, propertyAccessor]) => t.variableDeclarator(id, propertyAccessor)),
  202. ]),
  203. )
  204. path.replaceWith(
  205. t.exportNamedDeclaration(null, path.node.specifiers.map(spec => t.exportSpecifier(
  206. spec.local.name === 'default' ? requireCallIdentifier : namedExportIdentifiers.find(([,, s]) => s === spec)[0],
  207. spec.exported,
  208. ))),
  209. )
  210. }
  211. },
  212. // eslint-disable-next-line no-shadow
  213. ExportDefaultDeclaration (path) {
  214. const moduleExports = t.memberExpression(t.identifier('module'), t.identifier('exports'))
  215. if (!t.isDeclaration(path.node.declaration)) {
  216. path.replaceWith(
  217. t.assignmentExpression('=', moduleExports, path.node.declaration),
  218. )
  219. } else if (path.node.declaration.id != null) {
  220. const { id } = path.node.declaration
  221. path.insertBefore(path.node.declaration)
  222. path.replaceWith(
  223. t.assignmentExpression('=', moduleExports, id),
  224. )
  225. } else {
  226. const id = t.identifier('_default')
  227. path.node.declaration.id = id // eslint-disable-line no-param-reassign
  228. path.insertBefore(path.node.declaration)
  229. path.replaceWith(
  230. t.assignmentExpression('=', moduleExports, id),
  231. )
  232. }
  233. },
  234. },
  235. }] : undefined
  236. const { code, map } = await babel.transformFileAsync(file, { sourceMaps: true, plugins })
  237. await Promise.all([
  238. writeFile(libFile, code),
  239. writeFile(`${libFile}.map`, JSON.stringify(map)),
  240. ])
  241. console.log(chalk.green('Compiled lib:'), chalk.magenta(libFile))
  242. }
  243. /* eslint-enable no-continue */
  244. }
  245. console.log('Using Babel version:', require('@babel/core/package.json').version)
  246. buildLib().catch((err) => {
  247. console.error(err)
  248. process.exit(1)
  249. })