on master help sign in
Add --separate-js and --only-js flags. Improve error messages for missing and conflicting modules.
commit0bef882fb59a56be12b73beaa1af394f06cda7ea authorevancz <info@elm-lang.org> dateThu 14 Jun 2012, 05:02 (14 years ago) parent00619e6
3 files changed+57−26
elm/src/Compiler.hs+28−13 elm/src/Gen/CompileToJS.hs+11−5 elm/src/Gen/GenerateHtml.hs+18−8
elm/src/Compiler.hs+28−13
@@ -18,8 +18,8 @@ data ELM =
1818 ELM { make :: Bool
1919 , files :: [FilePath]
2020 , runtime :: Maybe FilePath
21−-- , separate_js :: Bool
22−-- , only_js :: Bool
21+ , separate_js :: Bool
22+ , only_js :: Bool
2323-- , verbose_js :: Bool
2424 }
2525 deriving (Data,Typeable,Show,Eq)
@@ -28,8 +28,8 @@ elm = ELM { make = False &= help "automatically compile dependencies."
2828 , files = def &= args &= typ "FILES"
2929 , runtime = Nothing &= typ "FILE" &=
3030 help "Specify a custom location for Elm's runtime system."
31−-- , separate_js = False &= help "Compile to separate HTML and JS files."
32−-- , only_js = False &= help "Compile only to JavaScript."
31+ , separate_js = False &= help "Compile to separate HTML and JS files."
32+ , only_js = False &= help "Compile only to JavaScript."
3333-- , verbose_js = False &= help "Produce JavaScript that may be easier to debug."
3434 } &=
3535 help "Compile Elm programs to HTML, CSS, and JavaScript." &=
@@ -40,20 +40,30 @@ main = do
4040 mini <- getDataFileName "elm-runtime-0.3.0.js"
4141 compileArgs mini args
4242
43−compileArgs mini (ELM _ [] _) =
43+compileArgs mini (ELM _ [] _ _ _) =
4444 putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
45−compileArgs mini (ELM make files rtLoc) =
46− mapM_ (fileToHtml $ fromMaybe mini rtLoc) files
45+compileArgs mini (ELM make files rtLoc split only) =
46+ mapM_ (fileTo get what $ fromMaybe mini rtLoc) files
47+ where get = if make then getModules [] else getModule
48+ what = if only then JS else
49+ if split then Split else HTML
4750
48−fileToHtml rtLoc file = do
49− ems <- getModules [] file
51+data What = JS | HTML | Split
52+
53+fileTo get what rtLoc file = do
54+ ems <- get file
5055 case ems of
5156 Left err -> putStrLn $ "Error while compiling " ++ file ++ ":\n" ++ err
5257 Right ms ->
53− let name = reverse . tail . dropWhile (/='.') $ reverse file in
54− writeFile (name ++ ".html") . renderHtml $
55− modulesToHtml rtLoc ms
56−
58+ let name = reverse . tail . dropWhile (/='.') $ reverse file
59+ js = name ++ ".js"
60+ html = name ++ ".html"
61+ in case what of
62+ JS -> writeFile js $ concatMap jsModule ms
63+ HTML -> writeFile html . renderHtml $ modulesToHtml rtLoc ms
64+ Split -> do
65+ writeFile html . renderHtml $ linkedHtml rtLoc js ms
66+ writeFile js $ concatMap jsModule ms
5767
5868getModules :: [String] -> FilePath -> IO (Either String [Module])
5969getModules uses file = do
@@ -71,6 +81,11 @@ getModules uses file = do
7181 [] -> Right $ concat (rights ems) ++ [modul]
7282 errs -> Left $ intercalate "\n" errs
7383
84+getModule :: FilePath -> IO (Either String [Module])
85+getModule file = do
86+ code <- readFile file
87+ return . fmap (:[]) $ initialize code
88+
7489toFilePath :: String -> FilePath
7590toFilePath modul = map (\c -> if c == '.' then '/' else c) modul ++ ".elm"
7691
elm/src/Gen/CompileToJS.hs+11−5
@@ -41,7 +41,7 @@ tryBlock names e =
4141jsModule (Module names exports imports defs) =
4242 tryBlock (tail modNames) $ unwords
4343 [ concatMap (\n -> globalAssign n $ n ++ " || {}") . map (intercalate ".") . drop 2 . inits $ take (length modNames - 1) modNames
44− , "\nif (" ++ modName ++ ") throw 'Module name collision, " ++ intercalate "." names ++ " is already defined.'; "
44+ , "\nif (" ++ modName ++ ") throw 'Module name collision, " ++ intercalate "." (tail modNames) ++ " is already defined.'; "
4545 , globalAssign modName $ jsFunc "" (includes ++ body ++ export) ++ "()"
4646 , mainEquals $ modName ++ ".main" ]
4747 where modNames = if null names then ["ElmCode", "Main"] else "ElmCode" : names
@@ -55,11 +55,17 @@ jsModule (Module names exports imports defs) =
5555 let y = reverse . tail . dropWhile isDigit $ reverse x in
5656 if y `elem` exps then Just $ y ++ ":" ++ x else Nothing
5757
58−jsImport (modul, As name) = assign name modul
59−jsImport (modul, Importing []) = jsImport (modul, Hiding [])
60−jsImport (modul, Importing vs) =
58+jsImport (modul, how) =
59+ unwords [ "\ntry{" ++ modul ++ "} catch(e) {throw 'Module "
60+ , drop 1 (dropWhile (/='.') modul)
61+ , " is missing. Compile with --make flag or load missing "
62+ , "module in a separate JS file.';}" ] ++ jsImport' (modul, how)
63+
64+jsImport' (modul, As name) = assign name modul
65+jsImport' (modul, Importing []) = jsImport' (modul, Hiding [])
66+jsImport' (modul, Importing vs) =
6167 concatMap (\v -> assign v $ modul ++ "." ++ v) vs
62−jsImport (modul, Hiding vs) =
68+jsImport' (modul, Hiding vs) =
6369 unwords [ "\nfor(var i in " ++ modul ++ "){"
6470 , assign "hiddenVars" . jsList $ map (\v -> "'" ++ v ++ "'") vs
6571 , "\nif (hiddenVars.indexOf(i) >= 0) continue;"
elm/src/Gen/GenerateHtml.hs+18−8
@@ -1,5 +1,8 @@
11{-# LANGUAGE OverloadedStrings #-}
2−module GenerateHtml (generateHtml, body, css, widgetBody, modulesToHtml) where
2+module GenerateHtml (generateHtml,
3+ body, css, widgetBody,
4+ modulesToHtml, linkedHtml
5+ ) where
36
47import Data.List (intercalate)
58import Text.Blaze (preEscapedToMarkup)
@@ -23,9 +26,11 @@ css = H.style ! A.type_ "text/css" $ preEscapedToMarkup
2326 \a:active {text-decoration: none}\
2427 \a:hover {text-decoration: underline; color: #ff8f12;}" :: String)
2528
26−makeScript :: String -> H.Html
27−makeScript s = H.script ! A.type_ "text/javascript" ! A.src (H.toValue s) $ ""
28−
29+makeScript :: Either String String -> H.Html
30+makeScript (Left s) =
31+ H.script ! A.type_ "text/javascript" ! A.src (H.toValue s) $ ""
32+makeScript (Right s) =
33+ H.script ! A.type_ "text/javascript" $ preEscapedToMarkup s
2934
3035-- |This function compiles Elm code into simple HTML.
3136--
@@ -38,22 +43,27 @@ generateHtml :: String -- ^ Location of elm-min.js as expected by the browser
3843 -> Html
3944generateHtml libLoc title source =
4045 case initialize source of
41− Left err -> createHtml libLoc title (showErr err) (H.noscript "")
46+ Left err -> createHtml libLoc title (Right $ showErr err) (H.noscript "")
4247 Right modul -> modulesToHtml libLoc [modul]
4348
4449
4550modulesToHtml libLoc modules = createHtml libLoc title js noscript
4651 where title = (\(Module names _ _ _) -> intercalate "." names) $ last modules
47− js = concatMap jsModule modules
52+ js = Right $ concatMap jsModule modules
4853 noscript = extract $ last modules
4954
55+linkedHtml rtLoc jsLoc modules =
56+ createHtml rtLoc title (Left jsLoc) (H.noscript "")
57+ where title = (\(Module names _ _ _) -> intercalate "." names) $ last modules
58+
59+
5060createHtml libLoc title js noscript =
5161 H.docTypeHtml $ do
5262 H.head $ do
5363 H.meta ! A.charset "UTF-8"
5464 H.title . H.toHtml $ title
55− makeScript libLoc
56− H.script ! A.type_ "text/javascript" $ preEscapedToMarkup js
65+ makeScript (Left libLoc)
66+ makeScript js
5767 css
5868 H.body $ body noscript
5969