@@ -7,6 +7,7 @@ import { PLUGIN_PREFIX } from './constants';
7
7
export type PluginOptions = {
8
8
scriptMatchPattern ?: RegExp [ ] ;
9
9
htmlMatchPattern ?: RegExp [ ] ;
10
+ assetPreservePattern ?: RegExp [ ] ;
10
11
} ;
11
12
12
13
class HtmlInlineScriptPlugin implements WebpackPluginInstance {
@@ -18,6 +19,8 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
18
19
19
20
ignoredHtmlFiles : string [ ] ;
20
21
22
+ assetPreservePattern : NonNullable < PluginOptions [ 'assetPreservePattern' ] > ;
23
+
21
24
constructor ( options : PluginOptions = { } ) {
22
25
if ( options && Array . isArray ( options ) ) {
23
26
// eslint-disable-next-line no-console
@@ -33,12 +36,14 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
33
36
34
37
const {
35
38
scriptMatchPattern = [ / .+ [ . ] j s $ / ] ,
36
- htmlMatchPattern = [ / .+ [ . ] h t m l $ / ]
39
+ htmlMatchPattern = [ / .+ [ . ] h t m l $ / ] ,
40
+ assetPreservePattern = [ ] ,
37
41
} = options ;
38
42
39
43
this . scriptMatchPattern = scriptMatchPattern ;
40
44
this . htmlMatchPattern = htmlMatchPattern ;
41
45
this . processedScriptFiles = [ ] ;
46
+ this . assetPreservePattern = assetPreservePattern ;
42
47
this . ignoredHtmlFiles = [ ] ;
43
48
}
44
49
@@ -48,6 +53,13 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
48
53
return this . scriptMatchPattern . some ( ( test ) => assetName . match ( test ) ) ;
49
54
}
50
55
56
+ isFileNeedsToBePreserved (
57
+ assetName : string
58
+ ) : boolean {
59
+ return this . assetPreservePattern . some ( ( test ) => assetName . match ( test ) ) ;
60
+ }
61
+
62
+
51
63
shouldProcessHtml (
52
64
templateName : string
53
65
) : boolean {
@@ -120,7 +132,9 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
120
132
} , ( assets ) => {
121
133
if ( this . ignoredHtmlFiles . length === 0 ) {
122
134
this . processedScriptFiles . forEach ( ( assetName ) => {
123
- delete assets [ assetName ] ;
135
+ if ( ! this . isFileNeedsToBePreserved ( assetName ) ) {
136
+ delete assets [ assetName ] ;
137
+ }
124
138
} ) ;
125
139
}
126
140
} ) ;
0 commit comments