utils.ts 665 B

123456789101112131415161718192021
  1. import type { CompilerOptions } from '@vue/compiler-core'
  2. import { compile } from '../src'
  3. export function getCompiledString(
  4. src: string,
  5. options?: CompilerOptions,
  6. ): string {
  7. // Wrap src template in a root div so that it doesn't get injected
  8. // fallthrough attr. This results in less noise in generated snapshots
  9. // but also means this util can only be used for non-root cases.
  10. const { code } = compile(`<div>${src}</div>`, options)
  11. const match = code.match(
  12. /_push\(\`<div\${\s*_ssrRenderAttrs\(_attrs\)\s*}>([^]*)<\/div>\`\)/,
  13. )
  14. if (!match) {
  15. throw new Error(`Unexpected compile result:\n${code}`)
  16. }
  17. return `\`${match[1]}\``
  18. }