Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): only transform relative asset URLs #628

Merged
merged 2 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export function render() {
return (openBlock(), createBlock(Fragment, null, [
createVNode(\\"img\\", { src: _imports_0 }),
createVNode(\\"img\\", { src: _imports_1 }),
createVNode(\\"img\\", { src: _imports_1 })
createVNode(\\"img\\", { src: _imports_1 }),
createVNode(\\"img\\", { src: \\"http://example.com/fixtures/logo.png\\" }),
createVNode(\\"img\\", { src: \\"/fixtures/logo.png\\" })
], 64 /* STABLE_FRAGMENT */))
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const _hoisted_4 = _imports_0 + ', ' + _imports_0 + '2x'
const _hoisted_5 = _imports_0 + '2x, ' + _imports_0
const _hoisted_6 = _imports_0 + '2x, ' + _imports_0 + '3x'
const _hoisted_7 = _imports_0 + ', ' + _imports_0 + '2x, ' + _imports_0 + '3x'
const _hoisted_8 = \\"/logo.png\\" + ', ' + \\"/logo.png\\" + '2x'
const _hoisted_9 = \\"https://example.com/logo.png\\" + ', ' + \\"https://example.com/logo.png\\" + '2x'
const _hoisted_10 = \\"/logo.png\\" + ', ' + _imports_0 + '2x'

export function render() {
const _ctx = this
Expand Down Expand Up @@ -43,6 +46,18 @@ export function render() {
createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: _hoisted_7
}),
createVNode(\\"img\\", {
src: \\"/logo.png\\",
srcset: _hoisted_8
}),
createVNode(\\"img\\", {
src: \\"https://example.com/logo.png\\",
srcset: _hoisted_9
}),
createVNode(\\"img\\", {
src: \\"/logo.png\\",
srcset: _hoisted_10
})
], 64 /* STABLE_FRAGMENT */))
}"
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/__tests__/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test('warn missing preprocessor', () => {
})

test('transform asset url options', () => {
const input = { source: `<foo bar="baz"/>`, filename: 'example.vue' }
const input = { source: `<foo bar="~baz"/>`, filename: 'example.vue' }
// Object option
const { code: code1 } = compileTemplate({
...input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ describe('compiler sfc: transform asset url', () => {
<img src="./logo.png"/>
<img src="~fixtures/logo.png"/>
<img src="~/fixtures/logo.png"/>
<img src="http://example.com/fixtures/logo.png"/>
<img src="/fixtures/logo.png"/>
`)

expect(result.code).toMatchSnapshot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ describe('compiler sfc: transform srcset', () => {
<img src="./logo.png" srcset="./logo.png 2x, ./logo.png"/>
<img src="./logo.png" srcset="./logo.png 2x, ./logo.png 3x"/>
<img src="./logo.png" srcset="./logo.png, ./logo.png 2x, ./logo.png 3x"/>
<img src="/logo.png" srcset="/logo.png, /logo.png 2x"/>
<img src="https://example.com/logo.png" srcset="https://example.com/logo.png, https://example.com/logo.png 2x"/>
<img src="/logo.png" srcset="/logo.png, ./logo.png 2x"/>
`)

expect(result.code).toMatchSnapshot()
Expand Down
26 changes: 18 additions & 8 deletions packages/compiler-sfc/src/templateTransformAssetUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SourceLocation,
TransformContext
} from '@vue/compiler-core'
import { parseUrl } from './templateUtils'
import { isRelativeUrl, parseUrl } from './templateUtils'

export interface AssetURLOptions {
[name: string]: string[]
Expand Down Expand Up @@ -46,13 +46,23 @@ export const transformAssetUrl: NodeTransform = (
if (attr.type !== NodeTypes.ATTRIBUTE) return
if (attr.name !== item) return
if (!attr.value) return
const url = parseUrl(attr.value.content)
const exp = getImportsExpressionExp(
url.path,
url.hash,
attr.loc,
context
)
let exp: ExpressionNode
if (isRelativeUrl(attr.value.content)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should simply return early if the URL is absolute. This keeps src a static attribute and allows the entire img element to be hoisted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, this didn't even occur to me to simply return. Changed it here accordingly and reverted the condition brackets.

const url = parseUrl(attr.value.content)
exp = getImportsExpressionExp(
url.path,
url.hash,
attr.loc,
context
)
} else {
exp = createSimpleExpression(
`"${attr.value.content}"`,
false,
attr.loc,
true
)
}
node.props[index] = {
type: NodeTypes.DIRECTIVE,
name: 'bind',
Expand Down
54 changes: 32 additions & 22 deletions packages/compiler-sfc/src/templateTransformSrcset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
NodeTypes,
SimpleExpressionNode
} from '@vue/compiler-core'
import { parseUrl } from './templateUtils'
import { isRelativeUrl, parseUrl } from './templateUtils'

const srcsetTags = ['img', 'source']

Expand Down Expand Up @@ -38,29 +38,39 @@ export const transformSrcset: NodeTransform = (node, context) => {

const compoundExpression = createCompoundExpression([], attr.loc)
imageCandidates.forEach(({ url, descriptor }, index) => {
const { path } = parseUrl(url)
let exp: SimpleExpressionNode
if (path) {
const importsArray = Array.from(context.imports)
const existingImportsIndex = importsArray.findIndex(
i => i.path === path
)
if (existingImportsIndex > -1) {
exp = createSimpleExpression(
`_imports_${existingImportsIndex}`,
false,
attr.loc,
true
)
} else {
exp = createSimpleExpression(
`_imports_${importsArray.length}`,
false,
attr.loc,
true
if (isRelativeUrl(url)) {
sisou marked this conversation as resolved.
Show resolved Hide resolved
const { path } = parseUrl(url)
let exp: SimpleExpressionNode
if (path) {
const importsArray = Array.from(context.imports)
const existingImportsIndex = importsArray.findIndex(
i => i.path === path
)
context.imports.add({ exp, path })
if (existingImportsIndex > -1) {
exp = createSimpleExpression(
`_imports_${existingImportsIndex}`,
false,
attr.loc,
true
)
} else {
exp = createSimpleExpression(
`_imports_${importsArray.length}`,
false,
attr.loc,
true
)
context.imports.add({ exp, path })
}
compoundExpression.children.push(exp)
}
} else {
const exp = createSimpleExpression(
`"${url}"`,
false,
attr.loc,
true
)
compoundExpression.children.push(exp)
}
const isNotLast = imageCandidates.length - 1 > index
Expand Down
13 changes: 8 additions & 5 deletions packages/compiler-sfc/src/templateUtils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { UrlWithStringQuery, parse as uriParse } from 'url'
import { isString } from '@vue/shared'

export function isRelativeUrl(url: string): boolean {
const firstChar = url.charAt(0)
return firstChar === '.' || firstChar === '~' || firstChar === '@'
}

// We need an extra transform context API for injecting arbitrary import
// statements.
export function parseUrl(url: string): UrlWithStringQuery {
const firstChar = url.charAt(0)
if (firstChar === '.' || firstChar === '~' || firstChar === '@') {
if (firstChar === '~') {
const secondChar = url.charAt(1)
url = url.slice(secondChar === '/' ? 2 : 1)
}
if (firstChar === '~') {
const secondChar = url.charAt(1)
url = url.slice(secondChar === '/' ? 2 : 1)
}
return parseUriParts(url)
}
Expand Down