Skip to content

Commit 2f052e4

Browse files
committed
generate a manifest json of export tokens
updated readme make the manifest a bit nicer by normalizing filenames to the root directory updated readme make the manifest a bit nicer by normalizing filenames to the root directory
1 parent ded3433 commit 2f052e4

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ b.bundle();
4949

5050
- `rootDir`: absolute path to your project's root directory. This is optional but providing it will result in better generated classnames.
5151
- `output`: path to write the generated css
52+
- `json`: optional path to write a json manifest of classnames
5253
- `use`: optional array of postcss plugins (by default we use the css-modules core plugins)
5354

5455
## PostCSS Plugins

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;
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)