Andy Frank avatar Andy Frank committed c2a1f91

LessTask: exclude files used as imports

Comments (0)

Files changed (1)

src/fan/LessTask.fan

       log.info("less [$podName]")
       log.indent
 
-// TODO FIXIT: match files with @import 'xxx' - don't output to CSS
-
-      // find source files
+      // find source files, exluding files used as imports
       files := File[,]
       findFiles(srcDir, files)
+      files = excludeImports(files)
       log.info("FindSourceFiles [$files.size files]")
 
       // compile to css
     else if (file.ext == "less") list.add(file)
   }
 
+  ** Filter out any files used as an import in another file.
+  private File[] excludeImports(File[] list)
+  {
+    // find files used as imports
+    map := Uri:Bool[:]
+    list.each |f|
+    {
+      f.readAllLines.each |line|
+      {
+        m := Regex<|^\s*@import\s*"([a-zA-Z0-9_\./]*)";|>.matcher(line)
+        while (m.find)
+        {
+          uri := f.uri + m.group(1).toUri
+          map[uri] = true
+        }
+      }
+    }
+
+    // debug
+    if (log.isDebug)
+    {
+      log.debug("ExcludeImports [$map.size]")
+      log.indent
+      map.keys.each |k| { log.debug(k.toFile.osPath) }
+      log.unindent
+    }
+
+    // remove from list
+    return list.findAll |f| { !map.containsKey(f.uri) }
+  }
+
   ** Compile less to css.
   private Void toCss(File src)
   {
     srcUri  := src.uri.relTo(srcDir.uri)
     outFile := outDir + (srcUri + `${src.basename}.css`)
-    log.debug("$srcUri -> $outFile")
+    log.debug("$srcUri -> $outFile.osPath")
 
     css := Less { file=src }.toCss
     out := outFile.out
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.