build.js 6.8 KB

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