From dc770751f035b05fe632e09ea451fa272bf8b456 Mon Sep 17 00:00:00 2001 From: kisenka Date: Fri, 6 Apr 2018 17:57:50 +0300 Subject: [PATCH 1/2] fix: restore loader object in postcss config context --- lib/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/index.js b/lib/index.js index d876c069..5afceeef 100644 --- a/lib/index.js +++ b/lib/index.js @@ -90,6 +90,8 @@ module.exports = function loader (css, map, meta) { } } + rc.ctx.webpack = this; + return postcssrc(rc.ctx, rc.path, { argv: false }) }).then((config) => { if (!config) config = {} From 53a000590142636c54c9a3459fff8643259b1329 Mon Sep 17 00:00:00 2001 From: kisenka Date: Fri, 6 Apr 2018 18:40:22 +0300 Subject: [PATCH 2/2] test: add testcase which checking webpack object exists in config context --- test/fixtures/config/context/plugin.js | 15 ++++++++++++++ .../fixtures/config/context/postcss.config.js | 5 +++++ test/options/config.test.js | 20 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 test/fixtures/config/context/plugin.js create mode 100644 test/fixtures/config/context/postcss.config.js diff --git a/test/fixtures/config/context/plugin.js b/test/fixtures/config/context/plugin.js new file mode 100644 index 00000000..027eb431 --- /dev/null +++ b/test/fixtures/config/context/plugin.js @@ -0,0 +1,15 @@ +'use strict' + +const postcss = require('postcss') + +// This plugin creates asset file in webpack compilation +module.exports = postcss.plugin('plugin', (ctx) => { + ctx.webpack._compilation.assets['asset.txt'] = { + source() { + return '123'; + }, + size() { + return 0; + } + } +}) diff --git a/test/fixtures/config/context/postcss.config.js b/test/fixtures/config/context/postcss.config.js new file mode 100644 index 00000000..4ba08736 --- /dev/null +++ b/test/fixtures/config/context/postcss.config.js @@ -0,0 +1,5 @@ +module.exports = (ctx) => ({ + plugins: [ + require('./plugin')(ctx) + ] +}) diff --git a/test/options/config.test.js b/test/options/config.test.js index 78135158..cc98c00c 100644 --- a/test/options/config.test.js +++ b/test/options/config.test.js @@ -75,4 +75,24 @@ describe('Options', () => { expect(src).toMatchSnapshot() }) }) + + test('Pass loader object to config context', () => { + const config = { + loader: { + options: { + config: { + path: 'test/fixtures/config/context/postcss.config.js' + } + } + } + } + + return webpack('css/index.js', config).then((stats) => { + const assets = stats.compilation.assets; + const expectedAssetName = 'asset.txt'; + + expect(expectedAssetName in assets).toBeTruthy(); + expect(assets[expectedAssetName].source()).toBe('123'); + }) + }) })