build.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Produces production builds and stitches together d.ts files.
  3. To specify the package to build, simply pass its name and the desired build
  4. formats to output (defaults to `buildOptions.formats` specified in that package,
  5. or "esm,cjs"):
  6. ```
  7. # name supports fuzzy match. will build all packages with name containing "dom":
  8. nr build dom
  9. # specify the format to output
  10. nr build core --formats cjs
  11. ```
  12. */
  13. // @ts-check
  14. import fs from 'node:fs/promises'
  15. import { existsSync, readFileSync } from 'node:fs'
  16. import path from 'node:path'
  17. import { fileURLToPath } from 'node:url'
  18. import minimist from 'minimist'
  19. import { gzipSync } from 'node:zlib'
  20. import { compress } from 'brotli'
  21. import chalk from 'chalk'
  22. import execa from 'execa'
  23. import { cpus } from 'node:os'
  24. import { createRequire } from 'node:module'
  25. import { targets as allTargets, fuzzyMatchTarget } from './utils.mjs'
  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 sourceMap = args.sourcemap || args.s
  33. const isRelease = args.release
  34. const buildAllMatching = args.all || args.a
  35. const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)
  36. run()
  37. async function run() {
  38. if (!targets.length) {
  39. await buildAll(allTargets)
  40. checkAllSizes(allTargets)
  41. } else {
  42. await buildAll(fuzzyMatchTarget(targets, buildAllMatching))
  43. checkAllSizes(fuzzyMatchTarget(targets, buildAllMatching))
  44. }
  45. }
  46. async function buildAll(targets) {
  47. await runParallel(cpus().length, targets, build)
  48. }
  49. async function runParallel(maxConcurrency, source, iteratorFn) {
  50. const ret = []
  51. const executing = []
  52. for (const item of source) {
  53. const p = Promise.resolve().then(() => iteratorFn(item, source))
  54. ret.push(p)
  55. if (maxConcurrency <= source.length) {
  56. const e = p.then(() => executing.splice(executing.indexOf(e), 1))
  57. executing.push(e)
  58. if (executing.length >= maxConcurrency) {
  59. await Promise.race(executing)
  60. }
  61. }
  62. }
  63. return Promise.all(ret)
  64. }
  65. async function build(target) {
  66. const pkgDir = path.resolve(`packages/${target}`)
  67. const pkg = require(`${pkgDir}/package.json`)
  68. // if this is a full build (no specific targets), ignore private packages
  69. if ((isRelease || !targets.length) && pkg.private) {
  70. return
  71. }
  72. // if building a specific format, do not remove dist.
  73. if (!formats && existsSync(`${pkgDir}/dist`)) {
  74. await fs.rm(`${pkgDir}/dist`, { recursive: true })
  75. }
  76. const env =
  77. (pkg.buildOptions && pkg.buildOptions.env) ||
  78. (devOnly ? 'development' : 'production')
  79. await execa(
  80. 'rollup',
  81. [
  82. '-c',
  83. '--environment',
  84. [
  85. `COMMIT:${commit}`,
  86. `NODE_ENV:${env}`,
  87. `TARGET:${target}`,
  88. formats ? `FORMATS:${formats}` : ``,
  89. prodOnly ? `PROD_ONLY:true` : ``,
  90. sourceMap ? `SOURCE_MAP:true` : ``
  91. ]
  92. .filter(Boolean)
  93. .join(',')
  94. ],
  95. { stdio: 'inherit' }
  96. )
  97. }
  98. function checkAllSizes(targets) {
  99. if (devOnly || (formats && !formats.includes('global'))) {
  100. return
  101. }
  102. console.log()
  103. for (const target of targets) {
  104. checkSize(target)
  105. }
  106. console.log()
  107. }
  108. function checkSize(target) {
  109. const pkgDir = path.resolve(`packages/${target}`)
  110. checkFileSize(`${pkgDir}/dist/${target}.global.prod.js`)
  111. if (!formats || formats.includes('global-runtime')) {
  112. checkFileSize(`${pkgDir}/dist/${target}.runtime.global.prod.js`)
  113. }
  114. }
  115. function checkFileSize(filePath) {
  116. if (!existsSync(filePath)) {
  117. return
  118. }
  119. const file = readFileSync(filePath)
  120. const minSize = (file.length / 1024).toFixed(2) + 'kb'
  121. const gzipped = gzipSync(file)
  122. const gzippedSize = (gzipped.length / 1024).toFixed(2) + 'kb'
  123. const compressed = compress(file)
  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. }