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

jsc.paths didnt work when file is not on root #2126

Closed
aelbore opened this issue Aug 21, 2021 · 7 comments · Fixed by #2712
Closed

jsc.paths didnt work when file is not on root #2126

aelbore opened this issue Aug 21, 2021 · 7 comments · Fixed by #2712
Labels
Milestone

Comments

@aelbore
Copy link

aelbore commented Aug 21, 2021

Describe the bug
jsc.paths didnt work when file is not on root folder

Input code
File => packages/a/src/index.ts
code =>

export function displayA() {
  return 'Display A'
}

File => src/index.ts

import { displayA } from '@print/a'

console.log(
  `${displayA()}`
)

File => package.json

{
  "name": "swc-tsconfig-paths",
  "main": "index.ts",
  "scripts": {
    "swc": "swc-node ./src/index.ts",
  },
  "devDependencies": {
    "@swc/core": "^1.2.80",
    "@swc/register": "^0.1.7"
  }
}

Error
image

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es2020",
    "paths": {
      "@print/a": [ "packages/a/src/index.ts" ],
    }
  },
  "module": {
    "type": "commonjs"
  }
}

Expected behavior
A clear and concise description of what you expected to happen.
Should work properly and recognise the paths

Version
The version of @swc/core: 1.2.80

Additional context
Add any other context about the problem here.

@aelbore aelbore added the C-bug label Aug 21, 2021
@kdy1
Copy link
Member

kdy1 commented Aug 21, 2021

Seems like duplicate of #2050

@aelbore
Copy link
Author

aelbore commented Aug 21, 2021

@kdy1 sure thanks apologies i didnt see it,
by the way if you put the src/index.ts to root folder index.ts in your package.json swc-node index.ts >>> it works as expected

@kdy1 kdy1 modified the milestones: v1.2.82, v1.2.83, v1.2.84, v1.2.85 Aug 21, 2021
@kdy1 kdy1 modified the milestones: v1.2.85, v1.2.86, v1.2.87 Sep 1, 2021
@kdy1 kdy1 modified the milestones: v1.2.87, v1.2.88, v1.2.89, v1.2.90 Sep 11, 2021
@aelbore
Copy link
Author

aelbore commented Sep 18, 2021

@kdy1 this works if you set baseUrl to src then adjust the paths
Maybe its a matter of adjusting the baseUrl

by the way uses swc: 1.2.88

This does not work

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es2020",
    "baseUrl": ".",
    "paths": {
      "@print/a": [ "./packages/a/src/index.ts" ]
    }
  },
  "module": {
    "type": "commonjs"
  }
}

This will works

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es2020",
    "baseUrl": "./src",
    "paths": {
      "@print/a": [ "../packages/a/src/index.ts" ]
    }
  },
  "module": {
    "type": "commonjs"
  }
}

@aelbore
Copy link
Author

aelbore commented Sep 18, 2021

@kdy1 another thing is if you set the type to module in the package.json it will not work

@aelbore
Copy link
Author

aelbore commented Sep 18, 2021

@kdy1 I know now, the baseUrl should be where the code will use, then the paths should adjust to the baseUrl dynamically

for example
if i add a folder in the src/folder-b the baseUrl should be src/folder-b

@kdy1
Copy link
Member

kdy1 commented Sep 19, 2021

@aelbore Current implementation uses baseUrl, and paths is adjusted based on it.
It sounds like the current implementation is correct, but I don't think it's the case.

@kdy1 kdy1 modified the milestones: v1.2.90, v1.2.91, v1.2.92 Sep 21, 2021
@kdy1 kdy1 modified the milestones: v1.2.103, v1.2.104 Oct 21, 2021
@kdy1 kdy1 modified the milestones: v1.2.104, v1.2.105, v1.2.106, v1.2.107 Oct 25, 2021
@kdy1 kdy1 modified the milestones: v1.2.107, v1.2.108 Nov 7, 2021
kdy1 added a commit to kdy1/swc that referenced this issue Nov 11, 2021
kdy1 added a commit that referenced this issue Nov 11, 2021
swc_ecma_loader:
 - `TsConfigResolver`: Use `baseUrl` on exact match while resolving. (Closes #2126)

swc_ecma_transforms_module:
 - Add `import_rewriter`.
 - `NodeImportResolver`: Don't strip extension if it's a directory. (Closes #2269)
 - `NodeImportResolver`: Don't strip extension if it's not `.ts`, `.js` nor `.tsx`.

swc:
 - Use `import_rewriter`. (Closes #1943, Closes #2523)
 - Update test (Closes #2715)
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 21, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants