build.js 4.0 KB

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