Can I use webpack to generate CSS and JS separately?

Should I even be using webpack for non-JS assets if I’m not going to mix them into my JS?

Maybe not. Webpack is definitely js-centric, with the implicit assumption that what you’re building is a js application. Its implementation of require() allows you to treat everything as a module (including Sass/LESS partials, JSON, pretty much anything), and automatically does your dependency management for you (everything that you require is bundled, and nothing else).

why would I require LESS in my JS when it has nothing to do with my JS code?

People do this because they’re defining a piece of their application (e.g. a React component, a Backbone View) with js. That piece of the application has CSS that goes with it. Depending on some external CSS resource that’s built separately and not directly referenced from the js module is fragile, harder to work with, and can lead to styles getting out of date, etc. Webpack encourages you to keep everything modular, so you have a CSS (Sass, whatever) partial that goes with that js component, and the js component require()s it to make the dependency clear (to you and to the build tool, which never builds styles you don’t need).

I don’t know if you could use webpack to bundle CSS on its own (when the CSS files aren’t referenced from any js). I’m sure you could wire something up with plugins, etc., but not sure it’s possible out of the box. If you do reference the CSS files from your js, you can easily bundle the CSS into a separate file with the Extract Text plugin, as you say.

Leave a Comment