score:2

Yes, this can be done. You'll need CommonsChunkPlugin in addition to ExtractTextPlugin. Also, define multiple entry points

entry: {
    A: "./a",
    B: "./b",
    C: "./c",
},

and configure ExtractTextPlugin to use entry point names as CSS file names

new ExtractTextPlugin({
    filename: "[name].css"
}),

See a full example here:

https://github.com/webpack/webpack/tree/master/examples/multiple-entry-points-commons-chunk-css-bundle

score:2

While I may not answer your question how to split css files so that only the necessary ones are loaded the way you want the question to be answered (no plugin or that sort of thing), I hope to give you a possible alternative.

styled-components use the new ES6 feature tagged template literal to style the components inside the javascript file. I think using this library would solve your problem of loading only the necessary css files, because there would be no more css files per se.

react-boilerplate chose styled-components over sass, because

styled-components have a more powerful approach: instead of trying to give a styling language programmatic abilities, it pulls logic and configuration out into JS these features belong.

So using styled-components would not only solve your problem of loading only the necessary css, but it would further add to the decoupling of your application, makes it easier to test the design and reason about your app.

Here's the live demo where you can experiment with the styled-components and check how it works.


Related Query

More Query from same tag