Compile Sass
This commit is contained in:
parent
18c5de4b86
commit
5c5a080f0f
@ -114,6 +114,7 @@ dependencies:
|
|||||||
- memcached-binary
|
- memcached-binary
|
||||||
- directory-tree
|
- directory-tree
|
||||||
- lifted-base
|
- lifted-base
|
||||||
|
- hsass
|
||||||
|
|
||||||
other-extensions:
|
other-extensions:
|
||||||
- GeneralizedNewtypeDeriving
|
- GeneralizedNewtypeDeriving
|
||||||
|
|||||||
@ -6,6 +6,7 @@ module Settings.StaticFiles
|
|||||||
import ClassyPrelude
|
import ClassyPrelude
|
||||||
|
|
||||||
import Settings (appStaticDir, compileTimeAppSettings)
|
import Settings (appStaticDir, compileTimeAppSettings)
|
||||||
|
import Settings.StaticFiles.Generator
|
||||||
import Yesod.EmbeddedStatic
|
import Yesod.EmbeddedStatic
|
||||||
|
|
||||||
-- This generates easy references to files in the static directory at compile time,
|
-- This generates easy references to files in the static directory at compile time,
|
||||||
@ -23,4 +24,4 @@ import Yesod.EmbeddedStatic
|
|||||||
#define DEV_BOOL False
|
#define DEV_BOOL False
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
mkEmbeddedStatic DEV_BOOL "embeddedStatic" [embedDir $ appStaticDir compileTimeAppSettings]
|
mkEmbeddedStatic DEV_BOOL "embeddedStatic" . pure . staticGenerator $ appStaticDir compileTimeAppSettings
|
||||||
|
|||||||
64
src/Settings/StaticFiles/Generator.hs
Normal file
64
src/Settings/StaticFiles/Generator.hs
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
module Settings.StaticFiles.Generator
|
||||||
|
( staticGenerator
|
||||||
|
) where
|
||||||
|
|
||||||
|
import ClassyPrelude
|
||||||
|
import Yesod.EmbeddedStatic.Types
|
||||||
|
import Yesod.EmbeddedStatic
|
||||||
|
|
||||||
|
import System.FilePath
|
||||||
|
import System.Directory.Tree
|
||||||
|
import Network.Mime
|
||||||
|
|
||||||
|
import Language.Haskell.TH
|
||||||
|
import Language.Haskell.TH.Syntax
|
||||||
|
|
||||||
|
import qualified Data.ByteString as BS
|
||||||
|
import qualified Data.ByteString.Lazy as LBS
|
||||||
|
|
||||||
|
import qualified Data.Map as Map
|
||||||
|
|
||||||
|
import qualified Text.Sass.Compilation as Sass
|
||||||
|
|
||||||
|
import Data.Default
|
||||||
|
|
||||||
|
import qualified Data.Foldable as Fold
|
||||||
|
|
||||||
|
|
||||||
|
staticGenerator :: FilePath -> Generator
|
||||||
|
staticGenerator staticDir = do
|
||||||
|
dirTree' <- runIO $ readDirectoryWith toEntries staticDir
|
||||||
|
Fold.forM_ (fst <$> zipPaths dirTree') addDependentFile
|
||||||
|
return . Fold.fold $ dirTree dirTree'
|
||||||
|
where
|
||||||
|
toEntries :: FilePath -- ^ Absolute path
|
||||||
|
-> IO [Entry]
|
||||||
|
toEntries loc = compile (mimeByExt mimeMap defaultMimeType $ pack loc) (makeRelative staticDir loc) loc
|
||||||
|
|
||||||
|
mimeMap = defaultMimeMap `mappend` Map.fromList
|
||||||
|
[ ("sass", "text/x-sass")
|
||||||
|
, ("scss", "text/x-scss")
|
||||||
|
]
|
||||||
|
|
||||||
|
compile :: MimeType
|
||||||
|
-> Location -- ^ Relative location
|
||||||
|
-> FilePath -- ^ Absolute filepath
|
||||||
|
-> IO [Entry]
|
||||||
|
compile sassMime sassLoc fp
|
||||||
|
| sassMime `elem` [ "text/x-sass", "text/x-scss" ]
|
||||||
|
= return . pure $ def
|
||||||
|
{ ebHaskellName = Just $ pathToName ebLocation
|
||||||
|
, ebLocation
|
||||||
|
, ebMimeType = "text/css"
|
||||||
|
, ebProductionContent = either (fail <=< Sass.errorMessage) (return . LBS.fromStrict) =<< Sass.compileFile fp def
|
||||||
|
, ebDevelReload = [| either (fail <=< Sass.errorMessage) (return . LBS.fromStrict) =<< Sass.compileFile $(litE $ stringL fp) def |]
|
||||||
|
}
|
||||||
|
where
|
||||||
|
ebLocation = sassLoc -<.> "css"
|
||||||
|
compile ebMimeType ebLocation fp = return . pure $ def
|
||||||
|
{ ebHaskellName = Just $ pathToName ebLocation
|
||||||
|
, ebLocation
|
||||||
|
, ebMimeType
|
||||||
|
, ebProductionContent = LBS.fromStrict <$> BS.readFile fp
|
||||||
|
, ebDevelReload = [| LBS.fromStrict <$> BS.readFile $(litE $ stringL fp) |]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user