namespace-css-module-loader
Webpack's css-loader implements CSS Modules by giving every .class
a unique hash which you then have to bind to your element/complement.
output with regular CSS Modules (webpack css-loader):
namespace-css-module-loader, as the name suggests, only gives one unique hash (a namespace) to all rules:
This hash is available as a single named export: {style}
which you only need to include in the parent div
:
; <div className=style> <div class='a'></div> <div class='b'></div></div>
Install
npm i namespace-css-module-loader
Usage
In your webpack config:
loader: 'css-loader!namespace-css-module-loader'
Use it after pre-processors, it only works on pure CSS
loader: 'css-loader?importLoaders=3!postcss-loader!namespace-css-module-loader!sass-loader' ... <- ^ <- pre
Options
Provide options to the module as a query string or query object
loader: 'css-loader!namespace-css-module-loader?id=root'
loader: 'css-loader' loader: 'namespace-css-module-loader' query: id: 'root'
id
(string) (default: 'style')
Change the default named export {style}
:
; <div className=root></div>
descendant
(boolean) (default: true)
Make the rule descendant of the namespace (default)
/* => */
combine
(boolean)
Combine the rule with the namespace instead of making it a descendant
/* => */
combine
& descendant
Both options can be used together to group the rules:
/* => */
Issues
Hot Module Replacement
It probably has nothing to do with this module but if HMR isn't working correctly try adding webpack-module-hot-accept to all your JS files.
it hot-reloads only if [you] have a module.hot.accept() call in the JS that require-s the CSS Module. (react-css-modules#51)
related: css-loader#186