Skip to content

Commit

Permalink
feat: allow to set publicPath to empty string ’’
Browse files Browse the repository at this point in the history
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’
  • Loading branch information
jantimon committed Feb 3, 2021
1 parent b109213 commit 7c3146d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -565,8 +565,8 @@ function hookIntoCompiler (compiler, options, plugin) {
*/
const webpackPublicPath = compilation.getAssetPath(compilation.outputOptions.publicPath, { hash: compilationHash });

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

let publicPath =
// If the html-webpack-plugin options contain a custom public path uset it
Expand Down
31 changes: 31 additions & 0 deletions spec/basic.spec.js
Expand Up @@ -843,6 +843,37 @@ describe('HtmlWebpackPlugin', () => {
}, ['<script defer="defer" src="/assets/index_bundle.js"'], null, done);
});

it('allows to set public path to an empty string', done => {
testHtmlPlugin({
mode: 'production',
entry: path.join(__dirname, 'fixtures/index.js'),
output: {
path: OUTPUT_DIR,
filename: 'assets/index_bundle.js',
publicPath: ''
},
plugins: [new HtmlWebpackPlugin({
filename: 'foo/index.html'
})]
}, ['<script defer="defer" src="assets/index_bundle.js"'], 'foo/index.html', done);
});

it('allows to set the html-webpack-plugin public path to an empty string', done => {
testHtmlPlugin({
mode: 'production',
entry: path.join(__dirname, 'fixtures/index.js'),
output: {
path: OUTPUT_DIR,
filename: 'assets/index_bundle.js',
publicPath: '/'
},
plugins: [new HtmlWebpackPlugin({
filename: 'foo/index.html',
publicPath: ''
})]
}, ['<script defer="defer" src="assets/index_bundle.js"'], 'foo/index.html', done);
});

it('handles subdirectories in the webpack output bundles along with a relative path', done => {
testHtmlPlugin({
mode: 'production',
Expand Down

0 comments on commit 7c3146d

Please sign in to comment.