From 415565b29971de398a69503262a49597e49e9c30 Mon Sep 17 00:00:00 2001 From: sigma-andex Date: Fri, 11 Feb 2022 08:24:11 +0000 Subject: [PATCH 1/3] Change spago run to support es modules from 0.15 onwards --- src/Spago/Build.hs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Spago/Build.hs b/src/Spago/Build.hs index d751eb65f..62a8bae39 100644 --- a/src/Spago/Build.hs +++ b/src/Spago/Build.hs @@ -322,12 +322,26 @@ runBackend runBackend maybeBackend RunDirectories{ sourceDir, executeDir } moduleName maybeSuccessMessage failureMessage extraArgs = do logDebug $ display $ "Running with backend: " <> fromMaybe "nodejs" maybeBackend BuildOptions{ pursArgs } <- view (the @BuildOptions) - let postBuild = maybe (nodeAction $ Path.getOutputPath pursArgs) backendAction maybeBackend + isES <- Purs.hasMinPursVersion "0.15.0" + let postBuild = maybe (nodeAction isES $ Path.getOutputPath pursArgs) backendAction maybeBackend build (Just postBuild) where fromFilePath = Text.pack . Turtle.encodeString nodeArgs = Text.intercalate " " $ map unBackendArg extraArgs - nodeContents outputPath' = + esContents outputPath' = + fold + [ "import { main } from '" + , Text.replace "\\" "/" (fromFilePath sourceDir) + , "/" + , Text.pack outputPath' + , "/" + , unModuleName moduleName + , "/" + , "index.js" + , "'\n\n" + , "main()" + ] + cjsContents outputPath' = fold [ "require('" , Text.replace "\\" "/" (fromFilePath sourceDir) @@ -337,14 +351,19 @@ runBackend maybeBackend RunDirectories{ sourceDir, executeDir } moduleName maybe , unModuleName moduleName , "').main()" ] - nodeCmd outputPath'= "node -e \"" <> nodeContents outputPath' <> "\" -- " <> nodeArgs - nodeAction outputPath' = do + nodeCmd isES outputPath'= + if isES then + "node --input-type=module -e \"" <> esContents outputPath' <> "\" -- " <> nodeArgs + else + "node -e \"" <> cjsContents outputPath' <> "\" -- " <> nodeArgs + + nodeAction isES outputPath' = do -- cd to executeDir in case it isn't the same as sourceDir logDebug $ "Executing from: " <> displayShow @FilePath executeDir Turtle.cd executeDir -- We build a process by hand here because we need to forward the stdin to the backend process - logDebug $ "Running node command: `" <> (display $ nodeCmd outputPath') <> "`" - let processWithStdin = (Process.shell (Text.unpack $ nodeCmd outputPath')) { Process.std_in = Process.Inherit } + logDebug $ "Running node command: `" <> display (nodeCmd isES outputPath') <> "`" + let processWithStdin = (Process.shell (Text.unpack $ nodeCmd isES outputPath')) { Process.std_in = Process.Inherit } Turtle.system processWithStdin empty >>= \case ExitSuccess -> maybe (pure ()) (logInfo . display) maybeSuccessMessage ExitFailure n -> die [ display failureMessage <> "exit code: " <> repr n ] From c8c8cbdfad6f2ec4e2ca76adb2a4d8533cd68729 Mon Sep 17 00:00:00 2001 From: sigma-andex Date: Fri, 11 Feb 2022 08:25:47 +0000 Subject: [PATCH 2/3] Update changelog with spago run --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad4be7c1d..45c93e218 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Bugfixes - Make `spago run` work when `node-args` includes flag-like value (#856) +Features: +- Make `spago run` use es modules for projects >= v0.15 ## [0.20.6] - 2022-02-09 From 65b39b849786f167db56b79d3f4c1314a1231a6c Mon Sep 17 00:00:00 2001 From: sigma-andex Date: Sun, 13 Feb 2022 10:32:32 +0000 Subject: [PATCH 3/3] Correct changelog entry --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45c93e218..4549d412c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +Features: +- Make `spago run` use es modules for projects >= v0.15 + ## [0.20.7] - 2022-02-12 Bugfixes - Make `spago run` work when `node-args` includes flag-like value (#856) -Features: -- Make `spago run` use es modules for projects >= v0.15 ## [0.20.6] - 2022-02-09