Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions packages/compiler-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
type NodeTransform,
type StructuralDirectiveTransform,
type DirectiveTransform,
type ImportItem,
} from './transform'
export {
generate,
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-core/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export type StructuralDirectiveTransform = (
) => void | (() => void)

export interface ImportItem {
exp: string | ExpressionNode
exp: SimpleExpressionNode
path: string
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ export function ssrRender(_ctx, _push, _parent, _attrs) {
} else {
return [
_createVNode("picture", null, [
_createVNode("source", {
srcset: _imports_1
}),
_createVNode("source", { srcset: _imports_1 }),
_createVNode("img", { src: _imports_1 })
])
]
Expand Down
60 changes: 27 additions & 33 deletions packages/compiler-sfc/src/template/transformSrcset.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import path from 'path'
import {
ConstantTypes,
type ExpressionNode,
type NodeTransform,
NodeTypes,
type SimpleExpressionNode,
createCompoundExpression,
createSimpleExpression,
} from '@vue/compiler-core'
import {
Expand Down Expand Up @@ -106,55 +103,52 @@ export const transformSrcset: NodeTransform = (
}
}

const compoundExpression = createCompoundExpression([], attr.loc)
let content = ''
imageCandidates.forEach(({ url, descriptor }, index) => {
if (shouldProcessUrl(url)) {
const { path } = parseUrl(url)
let exp: SimpleExpressionNode
if (path) {
let exp = ''
const existingImportsIndex = context.imports.findIndex(
i => i.path === path,
)
if (existingImportsIndex > -1) {
exp = createSimpleExpression(
`_imports_${existingImportsIndex}`,
false,
attr.loc,
ConstantTypes.CAN_STRINGIFY,
)
exp = `_imports_${existingImportsIndex}`
} else {
exp = createSimpleExpression(
`_imports_${context.imports.length}`,
false,
attr.loc,
ConstantTypes.CAN_STRINGIFY,
)
context.imports.push({ exp, path })
exp = `_imports_${context.imports.length}`
context.imports.push({
exp: createSimpleExpression(
exp,
false,
attr.loc,
ConstantTypes.CAN_STRINGIFY,
),
path,
})
}
compoundExpression.children.push(exp)
content += exp
}
} else {
const exp = createSimpleExpression(
`"${url}"`,
false,
attr.loc,
ConstantTypes.CAN_STRINGIFY,
)
compoundExpression.children.push(exp)
content += `"${url}"`
}
const isNotLast = imageCandidates.length - 1 > index
if (descriptor && isNotLast) {
compoundExpression.children.push(` + ' ${descriptor}, ' + `)
} else if (descriptor) {
compoundExpression.children.push(` + ' ${descriptor}'`)
if (descriptor) {
content += ` + ' ${descriptor}${isNotLast ? ', ' : ''}'${
isNotLast ? ' + ' : ''
}`
} else if (isNotLast) {
compoundExpression.children.push(` + ', ' + `)
content += ` + ', ' + `
}
})

let exp: ExpressionNode = compoundExpression
let exp = createSimpleExpression(
content,
false,
attr.loc,
ConstantTypes.CAN_STRINGIFY,
)
if (context.hoistStatic) {
exp = context.hoist(compoundExpression)
exp = context.hoist(exp)
exp.constType = ConstantTypes.CAN_STRINGIFY
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`compiler sfc: transform asset url > should allow for full base URLs, with paths 1`] = `
"import { template as _template } from 'vue';
const t0 = _template("<img src=\\"http://localhost:3000/src/logo.png\\">", true)

export function render(_ctx) {
const n0 = t0()
return n0
}"
`;

exports[`compiler sfc: transform asset url > should allow for full base URLs, without paths 1`] = `
"import { template as _template } from 'vue';
const t0 = _template("<img src=\\"http://localhost:3000/logo.png\\">", true)

export function render(_ctx) {
const n0 = t0()
return n0
}"
`;

exports[`compiler sfc: transform asset url > should allow for full base URLs, without port 1`] = `
"import { template as _template } from 'vue';
const t0 = _template("<img src=\\"http://localhost/logo.png\\">", true)

export function render(_ctx) {
const n0 = t0()
return n0
}"
`;

exports[`compiler sfc: transform asset url > should allow for full base URLs, without protocol 1`] = `
"import { template as _template } from 'vue';
const t0 = _template("<img src=\\"//localhost/logo.png\\">", true)

export function render(_ctx) {
const n0 = t0()
return n0
}"
`;

exports[`compiler sfc: transform asset url > support uri fragment 1`] = `
"import { template as _template } from 'vue';
import _imports_0 from '@svg/file.svg';
const t0 = _template("<use href=\\"" + _imports_0 + '#fragment' + "\\"></use>", false, 1)

export function render(_ctx) {
const n0 = t0()
const n1 = t0()
return [n0, n1]
}"
`;

exports[`compiler sfc: transform asset url > support uri is empty 1`] = `
"import { template as _template } from 'vue';
const t0 = _template("<use href=\\"\\"></use>", true, 1)

export function render(_ctx) {
const n0 = t0()
return n0
}"
`;

exports[`compiler sfc: transform asset url > transform assetUrls 1`] = `
"import { template as _template } from 'vue';
import _imports_0 from './logo.png';
import _imports_1 from 'fixtures/logo.png';
import _imports_2 from '/fixtures/logo.png';
const t0 = _template("<img src=\\"" + _imports_0 + "\\">")
const t1 = _template("<img src=\\"" + _imports_1 + "\\">")
const t2 = _template("<img src=\\"http://example.com/fixtures/logo.png\\">")
const t3 = _template("<img src=\\"//example.com/fixtures/logo.png\\">")
const t4 = _template("<img src=\\"" + _imports_2 + "\\">")
const t5 = _template("<img src=\\"data:image/png;base64,i\\">")

export function render(_ctx) {
const n0 = t0()
const n1 = t1()
const n2 = t1()
const n3 = t2()
const n4 = t3()
const n5 = t4()
const n6 = t5()
return [n0, n1, n2, n3, n4, n5, n6]
}"
`;

exports[`compiler sfc: transform asset url > transform with stringify 1`] = `
"import { template as _template } from 'vue';
import _imports_0 from './bar.png';
import _imports_1 from '/bar.png';
const t0 = _template("<div><img src=\\"" + _imports_0 + "\\"><img src=\\"" + _imports_1 + "\\"><img src=\\"https://foo.bar/baz.png\\"><img src=\\"//foo.bar/baz.png\\"><img src=\\"" + _imports_0 + "\\"></div>", true)

export function render(_ctx) {
const n0 = t0()
return n0
}"
`;

exports[`compiler sfc: transform asset url > with explicit base 1`] = `
"import { template as _template } from 'vue';
import _imports_0 from 'bar.png';
import _imports_1 from '@theme/bar.png';
const t0 = _template("<img src=\\"/foo/bar.png\\">")
const t1 = _template("<img src=\\"" + _imports_0 + "\\">")
const t2 = _template("<img src=\\"" + _imports_1 + "\\">")

export function render(_ctx) {
const n0 = t0()
const n1 = t1()
const n2 = t1()
const n3 = t2()
return [n0, n1, n2, n3]
}"
`;

exports[`compiler sfc: transform asset url > with includeAbsolute: true 1`] = `
"import { template as _template } from 'vue';
import _imports_0 from './bar.png';
import _imports_1 from '/bar.png';
const t0 = _template("<img src=\\"" + _imports_0 + "\\">")
const t1 = _template("<img src=\\"" + _imports_1 + "\\">")
const t2 = _template("<img src=\\"https://foo.bar/baz.png\\">")
const t3 = _template("<img src=\\"//foo.bar/baz.png\\">")

export function render(_ctx) {
const n0 = t0()
const n1 = t1()
const n2 = t2()
const n3 = t3()
return [n0, n1, n2, n3]
}"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`compiler sfc: transform srcset > srcset w/ explicit base option 1`] = `
"import { template as _template } from 'vue';
import _imports_0 from '@/logo.png';
import _imports_1 from '/foo/logo.png';
const t0 = _template("<img srcset=\\"" + _imports_0 + ', ' + _imports_0 + ' 2x' + "\\">")
const t1 = _template("<img srcset=\\"" + _imports_0 + ' 1x, ' + _imports_1 + ' 2x' + "\\">")
export function render(_ctx) {
const n0 = t0()
const n1 = t1()
return [n0, n1]
}"
`;
exports[`compiler sfc: transform srcset > transform srcset 1`] = `
"import { template as _template } from 'vue';
import _imports_0 from './logo.png';
import _imports_1 from '/logo.png';
const t0 = _template("<img src=\\"" + _imports_0 + "\\" srcset>")
const t1 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + "\\">")
const t2 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ' 2x' + "\\">")
const t3 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ', ' + _imports_0 + ' 2x' + "\\">")
const t4 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ' 2x, ' + _imports_0 + "\\">")
const t5 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ' 2x, ' + _imports_0 + ' 3x' + "\\">")
const t6 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ', ' + _imports_0 + ' 2x, ' + _imports_0 + ' 3x' + "\\">")
const t7 = _template("<img src=\\"" + _imports_1 + "\\" srcset=\\"" + _imports_1 + ', ' + _imports_1 + ' 2x' + "\\">")
const t8 = _template("<img src=\\"https://example.com/logo.png\\" srcset=\\"https://example.com/logo.png, https://example.com/logo.png 2x\\">")
const t9 = _template("<img src=\\"" + _imports_1 + "\\" srcset=\\"" + _imports_1 + ', ' + _imports_0 + ' 2x' + "\\">")
const t10 = _template("<img src=\\"data:image/png;base64,i\\" srcset=\\"data:image/png;base64,i 1x, data:image/png;base64,i 2x\\">")
export function render(_ctx) {
const n0 = t0()
const n1 = t1()
const n2 = t2()
const n3 = t2()
const n4 = t3()
const n5 = t4()
const n6 = t5()
const n7 = t6()
const n8 = t7()
const n9 = t8()
const n10 = t9()
const n11 = t10()
return [n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11]
}"
`;
exports[`compiler sfc: transform srcset > transform srcset w/ base 1`] = `
"import { template as _template } from 'vue';
import _imports_0 from '/logo.png';
import _imports_1 from '/foo/logo.png';
const t0 = _template("<img src=\\"/foo/logo.png\\" srcset>")
const t1 = _template("<img src=\\"/foo/logo.png\\" srcset=\\"/foo/logo.png\\">")
const t2 = _template("<img src=\\"/foo/logo.png\\" srcset=\\"/foo/logo.png 2x\\">")
const t3 = _template("<img src=\\"/foo/logo.png\\" srcset=\\"/foo/logo.png, /foo/logo.png 2x\\">")
const t4 = _template("<img src=\\"/foo/logo.png\\" srcset=\\"/foo/logo.png 2x, /foo/logo.png\\">")
const t5 = _template("<img src=\\"/foo/logo.png\\" srcset=\\"/foo/logo.png 2x, /foo/logo.png 3x\\">")
const t6 = _template("<img src=\\"/foo/logo.png\\" srcset=\\"/foo/logo.png, /foo/logo.png 2x, /foo/logo.png 3x\\">")
const t7 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ', ' + _imports_0 + ' 2x' + "\\">")
const t8 = _template("<img src=\\"https://example.com/logo.png\\" srcset=\\"https://example.com/logo.png, https://example.com/logo.png 2x\\">")
const t9 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ', ' + _imports_1 + ' 2x' + "\\">")
const t10 = _template("<img src=\\"data:image/png;base64,i\\" srcset=\\"data:image/png;base64,i 1x, data:image/png;base64,i 2x\\">")
export function render(_ctx) {
const n0 = t0()
const n1 = t1()
const n2 = t2()
const n3 = t2()
const n4 = t3()
const n5 = t4()
const n6 = t5()
const n7 = t6()
const n8 = t7()
const n9 = t8()
const n10 = t9()
const n11 = t10()
return [n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11]
}"
`;
exports[`compiler sfc: transform srcset > transform srcset w/ includeAbsolute: true 1`] = `
"import { template as _template } from 'vue';
import _imports_0 from './logo.png';
import _imports_1 from '/logo.png';
const t0 = _template("<img src=\\"" + _imports_0 + "\\" srcset>")
const t1 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + "\\">")
const t2 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ' 2x' + "\\">")
const t3 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ', ' + _imports_0 + ' 2x' + "\\">")
const t4 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ' 2x, ' + _imports_0 + "\\">")
const t5 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ' 2x, ' + _imports_0 + ' 3x' + "\\">")
const t6 = _template("<img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ', ' + _imports_0 + ' 2x, ' + _imports_0 + ' 3x' + "\\">")
const t7 = _template("<img src=\\"" + _imports_1 + "\\" srcset=\\"" + _imports_1 + ', ' + _imports_1 + ' 2x' + "\\">")
const t8 = _template("<img src=\\"https://example.com/logo.png\\" srcset=\\"https://example.com/logo.png, https://example.com/logo.png 2x\\">")
const t9 = _template("<img src=\\"" + _imports_1 + "\\" srcset=\\"" + _imports_1 + ', ' + _imports_0 + ' 2x' + "\\">")
const t10 = _template("<img src=\\"data:image/png;base64,i\\" srcset=\\"data:image/png;base64,i 1x, data:image/png;base64,i 2x\\">")
export function render(_ctx) {
const n0 = t0()
const n1 = t1()
const n2 = t2()
const n3 = t2()
const n4 = t3()
const n5 = t4()
const n6 = t5()
const n7 = t6()
const n8 = t7()
const n9 = t8()
const n10 = t9()
const n11 = t10()
return [n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11]
}"
`;
exports[`compiler sfc: transform srcset > transform srcset w/ stringify 1`] = `
"import { template as _template } from 'vue';
import _imports_0 from './logo.png';
import _imports_1 from '/logo.png';
const t0 = _template("<div><img src=\\"" + _imports_0 + "\\" srcset><img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + "\\"><img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ' 2x' + "\\"><img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ' 2x' + "\\"><img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ', ' + _imports_0 + ' 2x' + "\\"><img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ' 2x, ' + _imports_0 + "\\"><img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ' 2x, ' + _imports_0 + ' 3x' + "\\"><img src=\\"" + _imports_0 + "\\" srcset=\\"" + _imports_0 + ', ' + _imports_0 + ' 2x, ' + _imports_0 + ' 3x' + "\\"><img src=\\"" + _imports_1 + "\\" srcset=\\"" + _imports_1 + ', ' + _imports_1 + ' 2x' + "\\"><img src=\\"https://example.com/logo.png\\" srcset=\\"https://example.com/logo.png, https://example.com/logo.png 2x\\"><img src=\\"" + _imports_1 + "\\" srcset=\\"" + _imports_1 + ', ' + _imports_0 + ' 2x' + "\\"><img src=\\"data:image/png;base64,i\\" srcset=\\"data:image/png;base64,i 1x, data:image/png;base64,i 2x\\"></div>", true)
export function render(_ctx) {
const n0 = t0()
return n0
}"
`;
Loading
Loading