Skip to content

Commit dcf995c

Browse files
committed
Add user configurable PostCSS plugins.
1 parent 1e5bc24 commit dcf995c

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# css-modulesify
22

3-
A browserify transform to load [CSS Modules](https://github.com/css-modules/css-modules).
3+
A browserify transform to load [CSS Modules].
4+
5+
[CSS Modules]: https://github.com/css-modules/css-modules
46

57
**Please note that this is still highly experimental.**
68

@@ -27,6 +29,35 @@ var div = `<div class="${styles.inner}">...</div>`;
2729

2830
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.
2931

32+
### PostCSS Plugins
33+
34+
The following PostCSS plugins are enabled by default:
35+
36+
* [postcss-modules-local-by-default]
37+
* [postcss-modules-extract-imports]
38+
* [postcss-modules-scope]
39+
40+
(i.e. the [CSS Modules] specification).
41+
42+
You can supply your own additional PostCSS Plugins by passing `--use|-u` to `css-modulesify`.
43+
44+
In addion you may also wish to configure defined PostCSS plugins by passing `--plugin.option true`.
45+
46+
An example of this would be:
47+
48+
```
49+
browserify -p [css-modulesify -u postcss-modules-local-by-default \
50+
-u postcss-modules-extract-imports \
51+
-u postcss-modules-scope \
52+
-u postcss-color-rebeccapurple \
53+
-u autoprefixer --autoprefixer.browsers '> 5%' \
54+
-o dist/main.css] -o dist/index.js src/index.js
55+
```
56+
57+
[postcss-modules-local-by-default]: https://github.com/css-modules/postcss-modules-local-by-default
58+
[postcss-modules-extract-imports]: https://github.com/css-modules/postcss-modules-extract-imports
59+
[postcss-modules-scope]: https://github.com/css-modules/postcss-modules-scope
60+
3061
## Example
3162

3263
An example implementation can be found [here](https://github.com/css-modules/browserify-demo).

index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var fs = require('fs');
22
var path = require('path');
33
var through = require('through');
4+
var Core = require('css-modules-loader-core');
45
var FileSystemLoader = require('css-modules-loader-core/lib/file-system-loader');
56
var assign = require('object-assign');
67

@@ -13,6 +14,33 @@ module.exports = function (browserify, options) {
1314
throw new Error('css-modulesify needs the --output / -o option (path to output css file)');
1415
}
1516

17+
// PostCSS plugins passed to FileSystemLoader
18+
var plugins = options.use || options.u;
19+
if (!plugins) {
20+
plugins = Core.defaultPlugins;
21+
} else {
22+
if (typeof plugins === 'string') {
23+
plugins = [ plugins ];
24+
}
25+
26+
plugins = plugins.map(function requirePlugin (name) {
27+
// assume objects are already required plugins
28+
if (typeof name === 'object') {
29+
return name;
30+
}
31+
32+
var plugin = require(name);
33+
34+
if (name in options) {
35+
plugin = plugin(options[name]);
36+
} else {
37+
plugin = plugin.postcss || plugin();
38+
}
39+
40+
return plugin;
41+
});
42+
}
43+
1644
// keep track of css files visited
1745
var filenames = [];
1846

@@ -36,7 +64,7 @@ module.exports = function (browserify, options) {
3664
return through(function noop () {}, function end () {
3765
var self = this;
3866

39-
var loader = new FileSystemLoader(path.dirname(filename));
67+
var loader = new FileSystemLoader(path.dirname(filename), plugins);
4068

4169
// pre-populate the loader's tokensByFile
4270
loader.tokensByFile = tokensByFile;

0 commit comments

Comments
 (0)