How can Shake FilePatterns be used to minify JS and CSS files?

Tags: javascript,css,haskell,minify,shake-build-system

Problem :

Common practice with .js and .css file minification is to create .min.js and .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 reg.js and 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 getDirectoryFiles:

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 .min.js files.

Solution :

I can think of two alternative approaches:

Post process getDirectoryContents

Call 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 min files.

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.

    CSS Howto..

    How do I center these three blocks horizontally?

    How to set auto-margin boxes in flexible-width design using CSS?

    Make Dojo or Ext JS4 widgets look and behave like Kendo UI. How demanding will it be?

    How to isolate CSS rules inside a div?

    How to Click a div and have it change dimensions of other divs and then change back once clicked again

    PHP Header Include (nav bar) not showing up [closed]

    How to apply different css style to gwt datagrid widget in the same page? [duplicate]

    How to create shaded divs like this with CSS

    Angular 2 Materialize CSS Side-Nav Not Showing

    how to apply css to a specific element of a table?

    how to put css in selected row in angular js?

    How to fill larger image into smaller div?

    DropDown Menu won't show for more than a second..not working.

    How to build sliding horizontal menu. CSS

    How to create a scrollable Div Tag Vertically?

    How to close popup

    how to get a rounded corner table with a body in different color with CSS?

    How to write below CSS lines in zend framework 2 syntax?

    How to properly use css will-change property?

    How to show only half of bootstrap glyphicon with css

    How do I align this CSS correctly?

    How to set css class 'active' by using C#?

    Show Link on image at hovering in jquery

    How can I add image to facebook login button?

    How to override CSS code with a new class?

    How to select child element inside first, second or third html element with CSS classes?

    How to read contents of an external script/style?

    How can I animate a table row with a single column that spans multiple columns?

    How do I use the alignment of one class and the font of another?

    How to link css files to work correctly with wamp server?