Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path aliases not working on ESM modules #761

Closed
stelescuraul opened this issue Mar 26, 2024 · 1 comment · Fixed by #767
Closed

Path aliases not working on ESM modules #761

stelescuraul opened this issue Mar 26, 2024 · 1 comment · Fixed by #767

Comments

@stelescuraul
Copy link

Hi everyone,

I have encountered a problem where js files are not properly processed by the register if they are ESM modules. More specifically path aliases are not being replaced. In ts files this seems to be working, however in js files they are not being picked up.
You can find a reproducible example here. I am locking the core version to 1.3.105 in the example because of this issue, however I don't think they are related.

When building through swc into dist and then running the project, everything is working and js files are being picked up and the path aliases are being replaced, but not through node register.

-- works
npx swc -d ./dist -D --delete-dir-on-start --only="**/*.js,**/*.ts,**/*.cjs,**/*.mjs" --ignore ./node_modules,./dist .
node dist/projects/api/main.js 

-- doesn't work
SWCRC=true node --import=@swc-node/register/esm-register projects/api/main.ts

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@foo/common' imported from workspaces/libs/math/src/foo.js

The latest version of swc-node seems to be ignoring the paths and baseUrl on purpose here. Previous versions don't seem to pick up js files anyway.
A potential fix is the following load function implementation:

export const load: LoadFn = async (url, context, nextLoad) => {
  if (context.format === 'ts' || (tsconfig.allowJs && context.format === 'module')) { <<-- condition changed to include module
    const { source } = await nextLoad(url, context)
    const code = typeof source === 'string' ? source : Buffer.from(source).toString()
    const compiled = await compile(code, fileURLToPath(url), tsconfig, true) <<-- pass tsconfig with paths and baseurl
    return {
      format: 'module',
      source: compiled,
      shortCircuit: true,
    }
  } else {
    return nextLoad(url, context)
  }
}

Is this a valid solution and if so, can we get it merged?

@yeliex
Copy link
Contributor

yeliex commented Apr 1, 2024

since swc-node/register migrates to tsc as module resolver, it only resolves and transpiles ts files as default. It does not align with swc behavior.

I would create a PR trying to handle JS files as well, both for js and ts files. Or @Brooooooklyn have any other propose?

Brooooooklyn pushed a commit that referenced this issue May 1, 2024
* feat: update minimal @swc/core to 1.14.13

* feat: support compile js files

* Create smooth-ties-count.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants