Skip to content

Commit 2ff1735

Browse files
kisenkaai
authored andcommittedApr 16, 2018
fix: restore loader object in postcss config context (#355)
* fix: restore loader object in postcss config context * test: add testcase which checking webpack object exists in config context
1 parent ce2adca commit 2ff1735

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed
 

‎lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ module.exports = function loader (css, map, meta) {
9090
}
9191
}
9292

93+
rc.ctx.webpack = this;
94+
9395
return postcssrc(rc.ctx, rc.path, { argv: false })
9496
}).then((config) => {
9597
if (!config) config = {}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const postcss = require('postcss')
4+
5+
// This plugin creates asset file in webpack compilation
6+
module.exports = postcss.plugin('plugin', (ctx) => {
7+
ctx.webpack._compilation.assets['asset.txt'] = {
8+
source() {
9+
return '123';
10+
},
11+
size() {
12+
return 0;
13+
}
14+
}
15+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = (ctx) => ({
2+
plugins: [
3+
require('./plugin')(ctx)
4+
]
5+
})

‎test/options/config.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,24 @@ describe('Options', () => {
7575
expect(src).toMatchSnapshot()
7676
})
7777
})
78+
79+
test('Pass loader object to config context', () => {
80+
const config = {
81+
loader: {
82+
options: {
83+
config: {
84+
path: 'test/fixtures/config/context/postcss.config.js'
85+
}
86+
}
87+
}
88+
}
89+
90+
return webpack('css/index.js', config).then((stats) => {
91+
const assets = stats.compilation.assets;
92+
const expectedAssetName = 'asset.txt';
93+
94+
expect(expectedAssetName in assets).toBeTruthy();
95+
expect(assets[expectedAssetName].source()).toBe('123');
96+
})
97+
})
7898
})

0 commit comments

Comments
 (0)
Please sign in to comment.