usage-size.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. { name: 'defineVaporCustomElement', imports: ['defineVaporCustomElement'] },
  45. {
  46. name: 'overall',
  47. imports: [
  48. 'createApp',
  49. 'ref',
  50. 'watch',
  51. 'Transition',
  52. 'KeepAlive',
  53. 'Suspense',
  54. ],
  55. },
  56. ]
  57. main()
  58. /**
  59. * Main function that initiates the bundling process for the presets
  60. */
  61. async function main() {
  62. console.log()
  63. /** @type {Promise<{name: string, size: number, gzip: number, brotli: number}>[]} */
  64. const tasks = []
  65. for (const preset of presets) {
  66. tasks.push(generateBundle(preset))
  67. }
  68. const results = await Promise.all(tasks)
  69. for (const r of results) {
  70. console.log(
  71. `${pico.green(pico.bold(r.name))} - ` +
  72. `min:${prettyBytes(r.size, { minimumFractionDigits: 3 })} / ` +
  73. `gzip:${prettyBytes(r.gzip, { minimumFractionDigits: 3 })} / ` +
  74. `brotli:${prettyBytes(r.brotli, { minimumFractionDigits: 3 })}`,
  75. )
  76. }
  77. await mkdir(sizeDir, { recursive: true })
  78. await writeFile(
  79. path.resolve(sizeDir, '_usages.json'),
  80. JSON.stringify(Object.fromEntries(results.map(r => [r.name, r])), null, 2),
  81. 'utf-8',
  82. )
  83. }
  84. /**
  85. * Generates a bundle for a given preset
  86. *
  87. * @param {Preset} preset - The preset to generate the bundle for
  88. * @returns {Promise<{name: string, size: number, gzip: number, brotli: number}>} - The result of the bundling process
  89. */
  90. async function generateBundle(preset) {
  91. const id = 'virtual:entry'
  92. const exportSpecifiers =
  93. preset.imports === '*'
  94. ? `* as ${preset.name}`
  95. : `{ ${preset.imports.join(', ')} }`
  96. const content = `export ${exportSpecifiers} from '${vuePath}'`
  97. const result = await rolldown({
  98. input: id,
  99. plugins: [
  100. {
  101. name: 'usage-size-plugin',
  102. resolveId(_id) {
  103. if (_id === id) return id
  104. return null
  105. },
  106. load(_id) {
  107. if (_id === id) return content
  108. },
  109. },
  110. replacePlugin(
  111. {
  112. 'process.env.NODE_ENV': '"production"',
  113. __VUE_PROD_DEVTOOLS__: 'false',
  114. __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
  115. __VUE_OPTIONS_API__: 'true',
  116. ...preset.replace,
  117. },
  118. { preventAssignment: true },
  119. ),
  120. ],
  121. })
  122. const generated = await result.generate({})
  123. const bundled = generated.output[0].code
  124. const file = preset.name + '.js'
  125. const minified = (
  126. await minify(file, bundled, {
  127. mangle: {
  128. toplevel: true,
  129. },
  130. })
  131. ).code
  132. const size = minified.length
  133. const gzip = gzipSync(minified).length
  134. const brotli = brotliCompressSync(minified).length
  135. if (write) {
  136. await writeFile(path.resolve(sizeDir, file), bundled)
  137. }
  138. return {
  139. name: preset.name,
  140. size,
  141. gzip,
  142. brotli,
  143. }
  144. }