From 817680d09ba285f7a438ec6ea486736faafeba2c Mon Sep 17 00:00:00 2001 From: Michael Ciniawsky Date: Sat, 14 Oct 2017 16:31:19 +0200 Subject: [PATCH] fix(lib/options): handle `{Object}` return (`options.plugins`) --- lib/options.js | 2 +- lib/options.json | 7 ++++ .../__snapshots__/plugins.test.js.snap | 6 ++- test/options/plugins.test.js | 38 ++++++++++++++++++- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/options.js b/lib/options.js index 56cff551..980e9366 100644 --- a/lib/options.js +++ b/lib/options.js @@ -9,7 +9,7 @@ module.exports = function parseOptions (params) { if (typeof params.plugins === 'undefined') plugins = [] else if (Array.isArray(params.plugins)) plugins = params.plugins - else plugins = params.plugins + else plugins = [ params.plugins ] const options = {} diff --git a/lib/options.json b/lib/options.json index dd5f23c0..f1b194aa 100644 --- a/lib/options.json +++ b/lib/options.json @@ -25,6 +25,13 @@ "stringifier": { "type": [ "string", "object" ] }, + "plugins": { + "anyOf": [ + { "type": "array" }, + { "type": "object" }, + { "instanceof": "Function" } + ] + }, "sourceMap": { "type": [ "string", "boolean" ] } diff --git a/test/options/__snapshots__/plugins.test.js.snap b/test/options/__snapshots__/plugins.test.js.snap index 913df0fe..e2b12345 100644 --- a/test/options/__snapshots__/plugins.test.js.snap +++ b/test/options/__snapshots__/plugins.test.js.snap @@ -2,4 +2,8 @@ exports[`Options Plugins - {Array} 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`; -exports[`Options Plugins - {Function} 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`; +exports[`Options Plugins - {Function} - {Array} 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`; + +exports[`Options Plugins - {Function} - {Object} 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`; + +exports[`Options Plugins - {Object} 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`; diff --git a/test/options/plugins.test.js b/test/options/plugins.test.js index ddfc1bf2..32293cc6 100644 --- a/test/options/plugins.test.js +++ b/test/options/plugins.test.js @@ -22,7 +22,25 @@ describe('Options', () => { }) }) - test('Plugins - {Function}', () => { + test('Plugins - {Object}', () => { + const config = { + loader: { + options: { + ident: 'postcss', + plugins: require('../fixtures/config/plugin') + } + } + } + + return webpack('css/index.js', config).then((stats) => { + const src = loader(stats).src + + expect(src).toEqual("module.exports = \"a { color: rgba(255, 0, 0, 1.0) }\\n\"") + expect(src).toMatchSnapshot() + }) + }) + + test('Plugins - {Function} - {Array}', () => { const config = { loader: { options: { @@ -39,4 +57,22 @@ describe('Options', () => { expect(src).toMatchSnapshot() }) }) + + test('Plugins - {Function} - {Object}', () => { + const config = { + loader: { + options: { + ident: 'postcss', + plugins: () => require('../fixtures/config/plugin')() + } + } + } + + return webpack('css/index.js', config).then((stats) => { + const src = loader(stats).src + + expect(src).toEqual("module.exports = \"a { color: rgba(255, 0, 0, 1.0) }\\n\"") + expect(src).toMatchSnapshot() + }) + }) })