Is there a way to wrap the `spago bundle-app` output into an object?

Hello,

I’ve run into a slight problem when trying to upgrade a library.

Everything compiles/works but it seems that spago bundle-app produces an output with all exports just as flat top-level function .... in the generated JS.

In my case this will fail when the browser starts to interpret the file as it includes this:

// output/Web.HTML.Window/foreign.js
function document(window2) {
  return function() {
    return window2.document;
  };
}

which will fail because document is already defined.

Now --minify in spago will solve this issue but I wonder if there is a way to use something like global name to wrap all into an object which seems safer in my opinion.

for the moment you can adapt the underlying esbuild command to use iife format instead:

echo "import { main } from './output/Main/index.js'; main();" | esbuild --platform=browser --format=iife --bundle --outfile=index.js

or altenatively bundle to a module

spago bundle-module --to bundle.js
<script type="module">
      import { main } from './bundle.js'
      main()
</script>

I created a PR to change the format for browser apps to iife

1 Like

Thank you for your answer!

I had not considered the second option.
For my own projects I use the first option but here I was trying to help out with upgrading someones else’s library and I think it’s not the place of such a pull request to change the build - although thinking about it maybe it’s no issue.

Again thank you for the PR - hope it’ll get through.