2021-12-06 01:19:06 +08:00
|
|
|
import {
|
|
|
|
generate,
|
|
|
|
baseParse,
|
|
|
|
transform,
|
|
|
|
TransformOptions
|
|
|
|
} from '@vue/compiler-core'
|
2020-05-05 04:45:19 +08:00
|
|
|
import {
|
|
|
|
transformSrcset,
|
|
|
|
createSrcsetTransformWithOptions
|
|
|
|
} from '../src/templateTransformSrcset'
|
2019-12-03 12:06:14 +08:00
|
|
|
import { transformElement } from '../../compiler-core/src/transforms/transformElement'
|
|
|
|
import { transformBind } from '../../compiler-core/src/transforms/vBind'
|
2020-05-05 04:45:19 +08:00
|
|
|
import {
|
|
|
|
AssetURLOptions,
|
|
|
|
normalizeOptions
|
|
|
|
} from '../src/templateTransformAssetUrl'
|
2021-12-06 01:19:06 +08:00
|
|
|
import { stringifyStatic } from '../../compiler-dom/src/transforms/stringifyStatic'
|
2019-12-03 12:06:14 +08:00
|
|
|
|
2021-12-06 01:19:06 +08:00
|
|
|
function compileWithSrcset(
|
|
|
|
template: string,
|
|
|
|
options?: AssetURLOptions,
|
|
|
|
transformOptions?: TransformOptions
|
|
|
|
) {
|
2019-12-23 08:44:21 +08:00
|
|
|
const ast = baseParse(template)
|
2020-07-08 18:32:42 +08:00
|
|
|
const srcsetTransform = options
|
2020-05-05 04:45:19 +08:00
|
|
|
? createSrcsetTransformWithOptions(normalizeOptions(options))
|
|
|
|
: transformSrcset
|
2019-12-03 12:06:14 +08:00
|
|
|
transform(ast, {
|
2020-07-08 18:32:42 +08:00
|
|
|
nodeTransforms: [srcsetTransform, transformElement],
|
2019-12-03 12:06:14 +08:00
|
|
|
directiveTransforms: {
|
|
|
|
bind: transformBind
|
2021-12-06 01:19:06 +08:00
|
|
|
},
|
|
|
|
...transformOptions
|
2019-12-03 12:06:14 +08:00
|
|
|
})
|
|
|
|
return generate(ast, { mode: 'module' })
|
|
|
|
}
|
|
|
|
|
2020-05-05 04:45:19 +08:00
|
|
|
const src = `
|
2021-02-04 02:27:34 +08:00
|
|
|
<img src="./logo.png" srcset=""/>
|
2020-05-05 04:45:19 +08:00
|
|
|
<img src="./logo.png" srcset="./logo.png"/>
|
|
|
|
<img src="./logo.png" srcset="./logo.png 2x"/>
|
|
|
|
<img src="./logo.png" srcset="./logo.png 2x"/>
|
|
|
|
<img src="./logo.png" srcset="./logo.png, ./logo.png 2x"/>
|
|
|
|
<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"/>
|
2020-06-24 09:46:18 +08:00
|
|
|
<img src="data:image/png;base64,i" srcset="data:image/png;base64,i 1x, data:image/png;base64,i 2x"/>
|
2020-05-05 04:45:19 +08:00
|
|
|
`
|
|
|
|
|
2019-12-03 12:06:14 +08:00
|
|
|
describe('compiler sfc: transform srcset', () => {
|
|
|
|
test('transform srcset', () => {
|
2020-05-05 04:45:19 +08:00
|
|
|
expect(compileWithSrcset(src).code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('transform srcset w/ base', () => {
|
|
|
|
expect(
|
|
|
|
compileWithSrcset(src, {
|
|
|
|
base: '/foo'
|
|
|
|
}).code
|
|
|
|
).toMatchSnapshot()
|
|
|
|
})
|
2019-12-03 12:06:14 +08:00
|
|
|
|
2020-05-05 04:45:19 +08:00
|
|
|
test('transform srcset w/ includeAbsolute: true', () => {
|
|
|
|
expect(
|
|
|
|
compileWithSrcset(src, {
|
|
|
|
includeAbsolute: true
|
|
|
|
}).code
|
|
|
|
).toMatchSnapshot()
|
2019-12-03 12:06:14 +08:00
|
|
|
})
|
2021-12-06 01:19:06 +08:00
|
|
|
|
|
|
|
test('transform srcset w/ stringify', () => {
|
|
|
|
const code = compileWithSrcset(
|
|
|
|
`<div>${src}</div>`,
|
|
|
|
{
|
|
|
|
includeAbsolute: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hoistStatic: true,
|
|
|
|
transformHoist: stringifyStatic
|
|
|
|
}
|
|
|
|
).code
|
|
|
|
expect(code).toMatch(`_createStaticVNode`)
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
2019-12-03 12:06:14 +08:00
|
|
|
})
|