Skip to content

Call function inside ESM module #83

@Valian

Description

@Valian

Hi 👋

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

  • Extension is always required in ESM imports, if not importing package from node_modules.
  • NODE_PATH is not respected by imports. It would need to be manually implemented
  • there is no cache that could be busted between imports. There's a hack of adding ?update=RANDOM query string to import, but I believe it should be caller responsibility to do it, if necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions