In PureScript 0.15 + esbuild, how do I call a PS function from a JS function?

With purescript 0.14 I was exposing my main purescript functions in main.purs to the browser .window with with
main.js

exports._mainImpl = function() {
  return window.PS = PS;
}

main.purs

foreign import _mainImpl :: Effect Unit

main :: Effect Unit
main = _mainImpl

renderBookmarks :: String -> Array Bookmark -> Effect Unit

Then I could call renderBookmarks from JS with PS['Main'].renderBookmarks(...)

with purescript 0.15 + esbuild there is no longer a PS variable in scope in main.js and my bundle.js doesn’t appear to have main.purs renderBookmarks code bundled; only main is bundled.

Is there some way i can import the other main.purs functions and expose that in main.js in ps v0.15?

in this example I want to expose the main.purs function renderBookmarks to the window globally so that it can be called externally

1 Like

Hello to our community and thanks for asking - I puzzled about this also when I switched to esbuild :wink:

Personally I go with esbuild ./output/Main/index.js --bundle --minify --sourcemap --global-name=ps --target=chrome58,firefox57,edge18 --outfile... with this you have something very similar and you can call your exports with something like

<script>
    ps.renderBookmarks("hello")([...])()
</script>

(all exports from your Main module are included in the ps object)

It’s the global-name flag that does it.
The doc says that this only matters if you add --format=iife but for me it works without this flag too - maybe I use an older version of esbuild though - so better add it :wink:

2 Likes

global-name was the trick - thanks!