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:
Evan You
2020-05-04 16:45:19 -04:00
parent f9a3766fd6
commit 6a0be882d4
9 changed files with 333 additions and 98 deletions

View File

@@ -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
}
})
})