@@ -26,7 +26,8 @@ data ELM =
2626 , separate_js :: Bool
2727 , only_js :: Bool
2828 , import_js :: [FilePath]
29− , generate_noscript :: Bool
29+ , no_prelude :: Bool
30+ , noscript :: Bool
3031 , minify :: Bool
3132 , output_directory :: Maybe FilePath
3233 }
@@ -39,7 +40,8 @@ elm = ELM { make = False &= help "automatically compile dependencies."
3940 , separate_js = False &= help "Compile to separate HTML and JS files."
4041 , only_js = False &= help "Compile only to JavaScript."
4142 , import_js = [] &= typFile &= help "Include a JavaScript file before the body of the Elm program. Can be used many times. Files will be included in the given order."
42− , generate_noscript = True &= help "Add generated <noscript> tag to HTML output."
43+ , no_prelude = False &= help "Do not import Prelude by default, used only when compiling standard libraries."
44+ , noscript = True &= help "Add generated <noscript> tag to HTML output."
4345 , minify = False &= help "Minify generated JavaScript"
4446 , output_directory = Nothing &= typFile &= help "Output files to directory specified. Defaults to the location of original file."
4547 } &=
@@ -51,35 +53,39 @@ main = do
5153 mini <- getDataFileName "elm-runtime.js"
5254 compileArgs mini args
5355
54−compileArgs mini (ELM _ [] _ _ _ _ _ _ _) =
55− putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
56−compileArgs mini (ELM make files rtLoc split only js nscrpt isMini outputDir) =
57− mapM_ (fileTo isMini make what js nscrpt outputDir loc) files
58− where loc = fromMaybe mini rtLoc
59− what | only = JS
60− | split = Split
61− | otherwise = HTML
56+compileArgs mini flags =
57+ case files flags of
58+ [] -> putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
59+ fs -> mapM_ (fileTo flags what loc) fs
60+ where loc = fromMaybe mini (runtime flags)
61+ what | only_js flags = JS
62+ | separate_js flags = Split
63+ | otherwise = HTML
6264
6365data What = JS | HTML | Split
6466
65−fileTo isMini make what jsFiles noscript outputDir rtLoc file = do
66− let jsStyle = if isMini then Minified else Readable
67− formatJS = if isMini then BS.unpack . JS.minify . BS.pack else id
68− ems <- if make then build file else do src <- readFile file
69− return (fmap (:[]) (buildFromSource src))
70− jss <- concat `fmap` mapM readFile jsFiles
67+fileTo flags what rtLoc file = do
68+ let jsStyle = if minify flags then Minified else Readable
69+ formatJS = if minify flags then BS.unpack . JS.minify . BS.pack else id
70+ ems <- if make flags then build file
71+ else do src <- readFile file
72+ return (fmap (:[]) (buildFromSource src))
73+ jss <- concat `fmap` mapM readFile (import_js flags)
7174 case ems of
7275 Left err -> do putStrLn $ "Error while compiling " ++ file ++ ":\n" ++ err
7376 exitFailure
74− Right ms ->
75− let path = fromMaybe "" outputDir </> file
77+ Right ms' ->
78+ let path = fromMaybe "" (output_directory flags) </> file
7679 js = replaceExtension path ".js"
7780 html = replaceExtension path ".html"
81+ addPrelude (Module name exs ims stmts) =
82+ Module name exs (("Prelude", Importing []) : ims) stmts
83+ ms = if no_prelude flags then ms' else map addPrelude ms'
7884 txt = jss ++ concatMap jsModule ms
7985 in case what of
8086 JS -> writeFile js (formatJS txt)
8187 HTML -> writeFile html . renderHtml $
82− modulesToHtml jsStyle "" rtLoc jss noscript ms
88+ modulesToHtml jsStyle "" rtLoc jss (noscript flags) ms
8389 Split ->
8490 do writeFile html . renderHtml $ linkedHtml rtLoc js ms
8591 writeFile js (formatJS txt)