2019-12-23 08:44:21 +08:00
|
|
|
import { generate, baseParse, transform } from '@vue/compiler-core'
|
2020-05-03 02:49:28 +08:00
|
|
|
import {
|
|
|
|
transformAssetUrl,
|
2020-05-05 04:45:19 +08:00
|
|
|
createAssetUrlTransformWithOptions,
|
|
|
|
AssetURLOptions,
|
|
|
|
normalizeOptions
|
2020-05-03 02:49:28 +08:00
|
|
|
} from '../src/templateTransformAssetUrl'
|
2019-12-02 01:02:53 +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
|
|
|
function compileWithAssetUrls(template: string, options?: AssetURLOptions) {
|
2019-12-23 08:44:21 +08:00
|
|
|
const ast = baseParse(template)
|
2020-05-05 04:45:19 +08:00
|
|
|
const t = options
|
|
|
|
? createAssetUrlTransformWithOptions(normalizeOptions(options))
|
|
|
|
: transformAssetUrl
|
2019-12-02 01:02:53 +08:00
|
|
|
transform(ast, {
|
2020-05-05 04:45:19 +08:00
|
|
|
nodeTransforms: [t, transformElement],
|
2019-12-02 01:02:53 +08:00
|
|
|
directiveTransforms: {
|
|
|
|
bind: transformBind
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return generate(ast, { mode: 'module' })
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('compiler sfc: transform asset url', () => {
|
|
|
|
test('transform assetUrls', () => {
|
|
|
|
const result = compileWithAssetUrls(`
|
|
|
|
<img src="./logo.png"/>
|
|
|
|
<img src="~fixtures/logo.png"/>
|
|
|
|
<img src="~/fixtures/logo.png"/>
|
2020-01-20 22:57:17 +08:00
|
|
|
<img src="http://example.com/fixtures/logo.png"/>
|
|
|
|
<img src="/fixtures/logo.png"/>
|
2020-06-24 09:46:18 +08:00
|
|
|
<img src="data:image/png;base64,i"/>
|
2019-12-02 01:02:53 +08:00
|
|
|
`)
|
|
|
|
|
|
|
|
expect(result.code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vuejs/component-compiler-utils#22 Support uri fragment in transformed require
|
|
|
|
*/
|
|
|
|
test('support uri fragment', () => {
|
|
|
|
const result = compileWithAssetUrls(
|
|
|
|
'<use href="~@svg/file.svg#fragment"></use>'
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(result.code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vuejs/component-compiler-utils#22 Support uri fragment in transformed require
|
|
|
|
*/
|
|
|
|
test('support uri is empty', () => {
|
|
|
|
const result = compileWithAssetUrls('<use href="~"></use>')
|
|
|
|
|
|
|
|
expect(result.code).toMatchSnapshot()
|
|
|
|
})
|
2020-05-03 02:49:28 +08:00
|
|
|
|
|
|
|
test('with explicit base', () => {
|
2020-05-05 04:45:19 +08:00
|
|
|
const { code } = compileWithAssetUrls(
|
2020-05-03 02:49:28 +08:00
|
|
|
`<img src="./bar.png"></img>` + // -> /foo/bar.png
|
|
|
|
`<img src="bar.png"></img>` + // -> bar.png (untouched)
|
2021-01-05 04:09:36 +08:00
|
|
|
`<img src="~bar.png"></img>` + // -> still converts to import
|
|
|
|
`<img src="@theme/bar.png"></img>`, // -> still converts to import
|
2020-05-05 04:45:19 +08:00
|
|
|
{
|
|
|
|
base: '/foo'
|
|
|
|
}
|
2020-05-03 02:49:28 +08:00
|
|
|
)
|
2021-01-05 04:09:36 +08:00
|
|
|
expect(code).toMatch(`import _imports_0 from 'bar.png'`)
|
|
|
|
expect(code).toMatch(`import _imports_1 from '@theme/bar.png'`)
|
2020-05-05 04:45:19 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('with includeAbsolute: true', () => {
|
|
|
|
const { code } = compileWithAssetUrls(
|
2020-05-06 21:40:55 +08:00
|
|
|
`<img src="./bar.png"/>` +
|
|
|
|
`<img src="/bar.png"/>` +
|
|
|
|
`<img src="https://foo.bar/baz.png"/>`,
|
2020-05-05 04:45:19 +08:00
|
|
|
{
|
|
|
|
includeAbsolute: true
|
2020-05-03 02:49:28 +08:00
|
|
|
}
|
2020-05-05 04:45:19 +08:00
|
|
|
)
|
2020-05-03 02:49:28 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
2020-05-29 22:20:57 +08:00
|
|
|
|
|
|
|
// vitejs/vite#298
|
|
|
|
test('should not transform hash fragments', () => {
|
|
|
|
const { code } = compileWithAssetUrls(
|
|
|
|
`<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
|
|
<defs>
|
|
|
|
<circle id="myCircle" cx="0" cy="0" r="5" />
|
|
|
|
</defs>
|
|
|
|
<use x="5" y="5" xlink:href="#myCircle" />
|
|
|
|
</svg>`
|
|
|
|
)
|
|
|
|
// should not remove it
|
|
|
|
expect(code).toMatch(`"xlink:href": "#myCircle"`)
|
|
|
|
})
|
2020-12-05 07:20:25 +08:00
|
|
|
|
|
|
|
test('should allow for full base URLs, with paths', () => {
|
|
|
|
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
|
|
|
|
base: 'http://localhost:3000/src/'
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should allow for full base URLs, without paths', () => {
|
|
|
|
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
|
|
|
|
base: 'http://localhost:3000'
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should allow for full base URLs, without port', () => {
|
|
|
|
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
|
|
|
|
base: 'http://localhost'
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should allow for full base URLs, without protocol', () => {
|
|
|
|
const { code } = compileWithAssetUrls(`<img src="./logo.png" />`, {
|
|
|
|
base: '//localhost'
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
2019-12-02 01:02:53 +08:00
|
|
|
})
|