templateTransformAssetUrl.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { generate, baseParse, transform } from '@vue/compiler-core'
  2. import {
  3. transformAssetUrl,
  4. createAssetUrlTransformWithOptions,
  5. AssetURLOptions,
  6. normalizeOptions
  7. } from '../src/templateTransformAssetUrl'
  8. import { transformElement } from '../../compiler-core/src/transforms/transformElement'
  9. import { transformBind } from '../../compiler-core/src/transforms/vBind'
  10. function compileWithAssetUrls(template: string, options?: AssetURLOptions) {
  11. const ast = baseParse(template)
  12. const t = options
  13. ? createAssetUrlTransformWithOptions(normalizeOptions(options))
  14. : transformAssetUrl
  15. transform(ast, {
  16. nodeTransforms: [t, transformElement],
  17. directiveTransforms: {
  18. bind: transformBind
  19. }
  20. })
  21. return generate(ast, { mode: 'module' })
  22. }
  23. describe('compiler sfc: transform asset url', () => {
  24. test('transform assetUrls', () => {
  25. const result = compileWithAssetUrls(`
  26. <img src="./logo.png"/>
  27. <img src="~fixtures/logo.png"/>
  28. <img src="~/fixtures/logo.png"/>
  29. <img src="http://example.com/fixtures/logo.png"/>
  30. <img src="/fixtures/logo.png"/>
  31. `)
  32. expect(result.code).toMatchSnapshot()
  33. })
  34. /**
  35. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  36. */
  37. test('support uri fragment', () => {
  38. const result = compileWithAssetUrls(
  39. '<use href="~@svg/file.svg#fragment"></use>'
  40. )
  41. expect(result.code).toMatchSnapshot()
  42. })
  43. /**
  44. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  45. */
  46. test('support uri is empty', () => {
  47. const result = compileWithAssetUrls('<use href="~"></use>')
  48. expect(result.code).toMatchSnapshot()
  49. })
  50. test('with explicit base', () => {
  51. const { code } = compileWithAssetUrls(
  52. `<img src="./bar.png"></img>` + // -> /foo/bar.png
  53. `<img src="~bar.png"></img>` + // -> /foo/bar.png
  54. `<img src="bar.png"></img>` + // -> bar.png (untouched)
  55. `<img src="@theme/bar.png"></img>`, // -> @theme/bar.png (untouched)
  56. {
  57. base: '/foo'
  58. }
  59. )
  60. expect(code).toMatchSnapshot()
  61. })
  62. test('with includeAbsolute: true', () => {
  63. const { code } = compileWithAssetUrls(
  64. `<img src="./bar.png"/>` +
  65. `<img src="/bar.png"/>` +
  66. `<img src="https://foo.bar/baz.png"/>`,
  67. {
  68. includeAbsolute: true
  69. }
  70. )
  71. expect(code).toMatchSnapshot()
  72. })
  73. // vitejs/vite#298
  74. test('should not transform hash fragments', () => {
  75. const { code } = compileWithAssetUrls(
  76. `<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  77. <defs>
  78. <circle id="myCircle" cx="0" cy="0" r="5" />
  79. </defs>
  80. <use x="5" y="5" xlink:href="#myCircle" />
  81. </svg>`
  82. )
  83. // should not remove it
  84. expect(code).toMatch(`"xlink:href": "#myCircle"`)
  85. })
  86. })