on master help sign in
Add --bundle-runtime flag to make standalone files
e4742dbEvan Czaplicki
compiler/Build/Flags.hs+4
@@ -9,6 +9,7 @@ data Flags = Flags
9 , files :: [FilePath]
9 , files :: [FilePath]
10 , set_runtime :: Maybe FilePath
10 , set_runtime :: Maybe FilePath
11 , get_runtime :: Bool
11 , get_runtime :: Bool
12+ , bundle_runtime :: Bool
12 , only_js :: Bool
13 , only_js :: Bool
13 , print_types :: Bool
14 , print_types :: Bool
14 , scripts :: [FilePath]
15 , scripts :: [FilePath]
@@ -41,6 +42,9 @@ flags = Flags
41 , get_runtime = False
42 , get_runtime = False
42 &= help "Print the absolute path to the default Elm runtime."
43 &= help "Print the absolute path to the default Elm runtime."
43
44
45+ , bundle_runtime = False
46+ &= help "Bundle the runtime with the generated html or js to create a standalone file."
47+
44 , cache_dir = "cache" &= typFile
48 , cache_dir = "cache" &= typFile
45 &= help "Directory for files cached to make builds faster. Defaults to cache/ directory."
49 &= help "Directory for files cached to make builds faster. Defaults to cache/ directory."
46
50
compiler/Compiler.hs+23−7
@@ -50,9 +50,9 @@ build flags rootFile =
50 (extension, code) <-
50 (extension, code) <-
51 if Flag.only_js flags
51 if Flag.only_js flags
52 then do putStr "Generating JavaScript ... "
52 then do putStr "Generating JavaScript ... "
53− return ("js", js)
53+ makeJs js
54 else do putStr "Generating HTML ... "
54 else do putStr "Generating HTML ... "
55− return (makeHtml js moduleName)
55+ makeHtml js moduleName
56
56
57 let targetFile = Utils.buildPath flags rootFile extension
57 let targetFile = Utils.buildPath flags rootFile extension
58 createDirectoryIfMissing True (takeDirectory targetFile)
58 createDirectoryIfMissing True (takeDirectory targetFile)
@@ -65,9 +65,25 @@ build flags rootFile =
65 src <- BS.readFile filePath
65 src <- BS.readFile filePath
66 return (BS.append src js)
66 return (BS.append src js)
67
67
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
69
74
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
70− makeHtml js moduleName = ("html", BS.pack $ renderHtml html)
85+ return ("html", BS.pack $ renderHtml (html runtime))
71− where
86+ where
72− rtsPath = Maybe.fromMaybe Path.runtime (Flag.set_runtime flags)
87+ sources js = map Html.Link (Flag.scripts flags) ++ [ Html.Source js ]
88+ html runtime =
73− html = Html.generate rtsPath (takeBaseName rootFile) (sources js) moduleName ""
89+ Html.generate runtime (takeBaseName rootFile) (sources js) moduleName ""
compiler/Generate/Html.hs+3−3
@@ -17,13 +17,13 @@ makeScript source =
17 H.script ! A.type_ "text/javascript" $
17 H.script ! A.type_ "text/javascript" $
18 preEscapedToMarkup $ BS.unpack src
18 preEscapedToMarkup $ BS.unpack src
19
19
20−generate :: FilePath -> String -> [JSSource] -> String -> String -> H.Html
20+generate :: JSSource -> String -> [JSSource] -> String -> String -> H.Html
21−generate libLoc title scripts moduleName noscript =
21+generate runtime title scripts moduleName noscript =
22 H.docTypeHtml $ do
22 H.docTypeHtml $ do
23 H.head $ do
23 H.head $ do
24 H.meta ! A.charset "UTF-8"
24 H.meta ! A.charset "UTF-8"
25 H.title . H.toHtml $ title
25 H.title . H.toHtml $ title
26− makeScript (Link libLoc)
26+ makeScript runtime
27 mapM_ makeScript scripts
27 mapM_ makeScript scripts
28 H.body $ do
28 H.body $ do
29 H.script ! A.type_ "text/javascript" $
29 H.script ! A.type_ "text/javascript" $