Skip to content

Commit

Permalink
Merge fc37f1f into 11f7173
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed May 11, 2020
2 parents 11f7173 + fc37f1f commit ef61ce3
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.
36 changes: 22 additions & 14 deletions index.js
Expand Up @@ -41,23 +41,31 @@ module.exports = function pluginsrc (ctx, path, options) {

if (!ctx.env) process.env.NODE_ENV = 'development'

var file
function handleResult (result) {
if (!result) throw new Error('No PostCSS Config found in: ' + path)

return config('postcss', options)
.load(path)
.then(function (result) {
if (!result) throw new Error('No PostCSS Config found in: ' + path)
file = result ? result.filepath : ''

file = result ? result.filepath : ''
return result ? result.config : {}
}

return result ? result.config : {}
})
.then(function (plugins) {
if (typeof plugins === 'function') plugins = plugins(ctx)
else plugins = assign(plugins, ctx)
function composePlugins (plugins) {
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 }
})
return { plugins: loadPlugins(plugins), file: file }
}

var file

if (options.sync) {
return composePlugins(handleResult(config('postcss', options).load(path)))
}

return config('postcss', options)
.load(path)
.then(handleResult)
.then(composePlugins)
}
4 changes: 2 additions & 2 deletions lib/plugins.js
Expand Up @@ -26,7 +26,7 @@ module.exports = function plugins (config) {
if (plugin.default) plugin = plugin.default

if (
!(typeof plugin === 'object' && Array.isArray(plugin.plugins) ||
!((typeof plugin === 'object' && Array.isArray(plugin.plugins)) ||
typeof plugin === 'function')
) {
throw new TypeError('Invalid PostCSS Plugin found: ' + '[' + i + ']')
Expand Down Expand Up @@ -70,7 +70,7 @@ module.exports = function plugins (config) {
if (plugin.default) plugin = plugin.default

if (
!(typeof plugin === 'object' && Array.isArray(plugin.plugins) ||
!((typeof plugin === 'object' && Array.isArray(plugin.plugins)) ||
typeof plugin === 'function')
) {
throw new TypeError('Invalid PostCSS Plugin found: ' + '[' + i + ']')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -27,7 +27,7 @@
"postcss-import": "^11.1.0",
"postcss-nested": "^1.0.0",
"postcss-sprites": "^4.2.0",
"standard": "^10.0.3",
"standard": "^14.3.4",
"standard-changelog": "1.0.8",
"sugarss": "^0.2.0"
},
Expand Down
6 changes: 3 additions & 3 deletions test/err/index.js
Expand Up @@ -10,21 +10,21 @@ var pluginsrc = require('../..')

test('No Config - {Error} - Load Plugins', function (t) {
return pluginsrc({}, 'test').catch(function (err) {
t.is(err.message, 'No PostCSS Config found in: /home/travis/build/michael-ciniawsky/postcss-load-plugins/test')
t.is(/No PostCSS Config found in: (.*)\/postcss-load-plugins\/test/.test(err.message), true)
})
})

test('No Plugin - {Error} - Load Plugins', function (t) {
return pluginsrc({}, 'test/err/object').catch(function (err) {
t.is(err.message, "Loading PostCSS Plugin failed: Cannot find module 'no plugin'")
t.is(err.message.indexOf("Loading PostCSS Plugin failed: Cannot find module 'no plugin'") !== -1, true)
})
})

test('No Plugin (Options) - {Error} - Load Plugins', function (t) {
var ctx = { next: 1 }

return pluginsrc(ctx, 'test/err/object').catch(function (err) {
t.is(err.message, "Loading PostCSS Plugin failed: Cannot find module 'no plugin options'")
t.is(err.message.indexOf("Loading PostCSS Plugin failed: Cannot find module 'no plugin options'") !== -1, true)
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/js/object/postcss.config.js
Expand Up @@ -5,7 +5,7 @@ module.exports = function (ctx) {
'postcss-nested': {},
'postcss-sprites': {},
'postcss-cssnext': { warnForDuplicates: false },
'cssnano': ctx.env === 'production' ? {} : false
cssnano: ctx.env === 'production' ? {} : false
}
}
}
46 changes: 46 additions & 0 deletions test/rc/index.js
Expand Up @@ -68,3 +68,49 @@ test('.postcssrc - {Object} - Process SSS', function (t) {
})
})
})

test('.postcssrc - {Object} - Load Plugins Sync', function (t) {
const config = pluginsrc({}, 'test/rc', { sync: true })
var plugins = config.plugins

t.is(plugins.length, 4)
t.is(plugins[0].postcssPlugin, 'postcss-import')
t.is(plugins[1].postcssPlugin, 'postcss-nested')
t.is(plugins[2].postcssPlugin, 'postcss-sprites')
t.is(plugins[3].postcssPlugin, 'postcss-cssnext')

t.is(config.file, path.resolve('test/rc/.postcssrc'))
})

test('.postcssrc - {Object} - Process CSS Sync', function (t) {
const config = pluginsrc({}, 'test/rc', { sync: true })
var plugins = config.plugins

var options = {
from: 'test/rc/fixtures/index.css',
to: 'test/rc/expect/index.css'
}

return postcss(plugins)
.process(fixture('index.css'), options)
.then(function (result) {
t.is(expect('index.css'), result.css)
})
})

test('.postcssrc - {Object} - Process SSS', function (t) {
const config = pluginsrc({}, 'test/rc', { sync: true })
var plugins = config.plugins

var options = {
parser: require('sugarss'),
from: 'test/rc/fixtures/index.sss',
to: 'test/rc/expect/index.sss'
}

return postcss(plugins)
.process(fixture('index.sss'), options)
.then(function (result) {
t.is(expect('index.sss'), result.css)
})
})

0 comments on commit ef61ce3

Please sign in to comment.