Skip to content

Commit 2ff1735

Browse files
kisenkaai
authored andcommitted
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

Lines changed: 2 additions & 0 deletions
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 = {}
Lines changed: 15 additions & 0 deletions
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+
})
Lines changed: 5 additions & 0 deletions
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

Lines changed: 20 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy