build.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, rmSync } 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. const require = createRequire(import.meta.url)
  26. const args = minimist(process.argv.slice(2))
  27. const targets = args._
  28. const formats = args.formats || args.f
  29. const devOnly = args.devOnly || args.d
  30. const prodOnly = !devOnly && (args.prodOnly || args.p)
  31. const sourceMap = args.sourcemap || args.s
  32. const isRelease = args.release
  33. const buildAllMatching = args.all || args.a
  34. const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)
  35. run()
  36. async function run() {
  37. const removeCache = scanEnums()
  38. try {
  39. if (!targets.length) {
  40. await buildAll(allTargets)
  41. checkAllSizes(allTargets)
  42. } else {
  43. await buildAll(fuzzyMatchTarget(targets, buildAllMatching))
  44. checkAllSizes(fuzzyMatchTarget(targets, buildAllMatching))
  45. }
  46. } finally {
  47. removeCache()
  48. }
  49. }
  50. async function buildAll(targets) {
  51. await runParallel(cpus().length, targets, build)
  52. }
  53. async function runParallel(maxConcurrency, source, iteratorFn) {
  54. const ret = []
  55. const executing = []
  56. for (const item of source) {
  57. const p = Promise.resolve().then(() => iteratorFn(item, source))
  58. ret.push(p)
  59. if (maxConcurrency <= source.length) {
  60. const e = p.then(() => executing.splice(executing.indexOf(e), 1))
  61. executing.push(e)
  62. if (executing.length >= maxConcurrency) {
  63. await Promise.race(executing)
  64. }
  65. }
  66. }
  67. return Promise.all(ret)
  68. }
  69. async function build(target) {
  70. const pkgDir = path.resolve(`packages/${target}`)
  71. const pkg = require(`${pkgDir}/package.json`)
  72. // if this is a full build (no specific targets), ignore private packages
  73. if ((isRelease || !targets.length) && pkg.private) {
  74. return
  75. }
  76. // if building a specific format, do not remove dist.
  77. if (!formats && existsSync(`${pkgDir}/dist`)) {
  78. await fs.rm(`${pkgDir}/dist`, { recursive: true })
  79. }
  80. const env =
  81. (pkg.buildOptions && pkg.buildOptions.env) ||
  82. (devOnly ? 'development' : 'production')
  83. await execa(
  84. 'rollup',
  85. [
  86. '-c',
  87. '--environment',
  88. [
  89. `COMMIT:${commit}`,
  90. `NODE_ENV:${env}`,
  91. `TARGET:${target}`,
  92. formats ? `FORMATS:${formats}` : ``,
  93. prodOnly ? `PROD_ONLY:true` : ``,
  94. sourceMap ? `SOURCE_MAP:true` : ``
  95. ]
  96. .filter(Boolean)
  97. .join(',')
  98. ],
  99. { stdio: 'inherit' }
  100. )
  101. }
  102. function checkAllSizes(targets) {
  103. if (devOnly || (formats && !formats.includes('global'))) {
  104. return
  105. }
  106. console.log()
  107. for (const target of targets) {
  108. checkSize(target)
  109. }
  110. console.log()
  111. }
  112. function checkSize(target) {
  113. const pkgDir = path.resolve(`packages/${target}`)
  114. checkFileSize(`${pkgDir}/dist/${target}.global.prod.js`)
  115. if (!formats || formats.includes('global-runtime')) {
  116. checkFileSize(`${pkgDir}/dist/${target}.runtime.global.prod.js`)
  117. }
  118. }
  119. function checkFileSize(filePath) {
  120. if (!existsSync(filePath)) {
  121. return
  122. }
  123. const file = readFileSync(filePath)
  124. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  125. const gzipped = gzipSync(file)
  126. const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  127. const compressed = brotliCompressSync(file)
  128. // @ts-ignore
  129. const compressedSize = (compressed.length / 1024).toFixed(2) + 'kb'
  130. console.log(
  131. `${chalk.gray(
  132. chalk.bold(path.basename(filePath))
  133. )} min:${minSize} / gzip:${gzippedSize} / brotli:${compressedSize}`
  134. )
  135. }