Skip to content

Commit

Permalink
feat(compiler-sfc): support transforming absolute asset urls
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `@vue/compiler-sfc`'s `transformAssetUrlsBase` option
has been removed. It is merged into `trasnformAssetUrls` which now also
accepts the format of

  ```ts
  {
    base?: string
    includeAbsolute?: string
    tags?: { [name: string]: string[] }
  }
  ```
  • Loading branch information
yyx990803 committed May 4, 2020
1 parent f9a3766 commit 6a0be88
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ export function render(_ctx, _cache) {
], 64 /* STABLE_FRAGMENT */))
}"
`;

exports[`compiler sfc: transform asset url with includeAbsolute: true 1`] = `
"import { createVNode as _createVNode, Fragment as _Fragment, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
import _imports_0 from './bar.png'
import _imports_1 from '/bar.png'
export function render(_ctx, _cache) {
return (_openBlock(), _createBlock(_Fragment, null, [
_createVNode(\\"img\\", { src: _imports_0 }),
_createVNode(\\"img\\", { src: _imports_1 })
], 64 /* STABLE_FRAGMENT */))
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,115 @@ export function render(_ctx, _cache) {
], 64 /* STABLE_FRAGMENT */))
}"
`;

exports[`compiler sfc: transform srcset transform srcset w/ base 1`] = `
"import { createVNode as _createVNode, Fragment as _Fragment, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
export function render(_ctx, _cache) {
return (_openBlock(), _createBlock(_Fragment, null, [
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: \\"/foo/logo.png\\"
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: \\"/foo/logo.png 2x\\"
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: \\"/foo/logo.png 2x\\"
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: \\"/foo/logo.png, /foo/logo.png 2x\\"
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: \\"/foo/logo.png 2x, /foo/logo.png\\"
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: \\"/foo/logo.png 2x, /foo/logo.png 3x\\"
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: \\"/foo/logo.png, /foo/logo.png 2x, /foo/logo.png 3x\\"
}),
_createVNode(\\"img\\", {
src: \\"/logo.png\\",
srcset: \\"/logo.png, /logo.png 2x\\"
}),
_createVNode(\\"img\\", {
src: \\"https://example.com/logo.png\\",
srcset: \\"https://example.com/logo.png, https://example.com/logo.png 2x\\"
}),
_createVNode(\\"img\\", {
src: \\"/logo.png\\",
srcset: \\"/logo.png, /foo/logo.png 2x\\"
})
], 64 /* STABLE_FRAGMENT */))
}"
`;

exports[`compiler sfc: transform srcset transform srcset w/ includeAbsolute: true 1`] = `
"import { createVNode as _createVNode, Fragment as _Fragment, openBlock as _openBlock, createBlock as _createBlock } 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 = \\"https://example.com/logo.png\\" + ', ' + \\"https://example.com/logo.png\\" + '2x'
const _hoisted_10 = _imports_1 + ', ' + _imports_0 + '2x'
export function render(_ctx, _cache) {
return (_openBlock(), _createBlock(_Fragment, null, [
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: _hoisted_1
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: _hoisted_2
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: _hoisted_3
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: _hoisted_4
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: _hoisted_5
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: _hoisted_6
}),
_createVNode(\\"img\\", {
src: \\"./logo.png\\",
srcset: _hoisted_7
}),
_createVNode(\\"img\\", {
src: \\"/logo.png\\",
srcset: _hoisted_8
}),
_createVNode(\\"img\\", {
src: \\"https://example.com/logo.png\\",
srcset: _hoisted_9
}),
_createVNode(\\"img\\", {
src: \\"/logo.png\\",
srcset: _hoisted_10
})
], 64 /* STABLE_FRAGMENT */))
}"
`;
18 changes: 15 additions & 3 deletions packages/compiler-sfc/__tests__/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,27 @@ test('transform asset url options', () => {
// Object option
const { code: code1 } = compileTemplate({
...input,
transformAssetUrls: { foo: ['bar'] }
transformAssetUrls: {
tags: { foo: ['bar'] }
}
})
expect(code1).toMatch(`import _imports_0 from 'baz'\n`)
// false option

// legacy object option (direct tags config)
const { code: code2 } = compileTemplate({
...input,
transformAssetUrls: {
foo: ['bar']
}
})
expect(code2).toMatch(`import _imports_0 from 'baz'\n`)

// false option
const { code: code3 } = compileTemplate({
...input,
transformAssetUrls: false
})
expect(code2).not.toMatch(`import _imports_0 from 'baz'\n`)
expect(code3).not.toMatch(`import _imports_0 from 'baz'\n`)
})

test('source map', () => {
Expand Down
38 changes: 22 additions & 16 deletions packages/compiler-sfc/__tests__/templateTransformAssetUrl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { generate, baseParse, transform } from '@vue/compiler-core'
import {
transformAssetUrl,
createAssetUrlTransformWithOptions
createAssetUrlTransformWithOptions,
AssetURLOptions,
normalizeOptions
} from '../src/templateTransformAssetUrl'
import { transformElement } from '../../compiler-core/src/transforms/transformElement'
import { transformBind } from '../../compiler-core/src/transforms/vBind'

function compileWithAssetUrls(template: string) {
function compileWithAssetUrls(template: string, options?: AssetURLOptions) {
const ast = baseParse(template)
const t = options
? createAssetUrlTransformWithOptions(normalizeOptions(options))
: transformAssetUrl
transform(ast, {
nodeTransforms: [transformAssetUrl, transformElement],
nodeTransforms: [t, transformElement],
directiveTransforms: {
bind: transformBind
}
Expand Down Expand Up @@ -51,24 +56,25 @@ describe('compiler sfc: transform asset url', () => {
})

test('with explicit base', () => {
const ast = baseParse(
const { code } = compileWithAssetUrls(
`<img src="./bar.png"></img>` + // -> /foo/bar.png
`<img src="~bar.png"></img>` + // -> /foo/bar.png
`<img src="bar.png"></img>` + // -> bar.png (untouched)
`<img src="@theme/bar.png"></img>` // -> @theme/bar.png (untouched)
`<img src="@theme/bar.png"></img>`, // -> @theme/bar.png (untouched)
{
base: '/foo'
}
)
transform(ast, {
nodeTransforms: [
createAssetUrlTransformWithOptions({
base: '/foo'
}),
transformElement
],
directiveTransforms: {
bind: transformBind
expect(code).toMatchSnapshot()
})

test('with includeAbsolute: true', () => {
const { code } = compileWithAssetUrls(
`<img src="./bar.png"></img>` + `<img src="/bar.png"></img>`,
{
includeAbsolute: true
}
})
const { code } = generate(ast, { mode: 'module' })
)
expect(code).toMatchSnapshot()
})
})
58 changes: 42 additions & 16 deletions packages/compiler-sfc/__tests__/templateTransformSrcset.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,60 @@
import { generate, baseParse, transform } from '@vue/compiler-core'
import { transformSrcset } from '../src/templateTransformSrcset'
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'

function compileWithSrcset(template: string) {
function compileWithSrcset(template: string, options?: AssetURLOptions) {
const ast = baseParse(template)
const srcsetTrasnform = options
? createSrcsetTransformWithOptions(normalizeOptions(options))
: transformSrcset
transform(ast, {
nodeTransforms: [transformSrcset, transformElement],
nodeTransforms: [srcsetTrasnform, transformElement],
directiveTransforms: {
bind: transformBind
}
})
return generate(ast, { mode: 'module' })
}

const src = `
<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"/>
`

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"/>
<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"/>
`)
expect(compileWithSrcset(src).code).toMatchSnapshot()
})

