build.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Produces production builds and stitches together d.ts files.
  3. To specify the package to build, simply pass its name and the desired build
  4. formats to output (defaults to `buildOptions.formats` specified in that package,
  5. or "esm,cjs"):
  6. ```
  7. # name supports fuzzy match. will build all packages with name containing "dom":
  8. yarn build dom
  9. # specify the format to output
  10. yarn build core --formats cjs
  11. ```
  12. */
  13. const fs = require('fs-extra')
  14. const path = require('path')
  15. const chalk = require('chalk')
  16. const execa = require('execa')
  17. const { gzipSync } = require('zlib')
  18. const { compress } = require('brotli')
  19. const { targets: allTargets, fuzzyMatchTarget } = require('./utils')
  20. const args = require('minimist')(process.argv.slice(2))
  21. const targets = args._
  22. const formats = args.formats || args.f
  23. const devOnly = args.devOnly || args.d
  24. const prodOnly = !devOnly && (args.prodOnly || args.p)
  25. const sourceMap = args.sourcemap || args.s
  26. const isRelease = args.release
  27. const buildTypes = args.t || args.types || isRelease
  28. const buildAllMatching = args.all || args.a
  29. const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)
  30. run()
  31. async function run() {
  32. if (isRelease) {
  33. // remove build cache for release builds to avoid outdated enum values
  34. await fs.remove(path.resolve(__dirname, '../node_modules/.rts2_cache'))
  35. }
  36. if (!targets.length) {
  37. await buildAll(allTargets)
  38. checkAllSizes(allTargets)
  39. } else {
  40. await buildAll(fuzzyMatchTarget(targets, buildAllMatching))
  41. checkAllSizes(fuzzyMatchTarget(targets, buildAllMatching))
  42. }
  43. }
  44. async function buildAll(targets) {
  45. await runParallel(require('os').cpus().length, targets, build)
  46. }
  47. async function runParallel(maxConcurrency, source, iteratorFn) {
  48. const ret = []
  49. const executing = []
  50. for (const item of source) {
  51. const p = Promise.resolve().then(() => iteratorFn(item, source))
  52. ret.push(p)
  53. if (maxConcurrency <= source.length) {
  54. const e = p.then(() => executing.splice(executing.indexOf(e), 1))
  55. executing.push(e)
  56. if (executing.length >= maxConcurrency) {
  57. await Promise.race(executing)
  58. }
  59. }
  60. }
  61. return Promise.all(ret)
  62. }
  63. async function build(target) {
  64. const pkgDir = path.resolve(`packages/${target}`)
  65. const pkg = require(`${pkgDir}/package.json`)
  66. // only build published packages for release
  67. if (isRelease && pkg.private) {
  68. return
  69. }
  70. // if building a specific format, do not remove dist.
  71. if (!formats) {
  72. await fs.remove(`${pkgDir}/dist`)
  73. }
  74. const env =
  75. (pkg.buildOptions && pkg.buildOptions.env) ||
  76. (devOnly ? 'development' : 'production')
  77. await execa(
  78. 'rollup',
  79. [
  80. '-c',
  81. '--environment',
  82. [
  83. `COMMIT:${commit}`,
  84. `NODE_ENV:${env}`,
  85. `TARGET:${target}`,
  86. formats ? `FORMATS:${formats}` : ``,
  87. buildTypes ? `TYPES:true` : ``,
  88. prodOnly ? `PROD_ONLY:true` : ``,
  89. sourceMap ? `SOURCE_MAP:true` : ``
  90. ]
  91. .filter(Boolean)
  92. .join(',')
  93. ],
  94. { stdio: 'inherit' }
  95. )
  96. if (buildTypes && pkg.types) {
  97. console.log()
  98. console.log(
  99. chalk.bold(chalk.yellow(`Rolling up type definitions for ${target}...`))
  100. )
  101. // build types
  102. const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor')
  103. const extractorConfigPath = path.resolve(pkgDir, `api-extractor.json`)
  104. const extractorConfig = ExtractorConfig.loadFileAndPrepare(
  105. extractorConfigPath
  106. )
  107. const extractorResult = Extractor.invoke(extractorConfig, {
  108. localBuild: true,
  109. showVerboseMessages: true
  110. })
  111. if (extractorResult.succeeded) {
  112. // concat additional d.ts to rolled-up dts
  113. const typesDir = path.resolve(pkgDir, 'types')
  114. if (await fs.exists(typesDir)) {
  115. const dtsPath = path.resolve(pkgDir, pkg.types)
  116. const existing = await fs.readFile(dtsPath, 'utf-8')
  117. const typeFiles = await fs.readdir(typesDir)
  118. const toAdd = await Promise.all(
  119. typeFiles.map(file => {
  120. return fs.readFile(path.resolve(typesDir, file), 'utf-8')
  121. })
  122. )
  123. await fs.writeFile(dtsPath, existing + '\n' + toAdd.join('\n'))
  124. }
  125. console.log(
  126. chalk.bold(chalk.green(`API Extractor completed successfully.`))
  127. )
  128. } else {
  129. console.error(
  130. `API Extractor completed with ${extractorResult.errorCount} errors` +
  131. ` and ${extractorResult.warningCount} warnings`
  132. )
  133. process.exitCode = 1
  134. }
  135. await fs.remove(`${pkgDir}/dist/packages`)
  136. }
  137. }
  138. function checkAllSizes(targets) {
  139. if (devOnly) {
  140. return
  141. }
  142. console.log()
  143. for (const target of targets) {
  144. checkSize(target)
  145. }
  146. console.log()
  147. }
  148. function checkSize(target) {
  149. const pkgDir = path.resolve(`packages/${target}`)
  150. checkFileSize(`${pkgDir}/dist/${target}.global.prod.js`)
  151. checkFileSize(`${pkgDir}/dist/${target}.runtime.global.prod.js`)
  152. }
  153. function checkFileSize(filePath) {
  154. if (!fs.existsSync(filePath)) {
  155. return
  156. }
  157. const file = fs.readFileSync(filePath)
  158. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  159. const gzipped = gzipSync(file)
  160. const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  161. const compressed = compress(file)
  162. const compressedSize = (compressed.length / 1024).toFixed(2) + 'kb'
  163. console.log(
  164. `${chalk.gray(
  165. chalk.bold(path.basename(filePath))
  166. )} min:${minSize} / gzip:${gzippedSize} / brotli:${compressedSize}`
  167. )
  168. }