1- /*
2- MIT License http://www.opensource.org/licenses/mit-license.php
3- Author Tobias Koppers @sokra
4- */
5- var loaderUtils = require ( "loader-utils" ) ;
6- var path = require ( "path" ) ;
7-
8- module . exports = function getLocalIdent ( loaderContext , localIdentName , localName , options ) {
9- if ( ! options . context ) {
10- if ( loaderContext . rootContext ) {
11- options . context = loaderContext . rootContext ;
12- } else if ( loaderContext . options && typeof loaderContext . options . context === "string" ) {
13- options . context = loaderContext . options . context ;
14- } else {
15- options . context = loaderContext . context ;
16- }
17- }
18- var request = path . relative ( options . context , loaderContext . resourcePath ) ;
19- options . content = options . hashPrefix + request + "+" + localName ;
20- localIdentName = localIdentName . replace ( / \[ l o c a l \] / gi, localName ) ;
21- var hash = loaderUtils . interpolateName ( loaderContext , localIdentName , options ) ;
22- return hash . replace ( new RegExp ( "[^a-zA-Z0-9\\-_\u00A0-\uFFFF]" , "g" ) , "-" ) . replace ( / ^ ( ( - ? [ 0 - 9 ] ) | - - ) / , "_$1" ) ;
1+ import incstr from 'incstr' ;
2+
3+ //region CSS Scope Minify
4+ const createUniqueIdGenerator = ( ) => {
5+ const index = { } ;
6+
7+ const generateNextId = incstr . idGenerator ( {
8+ // Removed "d" letter to avoid accidental "ad" construct.
9+ // @see https://medium.com/@mbrevda/just-make-sure-ad-isnt-being-used-as-a-class-name-prefix-or-you-might-suffer-the-wrath-of-the-558d65502793
10+ // NOTE: allow "d" letter due to combination of UPPERCASES-lowercases
11+ alphabet : 'abcdefghijklmnopqrstuvwxyz0123456789_-'
12+ } ) ;
13+
14+ return ( name ) => {
15+ if ( index [ name ] ) {
16+ return index [ name ] ;
17+ }
18+
19+ let nextId ;
20+
21+ do {
22+ // Class name cannot start with a number.
23+ nextId = generateNextId ( ) ;
24+ } while ( / ^ [ 0 - 9 _ - ] / . test ( nextId ) ) ;
25+
26+ index [ name ] = generateNextId ( ) ;
27+ // console.log(`${name} has id = ${index[name]}`);
28+
29+ return index [ name ] ;
30+ } ;
2331} ;
32+
33+ const idLocal = createUniqueIdGenerator ( ) , idComponent = createUniqueIdGenerator ( ) ;
34+ const generateScopedName = ( localName , resourcePath ) => {
35+ const componentName = resourcePath . split ( '/' ) . slice ( - 2 ) . join ( '/' ) ;
36+ return idComponent ( componentName ) . toUpperCase ( ) + idLocal ( localName ) ;
37+ } ;
38+
39+ const getLocalIdent = ( context , localIdentName , localName ) => generateScopedName ( localName , context . resourcePath ) ;
40+ global . getLocalIdent = getLocalIdent ;
41+ //endregion
0 commit comments