11const path = require ( 'path' ) ;
22const loaderUtils = require ( 'loader-utils' ) ;
33const postcss = require ( 'postcss' ) ;
4+ const normalizePath = require ( 'normalize-path' ) ;
5+ const cssesc = require ( 'cssesc' ) ;
46
57const localByDefault = require ( 'postcss-modules-local-by-default' ) ;
68const modulesScope = require ( 'postcss-modules-scope' ) ;
@@ -9,16 +11,30 @@ const postCssPlugins = require('./postcss-plugins');
911const extractThemeRulesPlugin = postCssPlugins . extractThemeRulesPlugin ;
1012const removeCssModuleExports = postCssPlugins . removeCssModuleExports ;
1113
12- // copied from css-loader/lib/getLocalIdent .js
14+ // copied from css-loader/src/utils .js
1315function getLocalIdent ( loaderContext , localIdentName , localName , options ) {
14- if ( ! options . context )
15- options . context = loaderContext . options && typeof loaderContext . options . context === "string" ? loaderContext . options . context : loaderContext . context ;
16- var request = path . relative ( options . context , loaderContext . resourcePath ) ;
17- options . content = options . hashPrefix + request + "+" + localName ;
18- localIdentName = localIdentName . replace ( / \[ l o c a l \] / gi, localName ) ;
19- var hash = loaderUtils . interpolateName ( loaderContext , localIdentName , options ) ;
20- return hash . replace ( new RegExp ( "[^a-zA-Z0-9\\-_\u00A0-\uFFFF]" , "g" ) , "-" ) . replace ( / ^ ( [ ^ a - z A - Z _ ] ) / , "_$1" ) ;
21- } ;
16+ if ( ! options . context ) {
17+ // eslint-disable-next-line no-param-reassign
18+ options . context = loaderContext . rootContext ;
19+ }
20+
21+ const request = normalizePath (
22+ path . relative ( options . context || '' , loaderContext . resourcePath )
23+ ) ;
24+
25+ // eslint-disable-next-line no-param-reassign
26+ options . content = `${ options . hashPrefix + request } +${ unescape ( localName ) } ` ;
27+
28+ // Using `[path]` placeholder outputs `/` we need escape their
29+ // Also directories can contains invalid characters for css we need escape their too
30+ return cssesc (
31+ loaderUtils
32+ . interpolateName ( loaderContext , localIdentName , options )
33+ // For `[hash]` placeholder
34+ . replace ( / ^ ( ( - ? [ 0 - 9 ] ) | - - ) / , '_$1' ) ,
35+ { isIdentifier : true }
36+ ) . replace ( / \\ \[ l o c a l \\ \] / gi, localName ) ;
37+ }
2238
2339
2440module . exports = function ( source , map ) {
@@ -72,7 +88,7 @@ module.exports = function (source, map) {
7288 return customGetLocalIdent ( loader , localIdentName , exportName , {
7389 regExp : options . localIdentRegExp ,
7490 hashPrefix : options . hashPrefix || '' ,
75- context : loader . options . context
91+ context : loader . rootContext
7692 } ) ;
7793 }
7894 } ) ,
0 commit comments