@@ -7,6 +7,7 @@ import { PLUGIN_PREFIX } from './constants';
77export type PluginOptions = {
88 scriptMatchPattern ?: RegExp [ ] ;
99 htmlMatchPattern ?: RegExp [ ] ;
10+ assetPreservePattern ?: RegExp [ ] ;
1011} ;
1112
1213class HtmlInlineScriptPlugin implements WebpackPluginInstance {
@@ -18,6 +19,8 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
1819
1920 ignoredHtmlFiles : string [ ] ;
2021
22+ assetPreservePattern : NonNullable < PluginOptions [ 'assetPreservePattern' ] > ;
23+
2124 constructor ( options : PluginOptions = { } ) {
2225 if ( options && Array . isArray ( options ) ) {
2326 // eslint-disable-next-line no-console
@@ -33,12 +36,14 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
3336
3437 const {
3538 scriptMatchPattern = [ / .+ [ . ] j s $ / ] ,
36- htmlMatchPattern = [ / .+ [ . ] h t m l $ / ]
39+ htmlMatchPattern = [ / .+ [ . ] h t m l $ / ] ,
40+ assetPreservePattern = [ ] ,
3741 } = options ;
3842
3943 this . scriptMatchPattern = scriptMatchPattern ;
4044 this . htmlMatchPattern = htmlMatchPattern ;
4145 this . processedScriptFiles = [ ] ;
46+ this . assetPreservePattern = assetPreservePattern ;
4247 this . ignoredHtmlFiles = [ ] ;
4348 }
4449
@@ -48,6 +53,13 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
4853 return this . scriptMatchPattern . some ( ( test ) => assetName . match ( test ) ) ;
4954 }
5055
56+ isFileNeedsToBePreserved (
57+ assetName : string
58+ ) : boolean {
59+ return this . assetPreservePattern . some ( ( test ) => assetName . match ( test ) ) ;
60+ }
61+
62+
5163 shouldProcessHtml (
5264 templateName : string
5365 ) : boolean {
@@ -120,7 +132,9 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
120132 } , ( assets ) => {
121133 if ( this . ignoredHtmlFiles . length === 0 ) {
122134 this . processedScriptFiles . forEach ( ( assetName ) => {
123- delete assets [ assetName ] ;
135+ if ( ! this . isFileNeedsToBePreserved ( assetName ) ) {
136+ delete assets [ assetName ] ;
137+ }
124138 } ) ;
125139 }
126140 } ) ;
0 commit comments