build.js 3.6 KB

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