fix(compiler-sfc): make asset url imports stringifiable
This commit is contained in:
@@ -74,6 +74,22 @@ export function render(_ctx, _cache) {
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`compiler sfc: transform asset url transform with stringify 1`] = `
|
||||
"import { createElementVNode as _createElementVNode, createStaticVNode as _createStaticVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
|
||||
import _imports_0 from './bar.png'
|
||||
import _imports_1 from '/bar.png'
|
||||
|
||||
|
||||
const _hoisted_1 = /*#__PURE__*/_createStaticVNode(\\"<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 + \\"\\\\\\">\\", 5)
|
||||
const _hoisted_6 = [
|
||||
_hoisted_1
|
||||
]
|
||||
|
||||
export function render(_ctx, _cache) {
|
||||
return (_openBlock(), _createElementBlock(\\"div\\", null, _hoisted_6))
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`compiler sfc: transform asset url with explicit base 1`] = `
|
||||
"import { createElementVNode as _createElementVNode, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
|
||||
import _imports_0 from 'bar.png'
|
||||
|
||||
@@ -194,3 +194,28 @@ export function render(_ctx, _cache) {
|
||||
], 64 /* STABLE_FRAGMENT */))
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`compiler sfc: transform srcset transform srcset w/ stringify 1`] = `
|
||||
"import { createElementVNode as _createElementVNode, createStaticVNode as _createStaticVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
|
||||
import _imports_0 from './logo.png'
|
||||
import _imports_1 from '/logo.png'
|
||||
|
||||
|
||||
const _hoisted_1 = _imports_0
|
||||
const _hoisted_2 = _imports_0 + ' 2x'
|
||||
const _hoisted_3 = _imports_0 + ' 2x'
|
||||
const _hoisted_4 = _imports_0 + ', ' + _imports_0 + ' 2x'
|
||||
const _hoisted_5 = _imports_0 + ' 2x, ' + _imports_0
|
||||
const _hoisted_6 = _imports_0 + ' 2x, ' + _imports_0 + ' 3x'
|
||||
const _hoisted_7 = _imports_0 + ', ' + _imports_0 + ' 2x, ' + _imports_0 + ' 3x'
|
||||
const _hoisted_8 = _imports_1 + ', ' + _imports_1 + ' 2x'
|
||||
const _hoisted_9 = _imports_1 + ', ' + _imports_0 + ' 2x'
|
||||
const _hoisted_10 = /*#__PURE__*/_createStaticVNode(\\"<img src=\\\\\\"./logo.png\\\\\\" srcset=\\\\\\"\\\\\\"><img src=\\\\\\"./logo.png\\\\\\" srcset=\\\\\\"\\" + _hoisted_1 + \\"\\\\\\"><img src=\\\\\\"./logo.png\\\\\\" srcset=\\\\\\"\\" + _hoisted_2 + \\"\\\\\\"><img src=\\\\\\"./logo.png\\\\\\" srcset=\\\\\\"\\" + _hoisted_3 + \\"\\\\\\"><img src=\\\\\\"./logo.png\\\\\\" srcset=\\\\\\"\\" + _hoisted_4 + \\"\\\\\\"><img src=\\\\\\"./logo.png\\\\\\" srcset=\\\\\\"\\" + _hoisted_5 + \\"\\\\\\"><img src=\\\\\\"./logo.png\\\\\\" srcset=\\\\\\"\\" + _hoisted_6 + \\"\\\\\\"><img src=\\\\\\"./logo.png\\\\\\" srcset=\\\\\\"\\" + _hoisted_7 + \\"\\\\\\"><img src=\\\\\\"/logo.png\\\\\\" srcset=\\\\\\"\\" + _hoisted_8 + \\"\\\\\\"><img src=\\\\\\"https://example.com/logo.png\\\\\\" srcset=\\\\\\"https://example.com/logo.png, https://example.com/logo.png 2x\\\\\\"><img src=\\\\\\"/logo.png\\\\\\" srcset=\\\\\\"\\" + _hoisted_9 + \\"\\\\\\"><img src=\\\\\\"data:image/png;base64,i\\\\\\" srcset=\\\\\\"data:image/png;base64,i 1x, data:image/png;base64,i 2x\\\\\\">\\", 12)
|
||||
const _hoisted_22 = [
|
||||
_hoisted_10
|
||||
]
|
||||
|
||||
export function render(_ctx, _cache) {
|
||||
return (_openBlock(), _createElementBlock(\\"div\\", null, _hoisted_22))
|
||||
}"
|
||||
`;
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { generate, baseParse, transform } from '@vue/compiler-core'
|
||||
import {
|
||||
generate,
|
||||
baseParse,
|
||||
transform,
|
||||
TransformOptions
|
||||
} from '@vue/compiler-core'
|
||||
import {
|
||||
transformAssetUrl,
|
||||
createAssetUrlTransformWithOptions,
|
||||
@@ -7,8 +12,13 @@ import {
|
||||
} from '../src/templateTransformAssetUrl'
|
||||
import { transformElement } from '../../compiler-core/src/transforms/transformElement'
|
||||
import { transformBind } from '../../compiler-core/src/transforms/vBind'
|
||||
import { stringifyStatic } from '../../compiler-dom/src/transforms/stringifyStatic'
|
||||
|
||||
function compileWithAssetUrls(template: string, options?: AssetURLOptions) {
|
||||
function compileWithAssetUrls(
|
||||
template: string,
|
||||
options?: AssetURLOptions,
|
||||
transformOptions?: TransformOptions
|
||||
) {
|
||||
const ast = baseParse(template)
|
||||
const t = options
|
||||
? createAssetUrlTransformWithOptions(normalizeOptions(options))
|
||||
@@ -17,7 +27,8 @@ function compileWithAssetUrls(template: string, options?: AssetURLOptions) {
|
||||
nodeTransforms: [t, transformElement],
|
||||
directiveTransforms: {
|
||||
bind: transformBind
|
||||
}
|
||||
},
|
||||
...transformOptions
|
||||
})
|
||||
return generate(ast, { mode: 'module' })
|
||||
}
|
||||
@@ -131,4 +142,26 @@ describe('compiler sfc: transform asset url', () => {
|
||||
|
||||
expect(code).toMatchSnapshot()
|
||||
})
|
||||
|
||||
test('transform with stringify', () => {
|
||||
const { code } = compileWithAssetUrls(
|
||||
`<div>` +
|
||||
`<img src="./bar.png"/>` +
|
||||
`<img src="/bar.png"/>` +
|
||||
`<img src="https://foo.bar/baz.png"/>` +
|
||||
`<img src="//foo.bar/baz.png"/>` +
|
||||
`<img src="./bar.png"/>` +
|
||||
`</div>`,
|
||||
{
|
||||
includeAbsolute: true
|
||||
},
|
||||
{
|
||||
hoistStatic: true,
|
||||
transformHoist: stringifyStatic
|
||||
}
|
||||
)
|
||||
console.log(code)
|
||||
expect(code).toMatch(`_createStaticVNode`)
|
||||
expect(code).toMatchSnapshot()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { generate, baseParse, transform } from '@vue/compiler-core'
|
||||
import {
|
||||
generate,
|
||||
baseParse,
|
||||
transform,
|
||||
TransformOptions
|
||||
} from '@vue/compiler-core'
|
||||
import {
|
||||
transformSrcset,
|
||||
createSrcsetTransformWithOptions
|
||||
@@ -9,8 +14,13 @@ import {
|
||||
AssetURLOptions,
|
||||
normalizeOptions
|
||||
} from '../src/templateTransformAssetUrl'
|
||||
import { stringifyStatic } from '../../compiler-dom/src/transforms/stringifyStatic'
|
||||
|
||||
function compileWithSrcset(template: string, options?: AssetURLOptions) {
|
||||
function compileWithSrcset(
|
||||
template: string,
|
||||
options?: AssetURLOptions,
|
||||
transformOptions?: TransformOptions
|
||||
) {
|
||||
const ast = baseParse(template)
|
||||
const srcsetTransform = options
|
||||
? createSrcsetTransformWithOptions(normalizeOptions(options))
|
||||
@@ -19,7 +29,8 @@ function compileWithSrcset(template: string, options?: AssetURLOptions) {
|
||||
nodeTransforms: [srcsetTransform, transformElement],
|
||||
directiveTransforms: {
|
||||
bind: transformBind
|
||||
}
|
||||
},
|
||||
...transformOptions
|
||||
})
|
||||
return generate(ast, { mode: 'module' })
|
||||
}
|
||||
@@ -59,4 +70,19 @@ describe('compiler sfc: transform srcset', () => {
|
||||
}).code
|
||||
).toMatchSnapshot()
|
||||
})
|
||||
|
||||
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()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user