File tree Expand file tree Collapse file tree 3 files changed +83
-0
lines changed Expand file tree Collapse file tree 3 files changed +83
-0
lines changed Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ var interpolateName = require ( 'loader-utils' ) . interpolateName ;
4+ var path = require ( 'path' ) ;
5+ var util = require ( 'util' ) ;
6+
7+ /**
8+ * @param {string } pattern
9+ * @param {object } options
10+ * @param {string } options.context
11+ * @param {string } options.hashPrefix
12+ * @return {function }
13+ */
14+ module . exports = function createGenerator ( pattern , options ) {
15+ options = options || { } ;
16+ var context = options && typeof options . context === 'string'
17+ ? options . context
18+ : '' ;
19+ var hashPrefix = options && typeof options . hashPrefix
20+ ? options . hashPrefix
21+ : '' ;
22+
23+ /**
24+ * @param {string } localName Usually a class name
25+ * @param {string } filepath Absolute path
26+ * @return {string }
27+ */
28+ return function generate ( localName , filepath ) {
29+ var name = pattern . replace ( / \[ l o c a l \] / gi, localName ) ;
30+ var loaderContext = {
31+ resourcePath : filepath
32+ } ;
33+ var loaderOptions = {
34+ content : util . format ( '%s%s+%s' ,
35+ hashPrefix ,
36+ path . relative ( context , filepath ) ,
37+ localName ) ,
38+ context : context
39+ } ;
40+
41+ var genericName = interpolateName ( loaderContext , name , loaderOptions ) ;
42+ return genericName ;
43+ } ;
44+ } ;
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " generic-names" ,
3+ "version" : " 1.0.0-beta" ,
4+ "description" : " Helper for building generic names, similar to webpack" ,
5+ "main" : " index.js" ,
6+ "scripts" : {
7+ "test" : " echo \" Error: no test specified\" && exit 1"
8+ },
9+ "repository" : {
10+ "type" : " git" ,
11+ "url" : " git+https://github.com/sullenor/generic-names.git"
12+ },
13+ "keywords" : [
14+ " css-modules" ,
15+ " webpack"
16+ ],
17+ "author" : " Alexey Litvinov" ,
18+ "license" : " MIT" ,
19+ "bugs" : {
20+ "url" : " https://github.com/sullenor/generic-names/issues"
21+ },
22+ "homepage" : " https://github.com/sullenor/generic-names#readme" ,
23+ "dependencies" : {
24+ "loader-utils" : " ^0.2.11"
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ generic-names
2+ =============
3+
4+ Helper for building generic names, similar to webpack. Designed to be used with [ postcss-modules-scope] ( https://github.com/css-modules/postcss-modules-scope ) .
5+
6+ ## API
7+
8+ ``` javascript
9+ var genericNames = require (' generic-names' );
10+ var fn = genericNames (' [name]__[local]___[hash:base64:5]' , {
11+ context: process .cwd ()
12+ });
13+ ```
You can’t perform that action at this time.
0 commit comments