From 2f24af2cbd0413ff0dca02d4ca613d80853179f9 Mon Sep 17 00:00:00 2001 From: Simon Buchan Date: Mon, 12 Aug 2024 20:04:27 +1200 Subject: [PATCH] fix(register/esm): pass file urls as paths 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 https://github.com/swc-project/swc/pull/9422) This is a targeted fix which hopefully minimizes the chance of things breaking. --- packages/register/esm.mts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/register/esm.mts b/packages/register/esm.mts index 05692c6d9..4cacbd617 100644 --- a/packages/register/esm.mts +++ b/packages/register/esm.mts @@ -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)