You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feature request: support multiple configurations with one HTML file. eg type=module. #782
This would be awesome! I think it's important to keep it open to accepting multiple script sources. In my build, for example, three script tags get added because of code splitting:
@mastilver That's a really good question! I don't think this can work unless there's a shared cache 😞. Here's how I ended up hacking this together for our (specific) webpack config. It was complicated a bit by us emitting several modules instead of a single main.js or bundle.js. This also doesn't really handle all CSS cases (it worked for our single CSS bundle).
letsharedAssets={chunks: {},js: []}functionHtmlMultiBuildWebpackPlugin(){}HtmlMultiBuildWebpackPlugin.prototype.apply=function(compiler){compiler.plugin('compilation',function(compilation){compilation.plugin('html-webpack-plugin-before-html-processing',function(data,cb){// Avoid chunk name collisions, since they can be named the same between builds// { 'main': {}} to { 'main-modern': {}} if the filename matchesconstobj=mapKeys(data.assets.chunks,(v,k)=>{if(v.entry.includes('-modern')){return`${k}-modern`}returnk})sharedAssets.js=uniq(concat(sharedAssets.js,data.assets.js))sharedAssets.chunks=merge(sharedAssets.chunks,obj)constnewData=merge(data.assets,sharedAssets)cb(null,newData)})compilation.plugin('html-webpack-plugin-alter-asset-tags',function(args,cb){constnewBody=args.body.map((arg)=>{if(arg.attributes.src.includes('-modern')){returnmerge(arg,{attributes: {type: 'module'}})}else{returnmerge(arg,{attributes: {nomodule: true}})}})cb(null,merge(args,{body: newBody}))})})}
So I guess an api to accommodate that would look something like this? It's definitely weird!
newHtmlWebpackPlugin({multiBuild: {cacheName: 'unique-name',// used to cache all chunks that go togethersuffixes: ['-modern'],// list of suffixes to watch out for. Could just be a stringattributes: {// attributes to append based on the suffixes above. Would NEED to match'-modern': {type: 'module'},// attribute overrides for the modern filesdefault: {nomodule: true}// attribute overrides for everything else}}})
Reacted by Tyson Cleary, Mike Engel, Alex Parish, Sebastian Werner, Anthony Ciccarello, jscinoz, Tristan Teufel, Sam Sharpe, Brian Chirls, Houssein Djirdeh and 1 more
Just wanted to add my 2 cents here. I agree with @graingert in that using module/nomodule can help developers trim their bundle sizes by quite a bit (and Babel makes this transpilation process even easier with the esmodules target).
It's so great to see plugins and workarounds created to solve this problem (such as the many already linked in this thread 🎉), but it would be awesome to have this supported by HTMLWebpackPlugin. Happy to help where possible!
Activity
mike-engel commentedon Sep 19, 2017
This would be awesome! I think it's important to keep it open to accepting multiple script sources. In my build, for example, three script tags get added because of code splitting:
So I guess the solution there would be something like (obviously I'm somewhat making up the syntax here):
mastilver commentedon Sep 21, 2017
I'm all in for a multiple config setup: #780 (comment)
Can put down the api you had in mind? (ie: which options would you pass down to
new HtmlPlugin())mike-engel commentedon Sep 21, 2017
@mastilver That's a really good question! I don't think this can work unless there's a shared cache 😞. Here's how I ended up hacking this together for our (specific) webpack config. It was complicated a bit by us emitting several modules instead of a single
main.jsorbundle.js. This also doesn't really handle all CSS cases (it worked for our single CSS bundle).So I guess an api to accommodate that would look something like this? It's definitely weird!
robatwilliams commentedon Dec 3, 2017
I tried out @mike-engel 's plugin from above - it works great.
I created a gist with a few tweaks (functionally pretty much the same), and an usage example.
What use cases are there for making this more generic than the scenario described in the opening post?
stale commentedon Jun 1, 2018
This issue had no activity for at least half a year. It's subject to automatic issue closing if there is no activity in the next 15 days.
alexparish commentedon Jun 11, 2018
Is there still appetite for this?
firsttris commentedon Jul 4, 2018
created a small plugin for html-webpack-plugin for webpack4 multi-build configuration which also support FF, IE11, Edge, Chrome, Safari 11.
wrote down my findings...
https://github.com/firsttris/html-webpack-multi-build-plugin
housseindjirdeh commentedon Oct 4, 2018
Just wanted to add my 2 cents here. I agree with @graingert in that using module/nomodule can help developers trim their bundle sizes by quite a bit (and Babel makes this transpilation process even easier with the esmodules target).
It's so great to see plugins and workarounds created to solve this problem (such as the many already linked in this thread 🎉), but it would be awesome to have this supported by HTMLWebpackPlugin. Happy to help where possible!