snap-elm- Serve Elm files through the Snap web framework.

Safe HaskellNone



This module provides a few functions for conveniently serving Elm files through the Snap web framework. Any changes made to the served files will be reflected in the browser upon a refresh.

The easiest way to get started is to use the default ElmOptions:

 app = makeSnaplet ... $ do
     opts <- defaultElmOptions
     addRoutes $ routes opts

Then, provide routes to the Elm runtime, and to any Elm files you wish to serve.

 routes opts =
     [ ("/elm", serveElmFile opts "static/elm/test.elm")
     , ...
     , serveElmRuntime opts

Additionally, you can customize the URI of the Elm runtime, the file path to the Elm runtime, or the paths to the directores that Elm will use to build and cache the compiled files.

 app = makeSnaplet ... $ do
     opts <- setElmBuildPath "/tmp/elm" <$>
             setElmVerbose True <$>
     addRoutes $ routes opts

The FilePaths supplied to setElm{Source,Build,Cache}Path can be relative or absolute.



data ElmOptions Source

A set of options to coordinate the serving of Elm files and runtime.

defaultElmOptions :: MonadIO m => m ElmOptionsSource

The default set of options for serving Elm files. The values are as follows (IO aside):

   { elmIsVerbose   = False
   , elmRuntimeURI  = "/static/js/elm-runtime.js"
   , elmRuntimePath = <Language.Elm.runtime>
   , elmSourcePath  = "."
   , elmBuildPath   = "elm-build"
   , elmCachePath   = "elm-cache"

setElmVerbose :: Bool -> ElmOptions -> ElmOptionsSource

Tell Elm to be verbose (print all executed commands and their output to stdout), or quiet (print nothing).

setElmRuntimeURI :: ByteString -> ElmOptions -> ElmOptionsSource

Set the URI at which to serve the Elm runtime JS file.

setElmRuntimePath :: FilePath -> ElmOptions -> ElmOptionsSource

Set the FilePath to some custom Elm runtime.

setElmSourcePath :: FilePath -> ElmOptions -> ElmOptionsSource

Set the directory to look for .elm files. This allows for the elm binary to properly find local Elm modules.

setElmBuildPath :: FilePath -> ElmOptions -> ElmOptionsSource

Set the directory to use for storing the compiled .html that elm produces.

setElmCachePath :: FilePath -> ElmOptions -> ElmOptionsSource

Set the directory to use for storing the .elmi and .elmo files that elm produces.

serveElmFile :: MonadSnap m => ElmOptions -> FilePath -> m ()Source

Compile and serve an Elm file.



:: MonadSnap m 
=> ElmOptions 
-> ByteString

Route for serving directory.

-> (ByteString, m ()) 

Serve a directory of Elm files.

For example, a list of routes could contain:

 routes opts =
   [ ...
   , serveElmDirectory opts "/elm"

In this example, if the ElmOptions contained static/elm as the sourcePath, the route elm/file.elm would be handled by serveElmFile file.elm, run with the working directory static/elm.

serveElmRuntime :: MonadSnap m => ElmOptions -> (ByteString, m ())Source

A route handler for the Elm runtime. If given the ElmOptions used by serveElmFile, it will place the runtime at the route the Elm file will expect, as per the <script src=.../runtime.js> element included in the compiled file's <head> section.