build.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // @ts-check
  2. /*
  3. Produces production builds and stitches together d.ts files.
  4. To specify the package to build, simply pass its name and the desired build
  5. formats to output (defaults to `buildOptions.formats` specified in that package,
  6. or "esm,cjs"):
  7. ```
  8. # name supports fuzzy match. will build all packages with name containing "dom":
  9. nr build dom
  10. # specify the format to output
  11. nr build core --formats cjs
  12. ```
  13. */
  14. import fs from 'node:fs/promises'
  15. import { existsSync } from 'node:fs'
  16. import path from 'node:path'
  17. import minimist from 'minimist'
  18. import { brotliCompressSync, gzipSync } from 'node:zlib'
  19. import pico from 'picocolors'
  20. import { execa, execaSync } from 'execa'
  21. import { cpus } from 'node:os'
  22. import { createRequire } from 'node:module'
  23. import { targets as allTargets, fuzzyMatchTarget } from './utils.js'
  24. import { scanEnums } from './inline-enums.js'
  25. import prettyBytes from 'pretty-bytes'
  26. const require = createRequire(import.meta.url)
  27. const args = minimist(process.argv.slice(2))
  28. const targets = args._
  29. const formats = args.formats || args.f
  30. const devOnly = args.devOnly || args.d
  31. const prodOnly = !devOnly && (args.prodOnly || args.p)
  32. const buildTypes = args.withTypes || args.t
  33. const sourceMap = args.sourcemap || args.s
  34. const isRelease = args.release
  35. /** @type {boolean | undefined} */
  36. const buildAllMatching = args.all || args.a
  37. const writeSize = args.size
  38. const commit = execaSync('git', ['rev-parse', '--short=7', 'HEAD']).stdout
  39. const sizeDir = path.resolve('temp/size')
  40. run()
  41. async function run() {
  42. if (writeSize) await fs.mkdir(sizeDir, { recursive: true })
  43. const removeCache = scanEnums()
  44. try {
  45. const resolvedTargets = targets.length
  46. ? fuzzyMatchTarget(targets, buildAllMatching)
  47. : allTargets
  48. await buildAll(resolvedTargets)
  49. await checkAllSizes(resolvedTargets)
  50. if (buildTypes) {
  51. await execa(
  52. 'pnpm',
  53. [
  54. 'run',
  55. 'build-dts',
  56. ...(targets.length
  57. ? ['--environment', `TARGETS:${resolvedTargets.join(',')}`]
  58. : []),
  59. ],
  60. {
  61. stdio: 'inherit',
  62. },
  63. )
  64. }
  65. } finally {
  66. removeCache()
  67. }
  68. }
  69. /**
  70. * Builds all the targets in parallel.
  71. * @param {Array<string>} targets - An array of targets to build.
  72. * @returns {Promise<void>} - A promise representing the build process.
  73. */
  74. async function buildAll(targets) {
  75. await runParallel(cpus().length, targets, build)
  76. }
  77. /**
  78. * Runs iterator function in parallel.
  79. * @template T - The type of items in the data source
  80. * @param {number} maxConcurrency - The maximum concurrency.
  81. * @param {Array<T>} source - The data source
  82. * @param {(item: T) => Promise<void>} iteratorFn - The iteratorFn
  83. * @returns {Promise<void[]>} - A Promise array containing all iteration results.
  84. */
  85. async function runParallel(maxConcurrency, source, iteratorFn) {
  86. /**@type {Promise<void>[]} */
  87. const ret = []
  88. /**@type {Promise<void>[]} */
  89. const executing = []
  90. for (const item of source) {
  91. const p = Promise.resolve().then(() => iteratorFn(item))
  92. ret.push(p)
  93. if (maxConcurrency <= source.length) {
  94. const e = p.then(() => {
  95. executing.splice(executing.indexOf(e), 1)
  96. })
  97. executing.push(e)
  98. if (executing.length >= maxConcurrency) {
  99. await Promise.race(executing)
  100. }
  101. }
  102. }
  103. return Promise.all(ret)
  104. }
  105. /**
  106. * Builds the target.
  107. * @param {string} target - The target to build.
  108. * @returns {Promise<void>} - A promise representing the build process.
  109. */
  110. async function build(target) {
  111. const pkgDir = path.resolve(`packages/${target}`)
  112. const pkg = require(`${pkgDir}/package.json`)
  113. // if this is a full build (no specific targets), ignore private packages
  114. if ((isRelease || !targets.length) && pkg.private) {
  115. return
  116. }
  117. // if building a specific format, do not remove dist.
  118. if (!formats && existsSync(`${pkgDir}/dist`)) {
  119. await fs.rm(`${pkgDir}/dist`, { recursive: true })
  120. }
  121. const env =
  122. (pkg.buildOptions && pkg.buildOptions.env) ||
  123. (devOnly ? 'development' : 'production')
  124. await execa(
  125. 'rollup',
  126. [
  127. '-c',
  128. '--environment',
  129. [
  130. `COMMIT:${commit}`,
  131. `NODE_ENV:${env}`,
  132. `TARGET:${target}`,
  133. formats ? `FORMATS:${formats}` : ``,
  134. prodOnly ? `PROD_ONLY:true` : ``,
  135. sourceMap ? `SOURCE_MAP:true` : ``,
  136. ]
  137. .filter(Boolean)
  138. .join(','),
  139. ],
  140. { stdio: 'inherit' },
  141. )
  142. }
  143. /**
  144. * Checks the sizes of all targets.
  145. * @param {string[]} targets - The targets to check sizes for.
  146. * @returns {Promise<void>}
  147. */
  148. async function checkAllSizes(targets) {
  149. if (devOnly || (formats && !formats.includes('global'))) {
  150. return
  151. }
  152. console.log()
  153. for (const target of targets) {
  154. await checkSize(target)
  155. }
  156. console.log()
  157. }
  158. /**
  159. * Checks the size of a target.
  160. * @param {string} target - The target to check the size for.
  161. * @returns {Promise<void>}
  162. */
  163. async function checkSize(target) {
  164. const pkgDir = path.resolve(`packages/${target}`)
  165. await checkFileSize(`${pkgDir}/dist/${target}.global.prod.js`)
  166. if (!formats || formats.includes('global-runtime')) {
  167. await checkFileSize(`${pkgDir}/dist/${target}.runtime.global.prod.js`)
  168. }
  169. }
  170. /**
  171. * Checks the file size.
  172. * @param {string} filePath - The path of the file to check the size for.
  173. * @returns {Promise<void>}
  174. */
  175. async function checkFileSize(filePath) {
  176. if (!existsSync(filePath)) {
  177. return
  178. }
  179. const file = await fs.readFile(filePath)
  180. const fileName = path.basename(filePath)
  181. const gzipped = gzipSync(file)
  182. const brotli = brotliCompressSync(file)
  183. console.log(
  184. `${pico.gray(pico.bold(fileName))} min:${prettyBytes(
  185. file.length,
  186. )} / gzip:${prettyBytes(gzipped.length)} / brotli:${prettyBytes(
  187. brotli.length,
  188. )}`,
  189. )
  190. if (writeSize)
  191. await fs.writeFile(
  192. path.resolve(sizeDir, `${fileName}.json`),
  193. JSON.stringify({
  194. file: fileName,
  195. size: file.length,
  196. gzip: gzipped.length,
  197. brotli: brotli.length,
  198. }),
  199. 'utf-8',
  200. )
  201. }