usage-size.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // @ts-check
  2. import { mkdir, writeFile } from 'node:fs/promises'
  3. import path from 'node:path'
  4. import { rolldown } from 'rolldown'
  5. import { minify } from 'oxc-minify'
  6. import { replacePlugin } from 'rolldown/plugins'
  7. import { brotliCompressSync, gzipSync } from 'node:zlib'
  8. import { parseArgs } from 'node:util'
  9. import pico from 'picocolors'
  10. import prettyBytes from 'pretty-bytes'
  11. const {
  12. values: { write },
  13. } = parseArgs({
  14. options: {
  15. write: {
  16. type: 'boolean',
  17. default: false,
  18. },
  19. },
  20. })
  21. const sizeDir = path.resolve('temp/size')
  22. const vuePath = path.resolve('./packages/vue/dist/vue.runtime.esm-bundler.js')
  23. /**
  24. * @typedef {Object} Preset
  25. * @property {string} name - The name of the preset
  26. * @property {'*' | string[]} imports - The imports that are part of this preset
  27. * @property {Record<string, string>} [replace]
  28. */
  29. /** @type {Preset[]} */
  30. const presets = [
  31. {
  32. name: 'createApp (CAPI only)',
  33. imports: ['createApp'],
  34. replace: { __VUE_OPTIONS_API__: 'false' },
  35. },
  36. { name: 'createApp', imports: ['createApp'] },
  37. {
  38. name: 'createApp + vaporInteropPlugin',
  39. imports: ['createApp', 'vaporInteropPlugin'],
  40. },
  41. { name: 'createVaporApp', imports: ['createVaporApp'] },
  42. { name: 'createSSRApp', imports: ['createSSRApp'] },
  43. { name: 'defineCustomElement', imports: ['defineCustomElement'] },
  44. {
  45. name: 'overall',
  46. imports: [
  47. 'createApp',
  48. 'ref',
  49. 'watch',
  50. 'Transition',
  51. 'KeepAlive',
  52. 'Suspense',
  53. ],
  54. },
  55. ]
  56. main()
  57. /**
  58. * Main function that initiates the bundling process for the presets
  59. */
  60. async function main() {
  61. console.log()
  62. /** @type {Promise<{name: string, size: number, gzip: number, brotli: number}>[]} */
  63. const tasks = []
  64. for (const preset of presets) {
  65. tasks.push(generateBundle(preset))
  66. }
  67. const results = await Promise.all(tasks)
  68. for (const r of results) {
  69. console.log(
  70. `${pico.green(pico.bold(r.name))} - ` +
  71. `min:${prettyBytes(r.size, { minimumFractionDigits: 3 })} / ` +
  72. `gzip:${prettyBytes(r.gzip, { minimumFractionDigits: 3 })} / ` +
  73. `brotli:${prettyBytes(r.brotli, { minimumFractionDigits: 3 })}`,
  74. )
  75. }
  76. await mkdir(sizeDir, { recursive: true })
  77. await writeFile(
  78. path.resolve(sizeDir, '_usages.json'),
  79. JSON.stringify(Object.fromEntries(results.map(r => [r.name, r])), null, 2),
  80. 'utf-8',
  81. )
  82. }
  83. /**
  84. * Generates a bundle for a given preset
  85. *
  86. * @param {Preset} preset - The preset to generate the bundle for
  87. * @returns {Promise<{name: string, size: number, gzip: number, brotli: number}>} - The result of the bundling process
  88. */
  89. async function generateBundle(preset) {
  90. const id = 'virtual:entry'
  91. const exportSpecifiers =
  92. preset.imports === '*'
  93. ? `* as ${preset.name}`
  94. : `{ ${preset.imports.join(', ')} }`
  95. const content = `export ${exportSpecifiers} from '${vuePath}'`
  96. const result = await rolldown({
  97. input: id,
  98. plugins: [
  99. {
  100. name: 'usage-size-plugin',
  101. resolveId(_id) {
  102. if (_id === id) return id
  103. return null
  104. },
  105. load(_id) {
  106. if (_id === id) return content
  107. },
  108. },
  109. replacePlugin(
  110. {
  111. 'process.env.NODE_ENV': '"production"',
  112. __VUE_PROD_DEVTOOLS__: 'false',
  113. __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
  114. __VUE_OPTIONS_API__: 'true',
  115. ...preset.replace,
  116. },
  117. { preventAssignment: true },
  118. ),
  119. ],
  120. })
  121. const generated = await result.generate({})
  122. const bundled = generated.output[0].code
  123. const file = preset.name + '.js'
  124. const minified = (
  125. await minify(file, bundled, {
  126. mangle: {
  127. toplevel: true,
  128. },
  129. })
  130. ).code
  131. const size = minified.length
  132. const gzip = gzipSync(minified).length
  133. const brotli = brotliCompressSync(minified).length
  134. if (write) {
  135. await writeFile(path.resolve(sizeDir, file), bundled)
  136. }
  137. return {
  138. name: preset.name,
  139. size,
  140. gzip,
  141. brotli,
  142. }
  143. }