Hello everyone. I’m new to PureScript, savoring it as a switch from Elm because many things look more convenient (infixes, type classes, flexibility, FFI, etc…).
But there’s something cheesy in FFI. It seems handier to bundle external libraries with our project (not a library to be published, yet), so we wouldn’t need to worry about embedding it separately. But how?
The point is: we take the recent state of the ecosystem, and psc-package is more and more preferred. We see that it works in the purescript-
namespace. How can we bundle our FFI dependencies (like CodeMirror or highlight.js) in a psc-package based project (and build it together with PS code) ?
As far as I know, there are existing ways to do it outside of psc-package (let this thread be even more useful):
- HTML embedding (purescript-jquery), which is OK for non-NPM libraries, yet some of them exist in npm repositories (same for CodeMirror), but Node-based ones are in possible scope.
- Bower installation (purescript-sammy), which is OK too, but we recall to the point here?
- npm chaining (purescript-react, example at the link), which looks difficult (no guides found yet) and scary (building without pulp? oka~y).
And nothing clear about psc-package (EDIT: look below) . Would be glad for some existing examples or even guides. Nix compatibility notes are optional but useful.
1 Like
Hi @smithclarkson01! Welcome to PureScript
A few things:
- I suggest using Spago instead of
psc-package
to manage your PureScript dependencies. It’s the same idea (curated sets of packages known to build together) except with much better ergonomics.
- Spago and psc-package deal with PureScript dependencies. However, if you are using external libraries (which I take to mean JavaScript libraries) then you need to manage those dependencies separately. Typically that means downloading the libraries from NPM using either
npm
or yarn
, where the dependencies are listed in a package.json
file. This is standard JavaScript stuff, not related to PureScript.
- To use the FFI, you need to have a JavaScript file (
myModule.js
) in the same directory as the PureScript file which is importing the JavaScript (MyModule.purs
). That JavaScript file should implement whatever functions and data you are importing via the FFI. If you have a library you’re importing, then you’ll typically import the functions you want from that library into myModule.js
and then into MyModule.purs
.
The Real World Halogen project demonstrates using both JavaScript and PureScript libraries in one project. It imports the Marked library from JavaScript to parse and render markdown. You can see that the package.json
file imports the Marked library, and in the Foreign directory, there’s an FFI file which exports functions from the Marked library and a PureScript module which makes them available to PureScript.
3 Likes
Thank you so much for your reply!
My issue has been resolved now.
1 Like
hey,
If you’re already using nix you can add additional dependencies to your buildInputs, either via nixpkgs.nodePackages.somePackage or builtins.fetchTarball if not already in nixpkgs but available somewhere like npm.
builtins.fetchGit is a more general option for any dependency with a git repo but will probably need to be paired with stdenv.mkDerivation and require building the package from source via the buildPhase.
thanks and regards