build.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Produce prodcution builds and stitch toegether d.ts files.
  3. To specific 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 zlib = require('zlib')
  16. const chalk = require('chalk')
  17. const execa = require('execa')
  18. const { gzipSync } = require('zlib')
  19. const { compress } = require('brotli')
  20. const { targets: allTargets, fuzzyMatchTarget } = require('./utils')
  21. const args = require('minimist')(process.argv.slice(2))
  22. const targets = args._
  23. const formats = args.formats || args.f
  24. const devOnly = args.devOnly || args.d
  25. const prodOnly = !devOnly && (args.prodOnly || args.p)
  26. const buildAllMatching = args.all || args.a
  27. const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)
  28. ;(async () => {
  29. if (!targets.length) {
  30. await buildAll(allTargets)
  31. checkAllSizes(allTargets)
  32. } else {
  33. await buildAll(fuzzyMatchTarget(targets, buildAllMatching))
  34. checkAllSizes(fuzzyMatchTarget(targets, buildAllMatching))
  35. }
  36. })()
  37. async function buildAll(targets) {
  38. for (const target of targets) {
  39. await build(target)
  40. }
  41. }
  42. async function build(target) {
  43. const pkgDir = path.resolve(`packages/${target}`)
  44. const pkg = require(`${pkgDir}/package.json`)
  45. await fs.remove(`${pkgDir}/dist`)
  46. const env =
  47. (pkg.buildOptions && pkg.buildOptions.env) ||
  48. (devOnly ? 'development' : 'production')
  49. await execa(
  50. 'rollup',
  51. [
  52. '-c',
  53. '--environment',
  54. [
  55. `COMMIT:${commit}`,
  56. `NODE_ENV:${env}`,
  57. `TARGET:${target}`,
  58. formats ? `FORMATS:${formats}` : ``,
  59. args.types ? `TYPES:true` : ``,
  60. prodOnly ? `PROD_ONLY:true` : ``
  61. ]
  62. .filter(_ => _)
  63. .join(',')
  64. ],
  65. { stdio: 'inherit' }
  66. )
  67. if (args.types && pkg.types) {
  68. console.log()
  69. console.log(
  70. chalk.bold(chalk.yellow(`Rolling up type definitions for ${target}...`))
  71. )
  72. // build types
  73. const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor')
  74. const extractorConfigPath = path.resolve(pkgDir, `api-extractor.json`)
  75. const extractorConfig = ExtractorConfig.loadFileAndPrepare(
  76. extractorConfigPath
  77. )
  78. const result = Extractor.invoke(extractorConfig, {
  79. localBuild: true,
  80. showVerboseMessages: true
  81. })
  82. if (result.succeeded) {
  83. console.log(
  84. chalk.bold(chalk.green(`API Extractor completed successfully.`))
  85. )
  86. } else {
  87. console.error(
  88. `API Extractor completed with ${extractorResult.errorCount} errors` +
  89. ` and ${extractorResult.warningCount} warnings`
  90. )
  91. process.exitCode = 1
  92. }
  93. await fs.remove(`${pkgDir}/dist/packages`)
  94. }
  95. }
  96. function checkAllSizes(targets) {
  97. console.log()
  98. for (const target of targets) {
  99. checkSize(target)
  100. }
  101. console.log()
  102. }
  103. function checkSize(target) {
  104. const pkgDir = path.resolve(`packages/${target}`)
  105. const esmProdBuild = `${pkgDir}/dist/${target}.esm-browser.prod.js`
  106. if (fs.existsSync(esmProdBuild)) {
  107. const file = fs.readFileSync(esmProdBuild)
  108. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  109. const gzipped = gzipSync(file)
  110. const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  111. const compressed = compress(file)
  112. const compressedSize = (compressed.length / 1024).toFixed(2) + 'kb'
  113. console.log(
  114. `${chalk.gray(
  115. chalk.bold(target)
  116. )} min:${minSize} / gzip:${gzippedSize} / brotli:${compressedSize}`
  117. )
  118. }
  119. }