How can Shake FilePatterns be used to minify JS and CSS files?
Common practice with
.css file minification is to create
.min.css files. The problem is Shake FilePatterns will match both minified and non-minified files with this scheme with a pattern like
//*.js. The docs seem to recommend making unique extensions for each like
min.js but I'm not sure this is a path I want to take. How can this be worked around in Shake?
Edit: The problem is with using
js <- getDirectoryFiles "" ["//*.js"] let jsMins = map (-<.> "min" <.> "js") js need jsMins
There's a target for
"//*.min.js that works just fine but
getDirectoryFiles "" ["//*.js"] starts picking up the generated
I can think of two alternative approaches:
getDirectoryContents then filter out the values you don't care about. Something like:
xs <- getDirectoryFiles "" ["//*.js"] need [x -<.> "min.js" | x <- xs, takeExtensions x == ".js"]
If you are running this code outside of a rule, e.g. in an
action, then it is 100% correct. If you are running the code in a rule then the rule will run once, new files will appear, and then the rule will run again (which is also a lint violation, and not great build hygine). I suspect you are running outside of a rule, so this approach is probably a good one.
Write an Oracle
If you are inside a rule, you can define an Oracle which is like
getDirectoryFiles, but skips the
newtype JsFiles = JsFiles () deriving (Show,Typeable,Eq,Hashable,Binary,NFData) addOracle $ \(JsFiles _) -> fmap (\x -> takeExtensions x == ".js") $ getDirectoryFiles "" ["//*.js"]
Now you can use
askOracle $ JsFiles () in your rule, and it will only change when new non-
min files are added.