templateTransformAssetUrl.spec.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { generate, baseParse, transform } from '@vue/compiler-core'
  2. import {
  3. transformAssetUrl,
  4. createAssetUrlTransformWithOptions
  5. } from '../src/templateTransformAssetUrl'
  6. import { transformElement } from '../../compiler-core/src/transforms/transformElement'
  7. import { transformBind } from '../../compiler-core/src/transforms/vBind'
  8. function compileWithAssetUrls(template: string) {
  9. const ast = baseParse(template)
  10. transform(ast, {
  11. nodeTransforms: [transformAssetUrl, transformElement],
  12. directiveTransforms: {
  13. bind: transformBind
  14. }
  15. })
  16. return generate(ast, { mode: 'module' })
  17. }
  18. describe('compiler sfc: transform asset url', () => {
  19. test('transform assetUrls', () => {
  20. const result = compileWithAssetUrls(`
  21. <img src="./logo.png"/>
  22. <img src="~fixtures/logo.png"/>
  23. <img src="~/fixtures/logo.png"/>
  24. <img src="http://example.com/fixtures/logo.png"/>
  25. <img src="/fixtures/logo.png"/>
  26. `)
  27. expect(result.code).toMatchSnapshot()
  28. })
  29. /**
  30. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  31. */
  32. test('support uri fragment', () => {
  33. const result = compileWithAssetUrls(
  34. '<use href="~@svg/file.svg#fragment"></use>'
  35. )
  36. expect(result.code).toMatchSnapshot()
  37. })
  38. /**
  39. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  40. */
  41. test('support uri is empty', () => {
  42. const result = compileWithAssetUrls('<use href="~"></use>')
  43. expect(result.code).toMatchSnapshot()
  44. })
  45. test('with explicit base', () => {
  46. const ast = baseParse(
  47. `<img src="./bar.png"></img>` + // -> /foo/bar.png
  48. `<img src="~bar.png"></img>` + // -> /foo/bar.png
  49. `<img src="bar.png"></img>` + // -> bar.png (untouched)
  50. `<img src="@theme/bar.png"></img>` // -> @theme/bar.png (untouched)
  51. )
  52. transform(ast, {
  53. nodeTransforms: [
  54. createAssetUrlTransformWithOptions({
  55. base: '/foo'
  56. }),
  57. transformElement
  58. ],
  59. directiveTransforms: {
  60. bind: transformBind
  61. }
  62. })
  63. const { code } = generate(ast, { mode: 'module' })
  64. expect(code).toMatchSnapshot()
  65. })
  66. })