build.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. ;(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. `NODE_ENV:${env},` +
  54. `TARGET:${target}` +
  55. (formats ? `,FORMATS:${formats}` : ``) +
  56. (args.types ? `,TYPES:true` : ``) +
  57. (prodOnly ? `,PROD_ONLY:true` : ``)
  58. ],
  59. { stdio: 'inherit' }
  60. )
  61. if (args.types && pkg.types) {
  62. console.log()
  63. console.log(
  64. chalk.bold(chalk.yellow(`Rolling up type definitions for ${target}...`))
  65. )
  66. // build types
  67. const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor')
  68. const extractorConfigPath = path.resolve(pkgDir, `api-extractor.json`)
  69. const extractorConfig = ExtractorConfig.loadFileAndPrepare(
  70. extractorConfigPath
  71. )
  72. const result = Extractor.invoke(extractorConfig, {
  73. localBuild: true,
  74. showVerboseMessages: true
  75. })
  76. if (result.succeeded) {
  77. console.log(
  78. chalk.bold(chalk.green(`API Extractor completed successfully.`))
  79. )
  80. } else {
  81. console.error(
  82. `API Extractor completed with ${extractorResult.errorCount} errors` +
  83. ` and ${extractorResult.warningCount} warnings`
  84. )
  85. process.exitCode = 1
  86. }
  87. await fs.remove(`${pkgDir}/dist/packages`)
  88. }
  89. }
  90. function checkAllSizes(targets) {
  91. console.log()
  92. for (const target of targets) {
  93. checkSize(target)
  94. }
  95. console.log()
  96. }
  97. function checkSize(target) {
  98. const pkgDir = path.resolve(`packages/${target}`)
  99. const esmProdBuild = `${pkgDir}/dist/${target}.esm-browser.prod.js`
  100. if (fs.existsSync(esmProdBuild)) {
  101. const file = fs.readFileSync(esmProdBuild)
  102. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  103. const gzipped = gzipSync(file)
  104. const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  105. const compressed = compress(file)
  106. const compressedSize = (compressed.length / 1024).toFixed(2) + 'kb'
  107. console.log(
  108. `${chalk.gray(
  109. chalk.bold(target)
  110. )} min:${minSize} / gzip:${gzippedSize} / brotli:${compressedSize}`
  111. )
  112. }
  113. }