Skip to content

Commit 19b5122

Browse files
authored
feat: Add publicPath option to overrule the default path generation (#1516)
1 parent f3ccdd5 commit 19b5122

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ Allowed values are as follows
138138
|**`templateContent`**|`{string\|Function\|false}`|false| Can be used instead of `template` to provide an inline template - please read the [Writing Your Own Templates](https://github.com/jantimon/html-webpack-plugin#writing-your-own-templates) section |
139139
|**`templateParameters`**|`{Boolean\|Object\|Function}`| `false`| Allows to overwrite the parameters used in the template - see [example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/template-parameters) |
140140
|**`inject`**|`{Boolean\|String}`|`true`|`true \|\| 'head' \|\| 'body' \|\| false` Inject all assets into the given `template` or `templateContent`. When passing `true` or `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element - see the [inject:false example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/custom-insertion-position)|
141+
|**`publicPath`**|`{String|'auto'}`|`'auto'`|The publicPath used for script and link tags|
141142
|**`scriptLoading`**|`{'blocking'\|'defer'}`|`'blocking'`| Modern browsers support non blocking javascript loading (`'defer'`) to improve the page startup performance. |
142143
|**`favicon`**|`{String}`|``|Adds the given favicon path to the output HTML|
143144
|**`meta`**|`{Object}`|`{}`|Allows to inject `meta`-tags. E.g. `meta: {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`|

index.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class HtmlWebpackPlugin {
4444
templateContent: false,
4545
templateParameters: templateParametersGenerator,
4646
filename: 'index.html',
47+
publicPath: userOptions.publicPath === undefined ? 'auto' : userOptions.publicPath,
4748
hash: false,
4849
inject: userOptions.scriptLoading !== 'defer' ? 'body' : 'head',
4950
scriptLoading: 'blocking',
@@ -167,7 +168,7 @@ class HtmlWebpackPlugin {
167168
const isCompilationCached = templateResult.mainCompilationHash !== compilation.hash;
168169

169170
// Turn the entry point names into file paths
170-
const assets = self.htmlWebpackPluginAssets(compilation, childCompilationOutputName, sortedEntryNames);
171+
const assets = self.htmlWebpackPluginAssets(compilation, childCompilationOutputName, sortedEntryNames, this.options.publicPath);
171172

172173
// If the template and the assets did not change we don't have to emit the html
173174
const assetJson = JSON.stringify(self.getAssetFiles(assets));
@@ -519,6 +520,7 @@ class HtmlWebpackPlugin {
519520
* for all given entry names
520521
* @param {WebpackCompilation} compilation
521522
* @param {string[]} entryNames
523+
* @param {string | 'auto'} customPublicPath
522524
* @returns {{
523525
publicPath: string,
524526
js: Array<string>,
@@ -527,7 +529,7 @@ class HtmlWebpackPlugin {
527529
favicon?: string
528530
}}
529531
*/
530-
htmlWebpackPluginAssets (compilation, childCompilationOutputName, entryNames) {
532+
htmlWebpackPluginAssets (compilation, childCompilationOutputName, entryNames, customPublicPath) {
531533
const compilationHash = compilation.hash;
532534

533535
/**
@@ -539,13 +541,22 @@ class HtmlWebpackPlugin {
539541
? compilation.mainTemplate.getPublicPath({ hash: compilationHash })
540542
: compilation.getAssetPath(compilation.outputOptions.publicPath, { hash: compilationHash });
541543

542-
const isPublicPathDefined = webpackPublicPath.trim() !== '';
543-
let publicPath = isPublicPathDefined
544-
// If a hard coded public path exists use it
545-
? webpackPublicPath
546-
// If no public path was set get a relative url path
547-
: path.relative(path.resolve(compilation.options.output.path, path.dirname(childCompilationOutputName)), compilation.options.output.path)
548-
.split(path.sep).join('/');
544+
const isPublicPathDefined = webpackMajorVersion === 4
545+
? webpackPublicPath.trim() !== ''
546+
// Webpack 5 introduced "auto" - however it can not be retrieved at runtime
547+
: webpackPublicPath.trim() !== '' && webpackPublicPath !== 'auto';
548+
549+
let publicPath =
550+
// If the html-webpack-plugin options contain a custom public path uset it
551+
customPublicPath !== 'auto'
552+
? customPublicPath
553+
: (isPublicPathDefined
554+
// If a hard coded public path exists use it
555+
? webpackPublicPath
556+
// If no public path was set get a relative url path
557+
: path.relative(path.resolve(compilation.options.output.path, path.dirname(childCompilationOutputName)), compilation.options.output.path)
558+
.split(path.sep).join('/')
559+
);
549560

550561
if (publicPath.length && publicPath.substr(-1, 1) !== '/') {
551562
publicPath += '/';

typings.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ declare namespace HtmlWebpackPlugin {
6363
* @default 'index.html'
6464
*/
6565
filename: string;
66+
/**
67+
* By default the public path is set to `auto` - that way the html-webpack-plugin will try
68+
* to set the publicPath according to the current filename and the webpack publicPath setting
69+
*/
70+
publicPath: string | 'auto';
6671
/**
6772
* If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files.
6873
* This is useful for cache busting

0 commit comments

Comments
 (0)