Skip to content

Commit

Permalink
feat(lib): 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 2d73031 commit a64bb03
Showing 1 changed file with 17 additions and 49 deletions.
66 changes: 17 additions & 49 deletions lib/plugins.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ------------------------------------
// #POSTCSS - LOAD PLUGINS - PLUGINS
// # POSTCSS - LOAD PLUGINS - PLUGINS
// ------------------------------------

'use strict'
Expand All @@ -15,37 +15,21 @@ module.exports = function plugins (config) {
var plugins = []

if (Array.isArray(config.plugins)) {
plugins = config.plugins

plugins = plugins.filter(Boolean)
plugins = config.plugins.filter(Boolean)

if (plugins.length && plugins.length > 0) {
plugins.forEach(function (plugin) {
if (!plugin) {
var error = new Error(
'Loading PostCSS Plugin failed. Please check your PostCSS Config'
)

console.log(error.message)

return
}
plugins.forEach(function (plugin, i) {
if (!plugin) throw new Error('Loading PostCSS Plugin failed')

if (plugin.postcss) {
plugin = plugin.postcss
}
if (plugin.postcss) plugin = plugin.postcss

if (plugin.default) {
plugin = plugin.default
}
if (plugin.default) plugin = plugin.default

if (
!(typeof plugin === 'object' && Array.isArray(plugin.plugins) ||
typeof plugin === 'function')
) {
throw new TypeError(
'Invalid PostCSS Plugin found. Please check your PostCSS Config'
)
throw new TypeError('Invalid PostCSS Plugin found: ' + '[' + i + ']')
}
})
}
Expand All @@ -59,13 +43,17 @@ module.exports = function plugins (config) {
try {
return require(plugin)
} catch (err) {
console.log(err.message)
err.message = 'Loading PostCSS Plugin failed: ' + err.message

throw err
}
} else {
try {
return require(plugin)(options)
} catch (err) {
console.log(err.message)
err.message = 'Loading PostCSS Plugin failed: ' + err.message

throw err
}
}
}
Expand All @@ -74,38 +62,18 @@ module.exports = function plugins (config) {
.filter(function (plugin) {
return config[plugin] !== false ? plugin : ''
})
.forEach(function (plugin) {
.forEach(function (plugin, i) {
plugin = load(plugin, config[plugin])

if (!plugin) {
var error = new Error(
'Loading PostCSS Plugin failed. Please check your PostCSS Config'
)
if (plugin.postcss) plugin = plugin.postcss

console.log(error.message)

return
}

if (plugin.postcss) {
plugin = plugin.postcss
}

if (plugin.default) {
plugin = plugin.default
}
if (plugin.default) plugin = plugin.default

if (
!(typeof plugin === 'object' && Array.isArray(plugin.plugins) ||
typeof plugin === 'function')
) {
var err = new TypeError(
'Invalid PostCSS Plugin found. Please check your PostCSS Config'
)

console.log(err.message)

return
throw new TypeError('Invalid PostCSS Plugin found: ' + '[' + i + ']')
}

return plugins.push(plugin)
Expand Down

0 comments on commit a64bb03

Please sign in to comment.