build.js 3.1 KB

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