Skip to content

Commit db786b1

Browse files
authored
fix(compiler-sfc): support transforming asset urls with full base url. (#2477)
1 parent 3867bb4 commit db786b1

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

packages/compiler-sfc/__tests__/__snapshots__/templateTransformAssetUrl.spec.ts.snap

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`compiler sfc: transform asset url should allow for full base URLs, with paths 1`] = `
4+
"import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
5+
6+
export function render(_ctx, _cache) {
7+
return (_openBlock(), _createBlock(\\"img\\", { src: \\"http://localhost:3000/src/logo.png\\" }))
8+
}"
9+
`;
10+
11+
exports[`compiler sfc: transform asset url should allow for full base URLs, without paths 1`] = `
12+
"import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
13+
14+
export function render(_ctx, _cache) {
15+
return (_openBlock(), _createBlock(\\"img\\", { src: \\"http://localhost:3000/logo.png\\" }))
16+
}"
17+
`;
18+
19+
exports[`compiler sfc: transform asset url should allow for full base URLs, without port 1`] = `
20+
"import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
21+
22+
export function render(_ctx, _cache) {
23+
return (_openBlock(), _createBlock(\\"img\\", { src: \\"http://localhost/logo.png\\" }))
24+
}"
25+
`;
26+
27+
exports[`compiler sfc: transform asset url should allow for full base URLs, without protocol 1`] = `
28+
"import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
29+
30+
export function render(_ctx, _cache) {
31+
return (_openBlock(), _createBlock(\\"img\\", { src: \\"//localhost/logo.png\\" }))
32+
}"
33+
`;
34+
335
exports[`compiler sfc: transform asset url support uri fragment 1`] = `
436
"import { createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
537
import _imports_0 from '@svg/file.svg'

packages/compiler-sfc/__tests__/templateTransformAssetUrl.spec.ts

+32
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,36 @@ describe('compiler sfc: transform asset url', () => {
9494
// should not remove it
9595
expect(code).toMatch(`"xlink:href": "#myCircle"`)
9696
})
97+
98+
test('should allow for full base URLs, with paths', () => {
99+
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
100+
base: 'http://localhost:3000/src/'
101+
})
102+
103+
expect(code).toMatchSnapshot()
104+
})
105+
106+
test('should allow for full base URLs, without paths', () => {
107+
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
108+
base: 'http://localhost:3000'
109+
})
110+
111+
expect(code).toMatchSnapshot()
112+
})
113+
114+
test('should allow for full base URLs, without port', () => {
115+
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
116+
base: 'http://localhost'
117+
})
118+
119+
expect(code).toMatchSnapshot()
120+
})
121+
122+
test('should allow for full base URLs, without protocol', () => {
123+
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
124+
base: '//localhost'
125+
})
126+
127+
expect(code).toMatchSnapshot()
128+
})
97129
})

packages/compiler-sfc/src/templateTransformAssetUrl.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,17 @@ export const transformAssetUrl: NodeTransform = (
121121
attr.value.content[0] !== '@' &&
122122
isRelativeUrl(attr.value.content)
123123
) {
124+
// Allow for full hostnames provided in options.base
125+
const base = parseUrl(options.base)
126+
const protocol = base.protocol || ''
127+
const host = base.host ? protocol + '//' + base.host : ''
128+
const basePath = base.path || '/'
129+
124130
// when packaged in the browser, path will be using the posix-
125131
// only version provided by rollup-plugin-node-builtins.
126-
attr.value.content = (path.posix || path).join(
127-
options.base,
128-
url.path + (url.hash || '')
129-
)
132+
attr.value.content =
133+
host +
134+
(path.posix || path).join(basePath, url.path + (url.hash || ''))
130135
}
131136
return
132137
}

packages/compiler-sfc/src/templateUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ export function parseUrl(url: string): UrlWithStringQuery {
3535
function parseUriParts(urlString: string): UrlWithStringQuery {
3636
// A TypeError is thrown if urlString is not a string
3737
// @see https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
38-
return uriParse(isString(urlString) ? urlString : '')
38+
return uriParse(isString(urlString) ? urlString : '', false, true)
3939
}

0 commit comments

Comments
 (0)