test('transform srcset w/ base', () => {
expect(
compileWithSrcset(src, {
base: '/foo'
}).code
).toMatchSnapshot()
})

expect(result.code).toMatchSnapshot()
test('transform srcset w/ includeAbsolute: true', () => {
expect(
compileWithSrcset(src, {
includeAbsolute: true
}).code
).toMatchSnapshot()
})
})
44 changes: 19 additions & 25 deletions packages/compiler-sfc/src/compileTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import { SourceMapConsumer, SourceMapGenerator, RawSourceMap } from 'source-map'
import {
transformAssetUrl,
AssetURLOptions,
createAssetUrlTransformWithOptions
createAssetUrlTransformWithOptions,
AssetURLTagConfig,
normalizeOptions
} from './templateTransformAssetUrl'
import { transformSrcset } from './templateTransformSrcset'
import {
transformSrcset,
createSrcsetTransformWithOptions
} from './templateTransformSrcset'
import { isObject } from '@vue/shared'
import * as CompilerDOM from '@vue/compiler-dom'
import * as CompilerSSR from '@vue/compiler-ssr'
Expand Down Expand Up @@ -47,16 +52,10 @@ export interface SFCTemplateCompileOptions {
*/
preprocessCustomRequire?: (id: string) => any
/**
* Configure what tags/attributes to trasnform into relative asset url imports
* in the form of `{ [tag: string]: string[] }`, or disable the transform with
* `false`.
*/
transformAssetUrls?: AssetURLOptions | boolean
/**
* If base is provided, instead of transforming relative asset urls into
* imports, they will be directly rewritten to absolute urls.
* Configure what tags/attributes to trasnform into asset url imports,
* or disable the transform altogether with `false`.
*/
transformAssetUrlsBase?: string
transformAssetUrls?: AssetURLOptions | AssetURLTagConfig | boolean
}

function preprocess(
Expand Down Expand Up @@ -144,24 +143,19 @@ function doCompileTemplate({
ssr = false,
compiler = ssr ? (CompilerSSR as TemplateCompiler) : CompilerDOM,
compilerOptions = {},
transformAssetUrls,
transformAssetUrlsBase
transformAssetUrls
}: SFCTemplateCompileOptions): SFCTemplateCompileResults {
const errors: CompilerError[] = []

let nodeTransforms: NodeTransform[] = []
if (transformAssetUrls !== false) {
if (transformAssetUrlsBase || isObject(transformAssetUrls)) {
nodeTransforms = [
createAssetUrlTransformWithOptions({
base: transformAssetUrlsBase,
tags: isObject(transformAssetUrls) ? transformAssetUrls : undefined
}),
transformSrcset
]
} else {
nodeTransforms = [transformAssetUrl, transformSrcset]
}
if (isObject(transformAssetUrls)) {
const assetOptions = normalizeOptions(transformAssetUrls)
nodeTransforms = [
createAssetUrlTransformWithOptions(assetOptions),
createSrcsetTransformWithOptions(assetOptions)
]
} else if (transformAssetUrls !== false) {
nodeTransforms = [transformAssetUrl, transformSrcset]
}

let { code, map } = compiler.compile(source, {
Expand Down
Loading

0 comments on commit 6a0be88

Please sign in to comment.