Closed psychobolt closed 4 months ago
I have the same issue, however I think it is not specific to VSCode extension. I encounter it when I import a commonjs
file with node. I opened #796 but I think my fix was incorrect, so I closed it.
I'm even wondering if commonjs
files should be skipped altogether?
in packages/register/esm.mts
export const load: LoadHook = async (url, context, nextLoad) => {
debug('load', url, JSON.stringify(context))
if (url.startsWith('data:')) {
debug('skip load: data url', url)
return nextLoad(url, context)
}
if (['builtin', 'json', 'wasm'].includes(context.format)) {
debug('loaded: internal format', url)
return nextLoad(url, context)
}
+ if (context.format === "commonjs") {
+ debug('skip load: commonjs file', url);
+ return nextLoad(url, context);
+ }
const { source, format: resolvedFormat } = await nextLoad(url, context)
debug('loaded', url, resolvedFormat)
const code = !source || typeof source === 'string' ? source : Buffer.from(source).toString()
const compiled = await compile(code, url, tsconfigForSWCNode, true)
debug('compiled', url, resolvedFormat)
return addShortCircuitSignal({
// for lazy: ts-node think format would undefined, actually it should not, keep it as original temporarily
format: resolvedFormat,
source: compiled,
})
}
WDYT @Brooooooklyn?
I'm even wondering if commonjs files should be skipped altogether?
definitely no. import commonjs in esm is a very common case
I would assume we need a similar fallback, which fixed the Yarn loader https://github.com/swc-project/swc-node/issues/772#issuecomment-2094771696
A workaround should be. but ignoring by path prefix is too rude. I would look at this, maybe we could have a better way.
could you please provide a minimal reproduction
Got a different error on this sandbox, but fails on commonjs
script. Possibly related?
Hi, trying to execute my custom runtime with a vscode extension to support loading ESM files with register hook v1.10. However, I am getting this error in outputs:
What would be executed for example would be
ESLINT_USE_FLAT_CONFIG=true node bin/swc-node.js "../../.vscode/extensions/dbaeumer.vscode-eslint-3.0.10/server/out/eslintServer.js" -c eslint.config.ts
.Analysis
It seems that any
commonjs
file would cause the same error. For example I've created a simple test:Would recieve the same error:
Seeing here we shortcircuit and fails to compile when source is undefined. I would assume we need a similar fallback, which fixed the Yarn loader case . I have my workaround which works in my case: