Skip to content

Commit

Permalink
Add support for bundling with main
Browse files Browse the repository at this point in the history
Uses the same mechanism as spago run
  • Loading branch information
sigma-andex committed Jan 25, 2022
1 parent 5082d17 commit 6f5a369
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
33 changes: 29 additions & 4 deletions src/Spago/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,36 @@ bundleApp
-> BuildOptions
-> UsePsa
-> RIO env ()
bundleApp withMain maybeModuleName maybeTargetPath noBuild buildOpts usePsa =
let (moduleName, targetPath) = prepareBundleDefaults maybeModuleName maybeTargetPath
bundleApp withMain maybeModuleName maybeTargetPath noBuild buildOpts@(BuildOptions{ pursArgs }) usePsa = do
sourceDir <- Turtle.pwd
let
fromFilePath = Text.pack . Turtle.encodeString
bundleJsSource = fromFilePath (sourceDir Turtle.</> ".spago/bundle.js")

bundleJsContents outputPath' =
fold
[ "import { main } from '"
, Text.replace "\\" "/" (fromFilePath sourceDir)
, "/"
, Text.pack outputPath'
, "/"
, unModuleName moduleName
, "/"
, "index.js"
, "'\n\n"
, "main()"
]
(moduleName, targetPath) = prepareBundleDefaults maybeModuleName maybeTargetPath
writeBundleJs outputPath' = do
logDebug $ "Writing " <> displayShow @Text bundleJsSource
sourceDir <- Turtle.pwd
writeTextFile bundleJsSource (bundleJsContents outputPath')

bundleAction = Purs.bundle withMain (withSourceMap buildOpts) moduleName targetPath
in case noBuild of
DoBuild -> Run.withBuildEnv usePsa buildOpts $ build (Just bundleAction)
case noBuild of
DoBuild -> do
writeBundleJs $ Path.getOutputPath pursArgs
Run.withBuildEnv usePsa buildOpts $ build (Just bundleAction)
NoBuild -> Run.getEnv >>= (flip runRIO) bundleAction

-- | Bundle into a CommonJS module
Expand Down
9 changes: 6 additions & 3 deletions src/Spago/Purs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ repl sourcePaths extraArgs = do
bundle :: HasLogFunc env => WithMain -> WithSrcMap -> ModuleName -> TargetPath -> RIO env ()
bundle withMain withSourceMap (ModuleName moduleName) (TargetPath targetPath) = do
let
path = case withMain of
WithMain -> ".spago/bundle.js"
WithoutMain -> "\"output/" <> moduleName <> "/index.js\""

-- [TODO]: We probably want a flag here for node/browser
cmd
= "esbuild --platform=node --bundle \"output/"
<> moduleName
<> "/index.js\""
= "esbuild --platform=browser --bundle "
<> path
<> " --outfile=" <> targetPath

runWithOutput cmd
Expand Down

0 comments on commit 6f5a369

Please sign in to comment.