@@ -6,6 +6,14 @@ export interface AssetURLOptions {
66 [ name : string ] : string | string [ ]
77}
88
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+
917const defaultOptions : AssetURLOptions = {
1018 audio : 'src' ,
1119 video : [ 'src' , 'poster' ] ,
@@ -15,37 +23,52 @@ const defaultOptions: AssetURLOptions = {
1523 use : [ 'xlink:href' , 'href' ]
1624}
1725
18- export default ( userOptions ?: AssetURLOptions ) => {
26+ export default (
27+ userOptions ?: AssetURLOptions ,
28+ transformAssetUrlsOption ?: TransformAssetUrlsOptions
29+ ) => {
1930 const options = userOptions
2031 ? Object . assign ( { } , defaultOptions , userOptions )
2132 : defaultOptions
2233
2334 return {
2435 postTransformNode : ( node : ASTNode ) => {
25- transform ( node , options )
36+ transform ( node , options , transformAssetUrlsOption )
2637 }
2738 }
2839}
2940
30- function transform ( node : ASTNode , options : AssetURLOptions ) {
41+ function transform (
42+ node : ASTNode ,
43+ options : AssetURLOptions ,
44+ transformAssetUrlsOption ?: TransformAssetUrlsOptions
45+ ) {
3146 for ( const tag in options ) {
3247 if ( ( tag === '*' || node . tag === tag ) && node . attrs ) {
3348 const attributes = options [ tag ]
3449 if ( typeof attributes === 'string' ) {
35- node . attrs . some ( attr => rewrite ( attr , attributes ) )
50+ node . attrs . some ( attr =>
51+ rewrite ( attr , attributes , transformAssetUrlsOption )
52+ )
3653 } 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+ )
3857 }
3958 }
4059 }
4160}
4261
43- function rewrite ( attr : Attr , name : string ) {
62+ function rewrite (
63+ attr : Attr ,
64+ name : string ,
65+ transformAssetUrlsOption ?: TransformAssetUrlsOptions
66+ ) {
4467 if ( attr . name === name ) {
4568 const value = attr . value
4669 // only transform static URLs
4770 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 )
4972 return true
5073 }
5174 }
0 commit comments