Skip to content

Commit d2b8f39

Browse files
committed
Merge pull request webpack#34 from SimonDegraeve/support-postcss-js
Add support for postcss-js
2 parents 60e38f5 + 1d4d2be commit d2b8f39

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,35 @@ module.exports = {
123123
[webpack loader-context]: http://webpack.github.io/docs/loaders.html#loader-context
124124
[postcss-import]: https://github.com/postcss/postcss-import
125125

126+
## Integration with postcss-js
127+
128+
If you want to process styles written in Javascript you can use the [postcss-js] parser.
129+
130+
```js
131+
module.exports = {
132+
module: {
133+
loaders: [
134+
{
135+
test: /\.style.js$/,
136+
loader: "style-loader!css-loader!postcss-loader?parser=postcss-js"
137+
// Or using Babel
138+
// loader: "style-loader!css-loader!postcss-loader?parser=postcss-js!babel"
139+
}
140+
]
141+
},
142+
postcss: function (webpack) {
143+
return [
144+
postcssImport({
145+
addDependencyTo: webpack
146+
})
147+
];
148+
}
149+
}
150+
```
151+
152+
[postcss-js]: https://github.com/postcss/postcss-js
153+
154+
126155
## Custom Syntaxes
127156

128157
PostCSS can transforms styles in any syntax, not only in CSS.

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ module.exports = function (source, map) {
3939
var loader = this;
4040
var callback = this.async();
4141

42+
if (params.parser === 'postcss-js') {
43+
source = this.exec(source, this.resource);
44+
}
45+
4246
postcss(plugins)
4347
.process(source, opts).then(function (result) {
4448
result.warnings().forEach(function (msg) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"devDependencies": {
1717
"postcss-safe-parser": "1.0.1",
18+
"postcss-js": "0.1.0",
1819
"webpack-stream": "2.1.1",
1920
"gulp-eslint": "1.0.0",
2021
"gulp-mocha": "2.1.3",

test/cases/style.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
a: { color: 'black' }
3+
};

test/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ describe('postcss-loader', function () {
2323
expect(css).to.eql('a { one color: red }\n');
2424
});
2525

26+
it('processes CSS-in-JS', function () {
27+
var css = require('!raw-loader!' +
28+
'../?parser=postcss-js!' +
29+
'./cases/style.js');
30+
expect(css).to.eql('a {\n color: red\n}');
31+
});
32+
2633
});

0 commit comments

Comments
 (0)