build.js 3.0 KB

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