build.mjs 5.9 KB

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