summary:
I describe below my setup building CSS and Javascript separately with webpack. It works nicely. It is not what the webpack documentation recommends. So I am interested to know what you think about it.
webpack doc for loading css:
The webpack doc asks to import the CSS files from the Javascript code and to then run extract-text-webpack-plugin to extract the CSS. I think it is preferable to build CSS and Javascript separately for many reasons given below, but especially for 2 reasons:
- If one wants to build only CSS, it will be faster if the bundler does not need to parse the Javascript source code.
- SASS building in isolation has been the tradition for years. Therefore, we can achieve better HTML semantics and maintainability.
Here is my setup:
– parallel-webpack requires 2 separates config files, one for CSS and one for webpack.
It will run the building and the watching of the 2 configs in parallel.
– In the CSS config file, I use ExtractTextWebpackPlugin to create the CSS bundle. And the only accepted extension in the config file is “.scss”. There is no “.js”. And in production, I run OptimizeCssAssetsPlugin to minimize the CSS.
It seems to me that building separately is better for the following reasons:
–> compilation time:
– If one wants to build only CSS, it will be faster if the bundler does not need to parse the Javascript source code. I can use an env variable that I pass to parallel-webpack in order to ask to build only CSS.
– My build time in development is so fast, of around 2 s, that I don’t need to use webpack-dev-server or hot module replacement. I just run webpack –watch and I refresh the page.
– parallel-webpack will build CSS and Javascript in parallel in separate threads. It will be faster than with the official way of building CSS in webpack.
–> SASS building in isolation has been the tradition for years:
Traditionally, SASS building has been separated from building the Javascript source code.
– The traditional way of structuring SASS, has a manifest file (or main file) that imports everything. It is isomorph to the final result with a CSS bundle that will be global to the entire web page.
– Most methodologies for good SASS (SMACSS, 7-1, OOCSS, etc) rely on the traditionaly way
– good CSS structuring is meant to focus on the HTML semantics for structure and maintainability. The mapping Javascript to SCSS is a secondary issue that can be solved with good ways of working.
– In production the CSS and the Javascript will be separated. So the mapping of Javascript to CSS in the Javscript code can only be temporary.
– the way of using of a manifest file for SASS is probably optimized in the C library libsass that is used to compile it.
– the manifest file (or main file) is not confusing. In most languages there is a main.c like file.
– It seems to me that the many people that currently use Gulp as a SASS bundler are doing a very similar setup as I propose with webpack. So we follow the tradition.
–> simplification of config files
– Separating the config files simplifies greatly the config files. It is not possible to separate with the official way because one has to mix the build, so one has to mix files extensions, folder locations, etc.
–> future proof for the use of package:
With SASS from Javascript, you become dependent of the people maintaining all the packages (webpack, node-sass, etc). Whereas building raw SASS has many libraries.
A completely new way of building may show up. In the past few years we saw many new build frameworks.
–> the web geek-spider goes fast but experimental technology may not work well:
Building raw SASS is stone-like robust to work. The official webpack way is experimental because it is new. The ordering in the CSS may not be controlled. variables have no global scope. Code could be repeated. A package may behave stangely.
–> with parallel webpack, it is simple to build and watch both the Javascript and the CSS bundles. One can even pass an env variable to build only CSS.