1- const path = require ( 'path' )
2- const { interpolateName } = require ( 'loader-utils' )
1+ const path = require ( 'path' ) ;
2+ const cssesc = require ( 'cssesc' ) ;
3+ const { interpolateName } = require ( 'loader-utils' ) ;
34
45const pluginOptions = {
56 includePaths : [ ] ,
67 localIdentName : '[local]-[hash:base64:6]'
7- }
8+ } ;
89
910const regex = {
1011 module : / \$ ( s t y l e ) ? \. ( : ? [ \w \d - ] * ) / gm,
1112 style : / < s t y l e ( \s [ ^ ] * ?) ? > ( [ ^ ] * ?) < \/ s t y l e > / gi,
13+ pathUnallowed : / [ < > : " / \\ | ? * ] / g,
1214 class : ( className ) => {
1315 return new RegExp ( `\\.(${ className } )\\b(?![-_])` , 'gm' )
1416 }
@@ -17,27 +19,35 @@ const regex = {
1719let moduleClasses = { } ;
1820
1921function generateName ( resourcePath , styles , className ) {
20- const filePath = resourcePath
21- const fileName = path . basename ( filePath )
22+ const filePath = resourcePath ;
23+ const fileName = path . basename ( filePath ) ;
2224 const localName = pluginOptions . localIdentName . length
2325 ? pluginOptions . localIdentName . replace ( / \[ l o c a l \] / gi, ( ) => className )
24- : className
26+ : className ;
2527
26- const content = `${ styles } -${ filePath } -${ fileName } -${ className } `
28+ const content = `${ styles } -${ filePath } -${ fileName } -${ className } ` ;
2729
28- let interpolatedName = interpolateName ( { resourcePath } , localName , { content } )
30+ let interpolatedName = cssesc (
31+ interpolateName ( { resourcePath } , localName , { content } )
32+ . replace ( / \. / g, '-' )
33+ ) ;
34+
35+ // replace unwanted characters from [path]
36+ if ( regex . pathUnallowed . test ( interpolatedName ) ) {
37+ interpolatedName = interpolatedName . replace ( regex . pathUnallowed , '_' ) ;
38+ }
2939
3040 // prevent class error when the generated classname starts from a non word charater
3141 if ( / ^ (? ! [ a - z A - Z _ ] ) / . test ( interpolatedName ) ) {
32- interpolatedName = `_${ interpolatedName } `
42+ interpolatedName = `_${ interpolatedName } ` ;
3343 }
3444
3545 // prevent svelte "Unused CSS selector" warning when the generated classname ends by `-`
3646 if ( interpolatedName . slice ( - 1 ) === '-' ) {
37- interpolatedName = interpolatedName . slice ( 0 , - 1 )
47+ interpolatedName = interpolatedName . slice ( 0 , - 1 ) ;
3848 }
3949
40- return interpolatedName
50+ return interpolatedName ;
4151}
4252
4353const markup = async ( { content, filename } ) => {
0 commit comments