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[] }
}
```
This commit is contained in:
@@ -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'
|
||||
@@ -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`.
|
||||
* Configure what tags/attributes to trasnform into asset url imports,
|
||||
* or disable the transform altogether 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.
|
||||
*/
|
||||
transformAssetUrlsBase?: string
|
||||
transformAssetUrls?: AssetURLOptions | AssetURLTagConfig | boolean
|
||||
}
|
||||
|
||||
function preprocess(
|
||||
@@ -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, {
|
||||
|
||||
@@ -8,18 +8,28 @@ import {
|
||||
TransformContext
|
||||
} from '@vue/compiler-core'
|
||||
import { isRelativeUrl, parseUrl } from './templateUtils'
|
||||
import { isArray } from '@vue/shared'
|
||||
|
||||
export interface AssetURLOptions {
|
||||
export interface AssetURLTagConfig {
|
||||
[name: string]: string[]
|
||||
}
|
||||
|
||||
export interface NormlaizedAssetURLOptions {
|
||||
export interface AssetURLOptions {
|
||||
/**
|
||||
* If base is provided, instead of transforming relative asset urls into
|
||||
* imports, they will be directly rewritten to absolute urls.
|
||||
*/
|
||||
base?: string | null
|
||||
tags?: AssetURLOptions
|
||||
/**
|
||||
* If true, also processes absolute urls.
|
||||
*/
|
||||
includeAbsolute?: boolean
|
||||
tags?: AssetURLTagConfig
|
||||
}
|
||||
|
||||
const defaultAssetUrlOptions: Required<NormlaizedAssetURLOptions> = {
|
||||
export const defaultAssetUrlOptions: Required<AssetURLOptions> = {
|
||||
base: null,
|
||||
includeAbsolute: false,
|
||||
tags: {
|
||||
video: ['src', 'poster'],
|
||||
source: ['src'],
|
||||
@@ -29,15 +39,27 @@ const defaultAssetUrlOptions: Required<NormlaizedAssetURLOptions> = {
|
||||
}
|
||||
}
|
||||
|
||||
export const createAssetUrlTransformWithOptions = (
|
||||
options: NormlaizedAssetURLOptions
|
||||
): NodeTransform => {
|
||||
const mergedOptions = {
|
||||
export const normalizeOptions = (
|
||||
options: AssetURLOptions | AssetURLTagConfig
|
||||
): Required<AssetURLOptions> => {
|
||||
if (Object.keys(options).some(key => isArray((options as any)[key]))) {
|
||||
// legacy option format which directly passes in tags config
|
||||
return {
|
||||
...defaultAssetUrlOptions,
|
||||
tags: options as any
|
||||
}
|
||||
}
|
||||
return {
|
||||
...defaultAssetUrlOptions,
|
||||
...options
|
||||
}
|
||||
}
|
||||
|
||||
export const createAssetUrlTransformWithOptions = (
|
||||
options: Required<AssetURLOptions>
|
||||
): NodeTransform => {
|
||||
return (node, context) =>
|
||||
(transformAssetUrl as Function)(node, context, mergedOptions)
|
||||
(transformAssetUrl as Function)(node, context, options)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +78,7 @@ export const createAssetUrlTransformWithOptions = (
|
||||
export const transformAssetUrl: NodeTransform = (
|
||||
node,
|
||||
context,
|
||||
options: NormlaizedAssetURLOptions = defaultAssetUrlOptions
|
||||
options: AssetURLOptions = defaultAssetUrlOptions
|
||||
) => {
|
||||
if (node.type === NodeTypes.ELEMENT) {
|
||||
const tags = options.tags || defaultAssetUrlOptions.tags
|
||||
@@ -69,16 +91,21 @@ export const transformAssetUrl: NodeTransform = (
|
||||
attr.type !== NodeTypes.ATTRIBUTE ||
|
||||
attr.name !== name ||
|
||||
!attr.value ||
|
||||
!isRelativeUrl(attr.value.content)
|
||||
(!options.includeAbsolute && !isRelativeUrl(attr.value.content))
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
const url = parseUrl(attr.value.content)
|
||||
|
||||
if (options.base) {
|
||||
// explicit base - directly rewrite the url into absolute url
|
||||
// does not apply to url that starts with `@` since they are
|
||||
// aliases
|
||||
if (attr.value.content[0] !== '@') {
|
||||
// does not apply to absolute urls or urls that start with `@`
|
||||
// since they are aliases
|
||||
if (
|
||||
attr.value.content[0] !== '@' &&
|
||||
isRelativeUrl(attr.value.content)
|
||||
) {
|
||||
// when packaged in the browser, path will be using the posix-
|
||||
// only version provided by rollup-plugin-node-builtins.
|
||||
attr.value.content = (path.posix || path).join(
|
||||
@@ -86,24 +113,25 @@ export const transformAssetUrl: NodeTransform = (
|
||||
url.path + (url.hash || '')
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// otherwise, transform the url into an import.
|
||||
// this assumes a bundler will resolve the import into the correct
|
||||
// absolute url (e.g. webpack file-loader)
|
||||
const exp = getImportsExpressionExp(
|
||||
url.path,
|
||||
url.hash,
|
||||
attr.loc,
|
||||
context
|
||||
)
|
||||
node.props[index] = {
|
||||
type: NodeTypes.DIRECTIVE,
|
||||
name: 'bind',
|
||||
arg: createSimpleExpression(name, true, attr.loc),
|
||||
exp,
|
||||
modifiers: [],
|
||||
loc: attr.loc
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// otherwise, transform the url into an import.
|
||||
// this assumes a bundler will resolve the import into the correct
|
||||
// absolute url (e.g. webpack file-loader)
|
||||
const exp = getImportsExpressionExp(
|
||||
url.path,
|
||||
url.hash,
|
||||
attr.loc,
|
||||
context
|
||||
)
|
||||
node.props[index] = {
|
||||
type: NodeTypes.DIRECTIVE,
|
||||
name: 'bind',
|
||||
arg: createSimpleExpression(name, true, attr.loc),
|
||||
exp,
|
||||
modifiers: [],
|
||||
loc: attr.loc
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import path from 'path'
|
||||
import {
|
||||
createCompoundExpression,
|
||||
createSimpleExpression,
|
||||
@@ -5,7 +6,11 @@ import {
|
||||
NodeTypes,
|
||||
SimpleExpressionNode
|
||||
} from '@vue/compiler-core'
|
||||
import { isRelativeUrl, parseUrl } from './templateUtils'
|
||||
import { isRelativeUrl, parseUrl, isExternalUrl } from './templateUtils'
|
||||
import {
|
||||
AssetURLOptions,
|
||||
defaultAssetUrlOptions
|
||||
} from './templateTransformAssetUrl'
|
||||
|
||||
const srcsetTags = ['img', 'source']
|
||||
|
||||
@@ -17,13 +22,23 @@ interface ImageCandidate {
|
||||
// http://w3c.github.io/html/semantics-embedded-content.html#ref-for-image-candidate-string-5
|
||||
const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g
|
||||
|
||||
export const transformSrcset: NodeTransform = (node, context) => {
|
||||
export const createSrcsetTransformWithOptions = (
|
||||
options: Required<AssetURLOptions>
|
||||
): NodeTransform => {
|
||||
return (node, context) =>
|
||||
(transformSrcset as Function)(node, context, options)
|
||||
}
|
||||
|
||||
export const transformSrcset: NodeTransform = (
|
||||
node,
|
||||
context,
|
||||
options: Required<AssetURLOptions> = defaultAssetUrlOptions
|
||||
) => {
|
||||
if (node.type === NodeTypes.ELEMENT) {
|
||||
if (srcsetTags.includes(node.tag) && node.props.length) {
|
||||
node.props.forEach((attr, index) => {
|
||||
if (attr.name === 'srcset' && attr.type === NodeTypes.ATTRIBUTE) {
|
||||
if (!attr.value) return
|
||||
// same logic as in transform-require.js
|
||||
const value = attr.value.content
|
||||
|
||||
const imageCandidates: ImageCandidate[] = value.split(',').map(s => {
|
||||
@@ -37,11 +52,34 @@ export const transformSrcset: NodeTransform = (node, context) => {
|
||||
})
|
||||
|
||||
// When srcset does not contain any relative URLs, skip transforming
|
||||
if (!imageCandidates.some(({ url }) => isRelativeUrl(url))) return
|
||||
if (
|
||||
!options.includeAbsolute &&
|
||||
!imageCandidates.some(({ url }) => isRelativeUrl(url))
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
if (options.base) {
|
||||
const base = options.base
|
||||
const set: string[] = []
|
||||
imageCandidates.forEach(({ url, descriptor }, index) => {
|
||||
descriptor = descriptor ? ` ${descriptor}` : ``
|
||||
if (isRelativeUrl(url)) {
|
||||
set.push((path.posix || path).join(base, url) + descriptor)
|
||||
} else {
|
||||
set.push(url + descriptor)
|
||||
}
|
||||
})
|
||||
attr.value.content = set.join(', ')
|
||||
return
|
||||
}
|
||||
|
||||
const compoundExpression = createCompoundExpression([], attr.loc)
|
||||
imageCandidates.forEach(({ url, descriptor }, index) => {
|
||||
if (isRelativeUrl(url)) {
|
||||
if (
|
||||
!isExternalUrl(url) &&
|
||||
(options.includeAbsolute || isRelativeUrl(url))
|
||||
) {
|
||||
const { path } = parseUrl(url)
|
||||
let exp: SimpleExpressionNode
|
||||
if (path) {
|
||||
|
||||
@@ -6,6 +6,11 @@ export function isRelativeUrl(url: string): boolean {
|
||||
return firstChar === '.' || firstChar === '~' || firstChar === '@'
|
||||
}
|
||||
|
||||
const externalRE = /^https?:\/\//
|
||||
export function isExternalUrl(url: string): boolean {
|
||||
return externalRE.test(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses string url into URL object.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user