11import path from "path" ;
2+ import fs from "fs" ;
23
34import less from "less" ;
45import { klona } from "klona/full" ;
@@ -219,16 +220,63 @@ function normalizeSourceMap(map) {
219220
220221 return newMap ;
221222}
222- const cssFragReg = / \. [ ^ { } / \\ ] + { [ ^ { } ] * ?} / g;
223- const classNameFragReg = / \. [ ^ { } / \\ ] + (? = { ) / ;
223+ const getAllStyleVarFiles = ( loaderContext , options ) => {
224+ const styleVarFiles = options . multipleScopeVars ;
225+ let allStyleVarFiles = [ { scopeName : "" , path : "" } ] ;
226+ if ( Array . isArray ( styleVarFiles ) ) {
227+ allStyleVarFiles = styleVarFiles . filter ( ( item ) => {
228+ if ( ! item . scopeName ) {
229+ loaderContext . emitError (
230+ new Error ( "Not found scopeName in less-loader multipleScopeVars" )
231+ ) ;
232+ return false ;
233+ }
234+ if ( Array . isArray ( item . path ) ) {
235+ return item . path . every ( ( pathstr ) => {
236+ const exists = pathstr && fs . existsSync ( pathstr ) ;
237+ if ( ! exists ) {
238+ loaderContext . emitError (
239+ new Error (
240+ `Not found path: ${ pathstr } in less-loader multipleScopeVars`
241+ )
242+ ) ;
243+ }
244+ return exists ;
245+ } ) ;
246+ }
247+ if (
248+ ! item . path ||
249+ typeof item . path !== "string" ||
250+ ! fs . existsSync ( item . path )
251+ ) {
252+ loaderContext . emitError (
253+ new Error (
254+ `Not found path: ${ item . path } in less-loader multipleScopeVars`
255+ )
256+ ) ;
257+ return false ;
258+ }
259+ return true ;
260+ } ) ;
261+ }
262+ return allStyleVarFiles ;
263+ } ;
264+
265+ const cssFragReg = / [ ^ { } / \\ ] + { [ ^ { } ] * ?} / g;
266+ const classNameFragReg = / [ ^ { } / \\ ] + (? = { ) / ;
224267const addScopeName = ( css , scopeName ) => {
225268 const splitCodes = css . match ( cssFragReg ) || [ ] ;
226269
227270 if ( splitCodes . length && scopeName ) {
228271 const fragments = [ ] ;
229272 const resultCode = splitCodes . reduce ( ( codes , curr ) => {
230273 const replacerFragment = curr . replace ( classNameFragReg , ( a ) =>
231- a . split ( "," ) . reduce ( ( tol , c ) => tol . replace ( c , `.${ scopeName } ${ c } ` ) , a )
274+ a . split ( "," ) . reduce ( ( tol , c ) => {
275+ if ( / ^ h t m l / i. test ( c ) ) {
276+ return tol ;
277+ }
278+ return tol . replace ( c , `.${ scopeName } ${ c } ` ) ;
279+ } , a )
232280 ) ;
233281 fragments . push ( replacerFragment ) ;
234282 return codes . replace ( curr , replacerFragment ) ;
@@ -247,11 +295,11 @@ const addScopeName = (css, scopeName) => {
247295 } ;
248296} ;
249297
250- const getScropProcessResult = ( lessResults = [ ] , allStyleVarFiles = [ ] ) => {
298+ const getScropProcessResult = ( cssResults = [ ] , allStyleVarFiles = [ ] ) => {
251299 const preprocessResult = { deps : [ ] , code : "" , errors : [ ] } ;
252300 const fragmentsGroup = [ ] ;
253301 const sourceFragmentsGroup = [ ] ;
254- lessResults . forEach ( ( item , i ) => {
302+ cssResults . forEach ( ( item , i ) => {
255303 const { fragments, sourceFragments } = addScopeName (
256304 item . code ,
257305 allStyleVarFiles [ i ] . scopeName
@@ -262,26 +310,52 @@ const getScropProcessResult = (lessResults = [], allStyleVarFiles = []) => {
262310 ...( preprocessResult . errors || [ ] ) ,
263311 ...( item . errors || [ ] ) ,
264312 ] ;
265- if ( allStyleVarFiles [ i ] . path ) {
266- preprocessResult . deps . push ( allStyleVarFiles [ i ] . path ) ;
267- }
313+ const deps = Array . isArray ( allStyleVarFiles [ i ] . path )
314+ ? allStyleVarFiles [ i ] . path
315+ : [ allStyleVarFiles [ i ] . path ] ;
316+ deps . forEach ( ( str ) => {
317+ if ( str ) {
318+ preprocessResult . deps . push ( str ) ;
319+ }
320+ } ) ;
268321 } ) ;
269- if ( lessResults . length && sourceFragmentsGroup . length ) {
322+ if ( cssResults . length && sourceFragmentsGroup . length ) {
270323 preprocessResult . code = sourceFragmentsGroup [ 0 ] . reduce (
271324 ( tol , curr , i ) =>
272325 tol . replace ( curr , ( ) => fragmentsGroup . map ( ( g ) => g [ i ] ) . join ( "\n" ) ) ,
273- lessResults [ 0 ] . code
326+ cssResults [ 0 ] . code
274327 ) ;
275- preprocessResult . map = lessResults [ 0 ] . map ;
276- preprocessResult . deps = [ ...preprocessResult . deps , ...lessResults [ 0 ] . deps ] ;
328+ preprocessResult . map = cssResults [ 0 ] . map ;
329+ preprocessResult . deps = [ ...preprocessResult . deps , ...cssResults [ 0 ] . deps ] ;
277330 }
278331
279332 return preprocessResult ;
280333} ;
334+ const replaceFormSass = ( url ) => {
335+ let code = url ? fs . readFileSync ( url ) . toString ( ) : "" ;
336+ if ( / \. ( s c s s | s a s s ) $ / i. test ( url ) ) {
337+ code = code . replace ( / \$ / g, "@" ) . replace ( / ! d e f a u l t / g, "" ) ;
338+ }
339+ return code ;
340+ } ;
341+
342+ const getVarsContent = ( url ) => {
343+ let content = "" ;
344+ if ( Array . isArray ( url ) ) {
345+ url . forEach ( ( p ) => {
346+ content += replaceFormSass ( p ) ;
347+ } ) ;
348+ } else {
349+ content = replaceFormSass ( url ) ;
350+ }
351+ return content ;
352+ } ;
281353export {
282354 getLessOptions ,
283355 isUnsupportedUrl ,
284356 normalizeSourceMap ,
357+ getAllStyleVarFiles ,
285358 addScopeName ,
286359 getScropProcessResult ,
360+ getVarsContent ,
287361} ;
0 commit comments