@@ -6,6 +6,14 @@ export interface AssetURLOptions {
6
6
[ name : string ] : string | string [ ]
7
7
}
8
8
9
+ export interface TransformAssetUrlsOptions {
10
+ /**
11
+ * If base is provided, instead of transforming relative asset urls into
12
+ * imports, they will be directly rewritten to absolute urls.
13
+ */
14
+ base ?: string
15
+ }
16
+
9
17
const defaultOptions : AssetURLOptions = {
10
18
audio : 'src' ,
11
19
video : [ 'src' , 'poster' ] ,
@@ -15,37 +23,52 @@ const defaultOptions: AssetURLOptions = {
15
23
use : [ 'xlink:href' , 'href' ]
16
24
}
17
25
18
- export default ( userOptions ?: AssetURLOptions ) => {
26
+ export default (
27
+ userOptions ?: AssetURLOptions ,
28
+ transformAssetUrlsOption ?: TransformAssetUrlsOptions
29
+ ) => {
19
30
const options = userOptions
20
31
? Object . assign ( { } , defaultOptions , userOptions )
21
32
: defaultOptions
22
33
23
34
return {
24
35
postTransformNode : ( node : ASTNode ) => {
25
- transform ( node , options )
36
+ transform ( node , options , transformAssetUrlsOption )
26
37
}
27
38
}
28
39
}
29
40
30
- function transform ( node : ASTNode , options : AssetURLOptions ) {
41
+ function transform (
42
+ node : ASTNode ,
43
+ options : AssetURLOptions ,
44
+ transformAssetUrlsOption ?: TransformAssetUrlsOptions
45
+ ) {
31
46
for ( const tag in options ) {
32
47
if ( ( tag === '*' || node . tag === tag ) && node . attrs ) {
33
48
const attributes = options [ tag ]
34
49
if ( typeof attributes === 'string' ) {
35
- node . attrs . some ( attr => rewrite ( attr , attributes ) )
50
+ node . attrs . some ( attr =>
51
+ rewrite ( attr , attributes , transformAssetUrlsOption )
52
+ )
36
53
} else if ( Array . isArray ( attributes ) ) {
37
- attributes . forEach ( item => node . attrs . some ( attr => rewrite ( attr , item ) ) )
54
+ attributes . forEach ( item =>
55
+ node . attrs . some ( attr => rewrite ( attr , item , transformAssetUrlsOption ) )
56
+ )
38
57
}
39
58
}
40
59
}
41
60
}
42
61
43
- function rewrite ( attr : Attr , name : string ) {
62
+ function rewrite (
63
+ attr : Attr ,
64
+ name : string ,
65
+ transformAssetUrlsOption ?: TransformAssetUrlsOptions
66
+ ) {
44
67
if ( attr . name === name ) {
45
68
const value = attr . value
46
69
// only transform static URLs
47
70
if ( value . charAt ( 0 ) === '"' && value . charAt ( value . length - 1 ) === '"' ) {
48
- attr . value = urlToRequire ( value . slice ( 1 , - 1 ) )
71
+ attr . value = urlToRequire ( value . slice ( 1 , - 1 ) , transformAssetUrlsOption )
49
72
return true
50
73
}
51
74
}
0 commit comments