generate unique className for every React component based on directory path.
To let module css work, you should use html-path-loader is the mean time.
Note
your style files (scss or less) and your react files should in the same directory.
+-- home
| +-- home.jsx
| +-- home.scss
+-- about
| +-- about.jsx
| +-- about.scss
If you decide to separate all the styles file and your react files in different directory, this loader is not good choice.
Usage
Based on every react component’s path, in the precompile moment, wrap every style files with a name
// Assume this scss files relative path from this project is `src/component/test/index.js`, then the className is `.src-component-test`
.src-component-test {
// the original code from scss file
...
}
the html-path-loader will generate the same className for every components and every style files. And the react css module could be work.
css-path-loader
webpack loader to write React with module css.
To let module css work, you should use html-path-loader is the mean time.
Note
your style files (scss or less) and your react files should in the same directory.
If you decide to separate all the styles file and your react files in different directory, this loader is not good choice.
Usage
Based on every react component’s path, in the precompile moment, wrap every style files with a name
the html-path-loader will generate the same className for every components and every style files. And the react css module could be work.
Config
Tips
requireto require a css file fromnode_modules, this loader will ignore itrequirein your entry js filerequireto load a file that is from other directory(node_modules ), use @importcss 模块化工具
通过为每个 react 组件生成唯一的 className 值来实现 css 组件化
此loader需要和 html-path-loader 配合使用才能发挥作用
scss 文件或者 less 文件必须和 react 组件文件必须放置在同一目录下,
如果样式和组件是
完全分开放置请不要使用这个 loader,Usage
自动根据每一个 react 组件所在的路径, 在编译期间, 将该组件 require 的所有scss或者 less 代码代码包上一个
有了这个 loader 自动根据每个组件的文件所在路径生成 class 名称, 配合html-path-loader 会生成相同的 class 名称, 从而实现 css 模块化
Config
Tips
require引入npm安装的css文件, 会直接加到全局域中, 和通常webpack引入没什么区别scss或者less文件中通过@import引入node_modules里或者其他地方的scss或者less文件, 都只会作用于当前组件, 所有的样式的作用域也都属于当前组件