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

typescript paths alias #1935

Closed
yannbcf opened this issue Jul 19, 2021 · 4 comments · Fixed by #2043
Closed

typescript paths alias #1935

yannbcf opened this issue Jul 19, 2021 · 4 comments · Fixed by #2043
Assignees
Labels
Milestone

Comments

@yannbcf
Copy link

yannbcf commented Jul 19, 2021

Describe the bug

I try to get the typescript path aliases to work since it should be working right now on the 1.2.65 swc core version, am i doing something wrong ?

TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "@api" is not a valid package name imported from C:\Users\x\test\hello.js
    at packageResolve (internal/modules/esm/resolve.js:601:11)
    at moduleResolve (internal/modules/esm/resolve.js:691:18)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:805:11)
    at Loader.resolve (internal/modules/esm/loader.js:88:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:241:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:72:40)
    at link (internal/modules/esm/module_job.js:71:36) {
  code: 'ERR_INVALID_MODULE_SPECIFIER'
}

Input code

api.ts

export const api = 0;

hello.ts

import { api } from '@api';
console.log('api', api);

Tsconfig

{
    "compileOnSave": false,
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "@api": ["src/api"]
        }
    }
}

Config

{
    "jsc": {
      "parser": {
        "syntax": "typescript",
        "dynamicImport": true
      },
      "target": "es2016",
      "paths": {
        "@api": ["src/api"]
      }
    },
    "minify": false
}

Expected behavior
It should replace the alias with a relative path

Version
1.2.65

@yannbcf yannbcf added the C-bug label Jul 19, 2021
@kwonoj
Copy link
Member

kwonoj commented Jul 20, 2021

tsc, or babel (unless separate plugin specified) both doesn't transpile alias to runtime resolvable path and each tool like webpack / jest specifies way to resolve, may swc would leave as-is as well? for some cases, I expect jsc.paths being used as resolve time only - it's not unusal runtime points to different based on tooling such as jest test points to stub mock instead of real module for path aliases.

@kdy1 kdy1 added this to the v1.2.67 milestone Jul 21, 2021
@kdy1 kdy1 modified the milestones: v1.2.67, v1.2.68, v1.2.72, v1.2.73, v1.2.74, v1.2.75 Jul 31, 2021
@kdy1 kdy1 modified the milestones: v1.2.75, v1.2.76, v1.2.77 Aug 8, 2021
@kdy1 kdy1 modified the milestones: v1.2.77, v1.2.76 Aug 9, 2021
@kdy1 kdy1 self-assigned this Aug 9, 2021
@kdy1 kdy1 closed this as completed in #2043 Aug 9, 2021
kdy1 added a commit that referenced this issue Aug 9, 2021
swc_ecma_preset_env:
 - Update compat data.

swc_ecma_transforms_module:
 - Fix `paths`. (#1934, #1935)
@wi-ski
Copy link

wi-ski commented Nov 9, 2021

Please please please - how do we fix this <3

@EarthlingDavey
Copy link

I found that I had to use a .swcrc file with no paths during the build. BTW the --config-file flag didn't work for me.
Then, something like this in package.json

...
"type": "module",
...
"build": "swc src  -d dist",
"start": "node --experimental-specifier-resolution=node dist/index.js",
...
"imports": {
    "#lib/*": "./dist/lib/*"
}

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 18, 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 18, 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.

6 participants