module Media.JS (
    compressJsCompiler
) where

import qualified Data.ByteString.Lazy.Char8 as C
import           Data.Tuple.Extra           (dupe, first)
import           Hakyll                     (Compiler, Item, getResourceString,
                                             itemBody, itemSetBody)
import           Text.Jasmine               (minify)

compressJsCompiler :: Compiler (Item String)
compressJsCompiler :: Compiler (Item String)
compressJsCompiler = (String -> Item String -> Item String)
-> (String, Item String) -> Item String
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry String -> Item String -> Item String
forall a b. a -> Item b -> Item a
itemSetBody ((String, Item String) -> Item String)
-> (Item String -> (String, Item String))
-> Item String
-> Item String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Item String -> String)
-> (Item String, Item String) -> (String, Item String)
forall a a' b. (a -> a') -> (a, b) -> (a', b)
first Item String -> String
minifyJS ((Item String, Item String) -> (String, Item String))
-> (Item String -> (Item String, Item String))
-> Item String
-> (String, Item String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item String -> (Item String, Item String)
forall a. a -> (a, a)
dupe (Item String -> Item String)
-> Compiler (Item String) -> Compiler (Item String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Compiler (Item String)
getResourceString
    where
        minifyJS :: Item String -> String
minifyJS = ByteString -> String
C.unpack (ByteString -> String)
-> (Item String -> ByteString) -> Item String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
minify (ByteString -> ByteString)
-> (Item String -> ByteString) -> Item String -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
C.pack (String -> ByteString)
-> (Item String -> String) -> Item String -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item String -> String
forall a. Item a -> a
itemBody