Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack 4 Generates Empty CSS Vendors Bundle #6703

Closed
l0gicgate opened this issue Mar 7, 2018 · 33 comments
Closed

Webpack 4 Generates Empty CSS Vendors Bundle #6703

l0gicgate opened this issue Mar 7, 2018 · 33 comments

Comments

@l0gicgate
Copy link

l0gicgate commented Mar 7, 2018

Do you want to request a feature or report a bug?
Report a bug

What is the current behavior?
Empty CSS Vendors Bundle Being Generated

If the current behavior is a bug, please provide the steps to reproduce.
My config is outputting 2 bundles for both JS and CSS. I want to output one vendors.js and one bundle.js and once bundle.css file. Somehow it is creating an empty vendors.css file. I tried using removeEmptyChunks (which is enabled by default) but it doesn't do anything. It'll still emit an empty chunk:

Here is the config:

{
  splitChunks: {
    cacheGroups: {
      default: {
        chunks: 'initial',
        name: 'bundle',
        priority: -20,
        reuseExistingChunk: true,
      },
      vendor: {
        chunks: 'initial',
        name: 'vendor',
        priority: -10,
        test: /node_modules\/(.*)\.js/
      }
    }
  }
}

What is the expected behavior?
Generate 1 bundle for all CSS which is what was happening with the CommonsChunkPlugin and generate 2 bundles for JS, 1 cacheable vendor bundle and 1 commons bundle for everything else plus async route bundles.

If this is a feature request, what is motivation or use case for changing the behavior?
N/A

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.
Node 9.7.1, Webpack 4.1.1

The issue is replicated in this repository
https://github.com/l0gicgate/webpack-issue-6703

@sokra
Copy link
Member

sokra commented Mar 7, 2018

Thanks for your report. It would be great if you reduce your issue to a small reproducible example. Best put this example into a github repository together with instructions how to get to the problem.

@l0gicgate
Copy link
Author

@sokra will do. I will update the issue once I put the repo together.

Thanks

@AlexandrMardanov
Copy link

I think this issue could be the problem.
mini-css-extract-plugin fixes empty css, but you can't use [chunkhash] with it :(

@l0gicgate
Copy link
Author

@sokra I added the repository here to replicate the issue https://github.com/l0gicgate/webpack-issue-6703

I tried to replicate the current setup I have and remove as many dependencies as possible that weren't necessary

@shinygang
Copy link

@sokra Question:How can I chunk without css?
my code:

