templateTransformAssetUrl.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. '<use href="~@svg/file.svg#fragment"></use>'
  42. )
  43. expect(result.code).toMatchSnapshot()
  44. })
  45. /**
  46. * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
  47. */
  48. test('support uri is empty', () => {
  49. const result = compileWithAssetUrls('<use href="~"></use>')
  50. expect(result.code).toMatchSnapshot()
  51. })
  52. test('with explicit base', () => {
  53. const { code } = compileWithAssetUrls(
  54. `<img src="./bar.png"></img>` + // -> /foo/bar.png
  55. `<img src="bar.png"></img>` + // -> bar.png (untouched)
  56. `<img src="~bar.png"></img>` + // -> still converts to import
  57. `<img src="@theme/bar.png"></img>`, // -> still converts to import
  58. {
  59. base: '/foo'
  60. }
  61. )
  62. expect(code).toMatch(`import _imports_0 from 'bar.png'`)
  63. expect(code).toMatch(`import _imports_1 from '@theme/bar.png'`)
  64. expect(code).toMatchSnapshot()
  65. })
  66. test('with includeAbsolute: true', () => {
  67. const { code } = compileWithAssetUrls(
  68. `<img src="./bar.png"/>` +
  69. `<img src="/bar.png"/>` +
  70. `<img src="https://foo.bar/baz.png"/>`,
  71. {
  72. includeAbsolute: true
  73. }
  74. )
  75. expect(code).toMatchSnapshot()
  76. })
  77. // vitejs/vite#298
  78. test('should not transform hash fragments', () => {
  79. const { code } = compileWithAssetUrls(
  80. `<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  81. <defs>
  82. <circle id="myCircle" cx="0" cy="0" r="5" />
  83. </defs>
  84. <use x="5" y="5" xlink:href="#myCircle" />
  85. </svg>`
  86. )
  87. // should not remove it
  88. expect(code).toMatch(`"xlink:href": "#myCircle"`)
  89. })
  90. test('should allow for full base URLs, with paths', () => {
  91. const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
  92. base: 'http://localhost:3000/src/'
  93. })
  94. expect(code).toMatchSnapshot()
  95. })
  96. test('should allow for full base URLs, without paths', () => {
  97. const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
  98. base: 'http://localhost:3000'
  99. })
  100. expect(code).toMatchSnapshot()
  101. })
  102. test('should allow for full base URLs, without port', () => {
  103. const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
  104. base: 'http://localhost'
  105. })
  106. expect(code).toMatchSnapshot()
  107. })
  108. test('should allow for full base URLs, without protocol', () => {
  109. const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
  110. base: '//localhost'
  111. })
  112. expect(code).toMatchSnapshot()
  113. })
  114. })