Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions builder/src/Generate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ debug root details (Build.Artifacts pkg ifaces roots modules) =
let graph_ = objectsToGlobalGraph objects
graph <- Task.io $ Lamdera.AppConfig.injectConfig graph_
let mains = gatherMains pkg objects roots
esmEnabled <- Task.io $ Lamdera.useEsm
return $ JS.generate mode graph mains
& Lamdera.alternativeImplementationWhen esmEnabled
(JS.generateEsm mode graph mains)


dev :: FilePath -> Details.Details -> Build.Artifacts -> Task B.Builder
Expand All @@ -70,7 +73,10 @@ dev root details (Build.Artifacts pkg _ roots modules) =
let graph_ = objectsToGlobalGraph objects
graph <- Task.io $ Lamdera.AppConfig.injectConfig graph_
let mains = gatherMains pkg objects roots
esmEnabled <- Task.io $ Lamdera.useEsm
return $ JS.generate mode graph mains
& Lamdera.alternativeImplementationWhen esmEnabled
(JS.generateEsm mode graph mains)


prod :: FilePath -> Details.Details -> Build.Artifacts -> Task B.Builder
Expand All @@ -84,7 +90,10 @@ prod root details (Build.Artifacts pkg _ roots modules) =
& Lamdera.alternativeImplementationWhen longNamesEnabled
(Mode.Prod (Mode.legibleFieldNames graph))
let mains = gatherMains pkg objects roots
esmEnabled <- Task.io $ Lamdera.useEsm
return $ JS.generate mode graph mains
& Lamdera.alternativeImplementationWhen esmEnabled
(JS.generateEsm mode graph mains)


repl :: FilePath -> Details.Details -> Bool -> Build.ReplArtifacts -> N.Name -> Task B.Builder
Expand Down
21 changes: 21 additions & 0 deletions compiler/src/Generate/JavaScript.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Generate.JavaScript
( generate
, generateEsm
, generateForRepl
, generateForReplEndpoint
)
Expand Down Expand Up @@ -57,6 +58,19 @@ generate mode (Opt.GlobalGraph graph_ _) mains =
<> "}(this));"
<> "\n" <> Lamdera.Injection.elmPkgJs mode mains <> "\n"

generateEsm :: Mode.Mode -> Opt.GlobalGraph -> Mains -> B.Builder
generateEsm mode (Opt.GlobalGraph graph_ _) mains =
let
graph = Lamdera.Injection.graphModifications mode mains graph_
state = Map.foldrWithKey (addMain mode graph) emptyState mains
in
Functions.functions
-- <> perfNote mode -- @NOTE given user never manages JS generation in Lamdera, hide the perf note
<> stateToBuilder state
<> toMainEsmExports mode mains
-- <> Lamdera.Injection.source mode mains -- shadowing not allowed in esm
<> "\n" <> Lamdera.Injection.elmPkgJs mode mains <> "\n"

addMain :: Mode.Mode -> Graph -> ModuleName.Canonical -> Opt.Main -> State -> State
addMain mode graph home _ state =
addGlobal mode graph state (Opt.Global home "main")
Expand Down Expand Up @@ -546,6 +560,13 @@ toMainExports mode mains =
in
JsName.toBuilder export <> "(" <> exports <> ");"

toMainEsmExports :: Mode.Mode -> Mains -> B.Builder
toMainEsmExports mode mains =
let
exports = generateExports mode (Map.foldrWithKey addToTrie emptyTrie mains)
in
"export const Elm = " <> exports <> ";"


generateExports :: Mode.Mode -> Trie -> B.Builder
generateExports mode (Trie maybeMain subs) =
Expand Down
16 changes: 16 additions & 0 deletions extra/Lamdera.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ module Lamdera
, useLongNames_
, enableLongNames
, useLongNames
, useEsm_
, enableEsm
, useEsm
, isTest
, isLiveMode
, setLiveMode
Expand Down Expand Up @@ -460,6 +463,19 @@ enableLongNames = do
debug $ "🗜️ enableLongNames"
modifyMVar_ useLongNames_ (\_ -> pure True)

{-# NOINLINE useEsm_ #-}
useEsm_ :: MVar Bool
useEsm_ = unsafePerformIO $ newMVar False

{-# NOINLINE useEsm #-}
useEsm :: IO Bool
useEsm = do
readMVar useEsm_

enableEsm :: IO ()
enableEsm = do
debug $ "🗜️ enableEsm"
modifyMVar_ useEsm_ (\_ -> pure True)

isTest :: IO Bool
isTest = do
Expand Down
3 changes: 3 additions & 0 deletions extra/Lamdera/CLI/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ buildProductionJsFiles root inProduction_ versionInfo = do
, _docs = Nothing
, _noWire = False
, _optimizeLegible = True
, _esm = False
}

Make.run ["src" </> "LFR.elm"] $
Expand All @@ -610,6 +611,7 @@ buildProductionJsFiles root inProduction_ versionInfo = do
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
, _esm = False
}

Lamdera.AppConfig.writeUsage
Expand Down Expand Up @@ -713,6 +715,7 @@ migrationCheck root nextVersion changedTypes = do
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
, _esm = False
}

