Skip to content

Generate a manifest json of export tokens #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,18 @@ b.bundle();
### Options:

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

## Using CSS Modules on the backend

If you want to use CSS Modules in server-generated templates there are a couple of options:

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

- Option B: configure the `jsonOutput` option with a file path and `css-modulesify` will generate a JSON manifest of classnames.


## PostCSS Plugins

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

```
browserify -p [css-modulesify -u postcss-modules-local-by-default \
-u postcss-modules-extract-imports \
-u postcss-modules-scope \
-u postcss-color-rebeccapurple \
-u autoprefixer --autoprefixer.browsers '> 5%' \
browserify -p [css-modulesify \
--after autoprefixer --autoprefixer.browsers '> 5%' \
-o dist/main.css] -o dist/index.js src/index.js
```

Expand Down
34 changes: 33 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,40 @@ function createScopedNameFunc (plugin) {
}
};

/*

Normalize the manifest paths so that they are always relative
to the project root directory.

*/
function normalizeManifestPaths (tokensByFile, rootDir) {
var output = {};
var rootDirLength = rootDir.length + 1;

Object.keys(tokensByFile).forEach(function (filename) {
var normalizedFilename = filename.substr(rootDirLength);
output[normalizedFilename] = tokensByFile[filename];
});

return output;
}

var cssExt = /\.css$/;
module.exports = function (browserify, options) {
options = options || {};

var rootDir = options.rootDir || options.d || '/';
// if no root directory is specified, assume the cwd
var rootDir = options.rootDir || options.d;
if (rootDir) { rootDir = path.resolve(rootDir); }
if (!rootDir) { rootDir = process.cwd(); }

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

var jsonOutFilename = options.json || options.jsonOutput;

// PostCSS plugins passed to FileSystemLoader
var plugins = options.use || options.u;
if (!plugins) {
Expand Down Expand Up @@ -131,6 +154,15 @@ module.exports = function (browserify, options) {
browserify.emit('error', err);
}
});

// write the classname manifest
if (jsonOutFilename) {
fs.writeFile(jsonOutFilename, JSON.stringify(normalizeManifestPaths(tokensByFile, rootDir)), function (err) {
if (err) {
browserify.emit('error', err);
}
});
}
});
});

Expand Down