diff --git a/src/Spago/Build.hs b/src/Spago/Build.hs index 5ce0dc7cf..c1523ebcc 100644 --- a/src/Spago/Build.hs +++ b/src/Spago/Build.hs @@ -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 diff --git a/src/Spago/Purs.hs b/src/Spago/Purs.hs index 7fd52e0c5..3f35bf861 100644 --- a/src/Spago/Purs.hs +++ b/src/Spago/Purs.hs @@ -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