templateTransformAssetUrl.spec.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { generate, baseParse, transform } from '@vue/compiler-core'
  2. import { transformAssetUrl } from '../src/templateTransformAssetUrl'
  3. import { transformElement } from '../../compiler-core/src/transforms/transformElement'
  4. import { transformBind } from '../../compiler-core/src/transforms/vBind'
  5. function compileWithAssetUrls(template: string) {
  6. const ast = baseParse(template)
  7. transform(ast, {
  8. nodeTransforms: [transformAssetUrl, transformElement],
  9. directiveTransforms: {
  10. bind: transformBind
  11. }
  12. })
  13. return generate(ast, { mode: 'module' })
  14. }
  15. describe('compiler sfc: transform asset url', () => {
  16. test('transform assetUrls', () => {
  17. const result = compileWithAssetUrls(`
  18. <img src="./logo.png"/>
  19. <img src="~fixtures/logo.png"/>
  20. <img src="~/fixtures/logo.png"/>
  21. <img src="http://example.com/fixtures/logo.png"/>
  22. <img src="/fixtures/logo.png"/>
  23. `)
  24. expect(result.code).toMatchSnapshot()
  25. })
  26. /**
  27. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  28. */
  29. test('support uri fragment', () => {
  30. const result = compileWithAssetUrls(
  31. '<use href="~@svg/file.svg#fragment"></use>'
  32. )
  33. expect(result.code).toMatchSnapshot()
  34. })
  35. /**
  36. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  37. */
  38. test('support uri is empty', () => {
  39. const result = compileWithAssetUrls('<use href="~"></use>')
  40. expect(result.code).toMatchSnapshot()
  41. })
  42. })