Skip to content

Commit ec71bc7

Browse files
committed
Merge pull request #25 from css-modules/compose-node-module
Composing from a node_module
2 parents b45898e + e595dd2 commit ec71bc7

File tree

11 files changed

+43
-10
lines changed

11 files changed

+43
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules/*
2+
!node_modules/cool-styles

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Normally you need to use a strict naming convention like BEM to ensure that one
1414

1515
Read Mark Dalgleish's excellent ["End of Global CSS"](https://medium.com/seek-ui-engineering/the-end-of-global-css-90d2a4a06284) and check out [css-modules](https://github.com/css-modules/css-modules) for more context.
1616

17-
## Usage
17+
## Getting started
1818

1919
First install the package: `npm install --save css-modulesify`
2020

@@ -31,7 +31,27 @@ var div = `<div class="${styles.inner}">...</div>`;
3131

3232
The generated css will contain locally-scoped versions of any css you have `require`'d, and will be written out to the file you specify in the `--output` or `-o` option.
3333

34-
### PostCSS Plugins
34+
## API Usage
35+
36+
```js
37+
var b = require('browserify')();
38+
39+
b.add('./main.js');
40+
b.plugin(require('css-modulesify'), {
41+
rootDir: __dirname,
42+
output: './path/to/my.css'
43+
});
44+
45+
b.bundle();
46+
```
47+
48+
### Options:
49+
50+
- `rootDir`: absolute path to your project's root directory. This is optional but providing it will result in better generated classnames.
51+
- `output`: path to write the generated css
52+
- `use`: optional array of postcss plugins (by default we use the css-modules core plugins)
53+
54+
## PostCSS Plugins
3555

3656
The following PostCSS plugins are enabled by default:
3757

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var cssExt = /\.css$/;
2222
module.exports = function (browserify, options) {
2323
options = options || {};
2424

25+
var rootDir = options.rootDir || options.d || '/';
26+
2527
var cssOutFilename = options.output || options.o;
2628
if (!cssOutFilename) {
2729
throw new Error('css-modulesify needs the --output / -o option (path to output css file)');
@@ -84,13 +86,12 @@ module.exports = function (browserify, options) {
8486

8587
return through(function noop () {}, function end () {
8688
var self = this;
87-
88-
var loader = new FileSystemLoader(path.dirname(filename), plugins);
89+
var loader = new FileSystemLoader(rootDir, plugins);
8990

9091
// pre-populate the loader's tokensByFile
9192
loader.tokensByFile = tokensByFile;
9293

93-
loader.fetch(path.basename(filename), '/').then(function (tokens) {
94+
loader.fetch(path.relative(rootDir, filename), '/').then(function (tokens) {
9495
var output = "module.exports = " + JSON.stringify(tokens);
9596

9697
assign(tokensByFile, loader.tokensByFile);
@@ -101,7 +102,7 @@ module.exports = function (browserify, options) {
101102
self.queue(output);
102103
self.queue(null);
103104
}, function (err) {
104-
console.error(err);
105+
console.error('loader err', err);
105106
});
106107
});
107108
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A browserify transform to load CSS Modules",
55
"main": "index.js",
66
"dependencies": {
7-
"css-modules-loader-core": "0.0.11",
7+
"css-modules-loader-core": "0.0.12",
88
"object-assign": "^3.0.0",
99
"string-hash": "^1.1.0",
1010
"through": "^2.3.7"

tests/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
._cool_styles_styles__foo {
2+
color: #F00;
3+
}
4+
._styles__foo {
5+
background: black;
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var styles = require('./styles.css');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.foo {
2+
composes: foo from "cool-styles/styles.css";
3+
background: black;
4+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
._styles__foo {
1+
._node_modules_cool_styles_styles__foo {
22
color: #F00;
33
}

0 commit comments

Comments
 (0)