-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler-sfc): support transforming absolute asset urls
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
Showing
9 changed files
with
332 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 42 additions & 16 deletions
58
packages/compiler-sfc/__tests__/templateTransformSrcset.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.