Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack html-webpack-plugin, can I use conditional comments to import entry script?

Tags:

webpack

I have multiple entry like:

entry: {
  foo: 'foo.js'
  bar: 'bar.js'
  ie: 'ie.js'
},
output: {
  path: path.join(__dirname, '/dist/'),
  filename: '[name]-[hash].min.js'
}

when I use HtmlWebpackPlugin, It's for certain to import or not import an entry, using chunks. I hope to generate conditional comments in HTML file, like:

<!--[if lte IE 9]>
  <script type="text/javascript" src="ie-12dx....js"></script>
<![endif]-->

Any idea on how to achieve that?

like image 544
sww176 Avatar asked Mar 14 '26 11:03

sww176


1 Answers

Thanks to jantimon - collaborator of html-webpack-plugin.

You have to create a custom index file: https://github.com/ampedandwired/html-webpack-plugin/blob/master/default_index.html#L15

According to custom index file example, I made my own:

my scripts:

<!--[if lte IE 9]>
{% for (var chunk in o.htmlWebpackPlugin.files.chunks) { %}
{% if (/^ie-\w+\.min\.js$/.test(o.htmlWebpackPlugin.files.chunks[chunk].entry)) { %}
<script type="text/javascript" src="{%=o.htmlWebpackPlugin.files.chunks[chunk].entry %}></script>
{% } %}
{% } %}
<![endif]-->

works great!

like image 132
sww176 Avatar answered Mar 17 '26 03:03

sww176



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!