Skip to content

Commit 31b4cf9

Browse files
committed
using css-modules-loader-core, now supports :extends!
1 parent 3c4522f commit 31b4cf9

File tree

5 files changed

+20
-32
lines changed

5 files changed

+20
-32
lines changed

example/borders.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.dottyBorder {
2+
border: 1px dotted #000;
3+
}

example/box1.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.box {
2+
extends: dottyBorder from "./borders.css";
23
padding: 10px;
3-
border: 1px dotted #000;
44
}
55

66
.text {

example/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ insertCss(box2);
1212
var content = h('div', [
1313
h('p', 'This is a demonstration of using generic classnames in css files like `.box` and `.text`, without any danger of name collisions between components'),
1414

15-
h('div', { className: box1['.box'] }, [
16-
h('p', { className: box1['.text'] }, 'Box 1')
15+
h('div', { className: box1.box }, [
16+
h('p', { className: box1.text }, 'Box 1')
1717
]),
1818

19-
h('div', { className: box2['.box'] }, [
20-
h('p', { className: box2['.text'] }, 'Box 2')
19+
h('div', { className: box2.box }, [
20+
h('p', { className: box2.text }, 'Box 2')
2121
])
2222
]);
2323

lib/local-css.js

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,7 @@ var postcss = require('postcss');
44
var autoprefixer = require('autoprefixer-core');
55
var shasum = require('shasum');
66
var prefixSelector = require('./prefix-selector');
7-
8-
function localizeCss (basename, raw, cb) {
9-
var hash = shasum(raw).substr(0, 5);
10-
11-
postcss([ autoprefixer ]).process(raw).then(function (result) {
12-
result.warnings().forEach(function (warn) {
13-
console.warn(warn.toString());
14-
});
15-
16-
// prefix all class selectors to make them globally unique
17-
var classMap = {};
18-
result.root.eachRule(function (rule) {
19-
var uniqueSelector = prefixSelector(basename, hash, rule);
20-
classMap[rule.selector] = uniqueSelector.replace(/^./, '');
21-
rule.selector = uniqueSelector;
22-
});
23-
24-
return cb(null, classMap, result.root);
25-
});
26-
}
7+
var FileSystemLoader = require('css-modules-loader-core/lib/file-system-loader');
278

289
var cssExt = /\.css$/;
2910
module.exports = function (filename) {
@@ -37,17 +18,20 @@ module.exports = function (filename) {
3718
data.push(chunk);
3819
}, function end () {
3920
var raw = data.join('');
40-
var basename = path.basename(filename, '.css');
4121
var self = this;
4222

43-
localizeCss(basename, raw, function (err, classMap, stylesheet) {
44-
if (err) { throw err; }
45-
46-
var output = "module.exports = " + JSON.stringify(classMap) +
47-
"\nmodule.exports.toString = function () { return " + JSON.stringify(stylesheet.toString()) + "; }";
23+
var loader = new FileSystemLoader(path.dirname(filename));
24+
loader.fetch(path.basename(filename), '/').then(function (tokens) {
25+
var sources = Object.keys(loader.sources).map(function (key) {
26+
return loader.sources[key];
27+
});
28+
var output = "module.exports = " + JSON.stringify(tokens) +
29+
"\nmodule.exports.toString = function () { return " + JSON.stringify(sources.join('\n')) + "; }";
4830

4931
self.queue(output);
5032
self.queue(null);
33+
}, function (err) {
34+
console.error(err);
5135
});
5236
});
5337
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "local-css",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "A browserify transform allowing you to `require('styles.css')` and get back locally scoped classnames.",
55
"main": "index.js",
66
"dependencies": {
77
"autoprefixer-core": "^5.2.0",
8+
"css-modules-loader-core": "0.0.3",
89
"css-selector-tokenizer": "^0.3.1",
910
"postcss": "^4.1.11",
1011
"shasum": "^1.0.1",

0 commit comments

Comments
 (0)