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.
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
, notrequire
).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 specifyingesm: true
in opts, like this:Caveats
NODE_PATH
is not respected by imports. It would need to be manually implemented?update=RANDOM
query string to import, but I believe it should be caller responsibility to do it, if necessary.