Skip to content

update to allow passing a generateScopedName func directly #46

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 3 commits into from
Sep 21, 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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ b.bundle();
- `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).
- `generateScopedName`: (API only) a function to override the default behaviour of creating locally scoped classnames.

## Using CSS Modules on the backend

Expand Down Expand Up @@ -89,6 +90,20 @@ browserify -p [css-modulesify \
[postcss-modules-extract-imports]: https://github.com/css-modules/postcss-modules-extract-imports
[postcss-modules-scope]: https://github.com/css-modules/postcss-modules-scope

## Building for production

If you set `NODE_ENV=production` then `css-modulesify` will generate shorter (though less useful) classnames.

You can also manually switch to short names by setting the `generateScopedName` option. Eg:

```
browserify.plugin(cssModulesify, {
rootDir: __dirname,
output: './dist/main.css',
generateScopedName: cssModulesify.generateShortName
})
```

## Example

An example implementation can be found [here](https://github.com/css-modules/browserify-demo).
Expand Down
53 changes: 45 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,50 @@ var FileSystemLoader = require('css-modules-loader-core/lib/file-system-loader')
var assign = require('object-assign');
var stringHash = require('string-hash');


/*
Custom `generateScopedName` function for `postcss-modules-scope`.
Short names consisting of source hash and line number.
*/
function generateShortName (name, filename, css) {
// first occurrence of the name
// TOOD: better match with regex
var i = css.indexOf('.' + name);
var numLines = css.substr(0, i).split(/[\r\n]/).length;

var hash = stringHash(css).toString(36).substr(0, 5);
return '_' + name + '_' + hash + '_' + numLines;
}

/*
Custom `generateScopedName` function for `postcss-modules-scope`.
Appends a hash of the css source.
*/
function createScopedNameFunc (plugin) {
var orig = plugin.generateScopedName;
return function (name, filename, css) {
var hash = stringHash(css).toString(36).substr(0, 5);
return orig.apply(plugin, arguments) + '___' + hash;
};
function generateLongName (name, filename) {
var sanitisedPath = filename.replace(/\.[^\.\/\\]+$/, '')
.replace(/[\W_]+/g, '_')
.replace(/^_|_$/g, '');

return '_' + sanitisedPath + '__' + name;
}

/*
Get the default plugins and apply options.
*/
function getDefaultPlugins (options) {
var scope = Core.scope;
var customNameFunc = options.generateScopedName;
var defaultNameFunc = process.env.NODE_ENV === 'production' ?
generateShortName :
generateLongName;

scope.generateScopedName = customNameFunc || defaultNameFunc;

return [
Core.localByDefault
, Core.extractImports
, scope
];
}

/*
Expand Down Expand Up @@ -71,7 +105,7 @@ module.exports = function (browserify, options) {
// PostCSS plugins passed to FileSystemLoader
var plugins = options.use || options.u;
if (!plugins) {
plugins = Core.defaultPlugins;
plugins = getDefaultPlugins(options);
}
else {
if (typeof plugins === 'string') {
Expand All @@ -95,7 +129,7 @@ module.exports = function (browserify, options) {
if (name === 'postcss-modules-scope') {
options[name] = options[name] || {};
if (!options[name].generateScopedName) {
options[name].generateScopedName = createScopedNameFunc(plugin);
options[name].generateScopedName = generateLongName;
}
}

Expand Down Expand Up @@ -174,3 +208,6 @@ module.exports = function (browserify, options) {

return browserify;
};

module.exports.generateShortName = generateShortName;
module.exports.generateLongName = generateLongName;
1 change: 1 addition & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function runTestCase (dir) {
b.plugin(cssModulesify, {
rootDir: path.join(casesDir, dir)
, output: cssOutFilename
, generateScopedName: cssModulesify.generateLongName
});

b.bundle(function (err) {
Expand Down