File tree Expand file tree Collapse file tree 3 files changed +26
-21
lines changed Expand file tree Collapse file tree 3 files changed +26
-21
lines changed Original file line number Diff line number Diff line change @@ -66,7 +66,7 @@ interface Config {
66
66
position? : ' before' | ' after'
67
67
removeTarget? : boolean
68
68
}
69
- attributes? : any
69
+ attributes? : { [ name : string ] : string }
70
70
}
71
71
```
72
72
Original file line number Diff line number Diff line change @@ -66,6 +66,27 @@ export class BasePlugin {
66
66
}
67
67
}
68
68
69
+ protected getAttributesString ( config : Config ) : string {
70
+ if ( config . attributes && typeof config . attributes === 'object' ) {
71
+ return (
72
+ ' ' +
73
+ Object . keys ( config . attributes )
74
+ . map ( ( key ) => `${ key } ="${ config . attributes ! [ key ] || '' } "` )
75
+ . join ( ' ' )
76
+ )
77
+ }
78
+
79
+ if ( config . attributes === undefined && config . attributes === null ) {
80
+ throw new Error (
81
+ `Please provide a key/value object if intending to use the attributes option, not ${
82
+ config . attributes
83
+ } `,
84
+ )
85
+ }
86
+
87
+ return ''
88
+ }
89
+
69
90
protected addStyle ( {
70
91
html,
71
92
htmlFileName,
@@ -75,25 +96,9 @@ export class BasePlugin {
75
96
htmlFileName : string
76
97
style : string
77
98
} ) {
78
- let attributesString = ''
79
-
80
- if ( this . config . attributes ) {
81
- if (
82
- typeof this . config . attributes !== 'object' &&
83
- this . config . attributes === null
84
- ) {
85
- throw new Error (
86
- `Please provide a key/value object if intending to use the attributes option` ,
87
- )
88
- }
89
-
90
- Object . keys ( this . config . attributes ) . map ( ( key ) => {
91
- const value = this . config . attributes [ key ] || ''
92
- attributesString += ` ${ key } ="${ value } "`
93
- } )
94
- }
95
-
96
- const styleString = `<style${ attributesString } >${ style } </style>`
99
+ const styleString = `<style${ this . getAttributesString (
100
+ this . config ,
101
+ ) } >${ style } </style>`
97
102
98
103
const replaceValues = [ styleString , this . replaceConfig . target ]
99
104
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ export interface Config {
14
14
filter ?( fileName : string ) : boolean
15
15
leaveCSSFile ?: boolean
16
16
replace ?: ReplaceConfig
17
- attributes ?: any
17
+ attributes ?: { [ name : string ] : string }
18
18
}
19
19
20
20
export interface FileCache {
You can’t perform that action at this time.
0 commit comments