build.js 3.1 KB

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