@@ -1018,7 +1018,14 @@ function printParams(media, dedupe, supports, layer) {
1018
1018
return result ;
1019
1019
}
1020
1020
1021
- function getModuleCode ( result , api , replacements , options , loaderContext ) {
1021
+ function getModuleCode (
1022
+ result ,
1023
+ api ,
1024
+ replacements ,
1025
+ options ,
1026
+ isTemplateLiteralSupported ,
1027
+ loaderContext
1028
+ ) {
1022
1029
if ( options . modules . exportOnlyLocals === true ) {
1023
1030
return "" ;
1024
1031
}
@@ -1034,7 +1041,9 @@ function getModuleCode(result, api, replacements, options, loaderContext) {
1034
1041
) } `;
1035
1042
}
1036
1043
1037
- let code = JSON . stringify ( result . css ) ;
1044
+ let code = isTemplateLiteralSupported
1045
+ ? convertToTemplateLiteral ( result . css )
1046
+ : JSON . stringify ( result . css ) ;
1038
1047
1039
1048
let beforeCode = `var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(${
1040
1049
options . sourceMap
@@ -1067,12 +1076,21 @@ function getModuleCode(result, api, replacements, options, loaderContext) {
1067
1076
if ( localName ) {
1068
1077
code = code . replace ( new RegExp ( replacementName , "g" ) , ( ) =>
1069
1078
options . modules . namedExport
1070
- ? `" + ${ importName } _NAMED___[${ JSON . stringify (
1071
- getValidLocalName (
1072
- localName ,
1073
- options . modules . exportLocalsConvention
1074
- )
1075
- ) } ] + "`
1079
+ ? isTemplateLiteralSupported
1080
+ ? `\${ ${ importName } _NAMED___[${ JSON . stringify (
1081
+ getValidLocalName (
1082
+ localName ,
1083
+ options . modules . exportLocalsConvention
1084
+ )
1085
+ ) } ] }`
1086
+ : `" + ${ importName } _NAMED___[${ JSON . stringify (
1087
+ getValidLocalName (
1088
+ localName ,
1089
+ options . modules . exportLocalsConvention
1090
+ )
1091
+ ) } ] + "`
1092
+ : isTemplateLiteralSupported
1093
+ ? `\${${ importName } .locals[${ JSON . stringify ( localName ) } ]}`
1076
1094
: `" + ${ importName } .locals[${ JSON . stringify ( localName ) } ] + "`
1077
1095
) ;
1078
1096
} else {
@@ -1084,9 +1102,10 @@ function getModuleCode(result, api, replacements, options, loaderContext) {
1084
1102
getUrlOptions . length > 0 ? `, { ${ getUrlOptions . join ( ", " ) } }` : "" ;
1085
1103
1086
1104
beforeCode += `var ${ replacementName } = ___CSS_LOADER_GET_URL_IMPORT___(${ importName } ${ preparedOptions } );\n` ;
1087
- code = code . replace (
1088
- new RegExp ( replacementName , "g" ) ,
1089
- ( ) => `" + ${ replacementName } + "`
1105
+ code = code . replace ( new RegExp ( replacementName , "g" ) , ( ) =>
1106
+ isTemplateLiteralSupported
1107
+ ? `\${${ replacementName } }`
1108
+ : `" + ${ replacementName } + "`
1090
1109
) ;
1091
1110
}
1092
1111
}
@@ -1101,13 +1120,38 @@ function getModuleCode(result, api, replacements, options, loaderContext) {
1101
1120
return `${ beforeCode } // Module\n___CSS_LOADER_EXPORT___.push([module.id, ${ code } , ""${ sourceMapValue } ]);\n` ;
1102
1121
}
1103
1122
1123
+ const SLASH = "\\" . charCodeAt ( 0 ) ;
1124
+ const BACKTICK = "`" . charCodeAt ( 0 ) ;
1125
+ const DOLLAR = "$" . charCodeAt ( 0 ) ;
1126
+
1127
+ function convertToTemplateLiteral ( str ) {
1128
+ let escapedString = "" ;
1129
+
1130
+ for ( let i = 0 ; i < str . length ; i ++ ) {
1131
+ const code = str . charCodeAt ( i ) ;
1132
+
1133
+ escapedString +=
1134
+ code === SLASH || code === BACKTICK || code === DOLLAR
1135
+ ? `\\${ str [ i ] } `
1136
+ : str [ i ] ;
1137
+ }
1138
+
1139
+ return `\`${ escapedString } \`` ;
1140
+ }
1141
+
1104
1142
function dashesCamelCase ( str ) {
1105
1143
return str . replace ( / - + ( \w ) / g, ( match , firstLetter ) =>
1106
1144
firstLetter . toUpperCase ( )
1107
1145
) ;
1108
1146
}
1109
1147
1110
- function getExportCode ( exports , replacements , icssPluginUsed , options ) {
1148
+ function getExportCode (
1149
+ exports ,
1150
+ replacements ,
1151
+ icssPluginUsed ,
1152
+ options ,
1153
+ isTemplateLiteralSupported
1154
+ ) {
1111
1155
let code = "// Exports\n" ;
1112
1156
1113
1157
if ( icssPluginUsed ) {
@@ -1120,13 +1164,21 @@ function getExportCode(exports, replacements, icssPluginUsed, options) {
1120
1164
1121
1165
for ( const name of normalizedNames ) {
1122
1166
if ( options . modules . namedExport ) {
1123
- localsCode += `export var ${ name } = ${ JSON . stringify ( value ) } ;\n` ;
1167
+ localsCode += `export var ${ name } = ${
1168
+ isTemplateLiteralSupported
1169
+ ? convertToTemplateLiteral ( value )
1170
+ : JSON . stringify ( value )
1171
+ } ;\n`;
1124
1172
} else {
1125
1173
if ( localsCode ) {
1126
1174
localsCode += `,\n` ;
1127
1175
}
1128
1176
1129
- localsCode += `\t${ JSON . stringify ( name ) } : ${ JSON . stringify ( value ) } ` ;
1177
+ localsCode += `\t${ JSON . stringify ( name ) } : ${
1178
+ isTemplateLiteralSupported
1179
+ ? convertToTemplateLiteral ( value )
1180
+ : JSON . stringify ( value )
1181
+ } `;
1130
1182
}
1131
1183
}
1132
1184
} ;
@@ -1148,23 +1200,35 @@ function getExportCode(exports, replacements, icssPluginUsed, options) {
1148
1200
new RegExp ( replacementName , "g" ) ,
1149
1201
( ) => {
1150
1202
if ( options . modules . namedExport ) {
1151
- return `" + ${ importName } _NAMED___[${ JSON . stringify (
1152
- getValidLocalName (
1153
- localName ,
1154
- options . modules . exportLocalsConvention
1155
- )
1156
- ) } ] + "`;
1203
+ return isTemplateLiteralSupported
1204
+ ? `\${${ importName } _NAMED___[${ JSON . stringify (
1205
+ getValidLocalName (
1206
+ localName ,
1207
+ options . modules . exportLocalsConvention
1208
+ )
1209
+ ) } ]}`
1210
+ : `" + ${ importName } _NAMED___[${ JSON . stringify (
1211
+ getValidLocalName (
1212
+ localName ,
1213
+ options . modules . exportLocalsConvention
1214
+ )
1215
+ ) } ] + "`;
1157
1216
} else if ( options . modules . exportOnlyLocals ) {
1158
- return `" + ${ importName } [${ JSON . stringify ( localName ) } ] + "` ;
1217
+ return isTemplateLiteralSupported
1218
+ ? `\${${ importName } [${ JSON . stringify ( localName ) } ]}`
1219
+ : `" + ${ importName } [${ JSON . stringify ( localName ) } ] + "` ;
1159
1220
}
1160
1221
1161
- return `" + ${ importName } .locals[${ JSON . stringify ( localName ) } ] + "` ;
1222
+ return isTemplateLiteralSupported
1223
+ ? `\${${ importName } .locals[${ JSON . stringify ( localName ) } ]}`
1224
+ : `" + ${ importName } .locals[${ JSON . stringify ( localName ) } ] + "` ;
1162
1225
}
1163
1226
) ;
1164
1227
} else {
1165
- localsCode = localsCode . replace (
1166
- new RegExp ( replacementName , "g" ) ,
1167
- ( ) => `" + ${ replacementName } + "`
1228
+ localsCode = localsCode . replace ( new RegExp ( replacementName , "g" ) , ( ) =>
1229
+ isTemplateLiteralSupported
1230
+ ? `\${${ replacementName } }`
1231
+ : `" + ${ replacementName } + "`
1168
1232
) ;
1169
1233
}
1170
1234
}
0 commit comments