optimization: {
  runtimeChunk: false,
  splitChunks: {
    chunks: 'all',
    minSize: 30000,
    minChunks: 3,
    maxAsyncRequests: 5,
    maxInitialRequests: 3,
    name: true,
    cacheGroups: {
      commons: {
      name: 'commons',
      chunks: 'all',
      minChunks: 3,
      enforce: true
    },
    vendors: {
      test: /[\\/]node_modules[\\/]/,
      priority: -10
    }
  }
}

but this code chunked a ‘commons.css’ file,I don’t want to chunk with css, how to do ?

@yangmingshan
Copy link

@sokra There is another reproducible example: https://github.com/yangmingshan/react-demo/tree/next
run npm run build will generate three empty CSS

@Legends
Copy link
Member

Legends commented Mar 15, 2018

@l0gicgate I just build your gist, but I get a bundle.css which is not empty.

@Legends
Copy link
Member

Legends commented Mar 15, 2018

Hm, I have downloaded this repo.

npm install
npm run build:dev

I can only see one bundle.css file appearing in the dist folder, also checked via win explorer, to make sure my IDE is not fooling me. When I build it, there is no vendor.css at all.

image

@l0gicgate
Copy link
Author

@Legends I updated the repo. Please reinstall and run build again. You'll see empty vendor.css package.

@Legends
Copy link
Member

Legends commented Mar 16, 2018

@shinygang You have this configuration for vendor:

{
  splitChunks: {
    cacheGroups: {
             vendor: {
                     chunks: 'initial',
                      name: 'vendor',
                      priority: -10,
                     test: /[\\/]node_modules[\\/]/,  // <--- only for chunks from node_modules
          },

As I can see, you don't import any css/scss/... from within node_modules, therefore vendor.css is empty...
That's what I would say, but I am not an expert regarding webpack or node in general.

Or do you mean, why is there an empty vendor.css file as you don't have any css stuff imported from node_modules ? Why does it render this empty vendor.css?

Don't know, @sokra, @michael-ciniawsky ?

@l0gicgate
Copy link
Author

@Legends it shouldn’t output empty css files. I just want a a vendor.js bundle.

@Legends
Copy link
Member

Legends commented Mar 17, 2018

Yes, I agree, added sokra to take a look at it.

@phiphou
Copy link

phiphou commented Mar 24, 2018

Same issue for me:

splitChunks: {
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors',
          enforce: true,
          chunks: 'all'
        }
      }
    }

produce vendors.js but also an empty vendors.css

@phiphou
Copy link

phiphou commented Mar 25, 2018

@phiphou
Copy link

phiphou commented Mar 25, 2018

The issue happens also with webpack 4.5.0

@phiphou
Copy link

phiphou commented Mar 25, 2018

The issue is not happening for me with a very minimal project with no dependencies at all. But when I add VueJs in my case (React in @l0gicgate case) the issue happens. Hope this will help to fix this issue.

@Legends
Copy link
Member

Legends commented Mar 25, 2018

@ooflorent Can someone please take a look at this and explain what the issue is..

@benfavre
Copy link

This currently breaks all "complex" builds. Forcing the config to be set to not have a vendor group defined.

@phiphou
Copy link

phiphou commented Mar 31, 2018

Using mini-css-extract-plugin solves the problem for me. But it has currently no HMR support.

@pleerock
Copy link

pleerock commented Apr 5, 2018

Same issue here - adding simple splitChunks from an example completely breaks css output - css file is empty (Im using ExtractTextPlugin as well)

optimization: {
        splitChunks: {
            cacheGroups: {
                vendor: {
                    test: /node_modules/,
                    chunks: "initial",
                    name: "vendor",
                    priority: 10,
                    enforce: true
                }
            }
        }
    },

@Legends
Copy link
Member

Legends commented Apr 5, 2018

Try to use the mini-css-extract-plugin as metnioned by @phiphou

@l0gicgate
Copy link
Author

@Legends I can’t use that since sass-loader requires extract-text-webpack plugin. Why can’t this just be fixed?

@alexander-akait
Copy link
Member

@l0gicgate sass-loader don't requires extract-text-webpack-plugin

@Legends
Copy link
Member

Legends commented Apr 5, 2018

@l0gicgate I would suggest, you ask this question in the "extract-text-webpack plugin" they will for sure give you an answer, about their plugin and webpack 4 support...

For .scss files + sass-loader I use this configuration and I am using MiniCssExtractPlugin:

 {
                test: /\.(scss)$/,
                use: [
                    //    fallback: "style-loader",

                    {
                        loader: MiniCssExtractPlugin.loader
                    },
                    {
                        loader: "css-loader", // translates CSS into CommonJS modules
                    },
                    {
                        loader: "postcss-loader", // Run post css actions
                        options: {
                            plugins: function () { // post css plugins, can be exported to postcss.config.js
                                return [
                                    require("precss"),
                                    require("autoprefixer")
                                ];
                            }
                        }
                    },
                    {
                        loader: "sass-loader" // compiles Sass to CSS
                    }
                ]
            },

@phiphou
Copy link

phiphou commented Apr 6, 2018

Same config for me and you can implement HMR using this workaround : webpack-contrib/mini-css-extract-plugin#34 (comment)

@lili21
Copy link

lili21 commented Apr 16, 2018

The problem is related to extract-text-webpack-plugin. You should use mini-css-extract-plugin instead for webpack@4 supporting.

@phiphou
Copy link

phiphou commented May 3, 2018

@l0gicgate I think you can close the issue, since an alternative solution has been given.

You can though open a similar issue in extract-text-webpack-plugin's repository.

@l0gicgate
Copy link
Author

@phiphou how is that a fix? The issue is also on this repo’s end. There’s even an option to ensure that bundles that have a size of zero shouldn’t even be output. I’m not closing this until it is resolved.

@lili21
Copy link

lili21 commented May 4, 2018

@l0gicgate You are using extract-text-webpack-plugin which doesn't support webpack@4. as I said before, you should use mini-css-extract-plugin.

@phiphou
Copy link

phiphou commented May 4, 2018

@l0gicgate As @lili21 said, the issue is not related to Webpack but to extract-text-webpack-plugin, it's not a bug from Webpack.

There is nothing to fix here, but in the extract-text-webpack-plugin's repository.

Create an issue there and close this one if you don't really want to use the new mini-css-extract-plugin (for some reasons I don't understand)

@ChrisSargent
Copy link

ChrisSargent commented Jun 30, 2018

Thanks for the help here - I followed the instructions to use mini-css-extract-plugin and whilst it does put everything in to a single css file, I now get an extra styles.js file, which is also now included in my main html file as a <script>... any ideas how to solve this?

@ChrisSargent
Copy link

ChrisSargent commented Jun 30, 2018

Okay, I was actually able to get back to 'normal' operation, i.e. splitting js files but ignoring css files by adding the following to webpack in optimization.splitChunks:

cacheGroups: {
    vendors: {
        test: /[\\/]node_modules[\\/].*js/
    }
}

It simply tells Webpack to only split node_modules based on .js files

@l0gicgate
Copy link
Author

I have switched to mini-css-extract-plugin. Things broke even further trying to use extract-css-webpack-plugin when updating to Webpack 4.19.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests