Skip to content

Commit 04f56e1

Browse files
committed
fix: proper include/exclude logic
1 parent 8043966 commit 04f56e1

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/index.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ class PreloadPlugin {
2727

2828
generateLinks (compilation, htmlPluginData) {
2929
const options = this.options
30-
const htmlFilename = htmlPluginData.plugin.options.filename
31-
32-
// Bail out early if we're configured to exclude this HTML file.
33-
if (options.excludeHtmlNames.includes(htmlFilename)) {
34-
return htmlPluginData
35-
}
36-
37-
if (options.includeHtmlNames && !(options.includeHtmlNames.includes(htmlFilename))) {
38-
return htmlPluginData
39-
}
40-
4130
const extractedChunks = extractChunks({
4231
compilation,
4332
optionsInclude: options.include
@@ -109,29 +98,42 @@ class PreloadPlugin {
10998
}
11099

111100
apply (compiler) {
101+
const skip = data => {
102+
const htmlFilename = data.plugin.options.filename
103+
const exclude = this.options.excludeHtmlNames
104+
const include = this.options.includeHtmlNames
105+
return (
106+
(include && !(include.includes(htmlFilename))) ||
107+
(exclude && exclude.includes(htmlFilename))
108+
)
109+
}
110+
112111
compiler.hooks.compilation.tap(
113112
this.constructor.name,
114113
compilation => {
115-
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync(
114+
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tap(
116115
this.constructor.name,
117-
(htmlPluginData, callback) => {
118-
try {
119-
callback(null, this.generateLinks(compilation, htmlPluginData))
120-
} catch (error) {
121-
callback(error)
116+
(htmlPluginData) => {
117+
if (skip(htmlPluginData)) {
118+
return
122119
}
120+
this.generateLinks(compilation, htmlPluginData)
123121
}
124122
)
125123

126124
compilation.hooks.htmlWebpackPluginAlterAssetTags.tap(
127125
this.constructor.name,
128126
(htmlPluginData) => {
127+
if (skip(htmlPluginData)) {
128+
return
129+
}
129130
if (this.resourceHints) {
130131
htmlPluginData.head = [
131132
...this.resourceHints,
132133
...htmlPluginData.head
133134
]
134135
}
136+
return htmlPluginData
135137
}
136138
)
137139
}

0 commit comments

Comments
 (0)