build.js 3.7 KB

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