usage-size.js 3.6 KB

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