Skip to content

Commit 34ab1ca

Browse files
wadahiroSimenB
authored andcommitted
Add publicPath option (#9)
1 parent da0d4a7 commit 34ab1ca

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ Type: `boolean`, default: `false`
8484

8585
If `true`, will add `filename + '.map'` to the compilation as well.
8686

87+
#### `publicPath`
88+
Type: `string`
89+
90+
If set, will be used as the public path of the script or link tag.
91+
8792
#### `typeOfAsset`
8893
Type: `string`, default: `js`
8994

addAssetHtmlPlugin.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ import crypto from 'crypto';
33
import Promise from 'bluebird';
44

55
// Copied from html-webpack-plugin
6-
function resolvePublicPath(compilation, filename) {
7-
let publicPath = typeof compilation.options.output.publicPath !== 'undefined'
8-
? compilation.options.output.publicPath
9-
: path.relative(path.dirname(filename), '.');
6+
function resolvePublicPath(compilation, filename, publicPath) {
7+
let resolvedPublicPath;
8+
if (typeof publicPath === 'undefined') {
9+
resolvedPublicPath = typeof compilation.options.output.publicPath !== 'undefined'
10+
? compilation.options.output.publicPath
11+
: path.relative(path.dirname(filename), '.');
12+
} else {
13+
resolvedPublicPath = publicPath;
14+
}
1015

11-
if (publicPath.length && publicPath.substr(-1, 1) !== '/') {
12-
publicPath += '/';
16+
if (resolvedPublicPath.length && resolvedPublicPath.substr(-1, 1) !== '/') {
17+
resolvedPublicPath += '/';
1318
}
14-
return publicPath;
19+
return resolvedPublicPath;
1520
}
1621

17-
function addFileToAssets(htmlPluginData, compilation, { filename, typeOfAsset = 'js', includeSourcemap = true, hash = false } = {}) {
22+
function addFileToAssets(htmlPluginData, compilation,
23+
{ filename, typeOfAsset = 'js', includeSourcemap = true, hash = false, publicPath } = {}) {
1824
if (!filename) return compilation.errors.push(new Error('No filename defined'));
1925

2026
return htmlPluginData.plugin.addFileToAssets(filename, compilation)
@@ -25,7 +31,8 @@ function addFileToAssets(htmlPluginData, compilation, { filename, typeOfAsset =
2531
md5.update(compilation.assets[addedFilename].source());
2632
suffix = `?${md5.digest('hex').substr(0, 20)}`;
2733
}
28-
return htmlPluginData.assets[typeOfAsset].unshift(`${resolvePublicPath(compilation, addedFilename)}${addedFilename}${suffix}`);
34+
return htmlPluginData.assets[typeOfAsset]
35+
.unshift(`${resolvePublicPath(compilation, addedFilename, publicPath)}${addedFilename}${suffix}`);
2936
})
3037
.then(() => {
3138
if (includeSourcemap) {

0 commit comments

Comments
 (0)