Skip to content

Commit

Permalink
fix: restore loader object in postcss config context (#355)
Browse files Browse the repository at this point in the history
* fix: restore loader object in postcss config context

* test: add testcase which checking webpack object exists in config context
  • Loading branch information
kisenka authored and ai committed Apr 16, 2018
1 parent ce2adca commit 2ff1735
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/config/context/plugin.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
})
5 changes: 5 additions & 0 deletions test/fixtures/config/context/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = (ctx) => ({
plugins: [
require('./plugin')(ctx)
]
})
20 changes: 20 additions & 0 deletions test/options/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})
})
})

0 comments on commit 2ff1735

Please sign in to comment.