Skip to content

Commit

Permalink
fix(opts): inconsistency, update test
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Jan 3, 2017
1 parent b6f0f40 commit 44a16c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ const resolvePluginsSync = (plugins, opts) => {
return []
}

opts = extend({ prefix: '', first: false }, opts)
opts = extend({ prefix: '' }, opts)

return arrayify(plugins).map((plugin) => {
// allows `plugins: ['foo', 'bar', 'baz']`
if (typeof plugin === 'string') {
let id = `${opts.prefix}${plugin}`
return require(id)(opts.first)
let func = require(id)
let argz = opts.args ? opts.args : [opts.first]
return func.apply(opts.context, argz)
}

// allows nesting and passing options to each plugin
Expand All @@ -41,11 +43,11 @@ const resolvePluginsSync = (plugins, opts) => {

if (typeof plugin[0] === 'string') {
let id = `${opts.prefix}${plugin[0]}`
return require(id).apply(opts.thisArg, args)
return require(id).apply(opts.context, args)
}
if (typeof plugin[0] === 'function') {
let fn = plugin[0]
return fn.apply(opts.thisArg, args)
return fn.apply(opts.context, args)
}
if (typeof plugin[0] === 'object') {
return plugin[0]
Expand Down
10 changes: 6 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ test('8. opts.first for browserify-style transforms where 1st is `filename`, 2nd

test('9. if passed opts.args respect it more than per plugin options', function (done) {
const ret = resolve([
[towie, { x: 'b' }]
[towie, { x: 'b' }],
'./fixtures/foo-plugin-one'
], {
args: ['haha', { hello: 'world' }]
})

test.strictEqual(ret.length, 1)
test.strictEqual(ret.length, 2)

test.strictEqual(ret[0].name, 'two')
test.strictEqual(ret[0].filename, 'haha')
test.strictEqual(ret[0].opts.hello, 'world')
test.strictEqual(ret[0].filename, ret[1].filename)
test.strictEqual(ret[0].opts.hello, ret[1].opts.hello)
done()
})

0 comments on commit 44a16c3

Please sign in to comment.