build.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 (!targets.length) {
  33. await buildAll(allTargets)
  34. checkAllSizes(allTargets)
  35. } else {
  36. await buildAll(fuzzyMatchTarget(targets, buildAllMatching))
  37. checkAllSizes(fuzzyMatchTarget(targets, buildAllMatching))
  38. }
  39. }
  40. async function buildAll(targets) {
  41. for (const target of targets) {
  42. await build(target)
  43. }
  44. }
  45. async function build(target) {
  46. const pkgDir = path.resolve(`packages/${target}`)
  47. const pkg = require(`${pkgDir}/package.json`)
  48. // only build published packages for release
  49. if (isRelease && pkg.private) {
  50. return
  51. }
  52. // if building a specific format, do not remove dist.
  53. if (!formats) {
  54. await fs.remove(`${pkgDir}/dist`)
  55. }
  56. const env =
  57. (pkg.buildOptions && pkg.buildOptions.env) ||
  58. (devOnly ? 'development' : 'production')
  59. await execa(
  60. 'rollup',
  61. [
  62. '-c',
  63. '--environment',
  64. [
  65. `COMMIT:${commit}`,
  66. `NODE_ENV:${env}`,
  67. `TARGET:${target}`,
  68. formats ? `FORMATS:${formats}` : ``,
  69. buildTypes ? `TYPES:true` : ``,
  70. prodOnly ? `PROD_ONLY:true` : ``,
  71. sourceMap ? `SOURCE_MAP:true` : ``
  72. ]
  73. .filter(Boolean)
  74. .join(',')
  75. ],
  76. { stdio: 'inherit' }
  77. )
  78. if (buildTypes && pkg.types) {
  79. console.log()
  80. console.log(
  81. chalk.bold(chalk.yellow(`Rolling up type definitions for ${target}...`))
  82. )
  83. // build types
  84. const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor')
  85. const extractorConfigPath = path.resolve(pkgDir, `api-extractor.json`)
  86. const extractorConfig = ExtractorConfig.loadFileAndPrepare(
  87. extractorConfigPath
  88. )
  89. const extractorResult = Extractor.invoke(extractorConfig, {
  90. localBuild: true,
  91. showVerboseMessages: true
  92. })
  93. if (extractorResult.succeeded) {
  94. // concat additional d.ts to rolled-up dts
  95. const typesDir = path.resolve(pkgDir, 'types')
  96. if (await fs.exists(typesDir)) {
  97. const dtsPath = path.resolve(pkgDir, pkg.types)
  98. const existing = await fs.readFile(dtsPath, 'utf-8')
  99. const typeFiles = await fs.readdir(typesDir)
  100. const toAdd = await Promise.all(
  101. typeFiles.map(file => {
  102. return fs.readFile(path.resolve(typesDir, file), 'utf-8')
  103. })
  104. )
  105. await fs.writeFile(dtsPath, existing + '\n' + toAdd.join('\n'))
  106. }
  107. console.log(
  108. chalk.bold(chalk.green(`API Extractor completed successfully.`))
  109. )
  110. } else {
  111. console.error(
  112. `API Extractor completed with ${extractorResult.errorCount} errors` +
  113. ` and ${extractorResult.warningCount} warnings`
  114. )
  115. process.exitCode = 1
  116. }
  117. await fs.remove(`${pkgDir}/dist/packages`)
  118. }
  119. }
  120. function checkAllSizes(targets) {
  121. if (devOnly) {
  122. return
  123. }
  124. console.log()
  125. for (const target of targets) {
  126. checkSize(target)
  127. }
  128. console.log()
  129. }
  130. function checkSize(target) {
  131. const pkgDir = path.resolve(`packages/${target}`)
  132. checkFileSize(`${pkgDir}/dist/${target}.global.prod.js`)
  133. }
  134. function checkFileSize(filePath) {
  135. if (!fs.existsSync(filePath)) {
  136. return
  137. }
  138. const file = fs.readFileSync(filePath)
  139. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  140. const gzipped = gzipSync(file)
  141. const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  142. const compressed = compress(file)
  143. const compressedSize = (compressed.length / 1024).toFixed(2) + 'kb'
  144. console.log(
  145. `${chalk.gray(
  146. chalk.bold(path.basename(filePath))
  147. )} min:${minSize} / gzip:${gzippedSize} / brotli:${compressedSize}`
  148. )
  149. }