build.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Produces production builds and stitches together d.ts files.
  3. To specify 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. nr build dom
  9. # specify the format to output
  10. nr build core --formats cjs
  11. ```
  12. */
  13. const fs = require('fs-extra')
  14. const path = require('path')
  15. const chalk = require('chalk')
  16. const execa = require('execa')
  17. const { gzipSync } = require('zlib')
  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 sourceMap = args.sourcemap || args.s
  26. const isRelease = args.release
  27. const buildTypes = args.t || args.types || isRelease
  28. const buildAllMatching = args.all || args.a
  29. const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)
  30. run()
  31. async function run() {
  32. if (isRelease) {
  33. // remove build cache for release builds to avoid outdated enum values
  34. await fs.remove(path.resolve(__dirname, '../node_modules/.rts2_cache'))
  35. }
  36. if (!targets.length) {
  37. await buildAll(allTargets)
  38. checkAllSizes(allTargets)
  39. } else {
  40. await buildAll(fuzzyMatchTarget(targets, buildAllMatching))
  41. checkAllSizes(fuzzyMatchTarget(targets, buildAllMatching))
  42. }
  43. }
  44. async function buildAll(targets) {
  45. await runParallel(require('os').cpus().length, targets, build)
  46. }
  47. async function runParallel(maxConcurrency, source, iteratorFn) {
  48. const ret = []
  49. const executing = []
  50. for (const item of source) {
  51. const p = Promise.resolve().then(() => iteratorFn(item, source))
  52. ret.push(p)
  53. if (maxConcurrency <= source.length) {
  54. const e = p.then(() => executing.splice(executing.indexOf(e), 1))
  55. executing.push(e)
  56. if (executing.length >= maxConcurrency) {
  57. await Promise.race(executing)
  58. }
  59. }
  60. }
  61. return Promise.all(ret)
  62. }
  63. async function build(target) {
  64. const pkgDir = path.resolve(`packages/${target}`)
  65. const pkg = require(`${pkgDir}/package.json`)
  66. // if this is a full build (no specific targets), ignore private packages
  67. if ((isRelease || !targets.length) && pkg.private) {
  68. return
  69. }
  70. // if building a specific format, do not remove dist.
  71. if (!formats) {
  72. await fs.remove(`${pkgDir}/dist`)
  73. }
  74. const env =
  75. (pkg.buildOptions && pkg.buildOptions.env) ||
  76. (devOnly ? 'development' : 'production')
  77. await execa(
  78. 'rollup',
  79. [
  80. '-c',
  81. '--environment',
  82. [
  83. `COMMIT:${commit}`,
  84. `NODE_ENV:${env}`,
  85. `TARGET:${target}`,
  86. formats ? `FORMATS:${formats}` : ``,
  87. buildTypes ? `TYPES:true` : ``,
  88. prodOnly ? `PROD_ONLY:true` : ``,
  89. sourceMap ? `SOURCE_MAP:true` : ``
  90. ]
  91. .filter(Boolean)
  92. .join(',')
  93. ],
  94. { stdio: 'inherit' }
  95. )
  96. if (buildTypes && pkg.types) {
  97. console.log()
  98. console.log(
  99. chalk.bold(chalk.yellow(`Rolling up type definitions for ${target}...`))
  100. )
  101. // build types
  102. const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor')
  103. const extractorConfigPath = path.resolve(pkgDir, `api-extractor.json`)
  104. const extractorConfig =
  105. ExtractorConfig.loadFileAndPrepare(extractorConfigPath)
  106. const extractorResult = Extractor.invoke(extractorConfig, {
  107. localBuild: true,
  108. showVerboseMessages: true
  109. })
  110. if (extractorResult.succeeded) {
  111. // concat additional d.ts to rolled-up dts
  112. const typesDir = path.resolve(pkgDir, 'types')
  113. if (await fs.exists(typesDir)) {
  114. const dtsPath = path.resolve(pkgDir, pkg.types)
  115. const existing = await fs.readFile(dtsPath, 'utf-8')
  116. const typeFiles = await fs.readdir(typesDir)
  117. const toAdd = await Promise.all(
  118. typeFiles.map(file => {
  119. return fs.readFile(path.resolve(typesDir, file), 'utf-8')
  120. })
  121. )
  122. await fs.writeFile(dtsPath, existing + '\n' + toAdd.join('\n'))
  123. }
  124. console.log(
  125. chalk.bold(chalk.green(`API Extractor completed successfully.`))
  126. )
  127. } else {
  128. console.error(
  129. `API Extractor completed with ${extractorResult.errorCount} errors` +
  130. ` and ${extractorResult.warningCount} warnings`
  131. )
  132. process.exitCode = 1
  133. }
  134. await fs.remove(`${pkgDir}/dist/packages`)
  135. }
  136. }
  137. function checkAllSizes(targets) {
  138. if (devOnly || (formats && !formats.includes('global'))) {
  139. return
  140. }
  141. console.log()
  142. for (const target of targets) {
  143. checkSize(target)
  144. }
  145. console.log()
  146. }
  147. function checkSize(target) {
  148. const pkgDir = path.resolve(`packages/${target}`)
  149. checkFileSize(`${pkgDir}/dist/${target}.global.prod.js`)
  150. if (!formats || formats.includes('global-runtime')) {
  151. checkFileSize(`${pkgDir}/dist/${target}.runtime.global.prod.js`)
  152. }
  153. }
  154. function checkFileSize(filePath) {
  155. if (!fs.existsSync(filePath)) {
  156. return
  157. }
  158. const file = fs.readFileSync(filePath)
  159. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  160. const gzipped = gzipSync(file)
  161. const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  162. const compressed = compress(file)
  163. const compressedSize = (compressed.length / 1024).toFixed(2) + 'kb'
  164. console.log(
  165. `${chalk.gray(
  166. chalk.bold(path.basename(filePath))
  167. )} min:${minSize} / gzip:${gzippedSize} / brotli:${compressedSize}`
  168. )
  169. }