From 9a6a6289355f7014c6ac696ca7e2269b7adf19ba Mon Sep 17 00:00:00 2001 From: sigma-andex <77549848+sigma-andex@users.noreply.github.com> Date: Mon, 2 May 2022 11:02:46 +0100 Subject: [PATCH] Use iife when bundling browser apps (#880) --- CHANGELOG.md | 3 +++ src/Spago/Build.hs | 5 ++++- test/fixtures/bundle-app-esm.js | 20 +++++++++++--------- test/fixtures/bundle-app-src-map-esm.js | 20 +++++++++++--------- test/fixtures/packages-prepare-0-15.dhall | 5 ++--- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cdd1c9a4..61a4dfaa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +Other improvements: +- Use `iife` format when bundling browser apps (#880) + ## [0.20.8] - 2022-04-27 Features: diff --git a/src/Spago/Build.hs b/src/Spago/Build.hs index b4ef1b073..a3346427e 100644 --- a/src/Spago/Build.hs +++ b/src/Spago/Build.hs @@ -425,7 +425,10 @@ bundleWithEsbuild withMain srcMap (ModuleName moduleName) (TargetPath targetPath srcMapOpt = case srcMap of WithSrcMap -> ["--sourcemap"] WithoutSrcMap -> [] - esbuildBase = platformOpt <> minifyOpt <> srcMapOpt <> ["--format=esm", "--bundle", "--outfile=" <> targetPath] + formatOpt = case (platform, withMain) of + (Browser, WithMain) -> ["--format=iife"] + (_,_) -> ["--format=esm"] + esbuildBase = platformOpt <> minifyOpt <> srcMapOpt <> formatOpt <> ["--bundle", "--outfile=" <> targetPath] (input, cmd) = case withMain of WithMain -> do let diff --git a/test/fixtures/bundle-app-esm.js b/test/fixtures/bundle-app-esm.js index 0c581ab56..fef3eca9a 100644 --- a/test/fixtures/bundle-app-esm.js +++ b/test/fixtures/bundle-app-esm.js @@ -1,12 +1,14 @@ -// output/Effect.Console/foreign.js -var log = function(s) { - return function() { - console.log(s); +(() => { + // output/Effect.Console/foreign.js + var log = function(s) { + return function() { + console.log(s); + }; }; -}; -// output/Main/index.js -var main = /* @__PURE__ */ log("\u{1F35D}"); + // output/Main/index.js + var main = /* @__PURE__ */ log("\u{1F35D}"); -// -main(); + // + main(); +})(); diff --git a/test/fixtures/bundle-app-src-map-esm.js b/test/fixtures/bundle-app-src-map-esm.js index 0c26d4716..b167ded57 100644 --- a/test/fixtures/bundle-app-src-map-esm.js +++ b/test/fixtures/bundle-app-src-map-esm.js @@ -1,13 +1,15 @@ -// output/Effect.Console/foreign.js -var log = function(s) { - return function() { - console.log(s); +(() => { + // output/Effect.Console/foreign.js + var log = function(s) { + return function() { + console.log(s); + }; }; -}; -// output/Main/index.js -var main = /* @__PURE__ */ log("\u{1F35D}"); + // output/Main/index.js + var main = /* @__PURE__ */ log("\u{1F35D}"); -// -main(); + // + main(); +})(); //# sourceMappingURL=bundle-app-src-map-esm.js.map diff --git a/test/fixtures/packages-prepare-0-15.dhall b/test/fixtures/packages-prepare-0-15.dhall index aa940d7fe..44306857a 100644 --- a/test/fixtures/packages-prepare-0-15.dhall +++ b/test/fixtures/packages-prepare-0-15.dhall @@ -1,4 +1,3 @@ let upstream = - https://github.com/JordanMartinez/package-sets/releases/download/psc-0.15.0-20220329/packages.dhall - -in upstream \ No newline at end of file + https://github.com/purescript/package-sets/releases/download/psc-0.15.0-20220429/packages.dhall +in upstream