|
5 | 5 | NodeTypes,
|
6 | 6 | SimpleExpressionNode
|
7 | 7 | } from '@vue/compiler-core'
|
8 |
| -import { parseUrl } from './templateUtils' |
| 8 | +import { isRelativeUrl, parseUrl } from './templateUtils' |
9 | 9 |
|
10 | 10 | const srcsetTags = ['img', 'source']
|
11 | 11 |
|
@@ -36,31 +36,44 @@ export const transformSrcset: NodeTransform = (node, context) => {
|
36 | 36 | return { url, descriptor }
|
37 | 37 | })
|
38 | 38 |
|
| 39 | + // When srcset does not contain any relative URLs, skip transforming |
| 40 | + if (!imageCandidates.some(({ url }) => isRelativeUrl(url))) return |
| 41 | + |
39 | 42 | const compoundExpression = createCompoundExpression([], attr.loc)
|
40 | 43 | imageCandidates.forEach(({ url, descriptor }, index) => {
|
41 |
| - const { path } = parseUrl(url) |
42 |
| - let exp: SimpleExpressionNode |
43 |
| - if (path) { |
44 |
| - const importsArray = Array.from(context.imports) |
45 |
| - const existingImportsIndex = importsArray.findIndex( |
46 |
| - i => i.path === path |
47 |
| - ) |
48 |
| - if (existingImportsIndex > -1) { |
49 |
| - exp = createSimpleExpression( |
50 |
| - `_imports_${existingImportsIndex}`, |
51 |
| - false, |
52 |
| - attr.loc, |
53 |
| - true |
| 44 | + if (isRelativeUrl(url)) { |
| 45 | + const { path } = parseUrl(url) |
| 46 | + let exp: SimpleExpressionNode |
| 47 | + if (path) { |
| 48 | + const importsArray = Array.from(context.imports) |
| 49 | + const existingImportsIndex = importsArray.findIndex( |
| 50 | + i => i.path === path |
54 | 51 | )
|
55 |
| - } else { |
56 |
| - exp = createSimpleExpression( |
57 |
| - `_imports_${importsArray.length}`, |
58 |
| - false, |
59 |
| - attr.loc, |
60 |
| - true |
61 |
| - ) |
62 |
| - context.imports.add({ exp, path }) |
| 52 | + if (existingImportsIndex > -1) { |
| 53 | + exp = createSimpleExpression( |
| 54 | + `_imports_${existingImportsIndex}`, |
| 55 | + false, |
| 56 | + attr.loc, |
| 57 | + true |
| 58 | + ) |
| 59 | + } else { |
| 60 | + exp = createSimpleExpression( |
| 61 | + `_imports_${importsArray.length}`, |
| 62 | + false, |
| 63 | + attr.loc, |
| 64 | + true |
| 65 | + ) |
| 66 | + context.imports.add({ exp, path }) |
| 67 | + } |
| 68 | + compoundExpression.children.push(exp) |
63 | 69 | }
|
| 70 | + } else { |
| 71 | + const exp = createSimpleExpression( |
| 72 | + `"${url}"`, |
| 73 | + false, |
| 74 | + attr.loc, |
| 75 | + true |
| 76 | + ) |
64 | 77 | compoundExpression.children.push(exp)
|
65 | 78 | }
|
66 | 79 | const isNotLast = imageCandidates.length - 1 > index
|
|
0 commit comments