Build to commonjs #5523
-
Hey there, I was on an adventure to make myself a svelte-kit electron app, and while browsing premade templates, I noticed that all of them are using the static adapter. That makes no sense to me, electron ships a node + chromium runtime, so why not just simply use the node adapter? Well turns out It's impossible to import the esm modules produced by the svelte-kit(now vite) build command into electron (since electron uses only commonjs). I tried some stupid stuff with Any advice? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Solved this thanks to GHOST. The solution is to esbuild the const esbuild = require('esbuild');
const join = require('path').join;
esbuild.build({
entryPoints: [join(__dirname, '../build/svelte/handler.js')],
outfile: join(__dirname, '../build/svelte/handler.cjs'),
platform: 'node',
format: 'cjs',
bundle: true,
define: {
'import.meta.url': 'importMetaUrl'
},
inject: [join(__dirname, './shims.js')]
}); And here is the injected export const importMetaUrl = /* @__PURE__ */ new URL(
'file:' + __filename
).href; |
Beta Was this translation helpful? Give feedback.
Solved this thanks to GHOST.
The solution is to esbuild the
handler.js
output file ofvite build
when usingadapter node
.Here is the script
And here is the injected
shim.js
file