diff --git a/index.js b/index.js index 3d5c906..c3f814b 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,11 @@ // ------------------------------------ -// #POSTCSS - LOAD CONFIG - INDEX +// # POSTCSS - LOAD CONFIG - INDEX // ------------------------------------ 'use strict' +var resolve = require('path').resolve + var config = require('cosmiconfig') var assign = require('object-assign') @@ -11,11 +13,13 @@ var loadOptions = require('postcss-load-options/lib/options.js') var loadPlugins = require('postcss-load-plugins/lib/plugins.js') /** + * Autoload Config for PostCSS + * * @author Michael Ciniawsky (@michael-ciniawsky) - * @description Autoload Config for PostCSS + * @license MIT * * @module postcss-load-config - * @version 1.0.0 + * @version 1.1.0 * * @requires comsiconfig * @requires object-assign @@ -28,47 +32,38 @@ var loadPlugins = require('postcss-load-plugins/lib/plugins.js') * @param {String} path Config Directory * @param {Object} options Config Options * - * @return {Promise} config PostCSS Plugins, PostCSS Options + * @return {Promise} config PostCSS Config */ module.exports = function postcssrc (ctx, path, options) { - var defaults = { cwd: process.cwd(), env: process.env.NODE_ENV } + ctx = assign({ cwd: process.cwd(), env: process.env.NODE_ENV }, ctx) + + path = path ? resolve(path) : process.cwd() - ctx = assign(defaults, ctx) - path = path || process.cwd() - options = options || {} + options = assign({}, options) - if (ctx.env === undefined) { - process.env.NODE_ENV = 'development' - } + if (!ctx.env) process.env.NODE_ENV = 'development' + + var file return config('postcss', options) .load(path) .then(function (result) { - if (!result) { - console.log( - 'PostCSS Config could not be loaded. Please check your PostCSS Config.' - ) - } + if (!result) throw Error('No PostCSS Config found in: ' + path) + + file = result ? result.filepath : '' return result ? result.config : {} }) .then(function (config) { - if (typeof config === 'function') { - config = config(ctx) - } else { - config = assign(config, ctx) - } + if (typeof config === 'function') config = config(ctx) + else config = assign(config, ctx) - if (!config.plugins) { - config.plugins = [] - } + if (!config.plugins) config.plugins = [] return { plugins: loadPlugins(config), - options: loadOptions(config) + options: loadOptions(config), + file: file } }) - .catch(function (err) { - console.log(err) - }) }