Просмотр исходного кода

fix(compiler-sfc): asset url transform should ignore direct hash urls

Evan You 5 лет назад
Родитель
Сommit
5ddd9d2417

+ 14 - 0
packages/compiler-sfc/__tests__/templateTransformAssetUrl.spec.ts

@@ -79,4 +79,18 @@ describe('compiler sfc: transform asset url', () => {
     )
     )
     expect(code).toMatchSnapshot()
     expect(code).toMatchSnapshot()
   })
   })
+
+  // vitejs/vite#298
+  test('should not transform hash fragments', () => {
+    const { code } = compileWithAssetUrls(
+      `<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+        <defs>
+          <circle id="myCircle" cx="0" cy="0" r="5" />
+        </defs>
+        <use x="5" y="5" xlink:href="#myCircle" />
+      </svg>`
+    )
+    // should not remove it
+    expect(code).toMatch(`"xlink:href": "#myCircle"`)
+  })
 })
 })

+ 1 - 0
packages/compiler-sfc/src/templateTransformAssetUrl.ts

@@ -99,6 +99,7 @@ export const transformAssetUrl: NodeTransform = (
         !assetAttrs.includes(attr.name) ||
         !assetAttrs.includes(attr.name) ||
         !attr.value ||
         !attr.value ||
         isExternalUrl(attr.value.content) ||
         isExternalUrl(attr.value.content) ||
+        attr.value.content[0] === '#' ||
         (!options.includeAbsolute && !isRelativeUrl(attr.value.content))
         (!options.includeAbsolute && !isRelativeUrl(attr.value.content))
       ) {
       ) {
         return
         return