build.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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, readFileSync } from 'node:fs'
  16. import path from 'node:path'
  17. import minimist from 'minimist'
  18. import { gzipSync, brotliCompressSync } from 'node:zlib'
  19. import chalk from 'chalk'
  20. import execa 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 './const-enum.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. const buildAllMatching = args.all || args.a
  36. const writeSize = args.size
  37. const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)
  38. const sizeDir = path.resolve('temp/size')
  39. run()
  40. async function run() {
  41. if (writeSize) await fs.mkdir(sizeDir, { recursive: true })
  42. const removeCache = scanEnums()
  43. try {
  44. const resolvedTargets = targets.length
  45. ? fuzzyMatchTarget(targets, buildAllMatching)
  46. : allTargets
  47. await buildAll(resolvedTargets)
  48. await checkAllSizes(resolvedTargets)
  49. if (buildTypes) {
  50. await execa(
  51. 'pnpm',
  52. [
  53. 'run',
  54. 'build-dts',
  55. ...(targets.length
  56. ? ['--environment', `TARGETS:${resolvedTargets.join(',')}`]
  57. : [])
  58. ],
  59. {
  60. stdio: 'inherit'
  61. }
  62. )
  63. }
  64. } finally {
  65. removeCache()
  66. }
  67. }
  68. async function buildAll(targets) {
  69. await runParallel(cpus().length, targets, build)
  70. }
  71. async function runParallel(maxConcurrency, source, iteratorFn) {
  72. const ret = []
  73. const executing = []
  74. for (const item of source) {
  75. const p = Promise.resolve().then(() => iteratorFn(item, source))
  76. ret.push(p)
  77. if (maxConcurrency <= source.length) {
  78. const e = p.then(() => executing.splice(executing.indexOf(e), 1))
  79. executing.push(e)
  80. if (executing.length >= maxConcurrency) {
  81. await Promise.race(executing)
  82. }
  83. }
  84. }
  85. return Promise.all(ret)
  86. }
  87. async function build(target) {
  88. const pkgDir = path.resolve(`packages/${target}`)
  89. const pkg = require(`${pkgDir}/package.json`)
  90. // if this is a full build (no specific targets), ignore private packages
  91. if ((isRelease || !targets.length) && pkg.private) {
  92. return
  93. }
  94. // if building a specific format, do not remove dist.
  95. if (!formats && existsSync(`${pkgDir}/dist`)) {
  96. await fs.rm(`${pkgDir}/dist`, { recursive: true })
  97. }
  98. const env =
  99. (pkg.buildOptions && pkg.buildOptions.env) ||
  100. (devOnly ? 'development' : 'production')
  101. await execa(
  102. 'rollup',
  103. [
  104. '-c',
  105. '--environment',
  106. [
  107. `COMMIT:${commit}`,
  108. `NODE_ENV:${env}`,
  109. `TARGET:${target}`,
  110. formats ? `FORMATS:${formats}` : ``,
  111. prodOnly ? `PROD_ONLY:true` : ``,
  112. sourceMap ? `SOURCE_MAP:true` : ``
  113. ]
  114. .filter(Boolean)
  115. .join(',')
  116. ],
  117. { stdio: 'inherit' }
  118. )
  119. }
  120. async function checkAllSizes(targets) {
  121. if (devOnly || (formats && !formats.includes('global'))) {
  122. return
  123. }
  124. console.log()
  125. for (const target of targets) {
  126. await checkSize(target)
  127. }
  128. console.log()
  129. }
  130. async function checkSize(target) {
  131. const pkgDir = path.resolve(`packages/${target}`)
  132. await checkFileSize(`${pkgDir}/dist/${target}.global.prod.js`)
  133. if (!formats || formats.includes('global-runtime')) {
  134. await checkFileSize(`${pkgDir}/dist/${target}.runtime.global.prod.js`)
  135. }
  136. }
  137. async function checkFileSize(filePath) {
  138. if (!existsSync(filePath)) {
  139. return
  140. }
  141. const file = await fs.readFile(filePath)
  142. const fileName = path.basename(filePath)
  143. const gzipped = gzipSync(file)
  144. const brotli = brotliCompressSync(file)
  145. console.log(
  146. `${chalk.gray(chalk.bold(fileName))} min:${prettyBytes(
  147. file.length
  148. )} / gzip:${prettyBytes(gzipped.length)} / brotli:${prettyBytes(
  149. brotli.length
  150. )}`
  151. )
  152. if (writeSize)
  153. await fs.writeFile(
  154. path.resolve(sizeDir, `${fileName}.json`),
  155. JSON.stringify({
  156. file: fileName,
  157. size: file.length,
  158. gzip: gzipped.length,
  159. brotli: brotli.length
  160. }),
  161. 'utf-8'
  162. )
  163. }