Skip to content

Commit

Permalink
fix(register/esm): pass file urls as paths (#840)
Browse files Browse the repository at this point in the history
compile() expects a filename, not a url: it passes the
value directly to the bindings module, which immediately
parses it as a `Path` unconditionally.

This causes input file source maps to fail to load, as it
attempts to find the URL as if it were a file on disk.
(see swc-project/swc#9422)

This is a targeted fix which hopefully minimizes the chance of things breaking.
  • Loading branch information
simonbuchan authored Aug 13, 2024
1 parent 75ccc7e commit 27d922d
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/register/esm.mts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,13 @@ export const load: LoadHook = async (url, context, nextLoad) => {
debug('loaded', url, resolvedFormat)

const code = !source || typeof source === 'string' ? source : Buffer.from(source).toString()
const compiled = await compile(code, url, tsconfigForSWCNode, true)

// url may be essentially an arbitrary string, but fixing the binding module, which currently
// expects a real file path, to correctly interpret this doesn't have an obvious solution,
// and would likely be a breaking change anyway. Do a best effort to give a real path
// like it expects, which at least fixes relative input sourcemap paths.
const filename = url.startsWith('file:') ? fileURLToPath(url) : url
const compiled = await compile(code, filename, tsconfigForSWCNode, true)

debug('compiled', url, resolvedFormat)

Expand Down

0 comments on commit 27d922d

Please sign in to comment.