@@ -3,13 +3,12 @@ import {compilation, Compiler, Plugin} from 'webpack';
3
3
4
4
declare namespace HtmlWebpackInjectPreload {
5
5
interface Options {
6
- excludeOutputNames : RegExp [ ] ;
7
6
files : HtmlWebpackInjectPreload . File [ ] ;
8
7
}
9
8
10
9
interface File {
11
10
match : RegExp ;
12
- attributes : Record < string , string | boolean > ;
11
+ attributes : Record < string , string | boolean > ;
13
12
}
14
13
}
15
14
@@ -24,11 +23,11 @@ interface HtmlWebpackPluginData {
24
23
*
25
24
* @example
26
25
* new HtmlWebpackInjectPreload({
27
- * excludeOutputNames: [/scripts-hashed/],
28
26
* files: [
29
27
* {
30
28
* 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 },
32
31
* },
33
32
* {
34
33
* match: /vendors\.[a-z-0-9]*.css/,
@@ -40,10 +39,10 @@ interface HtmlWebpackPluginData {
40
39
* @class InjectPreloadFiles
41
40
*/
42
41
class HtmlWebpackInjectPreload implements Plugin {
43
- options : HtmlWebpackInjectPreload . Options = {
44
- excludeOutputNames : [ ] ,
42
+ private options : HtmlWebpackInjectPreload . Options = {
45
43
files : [ ] ,
46
44
} ;
45
+ private replaceString = '<!-- html-webpack-inject-preload -->' ;
47
46
48
47
/**
49
48
* Creates an instance of HtmlWebpackInjectPreload.
@@ -54,12 +53,6 @@ class HtmlWebpackInjectPreload implements Plugin {
54
53
this . options = Object . assign ( this . options , options ) ;
55
54
}
56
55
57
- private checkMatch ( regexpArray : RegExp [ ] , value : string ) {
58
- return regexpArray . some ( function ( regexp ) {
59
- return regexp . test ( value ) ;
60
- } ) ;
61
- }
62
-
63
56
public generateLink ( href : string ) {
64
57
const linkAttributes : string [ ] = [ ] ;
65
58
@@ -73,9 +66,8 @@ class HtmlWebpackInjectPreload implements Plugin {
73
66
}
74
67
75
68
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 ) ) {
79
71
const value = file . attributes [ attribute ] ;
80
72
if ( value === true ) {
81
73
linkAttributes . push ( `${ attribute } ` ) ;
@@ -85,30 +77,23 @@ class HtmlWebpackInjectPreload implements Plugin {
85
77
}
86
78
}
87
79
88
- return linkAttributes . length > 0
89
- ? `<link ${ linkAttributes . join ( ' ' ) } >`
90
- : false ;
80
+ return linkAttributes . length > 0 ?
81
+ `<link ${ linkAttributes . join ( ' ' ) } >` :
82
+ false ;
91
83
}
92
84
}
93
85
94
86
return false ;
95
87
}
96
88
97
89
private addLinks (
98
- compilation : compilation . Compilation ,
99
- htmlPluginData : HtmlWebpackPluginData ,
90
+ compilation : compilation . Compilation ,
91
+ htmlPluginData : HtmlWebpackPluginData ,
100
92
) {
101
- const options = this . options ;
102
93
const links : string [ ] = [ ] ;
103
94
104
95
// 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 ) ) {
112
97
return htmlPluginData ;
113
98
}
114
99
@@ -123,30 +108,32 @@ class HtmlWebpackInjectPreload implements Plugin {
123
108
} ) ;
124
109
125
110
htmlPluginData . html = htmlPluginData . html . replace (
126
- '<!-- html-webpack-inject-preload -->' ,
127
- links . join ( '' ) ,
111
+ this . replaceString ,
112
+ links . join ( '' ) ,
128
113
) ;
129
114
130
115
return htmlPluginData ;
131
116
}
132
117
133
118
apply ( compiler : Compiler ) {
134
119
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 ;
140
127
141
128
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
+ } ,
150
137
) ;
151
138
} ) ;
152
139
}
0 commit comments