Skip to content

Commit cfbae5b

Browse files
committed
Use public path
1 parent 735c204 commit cfbae5b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

addAssetHtmlPlugin.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
import path from 'path'
2+
3+
// Copied from html-webpack-plugin
4+
function resolvePublicPath (compilation, filename) {
5+
let publicPath = typeof compilation.options.output.publicPath !== 'undefined'
6+
? compilation.options.output.publicPath
7+
: path.relative(path.dirname(filename), '.')
8+
9+
if (publicPath.length && publicPath.substr(-1, 1) !== '/') {
10+
publicPath += '/'
11+
}
12+
return publicPath
13+
}
14+
115
export default class AddAssetHtmlPlugin {
216
constructor ({
317
filename,
@@ -13,9 +27,11 @@ export default class AddAssetHtmlPlugin {
1327
compiler.plugin('compilation', (compilation) => {
1428
if (!this.filename) return compilation.errors.push(new Error('No filename defined'))
1529

30+
const publicPath = resolvePublicPath(compilation, this.filename)
31+
1632
compilation.plugin('html-webpack-plugin-before-html-processing', (htmlPluginData, callback) => {
1733
htmlPluginData.plugin.addFileToAssets(this.filename, compilation)
18-
.then((filename) => htmlPluginData.assets[this.typeOfAsset].unshift(`/${filename}`))
34+
.then((filename) => htmlPluginData.assets[this.typeOfAsset].unshift(`${publicPath}${filename}`))
1935
.then(() => {
2036
if (this.includeSourcemap) {
2137
return htmlPluginData.plugin.addFileToAssets(`${this.filename}.map`, compilation)

0 commit comments

Comments
 (0)