Skip to content

Commit e09059c

Browse files
committed
update to allow passing a generateScopedName func directly, or fall back to default based on env
1 parent 8465246 commit e09059c

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ b.bundle();
5151
- `output`: path to write the generated css.
5252
- `jsonOutput`: optional path to write a json manifest of classnames.
5353
- `use`: optional array of postcss plugins (by default we use the css-modules core plugins).
54+
- `generateScopedName`: (API only) a function to override the default behaviour of creating locally scoped classnames.
5455

5556
## Using CSS Modules on the backend
5657

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

93+
## Building for production
94+
95+
If you set `NODE_ENV=production` then `css-modulesify` will generate shorter (though less useful) classnames.
96+
97+
You can also manually switch to short names by setting the `generateScopedName` option. Eg:
98+
99+
```
100+
browserify.plugin(cssModulesify, {
101+
rootDir: __dirname,
102+
output: './dist/main.css',
103+
generateScopedName: cssModulesify.generateShortName
104+
})
105+
```
106+
92107
## Example
93108

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

index.js

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,51 @@ var FileSystemLoader = require('css-modules-loader-core/lib/file-system-loader')
66
var assign = require('object-assign');
77
var stringHash = require('string-hash');
88

9+
10+
/*
11+
Custom `generateScopedName` function for `postcss-modules-scope`.
12+
Short names consisting of source hash and line number.
13+
*/
14+
function generateShortName (name, filename, css) {
15+
// first occurrence of the name
16+
// TOOD: better match with regex
17+
var i = css.indexOf('.' + name);
18+
var numLines = css.substr(0, i).split(/[\r\n]/).length;
19+
20+
var hash = stringHash(css).toString(36).substr(0, 5);
21+
return '_' + name + '_' + hash + '_' + numLines;
22+
}
23+
924
/*
1025
Custom `generateScopedName` function for `postcss-modules-scope`.
1126
Appends a hash of the css source.
1227
*/
13-
function createScopedNameFunc (plugin) {
14-
var orig = plugin.generateScopedName;
15-
return function (name, filename, css) {
16-
var hash = stringHash(css).toString(36).substr(0, 5);
17-
return orig.apply(plugin, arguments) + '___' + hash;
18-
};
28+
function generateLongName (name, filename, css) {
29+
var sanitisedPath = filename.replace(/\.[^\.\/\\]+$/, '')
30+
.replace(/[\W_]+/g, '_')
31+
.replace(/^_|_$/g, '');
32+
33+
var hash = stringHash(css).toString(36).substr(0, 5);
34+
return '_' + sanitisedPath + '__' + name + '___' + hash;
35+
}
36+
37+
/*
38+
Get the default plugins and apply options.
39+
*/
40+
function getDefaultPlugins (options) {
41+
var scope = Core.scope;
42+
var customNameFunc = options.generateScopedName;
43+
var defaultNameFunc = process.env.NODE_ENV === 'production' ?
44+
generateShortName :
45+
generateLongName;
46+
47+
scope.generateScopedName = customNameFunc || defaultNameFunc;
48+
49+
return [
50+
Core.localByDefault
51+
, Core.extractImports
52+
, scope
53+
];
1954
}
2055

2156
/*
@@ -71,7 +106,7 @@ module.exports = function (browserify, options) {
71106
// PostCSS plugins passed to FileSystemLoader
72107
var plugins = options.use || options.u;
73108
if (!plugins) {
74-
plugins = Core.defaultPlugins;
109+
plugins = getDefaultPlugins(options);
75110
}
76111
else {
77112
if (typeof plugins === 'string') {
@@ -95,7 +130,7 @@ module.exports = function (browserify, options) {
95130
if (name === 'postcss-modules-scope') {
96131
options[name] = options[name] || {};
97132
if (!options[name].generateScopedName) {
98-
options[name].generateScopedName = createScopedNameFunc(plugin);
133+
options[name].generateScopedName = generateLongName;
99134
}
100135
}
101136

@@ -174,3 +209,6 @@ module.exports = function (browserify, options) {
174209

175210
return browserify;
176211
};
212+
213+
module.exports.generateShortName = generateShortName;
214+
module.exports.generateLongName = generateLongName;

0 commit comments

Comments
 (0)