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

Aliases (Paths) Not Being Replaced #2523

Closed
adrians5j opened this issue Oct 24, 2021 · 5 comments · Fixed by #2712
Closed

Aliases (Paths) Not Being Replaced #2523

adrians5j opened this issue Oct 24, 2021 · 5 comments · Fixed by #2712
Labels
Milestone

Comments

@adrians5j
Copy link

adrians5j commented Oct 24, 2021

Describe the bug
Just started playing with SWC and I I'd just like to say this is an amazing tool. Just amazed how it's fast and how much work has been put into it. 🙏🏻

So, in my code, I have ~/........ import statements, but, once the SWC has done its magic, these are replaced with correct paths - only when I set "type": "commonjs" in my .swcrc file. Otherwise, they just stay the same.

Input code

image

So, this is the resulting code. As we can see, the import paths haven't changed (~ is still here).

image

But, if I were to use the commonjs module type, then I'd see the path built correctly:

image

Config

commonjs works (paths are changed correctly):

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es2020",
    "paths": {
      "~/*": ["./src/*"]
    },
    "baseUrl": "."
  },
  "module": {
    "type": "commonjs"
  }
}

This one does not work:

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es2020",
    "paths": {
      "~/*": ["./src/*"]
    },
    "baseUrl": "."
  }
}

Expected behavior
The paths to be correctly replaced.

Version
The version of @swc/core: 1.2.102

Additional context
Checked a couple of issues reported by other users (#2126, #2050), those didn't provide any clues unfortunately.

@ckken
Copy link

ckken commented Nov 3, 2021

amd is no work too

@eturino
Copy link

eturino commented Nov 4, 2021

es6 also doesn't work. This is mentioned in #702 (comment)

@kdy1 kdy1 modified the milestones: v1.2.107, v1.2.108 Nov 7, 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)
@adrians5j
Copy link
Author

adrians5j commented Nov 15, 2021

Hey everybody, I saw a PR that was dealing with this was merged, so I went ahead and tried the latest 1.2.108 SWC version.

Unfortunately, I have to report that, at least on my end, the paths are still not being transformed the way they should be.

The steps I did were the following...

  1. Created a brand new project and installed SWC.
  2. Created the following files:
src
├── index.ts
└── utils
    └── index.ts

The src/index.ts looks like the following:

import test from "~/utils";

test();
  1. I created the same .swcrc config I used above:
{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "target": "es2020",
    "paths": {
      "~/*": ["./src/*"]
    },
    "baseUrl": "."
  }
}
  1. Ran swc src -d dist --source-maps --copy-files --extensions '.ts'

Results?

Well, ultimately, the results were the same as reported initially. 😔

The compiled code was:

import test from "~/utils";
test();

//# sourceMappingURL=index.js.map

So, "~" wasn't replaced.

And, as stated above, once I switched to commonjs via ...

"module": {
    "type": "commonjs"
}

... the path transformation was successful:

"use strict";
var _utils = _interopRequireDefault(require("./utils"));
function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
(0, _utils).default();

//# sourceMappingURL=index.js.map

Hope I'm not missing something. If so, I'm sorry in advance for the trouble. 🙂

@kdy1 kdy1 reopened this Nov 15, 2021
@kdy1
Copy link
Member

kdy1 commented Nov 15, 2021

I think another issue is better.

@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.

5 participants