templateTransformAssetUrl.spec.ts 3.8 KB

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