From a78c580baf24e0d6fc66195e7aef30b641975837 Mon Sep 17 00:00:00 2001 From: Michael Ciniawsky Date: Wed, 12 Oct 2016 07:11:12 +0200 Subject: [PATCH] feat(index): function support, jsdoc, cleanups --- index.js | 66 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 5eea69a..c03cdc4 100644 --- a/index.js +++ b/index.js @@ -1,28 +1,66 @@ // ------------------------------------ -// #POSTCSS - LOAD CONFIG +// #POSTCSS - LOAD CONFIG - INDEX // ------------------------------------ 'use strict' -var config = require('cosmiconfig') +const config = require('cosmiconfig') -var loadOptions = require('postcss-load-options/lib/loadOptions.js') -var loadPlugins = require('postcss-load-plugins/lib/loadPlugins.js') +const loadOptions = require('postcss-load-options/lib/options.js') +const loadPlugins = require('postcss-load-plugins/lib/plugins.js') -module.exports = function loadConfig (options) { - return config('postcss') - .catch(function (error) { - console.log(error) - }) +/** + * @author Michael Ciniawsky (@michael-ciniawsky) + * @description Autoload Config for PostCSS + * + * @module postcss-load-config + * @version 1.0.0 + * + * @requires comsiconfig + * @requires postcss-load-options + * @requires postcss-load-plugins + * + * @method postcssrc + * + * @param {Object} ctx Context + * @param {String} path Config Directory + * @param {Object} options Config Options + * + * @return {Promise} config PostCSS Plugins, PostCSS Options + */ +module.exports = function postcssrc (ctx, path, options) { + const defaults = { + cwd: process.cwd(), + env: process.env.NODE_ENV + } + + ctx = Object.assign(defaults, ctx) || defaults + path = path || process.cwd() + options = options || {} + + return config('postcss', options) + .load(path) .then(function (result) { - return result || {} + result = result.config || {} + return result }) - .then(function (result) { - var config = result.config || { plugins: [] } + .then(function (config) { + if (typeof config === 'function') { + config = config(ctx) + } else { + config = Object.assign(config, ctx) + } + + if (!config.plugins) { + config.plugins = [] + } return { - plugins: loadPlugins(config, options), - options: loadOptions(config, options) + plugins: loadPlugins(config), + options: loadOptions(config) } }) + .catch(function (err) { + console.log(err) + }) }