2019-12-23 08:44:21 +08:00
|
|
|
import { generate, baseParse, transform } from '@vue/compiler-core'
|
2019-12-03 12:06:14 +08:00
|
|
|
import { transformSrcset } from '../src/templateTransformSrcset'
|
|
|
|
import { transformElement } from '../../compiler-core/src/transforms/transformElement'
|
|
|
|
import { transformBind } from '../../compiler-core/src/transforms/vBind'
|
|
|
|
|
|
|
|
function compileWithSrcset(template: string) {
|
2019-12-23 08:44:21 +08:00
|
|
|
const ast = baseParse(template)
|
2019-12-03 12:06:14 +08:00
|
|
|
transform(ast, {
|
|
|
|
nodeTransforms: [transformSrcset, transformElement],
|
|
|
|
directiveTransforms: {
|
|
|
|
bind: transformBind
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return generate(ast, { mode: 'module' })
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('compiler sfc: transform srcset', () => {
|
|
|
|
test('transform srcset', () => {
|
|
|
|
const result = compileWithSrcset(`
|
|
|
|
<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"/>
|
2020-01-20 22:57:17 +08:00
|
|
|
<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"/>
|
2019-12-03 12:06:14 +08:00
|
|
|
`)
|
|
|
|
|
|
|
|
expect(result.code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
})
|