build.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 lean = args.lean || args.l
  30. const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)
  31. run()
  32. async function run() {
  33. if (!targets.length) {
  34. await buildAll(allTargets)
  35. checkAllSizes(allTargets)
  36. } else {
  37. await buildAll(fuzzyMatchTarget(targets, buildAllMatching))
  38. checkAllSizes(fuzzyMatchTarget(targets, buildAllMatching))
  39. }
  40. }
  41. async function buildAll(targets) {
  42. for (const target of targets) {
  43. await build(target)
  44. }
  45. }
  46. async function build(target) {
  47. const pkgDir = path.resolve(`packages/${target}`)
  48. const pkg = require(`${pkgDir}/package.json`)
  49. // only build published packages for release
  50. if (isRelease && pkg.private) {
  51. return
  52. }
  53. // if building a specific format, do not remove dist.
  54. if (!formats) {
  55. await fs.remove(`${pkgDir}/dist`)
  56. }
  57. const env =
  58. (pkg.buildOptions && pkg.buildOptions.env) ||
  59. (devOnly ? 'development' : 'production')
  60. await execa(
  61. 'rollup',
  62. [
  63. '-c',
  64. '--environment',
  65. [
  66. `COMMIT:${commit}`,
  67. `NODE_ENV:${env}`,
  68. `TARGET:${target}`,
  69. formats ? `FORMATS:${formats}` : ``,
  70. buildTypes ? `TYPES:true` : ``,
  71. prodOnly ? `PROD_ONLY:true` : ``,
  72. lean ? `LEAN:true` : ``,
  73. sourceMap ? `SOURCE_MAP:true` : ``
  74. ]
  75. .filter(Boolean)
  76. .join(',')
  77. ],
  78. { stdio: 'inherit' }
  79. )
  80. if (buildTypes && pkg.types) {
  81. console.log()
  82. console.log(
  83. chalk.bold(chalk.yellow(`Rolling up type definitions for ${target}...`))
  84. )
  85. // build types
  86. const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor')
  87. const extractorConfigPath = path.resolve(pkgDir, `api-extractor.json`)
  88. const extractorConfig = ExtractorConfig.loadFileAndPrepare(
  89. extractorConfigPath
  90. )
  91. const result = Extractor.invoke(extractorConfig, {
  92. localBuild: true,
  93. showVerboseMessages: true
  94. })
  95. if (result.succeeded) {
  96. // concat additional d.ts to rolled-up dts (mostly for JSX)
  97. if (pkg.buildOptions && pkg.buildOptions.dts) {
  98. const dtsPath = path.resolve(pkgDir, pkg.types)
  99. const existing = await fs.readFile(dtsPath, 'utf-8')
  100. const toAdd = await Promise.all(
  101. pkg.buildOptions.dts.map(file => {
  102. return fs.readFile(path.resolve(pkgDir, 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. const esmProdBuild = `${pkgDir}/dist/${target}.global.prod.js`
  133. if (fs.existsSync(esmProdBuild)) {
  134. const file = fs.readFileSync(esmProdBuild)
  135. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  136. const gzipped = gzipSync(file)
  137. const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  138. const compressed = compress(file)
  139. const compressedSize = (compressed.length / 1024).toFixed(2) + 'kb'
  140. console.log(
  141. `${chalk.gray(
  142. chalk.bold(target)
  143. )} min:${minSize} / gzip:${gzippedSize} / brotli:${compressedSize}`
  144. )
  145. }
  146. }