build.js 3.3 KB

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