build.js 3.5 KB

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