|
1 | 1 | // ------------------------------------ |
2 | | -// #POSTCSS - LOAD CONFIG |
| 2 | +// #POSTCSS - LOAD CONFIG - INDEX |
3 | 3 | // ------------------------------------ |
4 | 4 |
|
5 | 5 | 'use strict' |
6 | 6 |
|
7 | | -var config = require('cosmiconfig') |
| 7 | +const config = require('cosmiconfig') |
8 | 8 |
|
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') |
11 | 11 |
|
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) |
17 | 43 | .then(function (result) { |
18 | | - return result || {} |
| 44 | + result = result.config || {} |
| 45 | + return result |
19 | 46 | }) |
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 | + } |
22 | 57 |
|
23 | 58 | return { |
24 | | - plugins: loadPlugins(config, options), |
25 | | - options: loadOptions(config, options) |
| 59 | + plugins: loadPlugins(config), |
| 60 | + options: loadOptions(config) |
26 | 61 | } |
27 | 62 | }) |
| 63 | + .catch(function (err) { |
| 64 | + console.log(err) |
| 65 | + }) |
28 | 66 | } |
0 commit comments