revelrylabs / elixir-nodejs

An Elixir API for calling Node.js functions
MIT License
217 stars 31 forks source link

Call function inside ESM module #83

Closed Valian closed 5 months ago

Valian commented 6 months ago

Hi :wave:

I'm working on implementing Vue.js SSR for Phoenix.

To bundle server code, I'm using Vite. Sadly, it only generated ESM modules (using import / export, not require).

Currently, elixir-nodejs always require files, thus making it impossible to call functions inside ESM modules.

Solution

I'll implement import inside server.js and let it be used by specifying esm: true in opts, like this:

# extension is required in ESM imports
NodeJS.call({"esm-module.js", :uuid}, [], esm: true)

# if using mjs extension esm: true will be automatically set
NodeJS.call({"esm-module.mjs", :uuid})

# to force module reload, add a cache busting string
NodeJS.call({"esm-module.mjs?q=#{System.unique_integer()}", :uuid})

Caveats