Skip to content

Commit dec6001

Browse files
committed
Merge pull request #36 from css-modules/json-manifest
Generate a manifest json of export tokens
2 parents ded3433 + ab28886 commit dec6001

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,18 @@ b.bundle();
4848
### Options:
4949

5050
- `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)
51+
- `output`: path to write the generated css.
52+
- `jsonOutput`: optional path to write a json manifest of classnames.
53+
- `use`: optional array of postcss plugins (by default we use the css-modules core plugins).
54+
55+
## Using CSS Modules on the backend
56+
57+
If you want to use CSS Modules in server-generated templates there are a couple of options:
58+
59+
- Option A (nodejs only): register the [require-hook](https://github.com/css-modules/css-modules-require-hook) so that `var styles = require('./foo.css')` operates the same way as on the frontend. Make sure that the `rootDir` option matches to guarantee that the classnames are the same.
60+
61+
- Option B: configure the `jsonOutput` option with a file path and `css-modulesify` will generate a JSON manifest of classnames.
62+
5363

5464
## PostCSS Plugins
5565

@@ -70,11 +80,8 @@ In addition you may also wish to configure defined PostCSS plugins by passing `-
7080
An example of this would be:
7181

7282
```
73-
browserify -p [css-modulesify -u postcss-modules-local-by-default \
74-
-u postcss-modules-extract-imports \
75-
-u postcss-modules-scope \
76-
-u postcss-color-rebeccapurple \
77-
-u autoprefixer --autoprefixer.browsers '> 5%' \
83+
browserify -p [css-modulesify \
84+
--after autoprefixer --autoprefixer.browsers '> 5%' \
7885
-o dist/main.css] -o dist/index.js src/index.js
7986
```
8087

index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,40 @@ function createScopedNameFunc (plugin) {
1818
}
1919
};
2020

21+
/*
22+
23+
Normalize the manifest paths so that they are always relative
24+
to the project root directory.
25+
26+
*/
27+
function normalizeManifestPaths (tokensByFile, rootDir) {
28+
var output = {};
29+
var rootDirLength = rootDir.length + 1;
30+
31+
Object.keys(tokensByFile).forEach(function (filename) {
32+
var normalizedFilename = filename.substr(rootDirLength);
33+
output[normalizedFilename] = tokensByFile[filename];
34+
});
35+
36+
return output;
37+
}
38+
2139
var cssExt = /\.css$/;
2240
module.exports = function (browserify, options) {
2341
options = options || {};
2442

25-
var rootDir = options.rootDir || options.d || '/';
43+
// if no root directory is specified, assume the cwd
44+
var rootDir = options.rootDir || options.d;
45+
if (rootDir) { rootDir = path.resolve(rootDir); }
46+
if (!rootDir) { rootDir = process.cwd(); }
2647

2748
var cssOutFilename = options.output || options.o;
2849
if (!cssOutFilename) {
2950
throw new Error('css-modulesify needs the --output / -o option (path to output css file)');
3051
}
3152

53+
var jsonOutFilename = options.json || options.jsonOutput;
54+
3255
// PostCSS plugins passed to FileSystemLoader
3356
var plugins = options.use || options.u;
3457
if (!plugins) {
@@ -131,6 +154,15 @@ module.exports = function (browserify, options) {
131154
browserify.emit('error', err);
132155
}
133156
});
157+
158+
// write the classname manifest
159+
if (jsonOutFilename) {
160+
fs.writeFile(jsonOutFilename, JSON.stringify(normalizeManifestPaths(tokensByFile, rootDir)), function (err) {
161+
if (err) {
162+
browserify.emit('error', err);
163+
}
164+
});
165+
}
134166
});
135167
});
136168

0 commit comments

Comments
 (0)