index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict'
  2. const resolve = require('path').resolve
  3. const config = require('cosmiconfig')
  4. const loadOptions = require('./options.js')
  5. const loadPlugins = require('./plugins.js')
  6. /**
  7. * Process the result from cosmiconfig
  8. *
  9. * @param {Object} ctx Config Context
  10. * @param {Object} result Cosmiconfig result
  11. *
  12. * @return {Object} PostCSS Config
  13. */
  14. const processResult = (ctx, result) => {
  15. let file = result.filepath || ''
  16. let config = result.config || {}
  17. if (typeof config === 'function') {
  18. config = config(ctx)
  19. } else {
  20. config = Object.assign({}, config, ctx)
  21. }
  22. if (!config.plugins) {
  23. config.plugins = []
  24. }
  25. return {
  26. plugins: loadPlugins(config, file),
  27. options: loadOptions(config, file),
  28. file: file
  29. }
  30. }
  31. /**
  32. * Builds the Config Context
  33. *
  34. * @param {Object} ctx Config Context
  35. *
  36. * @return {Object} Config Context
  37. */
  38. const createContext = (ctx) => {
  39. /**
  40. * @type {Object}
  41. *
  42. * @prop {String} cwd=process.cwd() Config search start location
  43. * @prop {String} env=process.env.NODE_ENV Config Enviroment, will be set to `development` by `postcss-load-config` if `process.env.NODE_ENV` is `undefined`
  44. */
  45. ctx = Object.assign({
  46. cwd: process.cwd(),
  47. env: process.env.NODE_ENV
  48. }, ctx)
  49. if (!ctx.env) {
  50. process.env.NODE_ENV = 'development'
  51. }
  52. return ctx
  53. }
  54. /**
  55. * Load Config
  56. *
  57. * @method rc
  58. *
  59. * @param {Object} ctx Config Context
  60. * @param {String} path Config Path
  61. * @param {Object} options Config Options
  62. *
  63. * @return {Promise} config PostCSS Config
  64. */
  65. const rc = (ctx, path, options) => {
  66. /**
  67. * @type {Object} The full Config Context
  68. */
  69. ctx = createContext(ctx)
  70. /**
  71. * @type {String} `process.cwd()`
  72. */
  73. path = path ? resolve(path) : process.cwd()
  74. return config('postcss', options)
  75. .search(path)
  76. .then((result) => {
  77. if (!result) {
  78. throw new Error(`No PostCSS Config found in: ${path}`)
  79. }
  80. return processResult(ctx, result)
  81. })
  82. }
  83. rc.sync = (ctx, path, options) => {
  84. /**
  85. * @type {Object} The full Config Context
  86. */
  87. ctx = createContext(ctx)
  88. /**
  89. * @type {String} `process.cwd()`
  90. */
  91. path = path ? resolve(path) : process.cwd()
  92. const result = config('postcss', options).searchSync(path)
  93. if (!result) {
  94. throw new Error(`No PostCSS Config found in: ${path}`)
  95. }
  96. return processResult(ctx, result)
  97. }
  98. /**
  99. * Autoload Config for PostCSS
  100. *
  101. * @author Michael Ciniawsky @michael-ciniawsky <michael.ciniawsky@gmail.com>
  102. * @license MIT
  103. *
  104. * @module postcss-load-config
  105. * @version 2.1.0
  106. *
  107. * @requires comsiconfig
  108. * @requires ./options
  109. * @requires ./plugins
  110. */
  111. module.exports = rc