Skip to content

Build to commonjs #5523

Answered by Fractal-Tess
Fractal-Tess asked this question in Q&A
Discussion options

You must be logged in to vote

Solved this thanks to GHOST.

The solution is to esbuild the handler.js output file of vite build when using adapter node.
Here is the script

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 shim.js file

export const importMetaUrl = /* @__PURE__ */ new URL(
  'file:' + __filename
).href;

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by benmccann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant