]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): support transforming asset urls with full base url. (#2477)
authorJoel Denning <joeldenning@gmail.com>
Fri, 4 Dec 2020 23:20:25 +0000 (16:20 -0700)
committerGitHub <noreply@github.com>
Fri, 4 Dec 2020 23:20:25 +0000 (18:20 -0500)
packages/compiler-sfc/__tests__/__snapshots__/templateTransformAssetUrl.spec.ts.snap
packages/compiler-sfc/__tests__/templateTransformAssetUrl.spec.ts
packages/compiler-sfc/src/templateTransformAssetUrl.ts
packages/compiler-sfc/src/templateUtils.ts

index e1c962a6ff5650fad0d6727d014d3e7f0b909d69..9e74a95aeba63fb2f07ff25b32763be1eddd53f7 100644 (file)
@@ -1,5 +1,37 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`compiler sfc: transform asset url should allow for full base URLs, with paths 1`] = `
+"import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
+
+export function render(_ctx, _cache) {
+  return (_openBlock(), _createBlock(\\"img\\", { src: \\"http://localhost:3000/src/logo.png\\" }))
+}"
+`;
+
+exports[`compiler sfc: transform asset url should allow for full base URLs, without paths 1`] = `
+"import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
+
+export function render(_ctx, _cache) {
+  return (_openBlock(), _createBlock(\\"img\\", { src: \\"http://localhost:3000/logo.png\\" }))
+}"
+`;
+
+exports[`compiler sfc: transform asset url should allow for full base URLs, without port 1`] = `
+"import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
+
+export function render(_ctx, _cache) {
+  return (_openBlock(), _createBlock(\\"img\\", { src: \\"http://localhost/logo.png\\" }))
+}"
+`;
+
+exports[`compiler sfc: transform asset url should allow for full base URLs, without protocol 1`] = `
+"import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
+
+export function render(_ctx, _cache) {
+  return (_openBlock(), _createBlock(\\"img\\", { src: \\"//localhost/logo.png\\" }))
+}"
+`;
+
 exports[`compiler sfc: transform asset url support uri fragment 1`] = `
 "import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
 import _imports_0 from '@svg/file.svg'
index 41eb6463cc30147726e40e3b6978eef59b35aa56..9fdfa77194c25df03ce1ecbe39766bd6b7f23a80 100644 (file)
@@ -94,4 +94,36 @@ describe('compiler sfc: transform asset url', () => {
     // should not remove it
     expect(code).toMatch(`"xlink:href": "#myCircle"`)
   })
+
+  test('should allow for full base URLs, with paths', () => {
+    const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
+      base: 'http://localhost:3000/src/'
+    })
+
+    expect(code).toMatchSnapshot()
+  })
+
+  test('should allow for full base URLs, without paths', () => {
+    const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
+      base: 'http://localhost:3000'
+    })
+
+    expect(code).toMatchSnapshot()
+  })
+
+  test('should allow for full base URLs, without port', () => {
+    const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
+      base: 'http://localhost'
+    })
+
+    expect(code).toMatchSnapshot()
+  })
+
+  test('should allow for full base URLs, without protocol', () => {
+    const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
+      base: '//localhost'
+    })
+
+    expect(code).toMatchSnapshot()
+  })
 })
index 78211e6ab0288d40823d5571647a160a156b45b8..7a92f6ea064edc932ff4d472ad5da658d8277de4 100644 (file)
@@ -121,12 +121,17 @@ export const transformAssetUrl: NodeTransform = (
           attr.value.content[0] !== '@' &&
           isRelativeUrl(attr.value.content)
         ) {
+          // Allow for full hostnames provided in options.base
+          const base = parseUrl(options.base)
+          const protocol = base.protocol || ''
+          const host = base.host ? protocol + '//' + base.host : ''
+          const basePath = base.path || '/'
+
           // when packaged in the browser, path will be using the posix-
           // only version provided by rollup-plugin-node-builtins.
-          attr.value.content = (path.posix || path).join(
-            options.base,
-            url.path + (url.hash || '')
-          )
+          attr.value.content =
+            host +
+            (path.posix || path).join(basePath, url.path + (url.hash || ''))
         }
         return
       }
index 6f93d612abfb45d2dccb6f144ee32b7bdd587bdf..b1befd927e23d052e7491673ffe77e2962a4ab3b 100644 (file)
@@ -35,5 +35,5 @@ export function parseUrl(url: string): UrlWithStringQuery {
 function parseUriParts(urlString: string): UrlWithStringQuery {
   // A TypeError is thrown if urlString is not a string
   // @see https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
-  return uriParse(isString(urlString) ? urlString : '')
+  return uriParse(isString(urlString) ? urlString : '', false, true)
 }