Skip to content

Commit a78c580

Browse files
feat(index): function support, jsdoc, cleanups
1 parent 9671219 commit a78c580

1 file changed

Lines changed: 52 additions & 14 deletions

File tree

index.js

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,66 @@
11
// ------------------------------------
2-
// #POSTCSS - LOAD CONFIG
2+
// #POSTCSS - LOAD CONFIG - INDEX
33
// ------------------------------------
44

55
'use strict'
66

7-
var config = require('cosmiconfig')
7+
const config = require('cosmiconfig')
88

9-
var loadOptions = require('postcss-load-options/lib/loadOptions.js')
10-
var loadPlugins = require('postcss-load-plugins/lib/loadPlugins.js')
9+
const loadOptions = require('postcss-load-options/lib/options.js')
10+
const loadPlugins = require('postcss-load-plugins/lib/plugins.js')
1111

12-
module.exports = function loadConfig (options) {
13-
return config('postcss')
14-
.catch(function (error) {
15-
console.log(error)
16-
})
12+
/**
13+
* @author Michael Ciniawsky (@michael-ciniawsky) <michael.ciniawsky@gmail.com>
14+
* @description Autoload Config for PostCSS
15+
*
16+
* @module postcss-load-config
17+
* @version 1.0.0
18+
*
19+
* @requires comsiconfig
20+
* @requires postcss-load-options
21+
* @requires postcss-load-plugins
22+
*
23+
* @method postcssrc
24+
*
25+
* @param {Object} ctx Context
26+
* @param {String} path Config Directory
27+
* @param {Object} options Config Options
28+
*
29+
* @return {Promise} config PostCSS Plugins, PostCSS Options
30+
*/
31+
module.exports = function postcssrc (ctx, path, options) {
32+
const defaults = {
33+
cwd: process.cwd(),
34+
env: process.env.NODE_ENV
35+
}
36+
37+
ctx = Object.assign(defaults, ctx) || defaults
38+
path = path || process.cwd()
39+
options = options || {}
40+
41+
return config('postcss', options)
42+
.load(path)
1743
.then(function (result) {
18-
return result || {}
44+
result = result.config || {}
45+
return result
1946
})
20-
.then(function (result) {
21-
var config = result.config || { plugins: [] }
47+
.then(function (config) {
48+
if (typeof config === 'function') {
49+
config = config(ctx)
50+
} else {
51+
config = Object.assign(config, ctx)
52+
}
53+
54+
if (!config.plugins) {
55+
config.plugins = []
56+
}
2257

2358
return {
24-
plugins: loadPlugins(config, options),
25-
options: loadOptions(config, options)
59+
plugins: loadPlugins(config),
60+
options: loadOptions(config)
2661
}
2762
})
63+
.catch(function (err) {
64+
console.log(err)
65+
})
2866
}

0 commit comments

Comments
 (0)