on master help sign in
Add --bundle-runtime flag to make standalone files

From @agazso’s request. @maxsnew or @johnpmayer, do you remember if there is an issue for this? I feel like one of you has suggested this before.

commite4742dbecebd0b0e1ab471a39d309ff0eb1aeec9 authorEvan Czaplicki <info@elm-lang.org> dateThu 12 Jun 2014, 11:12 (12 years ago) parentc14d029
3 files changed+30−10
compiler/Build/Flags.hs+4 compiler/Compiler.hs+23−7 compiler/Generate/Html.hs+3−3
compiler/Build/Flags.hs+4
@@ -9,6 +9,7 @@ data Flags = Flags
99 , files :: [FilePath]
1010 , set_runtime :: Maybe FilePath
1111 , get_runtime :: Bool
12+ , bundle_runtime :: Bool
1213 , only_js :: Bool
1314 , print_types :: Bool
1415 , scripts :: [FilePath]
@@ -41,6 +42,9 @@ flags = Flags
4142 , get_runtime = False
4243 &= help "Print the absolute path to the default Elm runtime."
4344
45+ , bundle_runtime = False
46+ &= help "Bundle the runtime with the generated html or js to create a standalone file."
47+
4448 , cache_dir = "cache" &= typFile
4549 &= help "Directory for files cached to make builds faster. Defaults to cache/ directory."
4650
compiler/Compiler.hs+23−7
@@ -50,9 +50,9 @@ build flags rootFile =
5050 (extension, code) <-
5151 if Flag.only_js flags
5252 then do putStr "Generating JavaScript ... "
53− return ("js", js)
53+ makeJs js
5454 else do putStr "Generating HTML ... "
55− return (makeHtml js moduleName)
55+ makeHtml js moduleName
5656
5757 let targetFile = Utils.buildPath flags rootFile extension
5858 createDirectoryIfMissing True (takeDirectory targetFile)
@@ -65,9 +65,25 @@ build flags rootFile =
6565 src <- BS.readFile filePath
6666 return (BS.append src js)
6767
68− sources js = map Html.Link (Flag.scripts flags) ++ [ Html.Source js ]
68+ getRuntime :: IO Html.JSSource
69+ getRuntime =
70+ let runtimePath = Maybe.fromMaybe Path.runtime (Flag.set_runtime flags) in
71+ case Flag.bundle_runtime flags of
72+ False -> return (Html.Link runtimePath)
73+ True -> Html.Source `fmap` BS.readFile runtimePath
6974
70− makeHtml js moduleName = ("html", BS.pack $ renderHtml html)
71− where
72− rtsPath = Maybe.fromMaybe Path.runtime (Flag.set_runtime flags)
73− html = Html.generate rtsPath (takeBaseName rootFile) (sources js) moduleName ""
75+ makeJs :: BS.ByteString -> IO (String, BS.ByteString)
76+ makeJs js =
77+ do runtime <- getRuntime
78+ case runtime of
79+ Html.Link _ -> return ("js", js)
80+ Html.Source rts -> return ("js", BS.append rts js)
81+
82+ makeHtml :: BS.ByteString -> String -> IO (String, BS.ByteString)
83+ makeHtml js moduleName =
84+ do runtime <- getRuntime
85+ return ("html", BS.pack $ renderHtml (html runtime))
86+ where
87+ sources js = map Html.Link (Flag.scripts flags) ++ [ Html.Source js ]
88+ html runtime =
89+ Html.generate runtime (takeBaseName rootFile) (sources js) moduleName ""
compiler/Generate/Html.hs+3−3
@@ -17,13 +17,13 @@ makeScript source =
1717 H.script ! A.type_ "text/javascript" $
1818 preEscapedToMarkup $ BS.unpack src
1919
20−generate :: FilePath -> String -> [JSSource] -> String -> String -> H.Html
21−generate libLoc title scripts moduleName noscript =
20+generate :: JSSource -> String -> [JSSource] -> String -> String -> H.Html
21+generate runtime title scripts moduleName noscript =
2222 H.docTypeHtml $ do
2323 H.head $ do
2424 H.meta ! A.charset "UTF-8"
2525 H.title . H.toHtml $ title
26− makeScript (Link libLoc)
26+ makeScript runtime
2727 mapM_ makeScript scripts
2828 H.body $ do
2929 H.script ! A.type_ "text/javascript" $