Skip to content

Commit 9344583

Browse files
committed
Make mixing CSS and JS imports work and add README.md
1 parent 6b07d37 commit 9344583

File tree

4 files changed

+88
-15
lines changed

4 files changed

+88
-15
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Nathan Tran ntran013@gmail.com
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# css-in-js-loader
2+
3+
> Use CSS in JS with [postcss-js](https://github.com/postcss/postcss-js) in webpack
4+
5+
## Installation
6+
7+
```sh
8+
npm i --save-dev css-in-js-loader
9+
```
10+
11+
## But [postcss-loader](https://github.com/postcss/postcss-loader) already supports postcss-js as a parser?
12+
13+
If you're using [babel](https://github.com/babel/babel) to process your Javascript then [postcss-loader](https://github.com/postcss/postcss-loader) only transforms `.js` files one level deep, i.e. it doesn't transform that file's imports.
14+
15+
## Usage and Example
16+
17+
Put `css-in-js` between your CSS and JS loaders. `css-in-js-loader` detects if the file is a `.js` file and converts it to CSS using [postcss-js](https://github.com/postcss/postcss-js) so you can use your CSS loaders (e.g. [postcss-loader](https://github.com/postcss/postcss-loader)) normally.
18+
19+
Example webpack configuration:
20+
21+
```js
22+
{
23+
loaders: [
24+
{ test: /\.css$/, loader: 'css!postcss!css-in-js!babel' },
25+
{ test: /\.css\.js$/, loader: 'css!postcss!css-in-js!babel' },
26+
{ test: /\.js$/, loader: 'babel' },
27+
],
28+
}
29+
```

index.js

+36-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
1+
var path = require('path');
2+
var fs = require('fs');
3+
var postcss = require('postcss');
4+
var postcssJs = require('postcss-js');
15
var NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
26
var NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
37
var LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
48
var SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
59
var LimitChunkCountPlugin = require('webpack/lib/optimize/LimitChunkCountPlugin');
610

7-
module.exports = function () {
11+
module.exports = function (source) {
812
if(this.cacheable) this.cacheable();
9-
var request = this.request.split('!').slice(this.loaderIndex + 1).join('!');
13+
return source;
14+
};
15+
16+
module.exports.pitch = function (request, prevRequest) {
17+
if(this.cacheable) this.cacheable();
18+
var callback = this.async();
19+
if (path.extname(request) === '.js') {
20+
produce(this, request, callback);
21+
} else {
22+
var parts = request.split('!');
23+
var filename = parts[parts.length - 1];
24+
this.addDependency(filename);
25+
fs.readFile(filename, 'utf8', callback);
26+
}
27+
};
28+
29+
function produce(loader, request, callback) {
1030
var childFilename = 'css-in-js-output-filename';
1131
var outputOptions = { filename: childFilename };
12-
var childCompiler = getRootCompilation(this)
32+
var childCompiler = getRootCompilation(loader)
1333
.createChildCompiler('css-in-js-compiler', outputOptions);
1434
childCompiler.apply(new NodeTemplatePlugin(outputOptions));
1535
childCompiler.apply(new LibraryTemplatePlugin(null, 'commonjs2'));
1636
childCompiler.apply(new NodeTargetPlugin());
17-
childCompiler.apply(new SingleEntryPlugin(this.context, '!!' + request));
37+
childCompiler.apply(new SingleEntryPlugin(loader.context, '!!' + request));
1838
childCompiler.apply(new LimitChunkCountPlugin({ maxChunks: 1 }));
1939
var subCache = 'subcache ' + __dirname + ' ' + request;
2040
childCompiler.plugin('compilation', function(compilation) {
@@ -45,7 +65,6 @@ module.exports = function () {
4565
callback();
4666
});
4767

48-
var callback = this.async();
4968
childCompiler.runAsChild(function(err, entries, compilation) {
5069
if(err) return callback(err);
5170

@@ -56,27 +75,29 @@ module.exports = function () {
5675
return callback(new Error("Didn't get a result from child compiler"));
5776
}
5877
compilation.fileDependencies.forEach(function(dep) {
59-
this.addDependency(dep);
60-
}, this);
78+
loader.addDependency(dep);
79+
}, loader);
6180
compilation.contextDependencies.forEach(function(dep) {
62-
this.addContextDependency(dep);
63-
}, this);
81+
loader.addContextDependency(dep);
82+
}, loader);
6483
try {
65-
var exports = this.exec(source, request);
84+
var exports = loader.exec(source, request);
6685
if (exports.default && typeof exports.default === 'object') {
6786
exports = exports.default;
6887
}
69-
var text = "\nmodule.exports = " + JSON.stringify(exports) + ";";
7088
} catch(e) {
7189
return callback(e);
7290
}
73-
if (text) {
74-
callback(null, text);
91+
if (exports) {
92+
postcss().process(exports, { parser: postcssJs })
93+
.then(function (res) {
94+
callback(null, res.css);
95+
});
7596
} else {
7697
callback();
7798
}
78-
}.bind(this));
79-
};
99+
});
100+
}
80101

81102
function getRootCompilation(loader) {
82103
var compiler = loader._compiler;

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"main": "index.js",
66
"scripts": {},
77
"peerDependencies": {
8+
"postcss": "^5.0.14",
9+
"postcss-js": "^0.1.1",
810
"webpack": "^1.12.9"
911
},
1012
"repository": {

0 commit comments

Comments
 (0)