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 = {} 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'); + }) + }) })