verify-treeshaking.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // @ts-check
  2. import fs from 'node:fs'
  3. import { exec } from './utils.js'
  4. exec('vp', ['run', 'build', 'vue', '-f', 'global-runtime']).then(() => {
  5. const errors = []
  6. const devBuild = fs.readFileSync(
  7. 'packages/vue/dist/vue.runtime.global.js',
  8. 'utf-8',
  9. )
  10. if (devBuild.includes('__spreadValues')) {
  11. errors.push(
  12. 'dev build contains unexpected object spread helper.\n' +
  13. 'This means { ...obj } syntax is used in runtime code. This should be ' +
  14. 'refactored to use the `extend` helper to avoid the extra code.',
  15. )
  16. }
  17. const prodBuild = fs.readFileSync(
  18. 'packages/vue/dist/vue.runtime.global.prod.js',
  19. 'utf-8',
  20. )
  21. if (prodBuild.includes('Vue warn')) {
  22. errors.push(
  23. 'prod build contains unexpected warning-related code.\n' +
  24. 'This means there are calls of warn() that are not guarded by the __DEV__ condition.',
  25. )
  26. }
  27. if (
  28. prodBuild.includes('html,body,base') ||
  29. prodBuild.includes('svg,animate,animateMotion') ||
  30. prodBuild.includes('annotation,annotation-xml,maction')
  31. ) {
  32. errors.push(
  33. 'prod build contains unexpected domTagConfig lists.\n' +
  34. 'This means helpers like isHTMLTag() is used in runtime code paths when it should be compiler-only.',
  35. )
  36. }
  37. if (errors.length) {
  38. throw new Error(
  39. `Found the following treeshaking errors:\n\n- ${errors.join('\n\n- ')}`,
  40. )
  41. }
  42. })