Skip to content

Commit 8063523

Browse files
authored
Merge pull request #21 from principalstudio:Applelo/issue20
Fix public path on webpack 4 version
2 parents 1f7549c + d8a1ed0 commit 8063523

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/main.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ class HtmlWebpackInjectPreload implements WebpackPluginInstance {
8282
compilation: Compilation,
8383
htmlPluginData: HtmlWebpackPluginData,
8484
) {
85+
//Get public path
86+
//html-webpack-plugin v5
87+
let publicPath = htmlPluginData.publicPath;
88+
89+
//html-webpack-plugin v4
90+
if (typeof publicPath === 'undefined') {
91+
if (
92+
htmlPluginData.plugin.options?.publicPath &&
93+
htmlPluginData.plugin.options?.publicPath !== 'auto'
94+
) {
95+
publicPath = htmlPluginData.plugin.options?.publicPath;
96+
} else {
97+
publicPath =
98+
typeof compilation.options.output.publicPath === 'string'
99+
? compilation.options.output.publicPath
100+
: '/';
101+
}
102+
103+
//prevent wrong url
104+
if (publicPath[publicPath.length - 1] !== '/') {
105+
publicPath = publicPath + '/';
106+
}
107+
}
108+
85109
//Get assets name
86110
const assets = new Set(Object.keys(compilation.assets));
87111
compilation.chunks.forEach(chunk => {
@@ -105,7 +129,7 @@ class HtmlWebpackInjectPreload implements WebpackPluginInstance {
105129
if (href === false || typeof href === 'undefined') {
106130
href = asset;
107131
}
108-
href = href[0] === '/' ? href : htmlPluginData.publicPath + href;
132+
href = href[0] === '/' ? href : publicPath + href;
109133

110134
const preload: HtmlTagObject = {
111135
tagName: 'link',

0 commit comments

Comments
 (0)