usage-size.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // @ts-check
  2. import { mkdir, writeFile } from 'node:fs/promises'
  3. import path from 'node:path'
  4. import { rollup } from 'rollup'
  5. import nodeResolve from '@rollup/plugin-node-resolve'
  6. import { minify } from '@swc/core'
  7. import replace from '@rollup/plugin-replace'
  8. import { brotliCompressSync, gzipSync } from 'node:zlib'
  9. import { parseArgs } from 'node:util'
  10. import pico from 'picocolors'
  11. import prettyBytes from 'pretty-bytes'
  12. const {
  13. values: { write },
  14. } = parseArgs({
  15. options: {
  16. write: {
  17. type: 'boolean',
  18. default: false,
  19. },
  20. },
  21. })
  22. const sizeDir = path.resolve('temp/size')
  23. const vuePath = path.resolve('./packages/vue/dist/vue.runtime.esm-bundler.js')
  24. /**
  25. * @typedef {Object} Preset
  26. * @property {string} name - The name of the preset
  27. * @property {'*' | string[]} imports - The imports that are part of this preset
  28. * @property {Record<string, string>} [replace]
  29. */
  30. /** @type {Preset[]} */
  31. const presets = [
  32. {
  33. name: 'createApp (CAPI only)',
  34. imports: ['createApp'],
  35. replace: { __VUE_OPTIONS_API__: 'false' },
  36. },
  37. { name: 'createApp', imports: ['createApp'] },
  38. {
  39. name: 'createApp + vaporInteropPlugin',
  40. imports: ['createApp', 'vaporInteropPlugin'],
  41. },
  42. { name: 'createVaporApp', imports: ['createVaporApp'] },
  43. { name: 'createSSRApp', imports: ['createSSRApp'] },
  44. { name: 'defineCustomElement', imports: ['defineCustomElement'] },
  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 rollup({
  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. nodeResolve(),
  111. replace({
  112. 'process.env.NODE_ENV': '"production"',
  113. __VUE_PROD_DEVTOOLS__: 'false',
  114. __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
  115. __VUE_OPTIONS_API__: 'true',
  116. preventAssignment: true,
  117. ...preset.replace,
  118. }),
  119. ],
  120. })
  121. const generated = await result.generate({})
  122. const bundled = generated.output[0].code
  123. const minified = (
  124. await minify(bundled, {
  125. module: true,
  126. toplevel: true,
  127. })
  128. ).code
  129. const size = minified.length
  130. const gzip = gzipSync(minified).length
  131. const brotli = brotliCompressSync(minified).length
  132. if (write) {
  133. await writeFile(path.resolve(sizeDir, preset.name + '.js'), bundled)
  134. }
  135. return {
  136. name: preset.name,
  137. size,
  138. gzip,
  139. brotli,
  140. }
  141. }