版博士V2.0程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

README.md 7.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # postcss-modules [![Build Status][ci-img]][ci]
  2. A [PostCSS] plugin to use [CSS Modules] everywhere. Not only at the client side.
  3. [postcss]: https://github.com/postcss/postcss
  4. [ci-img]: https://travis-ci.org/css-modules/postcss-modules.svg
  5. [ci]: https://travis-ci.org/css-modules/postcss-modules
  6. [css modules]: https://github.com/css-modules/css-modules
  7. <a href="https://evilmartians.com/?utm_source=postcss-modules">
  8. <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
  9. </a>
  10. What is this? For example, you have the following CSS:
  11. ```css
  12. /* styles.css */
  13. :global .page {
  14. padding: 20px;
  15. }
  16. .title {
  17. composes: title from "./mixins.css";
  18. color: green;
  19. }
  20. .article {
  21. font-size: 16px;
  22. }
  23. /* mixins.css */
  24. .title {
  25. color: black;
  26. font-size: 40px;
  27. }
  28. .title:hover {
  29. color: red;
  30. }
  31. ```
  32. After the transformation it will become like this:
  33. ```css
  34. ._title_116zl_1 {
  35. color: black;
  36. font-size: 40px;
  37. }
  38. ._title_116zl_1:hover {
  39. color: red;
  40. }
  41. .page {
  42. padding: 20px;
  43. }
  44. ._title_xkpkl_5 {
  45. color: green;
  46. }
  47. ._article_xkpkl_10 {
  48. font-size: 16px;
  49. }
  50. ```
  51. And the plugin will give you a JSON object for transformed classes:
  52. ```json
  53. {
  54. "title": "_title_xkpkl_5 _title_116zl_1",
  55. "article": "_article_xkpkl_10"
  56. }
  57. ```
  58. ## Usage
  59. ### Saving exported classes
  60. By default, a JSON file with exported classes will be placed next to corresponding CSS.
  61. But you have a freedom to make everything you want with exported classes, just
  62. use the `getJSON` callback. For example, save data about classes into a corresponding JSON file:
  63. ```js
  64. postcss([
  65. require("postcss-modules")({
  66. getJSON: function (cssFileName, json, outputFileName) {
  67. var path = require("path");
  68. var cssName = path.basename(cssFileName, ".css");
  69. var jsonFileName = path.resolve("./build/" + cssName + ".json");
  70. fs.writeFileSync(jsonFileName, JSON.stringify(json));
  71. },
  72. }),
  73. ]);
  74. ```
  75. `getJSON` may also return a `Promise`.
  76. ### Generating scoped names
  77. By default, the plugin assumes that all the classes are local. You can change
  78. this behaviour using the `scopeBehaviour` option:
  79. ```js
  80. postcss([
  81. require("postcss-modules")({
  82. scopeBehaviour: "global", // can be 'global' or 'local',
  83. }),
  84. ]);
  85. ```
  86. To define paths for global modules, use the `globalModulePaths` option.
  87. It is an array with regular expressions defining the paths:
  88. ```js
  89. postcss([
  90. require('postcss-modules')({
  91. globalModulePaths: [/path\/to\/legacy-styles/, /another\/paths/],
  92. });
  93. ]);
  94. ```
  95. To generate custom classes, use the `generateScopedName` callback:
  96. ```js
  97. postcss([
  98. require("postcss-modules")({
  99. generateScopedName: function (name, filename, css) {
  100. var path = require("path");
  101. var i = css.indexOf("." + name);
  102. var line = css.substr(0, i).split(/[\r\n]/).length;
  103. var file = path.basename(filename, ".css");
  104. return "_" + file + "_" + line + "_" + name;
  105. },
  106. }),
  107. ]);
  108. ```
  109. Or just pass an interpolated string to the `generateScopedName` option
  110. (More details [here](https://github.com/webpack/loader-utils#interpolatename)):
  111. ```js
  112. postcss([
  113. require("postcss-modules")({
  114. generateScopedName: "[name]__[local]___[hash:base64:5]",
  115. }),
  116. ]);
  117. ```
  118. It's possible to add custom hash to generate more unique classes using the `hashPrefix` option (like in [css-loader](https://webpack.js.org/loaders/css-loader/#hashprefix)):
  119. ```js
  120. postcss([
  121. require("postcss-modules")({
  122. generateScopedName: "[name]__[local]___[hash:base64:5]",
  123. hashPrefix: "prefix",
  124. }),
  125. ]);
  126. ```
  127. ### Exporting globals
  128. If you need to export global names via the JSON object along with the local ones, add the `exportGlobals` option:
  129. ```js
  130. postcss([
  131. require("postcss-modules")({
  132. exportGlobals: true,
  133. }),
  134. ]);
  135. ```
  136. ### Loading source files
  137. If you need, you can pass a custom loader (see [FileSystemLoader] for example):
  138. ```js
  139. postcss([
  140. require("postcss-modules")({
  141. Loader: CustomLoader,
  142. }),
  143. ]);
  144. ```
  145. You can also pass any needed root path:
  146. ```js
  147. postcss([
  148. require('postcss-modules')({
  149. root: 'C:\\',
  150. });
  151. ]);
  152. ```
  153. ### localsConvention
  154. Type: `String | (originalClassName: string, generatedClassName: string, inputFile: string) => className: string`
  155. Default: `null`
  156. Style of exported classnames, the keys in your json.
  157. | Name | Type | Description |
  158. | :-------------------: | :--------: | :----------------------------------------------------------------------------------------------- |
  159. | **`'camelCase'`** | `{String}` | Class names will be camelized, the original class name will not to be removed from the locals |
  160. | **`'camelCaseOnly'`** | `{String}` | Class names will be camelized, the original class name will be removed from the locals |
  161. | **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
  162. | **`'dashesOnly'`** | `{String}` | Dashes in class names will be camelized, the original class name will be removed from the locals |
  163. In lieu of a string, a custom function can generate the exported class names.
  164. ## Integration with templates
  165. The plugin only transforms CSS classes to CSS modules.
  166. But you probably want to integrate these CSS modules with your templates.
  167. Here are some examples.
  168. Assume you've saved project's CSS modules in `cssModules.json`:
  169. ```json
  170. {
  171. "title": "_title_xkpkl_5 _title_116zl_1",
  172. "article": "_article_xkpkl_10"
  173. }
  174. ```
  175. ### Pug (ex-Jade)
  176. Let's say you have a Pug template `about.jade`:
  177. ```jade
  178. h1(class=css.title) postcss-modules
  179. article(class=css.article) A PostCSS plugin to use CSS Modules everywhere
  180. ```
  181. Render it:
  182. ```js
  183. var jade = require("jade");
  184. var cssModules = require("./cssModules.json");
  185. var html = jade.compileFile("about.jade")({ css: cssModules });
  186. console.log(html);
  187. ```
  188. And you'll get the following HTML:
  189. ```html
  190. <h1 class="_title_xkpkl_5 _title_116zl_1">postcss-modules</h1>
  191. <article class="_article_xkpkl_10">
  192. A PostCSS plugin to use CSS Modules everywhere
  193. </article>
  194. ```
  195. ### HTML
  196. For HTML transformation we'll use [PostHTML](https://github.com/posthtml/posthtml) and [PostHTML CSS Modules](https://github.com/maltsev/posthtml-css-modules):
  197. ```bash
  198. npm install --save posthtml posthtml-css-modules
  199. ```
  200. Here is our template `about.html`:
  201. ```html
  202. <h1 css-module="title">postcss-modules</h1>
  203. <article css-module="article">
  204. A PostCSS plugin to use CSS Modules everywhere
  205. </article>
  206. ```
  207. Transform it:
  208. ```js
  209. var fs = require("fs");
  210. var posthtml = require("posthtml");
  211. var posthtmlCssModules = require("posthtml-css-modules");
  212. var template = fs.readFileSync("./about.html", "utf8");
  213. posthtml([posthtmlCssModules("./cssModules.json")])
  214. .process(template)
  215. .then(function (result) {
  216. console.log(result.html);
  217. });
  218. ```
  219. (for using other build systems please check [the documentation of PostHTML](https://github.com/posthtml/posthtml/blob/master/README.md))
  220. And you'll get the following HTML:
  221. ```html
  222. <h1 class="_title_xkpkl_5 _title_116zl_1">postcss-modules</h1>
  223. <article class="_article_xkpkl_10">
  224. A PostCSS plugin to use CSS Modules everywhere
  225. </article>
  226. ```
  227. ## May I see the plugin in action?
  228. Sure! Take a look at the [example](https://github.com/outpunk/postcss-modules-example).
  229. See [PostCSS] docs for examples for your environment and don't forget to run
  230. ```
  231. npm install --save-dev postcss postcss-modules
  232. ```
  233. [filesystemloader]: https://github.com/css-modules/css-modules-loader-core/blob/master/src/file-system-loader.js