jharris4 / html-webpack-tags-plugin

lets you define html tags to inject with html-webpack-plugin
MIT License
256 stars 31 forks source link

A bug when using protocol-relative paths in assets #41

Closed huruji closed 5 years ago

huruji commented 5 years ago

I want to inject a script which via HTTPS when needed:

{
    plugins: [
        new HtmlWebpackPlugin({
            template
        }),
        new HtmlWebpackIncludeAssetsPlugin({
            assets: [
                {
                    path: '//polyfill.alicdn.com/polyfill.min.js',
                    type: 'js',
                    attributes: {
                        crossorigin: 'anonymous'
                    }
                }
            ],
            resolvePaths: false,
            append: false
        })
    ]
}

But the .html output added a /, the link was ///polyfill.alicdn.com/polyfill.min.js, It was wrong.

image

Env

html-webpack-include-assets-plugin    v1.0.7

html-webpack-plugin        v3.2.0

webpack     v4.28.2
jharris4 commented 5 years ago

Oh interesting, I did not even know protocol relative urls existed! (https://www.paulirish.com/2010/the-protocol-relative-url/)

I'll have a look and see if there's an easy fix. Of course, a PR would be welcome if you'd like to have a look yourself... ;-)

huruji commented 5 years ago

well, I will have a look tommorrow when I am free.

jharris4 commented 5 years ago

Actually, there's a very easy fix for this. The / prefix is being added to your url because this is your publicPath from webpack.

If you add publicPath: false to your asset it should work, like this:

{
  path: '//polyfill.alicdn.com/polyfill.min.js',
  publicPath: false,
  type: 'js',
  attributes: {
    crossorigin: 'anonymous'
  }
}
jharris4 commented 5 years ago

Can you give that a try and let me know if that fixes it?

huruji commented 5 years ago

oh, it works @jharris4

jharris4 commented 5 years ago

Great, glad it was an easy fix!