@@ -9,24 +9,48 @@ import { HtmlData } from '../../html';
9
9
10
10
interface HtmlInjectOption {
11
11
data : HtmlData ;
12
- htmlXmlMode : boolean ;
12
+ htmlXmlMode ?: boolean ;
13
+ cors ?: boolean ;
13
14
}
14
15
15
16
class HtmlInjectPlugin {
16
17
private data : HtmlData ;
18
+ private cors : boolean ;
17
19
18
20
public constructor ( options : HtmlInjectOption ) {
19
21
this . data = options . data ;
22
+ this . cors = ! ! options . cors ;
20
23
}
21
24
22
25
public apply ( compiler : webpack . Compiler ) {
23
26
compiler . hooks . compilation . tap (
24
27
'HtmlInjectPlugin' ,
25
28
( compilation ) => {
29
+ // @ts -ignore
30
+ if ( this . cors && compilation . hooks . htmlWebpackPluginAlterAssetTags ) {
31
+ // @ts -ignore
32
+ compilation . hooks . htmlWebpackPluginAlterAssetTags . tap ( "HtmlInjectPlugin" , ( htmlPluginData , callback ) => {
33
+ if ( htmlPluginData . body ?. length ) {
34
+ htmlPluginData . body . forEach ( ( tag : any ) => {
35
+ if ( tag . tagName === 'script' ) {
36
+ tag . attributes . crossorigin = 'anonymous' ;
37
+ }
38
+ } ) ;
39
+ }
40
+
41
+ if ( typeof callback === 'function' ) {
42
+ callback ( null , htmlPluginData ) ;
43
+ } else {
44
+ return htmlPluginData ;
45
+ }
46
+ } ) ;
47
+ }
48
+
26
49
// @ts -ignore
27
50
if ( ! compilation . hooks . htmlWebpackPluginAfterHtmlProcessing ) {
28
51
return ;
29
52
}
53
+
30
54
// @ts -ignore
31
55
compilation . hooks . htmlWebpackPluginBeforeHtmlProcessing . tapAsync ( 'HtmlInjectPlugin' , ( data , callback ) => {
32
56
const $ = cheerio . load ( data . html , {
@@ -85,7 +109,7 @@ class HtmlInjectPlugin {
85
109
}
86
110
}
87
111
88
- export function htmlInjectPlugin ( config : Chain , options : { [ key : string ] : any } ) {
112
+ export function htmlInjectPlugin ( config : Chain , options : HtmlInjectOption ) {
89
113
createPlugin (
90
114
config ,
91
115
'HtmlInjectPlugin' ,
0 commit comments