-- @TODO this is because the migrationCheck does weird terminal stuff that mangles the display... how to fix this?
Expand Down
5 changes: 5 additions & 0 deletions extra/Lamdera/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ makeOptimizedWithCleanup cleanup root path = do
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
, _esm = False
}
wait r
remove tmp
Expand Down Expand Up @@ -69,6 +70,7 @@ make_ root = do
, _docs = Nothing
, _noWire = False
, _optimizeLegible = True
, _esm = False
}
wait r
-- The compilation process ends by printing to terminal in a way that overwrites
Expand Down Expand Up @@ -98,6 +100,7 @@ makeDev root paths = do
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
, _esm = False
}
wait r
-- The compilation process ends by printing to terminal in a way that overwrites
Expand Down Expand Up @@ -127,6 +130,7 @@ makeDevHtml root paths = do
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
, _esm = False
}
wait r
-- The compilation process ends by printing to terminal in a way that overwrites
Expand Down Expand Up @@ -158,6 +162,7 @@ makeHarnessDevJs root = do
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
, _esm = False
}
wait r
remove tmp
Expand Down
1 change: 1 addition & 0 deletions terminal/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ make =
|-- flag "docs" Make.docsFile "Generate a JSON file of documentation for a package. Eventually it will be possible to preview docs with `reactor` because it is quite hard to deal with these JSON files directly."
|-- onOff "no-wire" "Explicitly disable Lamdera's wire codegen."
|-- onOff "optimize-legible" "Same as --optimize but without identifier shortening, handy for debugging optimised code or for when identifiers are more useful than smaller JS compilations."
|-- onOff "esm" "Emit an ECMAScript module instead of an IIFE."
in
Terminal.Command "make" Uncommon details example (zeroOrMore elmFile) makeFlags Make.run

Expand Down
10 changes: 7 additions & 3 deletions terminal/src/Make.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ data Flags =
, _docs :: Maybe FilePath
, _noWire :: Bool -- @LAMDERA
, _optimizeLegible :: Bool -- @LAMDERA
, _esm :: Bool -- @LAMDERA
}


Expand All @@ -69,19 +70,20 @@ type Task a = Task.Task Exit.Make a


run :: [FilePath] -> Flags -> IO ()
run paths flags@(Flags _ _ _ report _ noWire optimizeLegible) =
run paths flags@(Flags _ _ _ report _ noWire optimizeLegible esm) =
do style <- getStyle report
maybeRoot <- Stuff.findRoot
Lamdera.onlyWhen noWire Lamdera.disableWire
Lamdera.onlyWhen optimizeLegible Lamdera.enableLongNames
Lamdera.onlyWhen esm Lamdera.enableEsm
Reporting.attemptWithStyle style Exit.makeToReport $
case maybeRoot of
Just root -> runHelp root paths style flags
Nothing -> return $ Left $ Exit.MakeNoOutline


runHelp :: FilePath -> [FilePath] -> Reporting.Style -> Flags -> IO (Either Exit.Make ())
runHelp root paths style (Flags debug optimize maybeOutput _ maybeDocs _ optimizeLegible) =
runHelp root paths style (Flags debug optimize maybeOutput _ maybeDocs _ optimizeLegible _) =
BW.withScope $ \scope ->
Stuff.withRootLock root $ Task.run $
do desiredMode <- getMode debug (optimize || optimizeLegible)
Expand Down Expand Up @@ -304,6 +306,7 @@ parseOutput name
| isDevNull name = Just DevNull
| hasExt ".html" name = Just (Html name)
| hasExt ".js" name = Just (JS name)
| hasExt ".mjs" name = Just (JS name) -- @LAMDERA
| otherwise = Nothing


Expand Down Expand Up @@ -334,11 +337,12 @@ isDevNull name =

-- Clone of run that uses attemptWithStyle_cleanup
run_cleanup :: IO () -> [FilePath] -> Flags -> IO ()
run_cleanup cleanup paths flags@(Flags _ _ _ report _ noWire optimizeLegible) =
run_cleanup cleanup paths flags@(Flags _ _ _ report _ noWire optimizeLegible esm) =
do style <- getStyle report
maybeRoot <- Stuff.findRoot
Lamdera.onlyWhen noWire Lamdera.disableWire
Lamdera.onlyWhen optimizeLegible Lamdera.enableLongNames
Lamdera.onlyWhen esm Lamdera.enableEsm
Reporting.attemptWithStyle_cleanup cleanup style Exit.makeToReport $
case maybeRoot of
Just root -> runHelp root paths style flags
Expand Down
4 changes: 4 additions & 0 deletions test/Test/JsOutput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ suite =
, _docs = Nothing
, _noWire = True
, _optimizeLegible = False
, _esm = False
}

fileContents <- readUtf8Text $ elmStuff ++ "/tmp.js"
Expand Down Expand Up @@ -74,6 +75,7 @@ suite =
, _docs = Nothing
, _noWire = True
, _optimizeLegible = False
, _esm = False
}

fileContents <- readUtf8Text $ elmStuff ++ "/tmp.js"
Expand Down Expand Up @@ -123,6 +125,7 @@ suite =
, _docs = Nothing
, _noWire = True
, _optimizeLegible = False
, _esm = False
}

fileContents <- readUtf8Text $ elmStuff ++ "/tmp.js"
Expand Down Expand Up @@ -174,6 +177,7 @@ suite =
, _docs = Nothing
, _noWire = True
, _optimizeLegible = False
, _esm = False
}

fileContents <- readUtf8Text $ elmStuff ++ "/tmp.js"
Expand Down