Skip to content
This repository was archived by the owner on Aug 4, 2020. It is now read-only.

Commit e676174

Browse files
committed
added rootDir and changed the way we put together paths so we can support composing from node_modules (but still depends on an update to core loader first)
1 parent 710426b commit e676174

File tree

10 files changed

+36
-13
lines changed

10 files changed

+36
-13
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules/*
2+
!node_modules/cool-styles

README.md

+22-2
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

+5-4
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
}

tests/.gitignore

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
._styles__foo {
1+
._cool_styles_styles__foo {
22
color: #F00;
33
}
4+
._styles__foo {
5+
background: black;
6+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.foo {
22
composes: foo from "cool-styles/styles.css";
3+
background: black;
34
}
+1-1
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
}

tests/cases/import-node-module/node_modules/cool-styles/styles.css

-3
This file was deleted.

tests/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function runTestCase (dir) {
3232
var b = browserify();
3333
b.add(path.join(casesDir, dir, 'main.js'));
3434
b.plugin(cssModulesify, {
35+
rootDir: path.join(casesDir, dir),
3536
output: cssOutFilename
3637
});
3738

0 commit comments

Comments
 (0)