@@ -18,43 +18,44 @@ class StylesheetsFileResolver {
1818 const cssFiles = [ ] ;
1919 for await ( const file of files ) {
2020 const content = await fs . promises . readFile ( file , { encoding : 'utf-8' } ) ;
21- const result = content . matchAll ( STYLESHEET_REGEXP ) ;
22- if ( result ) {
23- for ( const item of result ) {
24- // remove quotes
25- const filePath = item [ 1 ] . slice ( 1 , - 1 ) ;
26- const fileName = this . tryToResolveCssFileLocation ( filePath ) ;
27- if (
28- fileName &&
29- ! this . isStyleSheetAComment ( content , filePath ) &&
30- ! cssFiles . includes ( fileName )
31- ) {
32- cssFiles . push ( fileName ) ;
33- }
34- }
35- }
21+ const currentCssFiles = this . getStylesheets ( content ) ;
22+ cssFiles . push ( ...currentCssFiles ) ;
3623 }
3724
3825 return cssFiles ;
3926 }
4027
41- isStyleSheetAComment ( content , cssFilePath ) {
28+ extractFileNameFromMatchedResult ( item ) {
29+ // remove quotes
30+ const filePath = item [ 1 ] . slice ( 1 , - 1 ) ;
31+ const fileName = this . tryToResolveCssFileLocation ( filePath ) ;
32+ return fileName ;
33+ }
34+
35+ getStylesheets ( content ) {
36+ const self = this ;
37+ const stylesheets = [ ] ;
4238 const $ = cheerio . load ( content ) ;
43- const comments = $ ( '*' )
39+ // ignore non-text nodes (like comments, etc.)
40+ const stylesheetTexts = $ ( '*' )
4441 . contents ( )
4542 . filter ( function ( ) {
46- // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#node.comment_node
47- return this . nodeType === 8 ;
43+ return this . type === "text" && this . data && this . data . includes ( 'stylesheet' ) ;
44+ } )
45+ . map ( function ( ) {
46+ return $ ( this ) . text ( ) ;
4847 } ) ;
49- for ( const comment of comments ) {
50- const { data } = comment ;
51- if ( data && data . includes ( 'stylesheet' ) && data . includes ( cssFilePath ) && ! this . alreadyExlcudedComments . includes ( data ) ) {
52- this . alreadyExlcudedComments . push ( data ) ;
53- return true ;
48+ // extract stylesheet paths from the text nodes and add them to the array
49+ for ( const node of stylesheetTexts ) {
50+ const result = node . matchAll ( STYLESHEET_REGEXP ) ;
51+ if ( result ) {
52+ for ( const item of result ) {
53+ const extracted = self . extractFileNameFromMatchedResult ( item ) ;
54+ stylesheets . push ( extracted ) ;
55+ }
5456 }
5557 }
56-
57- return false ;
58+ return stylesheets ;
5859 }
5960
6061 // returns relative path starting from root scss/css folder
0 commit comments