-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(index): config.file, improve error handling
- Loading branch information
1 parent
39563c4
commit a6c32fd
Showing
1 changed file
with
23 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
// ------------------------------------ | ||
// #POSTCSS - LOAD CONFIG - INDEX | ||
// # POSTCSS - LOAD CONFIG - INDEX | ||
// ------------------------------------ | ||
|
||
'use strict' | ||
|
||
var resolve = require('path').resolve | ||
|
||
var config = require('cosmiconfig') | ||
var assign = require('object-assign') | ||
|
||
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) <[email protected]> | ||
* @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) | ||
}) | ||
} |