Skip to content

Commit

Permalink
feat(index): improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky committed Jan 11, 2017
1 parent a64bb03 commit f3a4048
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ------------------------------------
// #POSTCSS - LOAD PLUGINS - INDEX
// # POSTCSS - LOAD PLUGINS - INDEX
// ------------------------------------

'use strict'
Expand All @@ -12,12 +12,13 @@ var assign = require('object-assign')
var loadPlugins = require('./lib/plugins')

/**
* Autoload Plugins for PostCSS
*
* @author Michael Ciniawsky (@michael-ciniawsky) <michael.ciniawsky@gmail.com>
* @description Autoload Plugins for PostCSS
* @license MIT
*
* @module postcss-load-plugins
* @version 1.0.0
* @version 2.2.0
*
* @requires cosmiconfig
* @requires object-assign
Expand All @@ -38,41 +39,25 @@ module.exports = function pluginsrc (ctx, path, 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 === undefined) {
console.log('PostCSS Plugins could not be loaded.' + path)
}
if (!result) throw new Error('No PostCSS Config found in: ' + path)

file = result ? result.filepath : ''

return result ? result.config : {}
})
.then(function (plugins) {
if (typeof plugins === 'function') {
plugins = plugins(ctx)
}
if (typeof result === 'object') {
plugins = assign(plugins, ctx)
}
if (typeof plugins === 'function') plugins = plugins(ctx)
else plugins = assign(plugins, ctx)

if (!plugins.plugins) {
plugins.plugins = []
}
if (!plugins.plugins) plugins.plugins = []

return {
plugins: loadPlugins(plugins),
file: file
}
})
.catch(function (err) {
console.log(err)
return { plugins: loadPlugins(plugins), file: file }
})
}

0 comments on commit f3a4048

Please sign in to comment.