1
+ import path from 'path' ;
1
2
import { Compilation } from 'webpack' ;
2
3
import type { Compiler , WebpackPluginInstance } from 'webpack' ;
3
4
import htmlWebpackPlugin from 'html-webpack-plugin' ;
@@ -37,7 +38,7 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
37
38
const {
38
39
scriptMatchPattern = [ / .+ [ . ] j s $ / ] ,
39
40
htmlMatchPattern = [ / .+ [ . ] h t m l $ / ] ,
40
- assetPreservePattern = [ ] ,
41
+ assetPreservePattern = [ ]
41
42
} = options ;
42
43
43
44
this . scriptMatchPattern = scriptMatchPattern ;
@@ -59,7 +60,6 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
59
60
return this . assetPreservePattern . some ( ( test ) => assetName . match ( test ) ) ;
60
61
}
61
62
62
-
63
63
shouldProcessHtml (
64
64
templateName : string
65
65
) : boolean {
@@ -102,18 +102,48 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
102
102
} ;
103
103
}
104
104
105
- apply ( compiler : Compiler ) : void {
106
- let publicPath = compiler . options ?. output ?. publicPath as string || '' ;
105
+ getPublicPath (
106
+ compilation : Compilation ,
107
+ htmlFileName : string ,
108
+ customPublicPath : string
109
+ ) : string {
110
+ const webpackPublicPath = compilation . getAssetPath (
111
+ compilation . outputOptions . publicPath as string ,
112
+ { hash : compilation . hash }
113
+ ) ;
114
+ // Webpack 5 introduced "auto" as default value
115
+ const isPublicPathDefined = webpackPublicPath !== 'auto' ;
116
+
117
+ let publicPath = '' ;
118
+
119
+ if ( customPublicPath !== 'auto' ) {
120
+ // If the html-webpack-plugin options contain a custom public path uset it
121
+ publicPath = customPublicPath ;
122
+ } else if ( isPublicPathDefined ) {
123
+ // If a hard coded public path exists in webpack config use it
124
+ publicPath = webpackPublicPath ;
125
+ } else if ( compilation . options . output . path ) {
126
+ // If no public path for webpack and html-webpack-plugin was set get a relative url path
127
+ publicPath = path . relative (
128
+ path . resolve ( compilation . options . output . path , path . dirname ( htmlFileName ) ) ,
129
+ compilation . options . output . path
130
+ ) . split ( path . sep ) . join ( '/' ) ;
131
+ }
107
132
108
133
if ( publicPath && ! publicPath . endsWith ( '/' ) ) {
109
134
publicPath += '/' ;
110
135
}
111
136
137
+ return publicPath ;
138
+ }
139
+
140
+ apply ( compiler : Compiler ) : void {
112
141
compiler . hooks . compilation . tap ( `${ PLUGIN_PREFIX } _compilation` , ( compilation ) => {
113
142
const hooks = htmlWebpackPlugin . getHooks ( compilation ) ;
114
143
115
144
hooks . alterAssetTags . tap ( `${ PLUGIN_PREFIX } _alterAssetTags` , ( data ) => {
116
145
const htmlFileName = data . plugin . options ?. filename ;
146
+ const publicPath = this . getPublicPath ( compilation , data . outputName , data . publicPath ) ;
117
147
118
148
if ( htmlFileName && ! this . shouldProcessHtml ( htmlFileName ) ) {
119
149
this . ignoredHtmlFiles . push ( htmlFileName ) ;
0 commit comments