build.js 4.2 KB

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