Skip to content

Commit 76d7b8d

Browse files
committed
feat: throw error if there is not replace target
1 parent f2edce1 commit 76d7b8d

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/index.ts

+26-5
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,39 @@ interface Config {
3636
replace?: ReplaceConfig
3737
}
3838

39+
interface ConfigOfAddStyle {
40+
html: string
41+
htmlFileName: string
42+
style: string
43+
replaceConfig: ReplaceConfig
44+
}
45+
3946
const DEFAULT_REPLACE_CONFIG: ReplaceConfig = {
4047
target: '</head>',
4148
}
4249

4350
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) {
4557
const styleString = `<style type="text/css">${style}</style>`
4658
const replaceValues = [styleString, replaceConfig.target]
4759

4860
if (replaceConfig.position === 'after') {
4961
replaceValues.reverse()
5062
}
5163

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+
5272
return html.replace(replaceConfig.target, replaceValues.join(''))
5373
}
5474

@@ -108,11 +128,12 @@ export default class Plugin {
108128
// check if current html needs to be inlined
109129
if (this.isCurrentFileNeedsToBeInlined(data.outputName)) {
110130
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),
114135
replaceConfig,
115-
)
136+
})
116137
})
117138

118139
data.html = Plugin.cleanUp(data.html, replaceConfig)

0 commit comments

Comments
 (0)