File tree 2 files changed +26
-5
lines changed
2 files changed +26
-5
lines changed Original file line number Diff line number Diff line change 31
31
},
32
32
"peerDependencies" : {
33
33
"webpack" : " ^2 || ^3 || ^4" ,
34
- "html-webpack-plugin" : " ^2 || ^3"
34
+ "html-webpack-plugin" : " ^2 || ^3 || ^4 "
35
35
},
36
36
"devDependencies" : {
37
37
"codecov" : " ^3.1.0" ,
Original file line number Diff line number Diff line change @@ -6,6 +6,18 @@ const flatten = require('lodash/flatten');
6
6
const isFunction = require ( 'lodash/isFunction' ) ;
7
7
const get = require ( 'lodash/get' ) ;
8
8
9
+ // Attempt to load HtmlWebpackPlugin@4
10
+ // Borrowed from https://github.com/waysact/webpack-subresource-integrity/blob/master/index.js
11
+ let HtmlWebpackPlugin ;
12
+ try {
13
+ // eslint-disable-next-line global-require
14
+ HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
15
+ } catch ( e ) {
16
+ if ( ! ( e instanceof Error ) || e . code !== 'MODULE_NOT_FOUND' ) {
17
+ throw e ;
18
+ }
19
+ }
20
+
9
21
const defaultPolicy = {
10
22
'base-uri' : "'self'" ,
11
23
'object-src' : "'none'" ,
@@ -188,10 +200,19 @@ class CspHtmlWebpackPlugin {
188
200
apply ( compiler ) {
189
201
if ( compiler . hooks ) {
190
202
compiler . hooks . compilation . tap ( 'CspHtmlWebpackPlugin' , compilation => {
191
- compilation . hooks . htmlWebpackPluginAfterHtmlProcessing . tapAsync (
192
- 'CspHtmlWebpackPlugin' ,
193
- this . processCsp . bind ( this )
194
- ) ;
203
+ if ( HtmlWebpackPlugin && HtmlWebpackPlugin . getHooks ) {
204
+ // HTMLWebpackPlugin@4
205
+ HtmlWebpackPlugin . getHooks ( compilation ) . beforeEmit . tapAsync (
206
+ 'CspHtmlWebpackPlugin' ,
207
+ this . processCsp . bind ( this )
208
+ ) ;
209
+ } else {
210
+ // HTMLWebpackPlugin@3
211
+ compilation . hooks . htmlWebpackPluginAfterHtmlProcessing . tapAsync (
212
+ 'CspHtmlWebpackPlugin' ,
213
+ this . processCsp . bind ( this )
214
+ ) ;
215
+ }
195
216
} ) ;
196
217
} else {
197
218
compiler . plugin ( 'compilation' , compilation => {
You can’t perform that action at this time.
0 commit comments