You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

485 lines
10 KiB

4 years ago
  1. [![npm][npm]][npm-url]
  2. [![node][node]][node-url]
  3. [![deps][deps]][deps-url]
  4. [![test][test]][test-url]
  5. [![coverage][cover]][cover-url]
  6. [![code style][style]][style-url]
  7. [![chat][chat]][chat-url]
  8. <div align="center">
  9. <img width="100" height="100" title="Load Options" src="http://michael-ciniawsky.github.io/postcss-load-options/logo.svg">
  10. <a href="https://github.com/postcss/postcss">
  11. <img width="110" height="110" title="PostCSS" src="http://postcss.github.io/postcss/logo.svg" hspace="10">
  12. </a>
  13. <img width="100" height="100" title="Load Plugins" src="http://michael-ciniawsky.github.io/postcss-load-plugins/logo.svg">
  14. <h1>Load Config</h1>
  15. </div>
  16. <h2 align="center">Install</h2>
  17. ```bash
  18. npm i -D postcss-load-config
  19. ```
  20. <h2 align="center">Usage</h2>
  21. ```bash
  22. npm i -S|-D postcss-plugin
  23. ```
  24. Install all required postcss plugins and save them to your **package.json** `dependencies`/`devDependencies`
  25. Then create a postcss config file by choosing one of the following formats
  26. ### `package.json`
  27. Create a **`postcss`** section in your projects **`package.json`**
  28. ```
  29. Project (Root)
  30. |– client
  31. |– public
  32. |
  33. |- package.json
  34. ```
  35. ```json
  36. {
  37. "postcss": {
  38. "parser": "sugarss",
  39. "map": false,
  40. "plugins": {
  41. "postcss-plugin": {}
  42. }
  43. }
  44. }
  45. ```
  46. ### `.postcssrc`
  47. Create a **`.postcssrc`** file in JSON or YAML format
  48. > ℹ️ It's recommended to use an extension (e.g **`.postcssrc.json`** or **`.postcssrc.yml`**) instead of `.postcssrc`
  49. ```
  50. Project (Root)
  51. |– client
  52. |– public
  53. |
  54. |- (.postcssrc|.postcssrc.json|.postcssrc.yml)
  55. |- package.json
  56. ```
  57. **`.postcssrc.json`**
  58. ```json
  59. {
  60. "parser": "sugarss",
  61. "map": false,
  62. "plugins": {
  63. "postcss-plugin": {}
  64. }
  65. }
  66. ```
  67. **`.postcssrc.yml`**
  68. ```yaml
  69. parser: sugarss
  70. map: false
  71. plugins:
  72. postcss-plugin: {}
  73. ```
  74. ### `.postcssrc.js` or `postcss.config.js`
  75. You may need some logic within your config. In this case create JS file named **`.postcssrc.js`** or **`postcss.config.js`**
  76. ```
  77. Project (Root)
  78. |– client
  79. |– public
  80. |
  81. |- (.postcssrc.js|postcss.config.js)
  82. |- package.json
  83. ```
  84. You can export the config as an `{Object}`
  85. **.postcssrc.js**
  86. ```js
  87. module.exports = {
  88. parser: 'sugarss',
  89. map: false,
  90. plugins: {
  91. 'postcss-plugin': {}
  92. }
  93. }
  94. ```
  95. Or export a `{Function}` that returns the config (more about the `ctx` param below)
  96. **.postcssrc.js**
  97. ```js
  98. module.exports = (ctx) => ({
  99. parser: ctx.parser ? 'sugarss' : false,
  100. map: ctx.env === 'development' ? ctx.map : false,
  101. plugins: {
  102. 'postcss-plugin': ctx.options.plugin
  103. }
  104. })
  105. ```
  106. Plugins can be loaded either using an `{Object}` or an `{Array}`
  107. #### `{Object}`
  108. **.postcssrc.js**
  109. ```js
  110. module.exports = ({ env }) => ({
  111. ...options
  112. plugins: {
  113. 'postcss-plugin': env === 'production' ? {} : false
  114. }
  115. })
  116. ```
  117. #### `{Array}`
  118. **.postcssrc.js**
  119. ```js
  120. module.exports = ({ env }) => ({
  121. ...options
  122. plugins: [
  123. env === 'production' ? require('postcss-plugin')() : false
  124. ]
  125. })
  126. ```
  127. > :warning: When using an `{Array}`, make sure to `require()` each plugin
  128. <h2 align="center">Options</h2>
  129. |Name|Type|Default|Description|
  130. |:--:|:--:|:-----:|:----------|
  131. |[**`to`**](#to)|`{String}`|`undefined`|Destination File Path|
  132. |[**`map`**](#map)|`{String\|Object}`|`false`|Enable/Disable Source Maps|
  133. |[**`from`**](#from)|`{String}`|`undefined`|Source File Path|
  134. |[**`parser`**](#parser)|`{String\|Function}`|`false`|Custom PostCSS Parser|
  135. |[**`syntax`**](#syntax)|`{String\|Function}`|`false`|Custom PostCSS Syntax|
  136. |[**`stringifier`**](#stringifier)|`{String\|Function}`|`false`|Custom PostCSS Stringifier|
  137. ### `parser`
  138. **.postcssrc.js**
  139. ```js
  140. module.exports = {
  141. parser: 'sugarss'
  142. }
  143. ```
  144. ### `syntax`
  145. **.postcssrc.js**
  146. ```js
  147. module.exports = {
  148. syntax: 'postcss-scss'
  149. }
  150. ```
  151. ### `stringifier`
  152. **.postcssrc.js**
  153. ```js
  154. module.exports = {
  155. stringifier: 'midas'
  156. }
  157. ```
  158. ### [**`map`**](https://github.com/postcss/postcss/blob/master/docs/source-maps.md)
  159. **.postcssrc.js**
  160. ```js
  161. module.exports = {
  162. map: 'inline'
  163. }
  164. ```
  165. > :warning: In most cases `options.from` && `options.to` are set by the third-party which integrates this package (CLI, gulp, webpack). It's unlikely one needs to set/use `options.from` && `options.to` within a config file. Unless you're a third-party plugin author using this module and its Node API directly **dont't set `options.from` && `options.to` yourself**
  166. ### `to`
  167. ```js
  168. module.exports = {
  169. to: 'path/to/dest.css'
  170. }
  171. ```
  172. ### `from`
  173. ```js
  174. module.exports = {
  175. from: 'path/to/src.css'
  176. }
  177. ```
  178. <h2 align="center">Plugins</h2>
  179. ### `{} || null`
  180. The plugin will be loaded with defaults
  181. ```js
  182. 'postcss-plugin': {} || null
  183. ```
  184. **.postcssrc.js**
  185. ```js
  186. module.exports = {
  187. plugins: {
  188. 'postcss-plugin': {} || null
  189. }
  190. }
  191. ```
  192. > :warning: `{}` must be an **empty** `{Object}` literal
  193. ### `{Object}`
  194. The plugin will be loaded with given options
  195. ```js
  196. 'postcss-plugin': { option: '', option: '' }
  197. ```
  198. **.postcssrc.js**
  199. ```js
  200. module.exports = {
  201. plugins: {
  202. 'postcss-plugin': { option: '', option: '' }
  203. }
  204. }
  205. ```
  206. ### `false`
  207. The plugin will not be loaded
  208. ```js
  209. 'postcss-plugin': false
  210. ```
  211. **.postcssrc.js**
  212. ```js
  213. module.exports = {
  214. plugins: {
  215. 'postcss-plugin': false
  216. }
  217. }
  218. ```
  219. ### `Ordering`
  220. Plugin **execution order** is determined by declaration in the plugins section (**top-down**)
  221. ```js
  222. {
  223. plugins: {
  224. 'postcss-plugin': {}, // [0]
  225. 'postcss-plugin': {}, // [1]
  226. 'postcss-plugin': {} // [2]
  227. }
  228. }
  229. ```
  230. <h2 align="center">Context</h2>
  231. When using a `{Function}` (`postcss.config.js` or `.postcssrc.js`), it's possible to pass context to `postcss-load-config`, which will be evaluated while loading your config. By default `ctx.env (process.env.NODE_ENV)` and `ctx.cwd (process.cwd())` are available on the `ctx` `{Object}`
  232. > ℹ️ Most third-party integrations add additional properties to the `ctx` (e.g `postcss-loader`). Check the specific module's README for more information about what is available on the respective `ctx`
  233. <h2 align="center">Examples</h2>
  234. **postcss.config.js**
  235. ```js
  236. module.exports = (ctx) => ({
  237. parser: ctx.parser ? 'sugarss' : false,
  238. map: ctx.env === 'development' ? ctx.map : false,
  239. plugins: {
  240. 'postcss-import': {},
  241. 'postcss-nested': {},
  242. cssnano: ctx.env === 'production' ? {} : false
  243. }
  244. })
  245. ```
  246. <div align="center">
  247. <img width="80" height="80" src="https://worldvectorlogo.com/logos/nodejs-icon.svg">
  248. </div>
  249. ```json
  250. "scripts": {
  251. "build": "NODE_ENV=production node postcss",
  252. "start": "NODE_ENV=development node postcss"
  253. }
  254. ```
  255. ### `Async`
  256. ```js
  257. const { readFileSync } = require('fs')
  258. const postcss = require('postcss')
  259. const postcssrc = require('postcss-load-config')
  260. const css = readFileSync('index.sss', 'utf8')
  261. const ctx = { parser: true, map: 'inline' }
  262. postcssrc(ctx).then(({ plugins, options }) => {
  263. postcss(plugins)
  264. .process(css, options)
  265. .then((result) => console.log(result.css))
  266. })
  267. ```
  268. ### `Sync`
  269. ```js
  270. const { readFileSync } = require('fs')
  271. const postcss = require('postcss')
  272. const postcssrc = require('postcss-load-config')
  273. const css = readFileSync('index.sss', 'utf8')
  274. const ctx = { parser: true, map: 'inline' }
  275. const { plugins, options } = postcssrc.sync(ctx)
  276. ```
  277. <div align="center">
  278. <img width="80" height="80" halign="10" src="https://worldvectorlogo.com/logos/gulp.svg">
  279. </div>
  280. ```json
  281. "scripts": {
  282. "build": "NODE_ENV=production gulp",
  283. "start": "NODE_ENV=development gulp"
  284. }
  285. ```
  286. ```js
  287. const { task, src, dest, series, watch } = require('gulp')
  288. const postcss = require('gulp-postcssrc')
  289. const css = () => {
  290. src('src/*.css')
  291. .pipe(postcss())
  292. .pipe(dest('dest'))
  293. })
  294. task('watch', () => {
  295. watch(['src/*.css', 'postcss.config.js'], css)
  296. })
  297. task('default', series(css, 'watch'))
  298. ```
  299. <div align="center">
  300. <img width="80" height="80" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
  301. </div>
  302. ```json
  303. "scripts": {
  304. "build": "NODE_ENV=production webpack",
  305. "start": "NODE_ENV=development webpack-dev-server"
  306. }
  307. ```
  308. **webpack.config.js**
  309. ```js
  310. module.exports = (env) => ({
  311. module: {
  312. rules: [
  313. {
  314. test: /\.css$/,
  315. use: [
  316. 'style-loader',
  317. 'css-loader',
  318. 'postcss-loader'
  319. ]
  320. }
  321. ]
  322. }
  323. })
  324. ```
  325. <h2 align="center">Maintainers</h2>
  326. <table>
  327. <tbody>
  328. <tr>
  329. <td align="center">
  330. <img width="150" height="150"
  331. src="https://github.com/michael-ciniawsky.png?v=3&s=150">
  332. <br />
  333. <a href="https://github.com/michael-ciniawsky">Michael Ciniawsky</a>
  334. </td>
  335. <td align="center">
  336. <img width="150" height="150"
  337. src="https://github.com/ertrzyiks.png?v=3&s=150">
  338. <br />
  339. <a href="https://github.com/ertrzyiks">Mateusz Derks</a>
  340. </td>
  341. </tr>
  342. <tbody>
  343. </table>
  344. <h2 align="center">Contributors</h2>
  345. <table>
  346. <tbody>
  347. <tr>
  348. <td align="center">
  349. <img width="150" height="150"
  350. src="https://github.com/sparty02.png?v=3&s=150">
  351. <br />
  352. <a href="https://github.com/sparty02">Ryan Dunckel</a>
  353. </td>
  354. <td align="center">
  355. <img width="150" height="150"
  356. src="https://github.com/pcgilday.png?v=3&s=150">
  357. <br />
  358. <a href="https://github.com/pcgilday">Patrick Gilday</a>
  359. </td>
  360. <td align="center">
  361. <img width="150" height="150"
  362. src="https://github.com/daltones.png?v=3&s=150">
  363. <br />
  364. <a href="https://github.com/daltones">Dalton Santos</a>
  365. </td>
  366. </tr>
  367. <tbody>
  368. </table>
  369. [npm]: https://img.shields.io/npm/v/postcss-load-config.svg
  370. [npm-url]: https://npmjs.com/package/postcss-load-config
  371. [node]: https://img.shields.io/node/v/postcss-load-plugins.svg
  372. [node-url]: https://nodejs.org/
  373. [deps]: https://david-dm.org/michael-ciniawsky/postcss-load-config.svg
  374. [deps-url]: https://david-dm.org/michael-ciniawsky/postcss-load-config
  375. [test]: http://img.shields.io/travis/michael-ciniawsky/postcss-load-config.svg
  376. [test-url]: https://travis-ci.org/michael-ciniawsky/postcss-load-config
  377. [cover]: https://coveralls.io/repos/github/michael-ciniawsky/postcss-load-config/badge.svg
  378. [cover-url]: https://coveralls.io/github/michael-ciniawsky/postcss-load-config
  379. [style]: https://img.shields.io/badge/code%20style-standard-yellow.svg
  380. [style-url]: http://standardjs.com/
  381. [chat]: https://img.shields.io/gitter/room/postcss/postcss.svg
  382. [chat-url]: https://gitter.im/postcss/postcss