@@ -8,17 +8,42 @@ const loadOptions = require('./options.js')
88const loadPlugins = require ( './plugins.js' )
99
1010/**
11- * Load Config
11+ * Process the result from cosmiconfig
1212 *
13- * @method rc
13+ * @param {Object } ctx Config Context
14+ * @param {Object } result Cosmiconfig result
15+ *
16+ * @return {Object } PostCSS Config
17+ */
18+ const processResult = ( ctx , result ) => {
19+ let file = result . filepath || ''
20+ let config = result . config || { }
21+
22+ if ( typeof config === 'function' ) {
23+ config = config ( ctx )
24+ } else {
25+ config = Object . assign ( { } , config , ctx )
26+ }
27+
28+ if ( ! config . plugins ) {
29+ config . plugins = [ ]
30+ }
31+
32+ return {
33+ plugins : loadPlugins ( config , file ) ,
34+ options : loadOptions ( config , file ) ,
35+ file : file
36+ }
37+ }
38+
39+ /**
40+ * Builds the Config Context
1441 *
1542 * @param {Object } ctx Config Context
16- * @param {String } path Config Path
17- * @param {Object } options Config Options
1843 *
19- * @return {Promise } config PostCSS Config
44+ * @return {Object } Config Context
2045 */
21- const rc = ( ctx , path , options ) => {
46+ const createContext = ( ctx ) => {
2247 /**
2348 * @type {Object }
2449 *
@@ -29,24 +54,35 @@ const rc = (ctx, path, options) => {
2954 cwd : process . cwd ( ) ,
3055 env : process . env . NODE_ENV
3156 } , ctx )
57+
58+ if ( ! ctx . env ) {
59+ process . env . NODE_ENV = 'development'
60+ }
61+
62+ return ctx
63+ }
64+
65+ /**
66+ * Load Config
67+ *
68+ * @method rc
69+ *
70+ * @param {Object } ctx Config Context
71+ * @param {String } path Config Path
72+ * @param {Object } options Config Options
73+ *
74+ * @return {Promise } config PostCSS Config
75+ */
76+ const rc = ( ctx , path , options ) => {
3277 /**
33- * @type {String } `process.cwd()`
34- *
78+ * @type {Object } The full Config Context
3579 */
36- path = path ? resolve ( path ) : process . cwd ( )
80+ ctx = createContext ( ctx )
3781
3882 /**
39- * @type {Object }
40- *
41- * @prop {Boolean } rcExtensions=true
83+ * @type {String } `process.cwd()`
4284 */
43- options = Object . assign ( {
44- rcExtensions : true
45- } , options )
46-
47- if ( ! ctx . env ) {
48- process . env . NODE_ENV = 'development'
49- }
85+ path = path ? resolve ( path ) : process . cwd ( )
5086
5187 return config ( 'postcss' , options )
5288 . search ( path )
@@ -55,25 +91,28 @@ const rc = (ctx, path, options) => {
5591 throw new Error ( `No PostCSS Config found in: ${ path } ` )
5692 }
5793
58- let file = result . filepath || ''
59- let config = result . config || { }
94+ return processResult ( ctx , result )
95+ } )
96+ }
6097
61- if ( typeof config === 'function' ) {
62- config = config ( ctx )
63- } else {
64- config = Object . assign ( { } , config , ctx )
65- }
98+ rc . sync = ( ctx , path , options ) => {
99+ /**
100+ * @type { Object } The full Config Context
101+ */
102+ ctx = createContext ( ctx )
66103
67- if ( ! config . plugins ) {
68- config . plugins = [ ]
69- }
104+ /**
105+ * @type {String } `process.cwd()`
106+ */
107+ path = path ? resolve ( path ) : process . cwd ( )
70108
71- return {
72- plugins : loadPlugins ( config , file ) ,
73- options : loadOptions ( config , file ) ,
74- file : file
75- }
76- } )
109+ const result = config ( 'postcss' , options ) . searchSync ( path )
110+
111+ if ( ! result ) {
112+ throw new Error ( `No PostCSS Config found in: ${ path } ` )
113+ }
114+
115+ return processResult ( ctx , result )
77116}
78117
79118/**
0 commit comments