Skip to content

Commit 5ea7de4

Browse files
committed
feat: allow to set publicPath to empty string ’’
BREAKING CHANGE: Setting publicPath to an empty string will no longer calculate a relative path from the html file to the assets anymore. To keep the old behaviour set the publicPath to ‘auto’
1 parent 6593cd4 commit 5ea7de4

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,8 @@ function hookIntoCompiler (compiler, options, plugin) {
565565
*/
566566
const webpackPublicPath = compilation.getAssetPath(compilation.outputOptions.publicPath, { hash: compilationHash });
567567

568-
// Webpack 5 introduced "auto" - however it can not be retrieved at compile time
569-
const isPublicPathDefined = webpackPublicPath.trim() !== '' && webpackPublicPath !== 'auto';
568+
// Webpack 5 introduced "auto" as default value
569+
const isPublicPathDefined = webpackPublicPath !== 'auto';
570570

571571
let publicPath =
572572
// If the html-webpack-plugin options contain a custom public path uset it

spec/basic.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,37 @@ describe('HtmlWebpackPlugin', () => {
843843
}, ['<script defer="defer" src="/assets/index_bundle.js"'], null, done);
844844
});
845845

846+
it('allows to set public path to an empty string', done => {
847+
testHtmlPlugin({
848+
mode: 'production',
849+
entry: path.join(__dirname, 'fixtures/index.js'),
850+
output: {
851+
path: OUTPUT_DIR,
852+
filename: 'assets/index_bundle.js',
853+
publicPath: ''
854+
},
855+
plugins: [new HtmlWebpackPlugin({
856+
filename: 'foo/index.html'
857+
})]
858+
}, ['<script defer="defer" src="assets/index_bundle.js"'], 'foo/index.html', done);
859+
});
860+
861+
it('allows to set the html-webpack-plugin public path to an empty string', done => {
862+
testHtmlPlugin({
863+
mode: 'production',
864+
entry: path.join(__dirname, 'fixtures/index.js'),
865+
output: {
866+
path: OUTPUT_DIR,
867+
filename: 'assets/index_bundle.js',
868+
publicPath: '/'
869+
},
870+
plugins: [new HtmlWebpackPlugin({
871+
filename: 'foo/index.html',
872+
publicPath: ''
873+
})]
874+
}, ['<script defer="defer" src="assets/index_bundle.js"'], 'foo/index.html', done);
875+
});
876+
846877
it('handles subdirectories in the webpack output bundles along with a relative path', done => {
847878
testHtmlPlugin({
848879
mode: 'production',

0 commit comments

Comments
 (0)