build.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 { rolldown } from 'rolldown'
  15. import fs from 'node:fs'
  16. import { parseArgs } from 'node:util'
  17. import { existsSync, readFileSync } from 'node:fs'
  18. import path from 'node:path'
  19. import { brotliCompressSync, gzipSync } from 'node:zlib'
  20. import pico from 'picocolors'
  21. import { targets as allTargets, fuzzyMatchTarget } from './utils.js'
  22. import prettyBytes from 'pretty-bytes'
  23. import { spawnSync } from 'node:child_process'
  24. import { createConfigsForPackage } from './create-rolldown-config.js'
  25. import { scanEnums } from './inline-enums.js'
  26. import { fileURLToPath } from 'node:url'
  27. const __dirname = fileURLToPath(new URL('.', import.meta.url))
  28. const privatePackages = fs.readdirSync('packages-private')
  29. const commit = spawnSync('git', ['rev-parse', '--short=7', 'HEAD'])
  30. .stdout.toString()
  31. .trim()
  32. const { values, positionals: targets } = parseArgs({
  33. allowPositionals: true,
  34. options: {
  35. formats: {
  36. type: 'string',
  37. short: 'f',
  38. },
  39. devOnly: {
  40. type: 'boolean',
  41. short: 'd',
  42. },
  43. prodOnly: {
  44. type: 'boolean',
  45. short: 'p',
  46. },
  47. withTypes: {
  48. type: 'boolean',
  49. short: 't',
  50. },
  51. sourceMap: {
  52. type: 'boolean',
  53. short: 's',
  54. },
  55. release: {
  56. type: 'boolean',
  57. },
  58. all: {
  59. type: 'boolean',
  60. short: 'a',
  61. },
  62. size: {
  63. type: 'boolean',
  64. },
  65. },
  66. })
  67. const {
  68. formats: rawFormats,
  69. all: buildAllMatching,
  70. devOnly,
  71. prodOnly,
  72. withTypes: buildTypes,
  73. sourceMap,
  74. release: isRelease,
  75. size,
  76. } = values
  77. const formats = rawFormats?.split(',')
  78. const sizeDir = path.resolve('temp/size')
  79. run()
  80. async function run() {
  81. if (size) fs.mkdirSync(sizeDir, { recursive: true })
  82. const removeCache = scanEnums()
  83. try {
  84. const resolvedTargets = targets.length
  85. ? fuzzyMatchTarget(targets, buildAllMatching)
  86. : allTargets
  87. await buildAll(resolvedTargets)
  88. if (size) await checkAllSizes(resolvedTargets)
  89. if (buildTypes) {
  90. await import('./build-types.js')
  91. }
  92. } finally {
  93. removeCache()
  94. }
  95. }
  96. /**
  97. * Builds all the targets in parallel.
  98. * @param {Array<string>} targets - An array of targets to build.
  99. * @returns {Promise<void>} - A promise representing the build process.
  100. */
  101. async function buildAll(targets) {
  102. const start = performance.now()
  103. const all = []
  104. let count = 0
  105. for (const t of targets) {
  106. const configs = createConfigsForTarget(t)
  107. if (configs) {
  108. all.push(
  109. Promise.all(
  110. configs.map(c => {
  111. return rolldown(c).then(bundle => {
  112. // @ts-expect-error
  113. return bundle.write(c.output).then(() => {
  114. // @ts-expect-error
  115. return c.output.file
  116. })
  117. })
  118. }),
  119. ).then(files => {
  120. const from = process.cwd()
  121. files.forEach(f => {
  122. count++
  123. console.log(
  124. pico.gray('built: ') + pico.green(path.relative(from, f)),
  125. )
  126. })
  127. }),
  128. )
  129. }
  130. }
  131. await Promise.all(all)
  132. console.log(
  133. `\n${count} files built in ${(performance.now() - start).toFixed(2)}ms.`,
  134. )
  135. }
  136. /**
  137. * Builds the target.
  138. * @param {string} target - The target to build.
  139. * @returns {import('rolldown').RolldownOptions[] | void} - A promise representing the build process.
  140. */
  141. function createConfigsForTarget(target) {
  142. const pkgBase = privatePackages.includes(target)
  143. ? `packages-private`
  144. : `packages`
  145. const pkgDir = path.resolve(__dirname, `../${pkgBase}/${target}`)
  146. const pkg = JSON.parse(readFileSync(`${pkgDir}/package.json`, 'utf-8'))
  147. // if this is a full build (no specific targets), ignore private packages
  148. if ((isRelease || !targets.length) && pkg.private) {
  149. return
  150. }
  151. // if building a specific format, do not remove dist.
  152. if (!formats && existsSync(`${pkgDir}/dist`)) {
  153. fs.rmSync(`${pkgDir}/dist`, { recursive: true })
  154. }
  155. return createConfigsForPackage({
  156. target,
  157. commit,
  158. // @ts-expect-error
  159. formats,
  160. prodOnly,
  161. devOnly:
  162. (pkg.buildOptions && pkg.buildOptions.env === 'development') || devOnly,
  163. sourceMap,
  164. })
  165. }
  166. /**
  167. * Checks the sizes of all targets.
  168. * @param {string[]} targets - The targets to check sizes for.
  169. * @returns {Promise<void>}
  170. */
  171. async function checkAllSizes(targets) {
  172. if (devOnly || (formats && !formats.includes('global'))) {
  173. return
  174. }
  175. console.log()
  176. for (const target of targets) {
  177. await checkSize(target)
  178. }
  179. console.log()
  180. }
  181. /**
  182. * Checks the size of a target.
  183. * @param {string} target - The target to check the size for.
  184. * @returns {Promise<void>}
  185. */
  186. async function checkSize(target) {
  187. const pkgDir = path.resolve(__dirname, `../packages/${target}`)
  188. await checkFileSize(`${pkgDir}/dist/${target}.global.prod.js`)
  189. if (!formats || formats.includes('global-runtime')) {
  190. await checkFileSize(`${pkgDir}/dist/${target}.runtime.global.prod.js`)
  191. }
  192. }
  193. /**
  194. * Checks the file size.
  195. * @param {string} filePath - The path of the file to check the size for.
  196. * @returns {Promise<void>}
  197. */
  198. async function checkFileSize(filePath) {
  199. if (!existsSync(filePath)) {
  200. return
  201. }
  202. const file = fs.readFileSync(filePath)
  203. const fileName = path.basename(filePath)
  204. const gzipped = gzipSync(file)
  205. const brotli = brotliCompressSync(file)
  206. console.log(
  207. `${pico.gray(pico.bold(fileName))} min:${prettyBytes(
  208. file.length,
  209. )} / gzip:${prettyBytes(gzipped.length)} / brotli:${prettyBytes(
  210. brotli.length,
  211. )}`,
  212. )
  213. if (size)
  214. fs.writeFileSync(
  215. path.resolve(sizeDir, `${fileName}.json`),
  216. JSON.stringify({
  217. file: fileName,
  218. size: file.length,
  219. gzip: gzipped.length,
  220. brotli: brotli.length,
  221. }),
  222. 'utf-8',
  223. )
  224. }