├── .npmignore ├── .travis.yml ├── .gitignore ├── index.d.ts ├── .editorconfig ├── readme.md ├── package.json ├── LICENSE ├── test ├── index.js └── cssspec.js └── index.js /.npmignore: -------------------------------------------------------------------------------- 1 | .editorconfig 2 | .gitignore 3 | .npmignore 4 | .travis.yml 5 | test 6 | yarn.lock 7 | .idea 8 | *.iml 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | cache: 4 | directories: 5 | - "node_modules" 6 | 7 | node_js: 8 | - "6" 9 | - "8" 10 | - "10" 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | npm-debug.log* 3 | 4 | # Dependency directories 5 | node_modules 6 | 7 | # Optional npm cache directory 8 | .npm 9 | 10 | yarn.lock 11 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | interface Options { 2 | context: string; 3 | hashPrefix: string; 4 | } 5 | 6 | type Generator = (localName: string, filepath: string) => string; 7 | 8 | declare function createGenerator( 9 | pattern: string, 10 | options?: Partial 11 | ): Generator; 12 | 13 | export = createGenerator; 14 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [GNUmakefile] 12 | indent_style = tab 13 | 14 | [Makefile] 15 | indent_style = tab 16 | 17 | [makefile] 18 | indent_style = tab 19 | 20 | # cheat sheet: http://EditorConfig.org 21 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 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 | Uses [interpolateName](https://github.com/webpack/loader-utils#interpolatename) from the webpack/loader-utils. 7 | 8 | ## API 9 | 10 | ```javascript 11 | var genericNames = require('generic-names'); 12 | var generate = genericNames('[name]__[local]___[hash:base64:5]', { 13 | context: process.cwd() 14 | }); 15 | 16 | generate('foo', '/case/source.css'); // 'source__foo___3mRq8' 17 | ``` 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "generic-names", 3 | "version": "4.0.0", 4 | "description": "Helper for building generic names, similar to webpack", 5 | "main": "index.js", 6 | "types": "index.d.ts", 7 | "scripts": { 8 | "test": "tape test/*.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/css-modules/generic-names.git" 13 | }, 14 | "keywords": [ 15 | "css-modules", 16 | "postcss-modules-scope", 17 | "webpack" 18 | ], 19 | "author": "Alexey Litvinov", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/css-modules/generic-names/issues" 23 | }, 24 | "homepage": "https://github.com/css-modules/generic-names#readme", 25 | "devDependencies": { 26 | "tape": "^4.6.2" 27 | }, 28 | "dependencies": { 29 | "loader-utils": "^3.2.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Alexey Litvinov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const genericNames = require("../index"); 4 | const test = require("tape"); 5 | const path = require("path"); 6 | 7 | const pattern = "[name]__[local]___[hash:base64:5]"; 8 | 9 | test("use `cwd` if no context was provided", t => { 10 | const generate = genericNames(pattern); 11 | 12 | t.equal( 13 | generate("foo", path.join(__dirname, "test/case/source.css")), 14 | "source__foo___VihAC" 15 | ); 16 | t.end(); 17 | }); 18 | 19 | test("generate distinct hash for the provided context", t => { 20 | const generate = genericNames(pattern, { 21 | context: path.join(__dirname, "/test") 22 | }); 23 | 24 | t.equal( 25 | generate("foo", path.join(__dirname, "test/case/source.css")), 26 | "source__foo___ZIJxV" 27 | ); 28 | t.end(); 29 | }); 30 | 31 | test("generate distinct hash for the provided hashPrefix", t => { 32 | const generate = genericNames(pattern, { 33 | context: path.join(__dirname, "/test"), 34 | hashPrefix: "--" 35 | }); 36 | 37 | t.equal( 38 | generate("foo", path.join(__dirname, "test/case/source.css")), 39 | "source__foo___QTVQp" 40 | ); 41 | t.end(); 42 | }); 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var interpolateName = require("loader-utils/lib/interpolateName"); 4 | var path = require("path"); 5 | 6 | /** 7 | * @param {string} pattern 8 | * @param {object} options 9 | * @param {string} options.context 10 | * @param {string} options.hashPrefix 11 | * @return {function} 12 | */ 13 | module.exports = function createGenerator(pattern, options) { 14 | options = options || {}; 15 | var context = 16 | options && typeof options.context === "string" 17 | ? options.context 18 | : process.cwd(); 19 | var hashPrefix = 20 | options && typeof options.hashPrefix === "string" ? options.hashPrefix : ""; 21 | 22 | /** 23 | * @param {string} localName Usually a class name 24 | * @param {string} filepath Absolute path 25 | * @return {string} 26 | */ 27 | return function generate(localName, filepath) { 28 | var name = pattern.replace(/\[local\]/gi, localName); 29 | var loaderContext = { 30 | resourcePath: filepath, 31 | }; 32 | 33 | var loaderOptions = { 34 | content: 35 | hashPrefix + 36 | path.relative(context, filepath).replace(/\\/g, "/") + 37 | "\x00" + 38 | localName, 39 | context: context, 40 | }; 41 | 42 | var genericName = interpolateName(loaderContext, name, loaderOptions); 43 | return genericName 44 | .replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-") 45 | .replace(/^((-?[0-9])|--)/, "_$1"); 46 | }; 47 | }; 48 | -------------------------------------------------------------------------------- /test/cssspec.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const genericNames = require("../index"); 4 | const test = require("tape"); 5 | const path = require("path"); 6 | 7 | // According to the CSS spec, identifiers cannot 8 | // start with a digit, two hyphens, or a hyphen 9 | // followed by a digit. 10 | // 11 | // relates: https://github.com/webpack/css-loader/commit/da27c07d0df25a38699344c13b6614c53a469fd9 12 | 13 | test("identity", t => { 14 | const generate = genericNames("[local]"); 15 | 16 | t.equal(generate("foo", path.join(__dirname, "test/case/source.css")), "foo"); 17 | t.end(); 18 | }); 19 | 20 | test("leading digit", t => { 21 | const generate = genericNames("0[local]"); 22 | 23 | t.equal( 24 | generate("foo", path.join(__dirname, "test/case/source.css")), 25 | "_0foo" 26 | ); 27 | t.end(); 28 | }); 29 | 30 | test("leading digit in the token", t => { 31 | const generate = genericNames("[local]"); 32 | 33 | t.equal( 34 | generate("0foo", path.join(__dirname, "test/case/source.css")), 35 | "_0foo" 36 | ); 37 | t.end(); 38 | }); 39 | 40 | test("leading two hyphens", t => { 41 | const generate = genericNames("--[local]"); 42 | 43 | t.equal( 44 | generate("foo", path.join(__dirname, "test/case/source.css")), 45 | "_--foo" 46 | ); 47 | t.end(); 48 | }); 49 | 50 | test("leading hyphen and digit", t => { 51 | const generate = genericNames("-0[local]"); 52 | 53 | t.equal( 54 | generate("foo", path.join(__dirname, "test/case/source.css")), 55 | "_-0foo" 56 | ); 57 | t.end(); 58 | }); 59 | --------------------------------------------------------------------------------