Skip to content

Commit a153f49

Browse files
committed
close #2
1 parent 6048acf commit a153f49

File tree

2 files changed

+34
-48
lines changed

2 files changed

+34
-48
lines changed

src/main.ts

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import {compilation, Compiler, Plugin} from 'webpack';
33

44
declare namespace HtmlWebpackInjectPreload {
55
interface Options {
6-
excludeOutputNames: RegExp[];
76
files: HtmlWebpackInjectPreload.File[];
87
}
98

109
interface File {
1110
match: RegExp;
12-
attributes: Record<string, string | boolean>;
11+
attributes: Record<string, string|boolean>;
1312
}
1413
}
1514

@@ -24,11 +23,11 @@ interface HtmlWebpackPluginData {
2423
*
2524
* @example
2625
* new HtmlWebpackInjectPreload({
27-
* excludeOutputNames: [/scripts-hashed/],
2826
* files: [
2927
* {
3028
* match: /.*\.woff2/,
31-
* attributes: { rel: 'preload', as: 'font', type: 'font/woff2', crossorigin: true },
29+
* attributes: { rel: 'preload', as: 'font', type: 'font/woff2',
30+
* crossorigin: true },
3231
* },
3332
* {
3433
* match: /vendors\.[a-z-0-9]*.css/,
@@ -40,10 +39,10 @@ interface HtmlWebpackPluginData {
4039
* @class InjectPreloadFiles
4140
*/
4241
class HtmlWebpackInjectPreload implements Plugin {
43-
options: HtmlWebpackInjectPreload.Options = {
44-
excludeOutputNames: [],
42+
private options: HtmlWebpackInjectPreload.Options = {
4543
files: [],
4644
};
45+
private replaceString = '<!-- html-webpack-inject-preload -->';
4746

4847
/**
4948
* Creates an instance of HtmlWebpackInjectPreload.
@@ -54,12 +53,6 @@ class HtmlWebpackInjectPreload implements Plugin {
5453
this.options = Object.assign(this.options, options);
5554
}
5655

57-
private checkMatch(regexpArray: RegExp[], value: string) {
58-
return regexpArray.some(function (regexp) {
59-
return regexp.test(value);
60-
});
61-
}
62-
6356
public generateLink(href: string) {
6457
const linkAttributes: string[] = [];
6558

@@ -73,9 +66,8 @@ class HtmlWebpackInjectPreload implements Plugin {
7366
}
7467

7568
for (const attribute in file.attributes) {
76-
if (
77-
Object.prototype.hasOwnProperty.call(file.attributes, attribute)
78-
) {
69+
if (Object.prototype.hasOwnProperty.call(
70+
file.attributes, attribute)) {
7971
const value = file.attributes[attribute];
8072
if (value === true) {
8173
linkAttributes.push(`${attribute}`);
@@ -85,30 +77,23 @@ class HtmlWebpackInjectPreload implements Plugin {
8577
}
8678
}
8779

88-
return linkAttributes.length > 0
89-
? `<link ${linkAttributes.join(' ')}>`
90-
: false;
80+
return linkAttributes.length > 0 ?
81+
`<link ${linkAttributes.join(' ')}>` :
82+
false;
9183
}
9284
}
9385

9486
return false;
9587
}
9688

9789
private addLinks(
98-
compilation: compilation.Compilation,
99-
htmlPluginData: HtmlWebpackPluginData,
90+
compilation: compilation.Compilation,
91+
htmlPluginData: HtmlWebpackPluginData,
10092
) {
101-
const options = this.options;
10293
const links: string[] = [];
10394

10495
// Bail out early if we're configured to exclude this file.
105-
if (
106-
this.checkMatch(
107-
options.excludeOutputNames,
108-
// @ts-ignore
109-
htmlPluginData.plugin.options.filename,
110-
)
111-
) {
96+
if (!htmlPluginData.html.includes(this.replaceString)) {
11297
return htmlPluginData;
11398
}
11499

@@ -123,30 +108,32 @@ class HtmlWebpackInjectPreload implements Plugin {
123108
});
124109

125110
htmlPluginData.html = htmlPluginData.html.replace(
126-
'<!-- html-webpack-inject-preload -->',
127-
links.join(''),
111+
this.replaceString,
112+
links.join(''),
128113
);
129114

130115
return htmlPluginData;
131116
}
132117

133118
apply(compiler: Compiler) {
134119
compiler.hooks.compilation.tap('HtmlWebpackInjectPreload', compilation => {
135-
// @ts-ignore
136-
const hook = compilation.hooks.htmlWebpackPluginAfterHtmlProcessing
137-
? // @ts-ignore
138-
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing
139-
: HtmlWebpackPlugin.getHooks(compilation).beforeEmit;
120+
const hook = compilation
121+
.hooks
122+
// @ts-ignore
123+
.htmlWebpackPluginAfterHtmlProcessing ?
124+
// @ts-ignore
125+
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing :
126+
HtmlWebpackPlugin.getHooks(compilation).beforeEmit;
140127

141128
hook.tapAsync(
142-
'HtmlWebpackInjectPreload',
143-
(htmlPluginData: HtmlWebpackPluginData, callback: any) => {
144-
try {
145-
callback(null, this.addLinks(compilation, htmlPluginData));
146-
} catch (error) {
147-
callback(error);
148-
}
149-
},
129+
'HtmlWebpackInjectPreload',
130+
(htmlPluginData: HtmlWebpackPluginData, callback: any) => {
131+
try {
132+
callback(null, this.addLinks(compilation, htmlPluginData));
133+
} catch (error) {
134+
callback(error);
135+
}
136+
},
150137
);
151138
});
152139
}

test/index.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import HtmlWebpackInjectPreload from '../src/main';
22

33
const options: HtmlWebpackInjectPreload.Options = {
4-
excludeOutputNames: [/scripts-hashed/],
54
files: [
65
{
76
match: /.*\.woff2/,
@@ -27,20 +26,20 @@ describe('HTMLWebpackInjectPreload', () => {
2726
it('generateLink', async () => {
2827
const expectCss = '<link rel="preload" as="style" href="test-alt.css">';
2928
const linkCss = new HtmlWebpackInjectPreload(options).generateLink(
30-
'test.css',
29+
'test.css',
3130
);
3231
expect(linkCss).toBe(expectCss);
3332

3433
const expectWoff2 =
35-
'<link href="test.woff2" rel="preload" as="font" type="font/woff2" crossorigin>';
34+
'<link href="test.woff2" rel="preload" as="font" type="font/woff2" crossorigin>';
3635
const linkWoff2 = new HtmlWebpackInjectPreload(options).generateLink(
37-
'test.woff2',
36+
'test.woff2',
3837
);
3938
expect(linkWoff2).toBe(expectWoff2);
4039

4140
const expectNull = false;
4241
const linkNull = new HtmlWebpackInjectPreload(options).generateLink(
43-
'test.null',
42+
'test.null',
4443
);
4544
expect(linkNull).toBe(expectNull);
4645
});

0 commit comments

Comments
 (0)