import { generate, baseParse, transform, TransformOptions } from '@vue/compiler-core' import { transformSrcset, createSrcsetTransformWithOptions } from '../src/templateTransformSrcset' import { transformElement } from '../../compiler-core/src/transforms/transformElement' import { transformBind } from '../../compiler-core/src/transforms/vBind' import { AssetURLOptions, normalizeOptions } from '../src/templateTransformAssetUrl' import { stringifyStatic } from '../../compiler-dom/src/transforms/stringifyStatic' function compileWithSrcset( template: string, options?: AssetURLOptions, transformOptions?: TransformOptions ) { const ast = baseParse(template) const srcsetTransform = options ? createSrcsetTransformWithOptions(normalizeOptions(options)) : transformSrcset transform(ast, { hoistStatic: true, nodeTransforms: [srcsetTransform, transformElement], directiveTransforms: { bind: transformBind }, ...transformOptions }) return generate(ast, { mode: 'module' }) } const src = ` ` describe('compiler sfc: transform srcset', () => { test('transform srcset', () => { expect(compileWithSrcset(src).code).toMatchSnapshot() }) test('transform srcset w/ base', () => { expect( compileWithSrcset(src, { base: '/foo' }).code ).toMatchSnapshot() }) test('transform srcset w/ includeAbsolute: true', () => { expect( compileWithSrcset(src, { includeAbsolute: true }).code ).toMatchSnapshot() }) test('transform srcset w/ stringify', () => { const code = compileWithSrcset( `
${src}
`, { includeAbsolute: true }, { hoistStatic: true, transformHoist: stringifyStatic } ).code expect(code).toMatch(`_createStaticVNode`) expect(code).toMatchSnapshot() }) test('srcset w/ explicit base option', () => { const code = compileWithSrcset( ` `, { base: '/foo/' }, { hoistStatic: true } ).code expect(code).toMatchSnapshot() }) })