templateTransformAssetUrl.spec.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. <img src="data:image/png;base64,i"/>
  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 fragment', () => {
  39. const result = compileWithAssetUrls(
  40. '<use href="~@svg/file.svg#fragment"></use>'
  41. )
  42. expect(result.code).toMatchSnapshot()
  43. })
  44. /**
  45. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  46. */
  47. test('support uri is empty', () => {
  48. const result = compileWithAssetUrls('<use href="~"></use>')
  49. expect(result.code).toMatchSnapshot()
  50. })
  51. test('with explicit base', () => {
  52. const { code } = compileWithAssetUrls(
  53. `<img src="./bar.png"></img>` + // -> /foo/bar.png
  54. `<img src="~bar.png"></img>` + // -> /foo/bar.png
  55. `<img src="bar.png"></img>` + // -> bar.png (untouched)
  56. `<img src="@theme/bar.png"></img>`, // -> @theme/bar.png (untouched)
  57. {
  58. base: '/foo'
  59. }
  60. )
  61. expect(code).toMatchSnapshot()
  62. })
  63. test('with includeAbsolute: true', () => {
  64. const { code } = compileWithAssetUrls(
  65. `<img src="./bar.png"/>` +
  66. `<img src="/bar.png"/>` +
  67. `<img src="https://foo.bar/baz.png"/>`,
  68. {
  69. includeAbsolute: true
  70. }
  71. )
  72. expect(code).toMatchSnapshot()
  73. })
  74. // vitejs/vite#298
  75. test('should not transform hash fragments', () => {
  76. const { code } = compileWithAssetUrls(
  77. `<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  78. <defs>
  79. <circle id="myCircle" cx="0" cy="0" r="5" />
  80. </defs>
  81. <use x="5" y="5" xlink:href="#myCircle" />
  82. </svg>`
  83. )
  84. // should not remove it
  85. expect(code).toMatch(`"xlink:href": "#myCircle"`)
  86. })
  87. })