Use ByteStrings to make writing html and js a bit faster

This commit is contained in:
Evan Czaplicki 2013-09-25 14:25:54 -04:00
parent 045852a0c5
commit de232b677e
2 changed files with 13 additions and 12 deletions

View file

@ -179,7 +179,7 @@ build flags rootFile = do
files <- if make flags then getSortedDependencies noPrelude rootFile else return [rootFile]
let ifaces = if noPrelude then Map.empty else Prelude.interfaces
(moduleName, interfaces) <- buildFiles flags (length files) ifaces "" files
js <- foldM appendToOutput "" files
js <- foldM appendToOutput BS.empty files
(extension, code) <- case only_js flags of
True -> do
@ -187,22 +187,23 @@ build flags rootFile = do
return ("js", genJs js)
False -> do
putStr "Generating HTML ... "
runtime <- getRuntime flags
return ("html", genHtml $ createHtml runtime (takeBaseName rootFile) (sources js) moduleName "")
rtsPath <- getRuntime flags
return ("html", BS.pack . genHtml $
createHtml rtsPath (takeBaseName rootFile) (sources js) moduleName "")
let targetFile = buildPath flags rootFile extension
createDirectoryIfMissing True (takeDirectory targetFile)
writeFile targetFile code
BS.writeFile targetFile code
putStrLn "Done"
where
appendToOutput :: String -> FilePath -> IO String
appendToOutput :: BS.ByteString -> FilePath -> IO BS.ByteString
appendToOutput js filePath =
do src <- readFile (elmo flags filePath)
return (src ++ js)
do src <- BS.readFile (elmo flags filePath)
return (BS.append src js)
genHtml = if minify flags then Normal.renderHtml else Pretty.renderHtml
genJs = if minify flags then BS.unpack . JS.minify . BS.pack else id
genJs = if minify flags then JS.minify else id
sources js = map Link (scripts flags) ++
[ Source (if minify flags then Minified else Readable) js ]

View file

@ -19,7 +19,7 @@ import Generate.JavaScript
import Generate.Noscript
data JSStyle = Minified | Readable
data JSSource = Link String | Source JSStyle String
data JSSource = Link String | Source JSStyle BS.ByteString
makeScript :: JSSource -> H.Html
makeScript source =
@ -27,9 +27,9 @@ makeScript source =
Link src -> H.script ! A.type_ "text/javascript" ! A.src (H.toValue src) $ ""
Source style src ->
H.script ! A.type_ "text/javascript" $
preEscapedToMarkup $ case style of
Minified -> BS.unpack . minify . BS.pack $ src
Readable -> src
preEscapedToMarkup $ BS.unpack $ case style of
Minified -> minify src
Readable -> src
-- |This function compiles Elm code into simple HTML.
--