2020-05-03 02:49:28 +08:00
|
|
|
import path from 'path'
|
2019-12-02 01:02:53 +08:00
|
|
|
import {
|
|
|
|
createSimpleExpression,
|
|
|
|
ExpressionNode,
|
|
|
|
NodeTransform,
|
|
|
|
NodeTypes,
|
|
|
|
SourceLocation,
|
|
|
|
TransformContext
|
|
|
|
} from '@vue/compiler-core'
|
2020-01-20 22:57:17 +08:00
|
|
|
import { isRelativeUrl, parseUrl } from './templateUtils'
|
2019-11-07 10:58:15 +08:00
|
|
|
|
2019-12-02 01:02:53 +08:00
|
|
|
export interface AssetURLOptions {
|
|
|
|
[name: string]: string[]
|
|
|
|
}
|
|
|
|
|
2020-05-03 02:49:28 +08:00
|
|
|
export interface NormlaizedAssetURLOptions {
|
|
|
|
base?: string | null
|
|
|
|
tags?: AssetURLOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultAssetUrlOptions: Required<NormlaizedAssetURLOptions> = {
|
|
|
|
base: null,
|
|
|
|
tags: {
|
|
|
|
video: ['src', 'poster'],
|
|
|
|
source: ['src'],
|
|
|
|
img: ['src'],
|
|
|
|
image: ['xlink:href', 'href'],
|
|
|
|
use: ['xlink:href', 'href']
|
|
|
|
}
|
2019-12-02 01:02:53 +08:00
|
|
|
}
|
|
|
|
|
2019-12-11 01:22:23 +08:00
|
|
|
export const createAssetUrlTransformWithOptions = (
|
2020-05-03 02:49:28 +08:00
|
|
|
options: NormlaizedAssetURLOptions
|
2019-12-11 01:22:23 +08:00
|
|
|
): NodeTransform => {
|
|
|
|
const mergedOptions = {
|
2020-05-03 02:49:28 +08:00
|
|
|
...defaultAssetUrlOptions,
|
2019-12-11 01:22:23 +08:00
|
|
|
...options
|
|
|
|
}
|
|
|
|
return (node, context) =>
|
|
|
|
(transformAssetUrl as Function)(node, context, mergedOptions)
|
|
|
|
}
|
|
|
|
|
2020-05-03 02:49:28 +08:00
|
|
|
/**
|
|
|
|
* A `@vue/compiler-core` plugin that transforms relative asset urls into
|
|
|
|
* either imports or absolute urls.
|
|
|
|
*
|
|
|
|
* ``` js
|
|
|
|
* // Before
|
|
|
|
* createVNode('img', { src: './logo.png' })
|
|
|
|
*
|
|
|
|
* // After
|
|
|
|
* import _imports_0 from './logo.png'
|
|
|
|
* createVNode('img', { src: _imports_0 })
|
|
|
|
* ```
|
|
|
|
*/
|
2019-12-11 01:22:23 +08:00
|
|
|
export const transformAssetUrl: NodeTransform = (
|
|
|
|
node,
|
|
|
|
context,
|
2020-05-03 02:49:28 +08:00
|
|
|
options: NormlaizedAssetURLOptions = defaultAssetUrlOptions
|
2019-12-11 01:22:23 +08:00
|
|
|
) => {
|
2019-12-02 01:02:53 +08:00
|
|
|
if (node.type === NodeTypes.ELEMENT) {
|
2020-05-03 02:49:28 +08:00
|
|
|
const tags = options.tags || defaultAssetUrlOptions.tags
|
|
|
|
for (const tag in tags) {
|
2019-12-02 01:02:53 +08:00
|
|
|
if ((tag === '*' || node.tag === tag) && node.props.length) {
|
2020-05-03 02:49:28 +08:00
|
|
|
const attributes = tags[tag]
|
|
|
|
attributes.forEach(name => {
|
2020-03-23 23:08:22 +08:00
|
|
|
node.props.forEach((attr, index) => {
|
2020-05-03 02:49:28 +08:00
|
|
|
if (
|
|
|
|
attr.type !== NodeTypes.ATTRIBUTE ||
|
|
|
|
attr.name !== name ||
|
|
|
|
!attr.value ||
|
|
|
|
!isRelativeUrl(attr.value.content)
|
|
|
|
) {
|
|
|
|
return
|
|
|
|
}
|
2019-12-02 01:02:53 +08:00
|
|
|
const url = parseUrl(attr.value.content)
|
2020-05-03 02:49:28 +08:00
|
|
|
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] !== '@') {
|
|
|
|
// 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(
|
|
|
|
options.base,
|
|
|
|
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
|
|
|
|
}
|
2019-12-02 01:02:53 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getImportsExpressionExp(
|
2020-04-16 21:33:30 +08:00
|
|
|
path: string | null,
|
|
|
|
hash: string | null,
|
2019-12-02 01:02:53 +08:00
|
|
|
loc: SourceLocation,
|
|
|
|
context: TransformContext
|
|
|
|
): ExpressionNode {
|
|
|
|
if (path) {
|
|
|
|
const importsArray = Array.from(context.imports)
|
|
|
|
const existing = importsArray.find(i => i.path === path)
|
|
|
|
if (existing) {
|
|
|
|
return existing.exp as ExpressionNode
|
|
|
|
}
|
|
|
|
const name = `_imports_${importsArray.length}`
|
|
|
|
const exp = createSimpleExpression(name, false, loc, true)
|
2020-05-05 03:15:26 +08:00
|
|
|
exp.isRuntimeConstant = true
|
2019-12-02 01:02:53 +08:00
|
|
|
context.imports.add({ exp, path })
|
|
|
|
if (hash && path) {
|
2020-05-05 03:15:26 +08:00
|
|
|
const ret = context.hoist(
|
2019-12-02 01:02:53 +08:00
|
|
|
createSimpleExpression(`${name} + '${hash}'`, false, loc, true)
|
|
|
|
)
|
2020-05-05 03:15:26 +08:00
|
|
|
ret.isRuntimeConstant = true
|
|
|
|
return ret
|
2019-12-02 01:02:53 +08:00
|
|
|
} else {
|
|
|
|
return exp
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return createSimpleExpression(`''`, false, loc, true)
|
|
|
|
}
|
2019-11-07 10:58:15 +08:00
|
|
|
}
|