build.js 4.3 KB

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