build.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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'
  15. import { parseArgs } from 'node:util'
  16. import path from 'node:path'
  17. import { brotliCompressSync, gzipSync } from 'node:zlib'
  18. import pico from 'picocolors'
  19. import { cpus } from 'node:os'
  20. import { targets as allTargets, exec, fuzzyMatchTarget } from './utils.js'
  21. import { scanEnums } from './inline-enums.js'
  22. import prettyBytes from 'pretty-bytes'
  23. import { spawnSync } from 'node:child_process'
  24. const commit = spawnSync('git', ['rev-parse', '--short=7', 'HEAD'])
  25. .stdout.toString()
  26. .trim()
  27. const { values, positionals: targets } = parseArgs({
  28. allowPositionals: true,
  29. options: {
  30. formats: {
  31. type: 'string',
  32. short: 'f',
  33. },
  34. devOnly: {
  35. type: 'boolean',
  36. short: 'd',
  37. },
  38. prodOnly: {
  39. type: 'boolean',
  40. short: 'p',
  41. },
  42. withTypes: {
  43. type: 'boolean',
  44. short: 't',
  45. },
  46. sourceMap: {
  47. type: 'boolean',
  48. short: 's',
  49. },
  50. release: {
  51. type: 'boolean',
  52. },
  53. all: {
  54. type: 'boolean',
  55. short: 'a',
  56. },
  57. size: {
  58. type: 'boolean',
  59. },
  60. },
  61. })
  62. const {
  63. formats,
  64. all: buildAllMatching,
  65. devOnly,
  66. prodOnly,
  67. withTypes: buildTypes,
  68. sourceMap,
  69. release: isRelease,
  70. size: writeSize,
  71. } = values
  72. const sizeDir = path.resolve('temp/size')
  73. run()
  74. async function run() {
  75. if (writeSize) fs.mkdirSync(sizeDir, { recursive: true })
  76. const removeCache = scanEnums()
  77. try {
  78. const resolvedTargets = targets.length
  79. ? fuzzyMatchTarget(targets, buildAllMatching)
  80. : allTargets
  81. await buildAll(resolvedTargets)
  82. await checkAllSizes(resolvedTargets)
  83. if (buildTypes) {
  84. await exec(
  85. 'pnpm',
  86. [
  87. 'run',
  88. 'build-dts',
  89. ...(targets.length
  90. ? ['--environment', `TARGETS:${resolvedTargets.join(',')}`]
  91. : []),
  92. ],
  93. {
  94. stdio: 'inherit',
  95. },
  96. )
  97. }
  98. } finally {
  99. removeCache()
  100. }
  101. }
  102. /**
  103. * Builds all the targets in parallel.
  104. * @param {Array<string>} targets - An array of targets to build.
  105. * @returns {Promise<void>} - A promise representing the build process.
  106. */
  107. async function buildAll(targets) {
  108. await runParallel(cpus().length, targets, build)
  109. }
  110. /**
  111. * Runs iterator function in parallel.
  112. * @template T - The type of items in the data source
  113. * @param {number} maxConcurrency - The maximum concurrency.
  114. * @param {Array<T>} source - The data source
  115. * @param {(item: T) => Promise<void>} iteratorFn - The iteratorFn
  116. * @returns {Promise<void[]>} - A Promise array containing all iteration results.
  117. */
  118. async function runParallel(maxConcurrency, source, iteratorFn) {
  119. /**@type {Promise<void>[]} */
  120. const ret = []
  121. /**@type {Promise<void>[]} */
  122. const executing = []
  123. for (const item of source) {
  124. const p = Promise.resolve().then(() => iteratorFn(item))
  125. ret.push(p)
  126. if (maxConcurrency <= source.length) {
  127. const e = p.then(() => {
  128. executing.splice(executing.indexOf(e), 1)
  129. })
  130. executing.push(e)
  131. if (executing.length >= maxConcurrency) {
  132. await Promise.race(executing)
  133. }
  134. }
  135. }
  136. return Promise.all(ret)
  137. }
  138. const privatePackages = fs.readdirSync('packages-private')
  139. /**
  140. * Builds the target.
  141. * @param {string} target - The target to build.
  142. * @returns {Promise<void>} - A promise representing the build process.
  143. */
  144. async function build(target) {
  145. const pkgBase = privatePackages.includes(target)
  146. ? `packages-private`
  147. : `packages`
  148. const pkgDir = path.resolve(`${pkgBase}/${target}`)
  149. const pkg = JSON.parse(fs.readFileSync(`${pkgDir}/package.json`, 'utf-8'))
  150. // if this is a full build (no specific targets), ignore private packages
  151. if ((isRelease || !targets.length) && pkg.private) {
  152. return
  153. }
  154. // if building a specific format, do not remove dist.
  155. if (!formats && fs.existsSync(`${pkgDir}/dist`)) {
  156. fs.rmSync(`${pkgDir}/dist`, { recursive: true })
  157. }
  158. const env =
  159. (pkg.buildOptions && pkg.buildOptions.env) ||
  160. (devOnly ? 'development' : 'production')
  161. await exec(
  162. 'rollup',
  163. [
  164. '-c',
  165. '--environment',
  166. [
  167. `COMMIT:${commit}`,
  168. `NODE_ENV:${env}`,
  169. `TARGET:${target}`,
  170. formats ? `FORMATS:${formats}` : ``,
  171. prodOnly ? `PROD_ONLY:true` : ``,
  172. sourceMap ? `SOURCE_MAP:true` : ``,
  173. ]
  174. .filter(Boolean)
  175. .join(','),
  176. ],
  177. { stdio: 'inherit' },
  178. )
  179. }
  180. /**
  181. * Checks the sizes of all targets.
  182. * @param {string[]} targets - The targets to check sizes for.
  183. * @returns {Promise<void>}
  184. */
  185. async function checkAllSizes(targets) {
  186. if (devOnly || (formats && !formats.includes('global'))) {
  187. return
  188. }
  189. console.log()
  190. for (const target of targets) {
  191. await checkSize(target)
  192. }
  193. console.log()
  194. }
  195. /**
  196. * Checks the size of a target.
  197. * @param {string} target - The target to check the size for.
  198. * @returns {Promise<void>}
  199. */
  200. async function checkSize(target) {
  201. const pkgDir = path.resolve(`packages/${target}`)
  202. await checkFileSize(`${pkgDir}/dist/${target}.global.prod.js`)
  203. if (!formats || formats.includes('global-runtime')) {
  204. await checkFileSize(`${pkgDir}/dist/${target}.runtime.global.prod.js`)
  205. }
  206. }
  207. /**
  208. * Checks the file size.
  209. * @param {string} filePath - The path of the file to check the size for.
  210. * @returns {Promise<void>}
  211. */
  212. async function checkFileSize(filePath) {
  213. if (!fs.existsSync(filePath)) {
  214. return
  215. }
  216. const file = fs.readFileSync(filePath)
  217. const fileName = path.basename(filePath)
  218. const gzipped = gzipSync(file)
  219. const brotli = brotliCompressSync(file)
  220. console.log(
  221. `${pico.gray(pico.bold(fileName))} min:${prettyBytes(
  222. file.length,
  223. )} / gzip:${prettyBytes(gzipped.length)} / brotli:${prettyBytes(
  224. brotli.length,
  225. )}`,
  226. )
  227. if (writeSize)
  228. fs.writeFileSync(
  229. path.resolve(sizeDir, `${fileName}.json`),
  230. JSON.stringify({
  231. file: fileName,
  232. size: file.length,
  233. gzip: gzipped.length,
  234. brotli: brotli.length,
  235. }),
  236. 'utf-8',
  237. )
  238. }