build-with-rollup.js 7.2 KB

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