mini-css-extract-plugin: Don't output empty JS files
I have seen people use the Extract plugin and name the file the same to prevent a useless .js file, however, naming the file the same in this instance gives the error:
Conflict: Multiple assets emit to the same filename main.js
I only want to take a CSS file and run it though autoprefix and minify, I don’t need it to output a JS file.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 80
- Comments: 45 (10 by maintainers)
@Tiendq I don’t want Webpack to output a JS file for CSS. It seems strange to me for it to do that. At the moment I’m getting the CSS file and a JS file. I’m coming from Gulp so maybe it’s just I don’t understand Webpack.
Same issue. I have a PHP application but I am managing assets with Webpack. With Webpack 4 & MiniCssExtractPlugin, generating CSS files is a pain at the moment. I need to directly load CSS file.
Part of my config:
Files generated are:
admin.css
&admin.js
.I don’t need
admin.js
file. It’s useless in my case.Weppack is
js
bundle, i can not imagine why this might be necessary, please provide use caseI faced the same issue of having a style only entry (css/sass/less) generating an extra .js file, and ended up creating a webpack plugin to remove the js file from the compilation.
I published it on npm as webpack-fix-style-only-entries. You can find the source code on https://github.com/fqborges/webpack-fix-style-only-entries.
😃 shamelessly promoting my package 😃
@danechitoaie thx for the tip!
I’ve just created a more abstract class to do this:
We search way to fix it inside plugin
v5 is here and it has changed absolutely nothing. Seems it’s being tracked at https://github.com/webpack/webpack/issues/11671.
I had to make my own plugin:
It’s very specific for my use case and has things hardcoded and I even have just put it directly in the webpack.config.js file (so not published on npm) but maybe it can be integrated somehow in some version directly into mini-css-extract-plugin? And made configurable with some additional options.
If it helps anyone I used the plugin remove-files-webpack-plugin, just need to change
folder
to match your project structure:Having same issue. I’m using webpack to compile both JS and SCSS and different entries (i.e. not requiring the css inside JS) and for each .css file generated I also get a .js file.
Fixed for
webpack@5
, anyway you can send a PR with fix it onmini-css-extract-plugin
side (using option)It will be solved for webpack@5 (near future), right now please use solutions above
To give you another “solution”, I’m currently just excluding the useless .js on the bundled index.html:
@TomS- I don’t want it too 😃
@evilebottnawi Yup, I’ll set it up this evening
I use Symfony Encore and have to fix the entrypoints too. But this should also work with a normal Webpack.
My approach to this problem:
and i use it with:
Thank you for the answer, i was in hope to not get this ‘answer’.
Here we have a thread discussing this very problem since 1 1/2 years, the tip to wait for v5 had already been given a year ago, but the discussion went on.
I did use a solution from above, i tried all of them. I am looking for a solution for my problem (as described above). Any help is welcome.
@fqborges I’m trying to use your plugin, but I’m running into a problem where the
file
is getting passed as*
instead of the actual file path.For example, if I add a
console.log(resources, file)
, I get a bunch of results like this:[ '<redacted>/src/app/css/pages/Home.scss' ] '*'
Do you know why this might be happening? The webpack docs on what gets passed for the
chunkAsset
hook are fairly light…Use
{ filename: '[name].css' }
. The empty JS File is going away (inwebpack >= v5.0.0
), but this needs to be fixed in webpack core itself