@@ -36,19 +36,39 @@ interface Config {
36
36
replace ?: ReplaceConfig
37
37
}
38
38
39
+ interface ConfigOfAddStyle {
40
+ html : string
41
+ htmlFileName : string
42
+ style : string
43
+ replaceConfig : ReplaceConfig
44
+ }
45
+
39
46
const DEFAULT_REPLACE_CONFIG : ReplaceConfig = {
40
47
target : '</head>' ,
41
48
}
42
49
43
50
export default class Plugin {
44
- static addStyle ( html : string , style : string , replaceConfig : ReplaceConfig ) {
51
+ static addStyle ( {
52
+ html,
53
+ htmlFileName,
54
+ replaceConfig,
55
+ style,
56
+ } : ConfigOfAddStyle ) {
45
57
const styleString = `<style type="text/css">${ style } </style>`
46
58
const replaceValues = [ styleString , replaceConfig . target ]
47
59
48
60
if ( replaceConfig . position === 'after' ) {
49
61
replaceValues . reverse ( )
50
62
}
51
63
64
+ if ( html . indexOf ( replaceConfig . target ) === - 1 ) {
65
+ throw new Error (
66
+ `Can not inject css style into "${ htmlFileName } ", as there is not replace target "${
67
+ replaceConfig . target
68
+ } "`,
69
+ )
70
+ }
71
+
52
72
return html . replace ( replaceConfig . target , replaceValues . join ( '' ) )
53
73
}
54
74
@@ -108,11 +128,12 @@ export default class Plugin {
108
128
// check if current html needs to be inlined
109
129
if ( this . isCurrentFileNeedsToBeInlined ( data . outputName ) ) {
110
130
data . assets . css . forEach ( ( cssLink ) => {
111
- data . html = Plugin . addStyle (
112
- data . html ,
113
- this . getCSSFile ( cssLink , data . assets . publicPath ) ,
131
+ data . html = Plugin . addStyle ( {
132
+ html : data . html ,
133
+ htmlFileName : data . outputName ,
134
+ style : this . getCSSFile ( cssLink , data . assets . publicPath ) ,
114
135
replaceConfig,
115
- )
136
+ } )
116
137
} )
117
138
118
139
data . html = Plugin . cleanUp ( data . html , replaceConfig )
0 commit comments