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.

500 lines
11 KiB

2 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 project's **`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. > ℹ️ When using an `{Object}`, the key can be a Node.js module name, a path to a JavaScript file that is relative to the directory of the PostCSS config file, or an absolute path to a JavaScript file.
  118. #### `{Array}`
  119. **.postcssrc.js**
  120. ```js
  121. module.exports = ({ env }) => ({
  122. ...options,
  123. plugins: [
  124. env === 'production' ? require('postcss-plugin')() : false
  125. ]
  126. })
  127. ```
  128. > :warning: When using an `{Array}`, make sure to `require()` each plugin
  129. <h2 align="center">Options</h2>
  130. |Name|Type|Default|Description|
  131. |:--:|:--:|:-----:|:----------|
  132. |[**`to`**](#to)|`{String}`|`undefined`|Destination File Path|
  133. |[**`map`**](#map)|`{String\|Object}`|`false`|Enable/Disable Source Maps|
  134. |[**`from`**](#from)|`{String}`|`undefined`|Source File Path|
  135. |[**`parser`**](#parser)|`{String\|Function}`|`false`|Custom PostCSS Parser|
  136. |[**`syntax`**](#syntax)|`{String\|Function}`|`false`|Custom PostCSS Syntax|
  137. |[**`stringifier`**](#stringifier)|`{String\|Function}`|`false`|Custom PostCSS Stringifier|
  138. ### `parser`
  139. **.postcssrc.js**
  140. ```js
  141. module.exports = {
  142. parser: 'sugarss'
  143. }
  144. ```
  145. ### `syntax`
  146. **.postcssrc.js**
  147. ```js
  148. module.exports = {
  149. syntax: 'postcss-scss'
  150. }
  151. ```
  152. ### `stringifier`
  153. **.postcssrc.js**
  154. ```js
  155. module.exports = {
  156. stringifier: 'midas'
  157. }
  158. ```
  159. ### [**`map`**](https://github.com/postcss/postcss/blob/master/docs/source-maps.md)
  160. **.postcssrc.js**
  161. ```js
  162. module.exports = {
  163. map: 'inline'
  164. }
  165. ```
  166. > :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**
  167. ### `to`
  168. ```js
  169. module.exports = {
  170. to: 'path/to/dest.css'
  171. }
  172. ```
  173. ### `from`
  174. ```js
  175. module.exports = {
  176. from: 'path/to/src.css'
  177. }
  178. ```
  179. <h2 align="center">Plugins</h2>
  180. ### `{} || null`
  181. The plugin will be loaded with defaults
  182. ```js
  183. 'postcss-plugin': {} || null
  184. ```
  185. **.postcssrc.js**
  186. ```js
  187. module.exports = {
  188. plugins: {
  189. 'postcss-plugin': {} || null
  190. }
  191. }
  192. ```
  193. > :warning: `{}` must be an **empty** `{Object}` literal
  194. ### `{Object}`
  195. The plugin will be loaded with given options
  196. ```js
  197. 'postcss-plugin': { option: '', option: '' }
  198. ```
  199. **.postcssrc.js**
  200. ```js
  201. module.exports = {
  202. plugins: {
  203. 'postcss-plugin': { option: '', option: '' }
  204. }
  205. }
  206. ```
  207. ### `false`
  208. The plugin will not be loaded
  209. ```js
  210. 'postcss-plugin': false
  211. ```
  212. **.postcssrc.js**
  213. ```js
  214. module.exports = {
  215. plugins: {
  216. 'postcss-plugin': false
  217. }
  218. }
  219. ```
  220. ### `Ordering`
  221. Plugin **execution order** is determined by declaration in the plugins section (**top-down**)
  222. ```js
  223. {
  224. plugins: {
  225. 'postcss-plugin': {}, // [0]
  226. 'postcss-plugin': {}, // [1]
  227. 'postcss-plugin': {} // [2]
  228. }
  229. }
  230. ```
  231. <h2 align="center">Context</h2>
  232. 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}`
  233. > ℹ️ 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`
  234. <h2 align="center">Examples</h2>
  235. **postcss.config.js**
  236. ```js
  237. module.exports = (ctx) => ({
  238. parser: ctx.parser ? 'sugarss' : false,
  239. map: ctx.env === 'development' ? ctx.map : false,
  240. plugins: {
  241. 'postcss-import': {},
  242. 'postcss-nested': {},
  243. cssnano: ctx.env === 'production' ? {} : false
  244. }
  245. })
  246. ```
  247. <div align="center">
  248. <img width="80" height="80" src="https://worldvectorlogo.com/logos/nodejs-icon.svg">
  249. </div>
  250. ```json
  251. "scripts": {
  252. "build": "NODE_ENV=production node postcss",
  253. "start": "NODE_ENV=development node postcss"
  254. }
  255. ```
  256. ### `Async`
  257. ```js
  258. const { readFileSync } = require('fs')
  259. const postcss = require('postcss')
  260. const postcssrc = require('postcss-load-config')
  261. const css = readFileSync('index.sss', 'utf8')
  262. const ctx = { parser: true, map: 'inline' }
  263. postcssrc(ctx).then(({ plugins, options }) => {
  264. postcss(plugins)
  265. .process(css, options)
  266. .then((result) => console.log(result.css))
  267. })
  268. ```
  269. ### `Sync`
  270. ```js
  271. const { readFileSync } = require('fs')
  272. const postcss = require('postcss')
  273. const postcssrc = require('postcss-load-config')
  274. const css = readFileSync('index.sss', 'utf8')
  275. const ctx = { parser: true, map: 'inline' }
  276. const { plugins, options } = postcssrc.sync(ctx)
  277. ```
  278. <div align="center">
  279. <img width="80" height="80" halign="10" src="https://worldvectorlogo.com/logos/gulp.svg">
  280. </div>
  281. ```json
  282. "scripts": {
  283. "build": "NODE_ENV=production gulp",
  284. "start": "NODE_ENV=development gulp"
  285. }
  286. ```
  287. ```js
  288. const { task, src, dest, series, watch } = require('gulp')
  289. const postcss = require('gulp-postcssrc')
  290. const css = () => {
  291. src('src/*.css')
  292. .pipe(postcss())
  293. .pipe(dest('dest'))
  294. })
  295. task('watch', () => {
  296. watch(['src/*.css', 'postcss.config.js'], css)
  297. })
  298. task('default', series(css, 'watch'))
  299. ```
  300. <div align="center">
  301. <img width="80" height="80" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
  302. </div>
  303. ```json
  304. "scripts": {
  305. "build": "NODE_ENV=production webpack",
  306. "start": "NODE_ENV=development webpack-dev-server"
  307. }
  308. ```
  309. **webpack.config.js**
  310. ```js
  311. module.exports = (env) => ({
  312. module: {
  313. rules: [
  314. {
  315. test: /\.css$/,
  316. use: [
  317. 'style-loader',
  318. 'css-loader',
  319. 'postcss-loader'
  320. ]
  321. }
  322. ]
  323. }
  324. })
  325. ```
  326. <h2 align="center">Maintainers</h2>
  327. <table>
  328. <tbody>
  329. <tr>
  330. <td align="center">
  331. <img width="150" height="150"
  332. src="https://github.com/michael-ciniawsky.png?v=3&s=150">
  333. <br />
  334. <a href="https://github.com/michael-ciniawsky">Michael Ciniawsky</a>
  335. </td>
  336. <td align="center">
  337. <img width="150" height="150"
  338. src="https://github.com/ertrzyiks.png?v=3&s=150">
  339. <br />
  340. <a href="https://github.com/ertrzyiks">Mateusz Derks</a>
  341. </td>
  342. </tr>
  343. <tbody>
  344. </table>
  345. <h2 align="center">Contributors</h2>
  346. <table>
  347. <tbody>
  348. <tr>
  349. <td align="center">
  350. <img width="150" height="150"
  351. src="https://github.com/sparty02.png?v=3&s=150">
  352. <br />
  353. <a href="https://github.com/sparty02">Ryan Dunckel</a>
  354. </td>
  355. <td align="center">
  356. <img width="150" height="150"
  357. src="https://github.com/pcgilday.png?v=3&s=150">
  358. <br />
  359. <a href="https://github.com/pcgilday">Patrick Gilday</a>
  360. </td>
  361. <td align="center">
  362. <img width="150" height="150"
  363. src="https://github.com/daltones.png?v=3&s=150">
  364. <br />
  365. <a href="https://github.com/daltones">Dalton Santos</a>
  366. </td>
  367. <td align="center">
  368. <img width="150" height="150"
  369. src="https://github.com/fwouts.png?v=3&s=150">
  370. <br />
  371. <a href="https://github.com/fwouts">François Wouts</a>
  372. </td>
  373. </tr>
  374. <tbody>
  375. </table>
  376. [npm]: https://img.shields.io/npm/v/postcss-load-config.svg
  377. [npm-url]: https://npmjs.com/package/postcss-load-config
  378. [node]: https://img.shields.io/node/v/postcss-load-plugins.svg
  379. [node-url]: https://nodejs.org/
  380. [deps]: https://david-dm.org/michael-ciniawsky/postcss-load-config.svg
  381. [deps-url]: https://david-dm.org/michael-ciniawsky/postcss-load-config
  382. [test]: http://img.shields.io/travis/michael-ciniawsky/postcss-load-config.svg
  383. [test-url]: https://travis-ci.org/michael-ciniawsky/postcss-load-config
  384. [cover]: https://coveralls.io/repos/github/michael-ciniawsky/postcss-load-config/badge.svg
  385. [cover-url]: https://coveralls.io/github/michael-ciniawsky/postcss-load-config
  386. [style]: https://img.shields.io/badge/code%20style-standard-yellow.svg
  387. [style-url]: http://standardjs.com/
  388. [chat]: https://img.shields.io/gitter/room/postcss/postcss.svg
  389. [chat-url]: https://gitter.im/postcss/postcss
  390. ## Security Contact
  391. To report a security vulnerability, please use the [Tidelift security contact].
  392. Tidelift will coordinate the fix and disclosure.
  393. [Tidelift security contact]: https://tidelift.com/security