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

feat(register): @swc-node/register/esm use TypeScript resolver #727

Merged
merged 4 commits into from
Sep 26, 2023

Conversation

cm-ayf
Copy link
Contributor

@cm-ayf cm-ayf commented Sep 2, 2023

Currently @swc-node/register/esm has its own implementation in register hook to resolve imports like ./foo or ./foo.ts. However, the resolution should be aligned with that by TypeScript (specifically tsserver) which is far more complicated.

This PR just uses TypeScript resolver in register hook. In this way, the resolution will respect setting files such as tsconfig.json, package.json, and so on. Thus, this PR potentially fixes issues #710 and #724, and adds support for paths in tsconfig.json.

As of the performance issue, it can be said that TypeScript resolver will not search deep into node_modules; if parentURL is in node_modules the resolver falls back to default one, and so does if the resolution result. Note that TypeScript resolver itself is already fast enough.

This PR also fixes the problem where node --watch --loader @swc-node/register/esm main.ts does not refresh after editing main.ts. this might have been a bug on the previous implementation.

@CLAassistant
Copy link

CLAassistant commented Sep 2, 2023

CLA assistant check
All committers have signed the CLA.

@cm-ayf cm-ayf changed the title Register esm use ts resolver @swc-node/register/esm use ts resolver Sep 2, 2023
@cm-ayf cm-ayf changed the title @swc-node/register/esm use ts resolver @swc-node/register/esm use TypeScript resolver Sep 2, 2023
@cm-ayf
Copy link
Contributor Author

cm-ayf commented Sep 2, 2023

With a little fix, I have verified that issues #710 and #724 will be fixed. However, paths in tsconfig.json was not properly handled. The reason for this is not clear, but I believe this PR is still a big step forward.

@DouglasGabr
Copy link

This solves an issue I'm facing. With "moduleResolution": "Bundler", typescript accepts imports like import { foo } from "./test.model" (file name is test.model.ts), but @swc-node/register/esm fails to resolve that file since it thinks .model is the file extension.

Current workaround that worked for me is changing the import to import { foo } from "./test.model.js".

Just tested with this PR patched code and it works for both cases.

@Brooooooklyn Brooooooklyn changed the title @swc-node/register/esm use TypeScript resolver feat(register): @swc-node/register/esm use TypeScript resolver Sep 26, 2023
@Brooooooklyn Brooooooklyn merged commit 1ea2734 into swc-project:master Sep 26, 2023
6 checks passed
tsang-bot bot added a commit to tsangste/nx-package-test that referenced this pull request Oct 1, 2023
This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@swc-node/register](https://togithub.com/swc-project/swc-node) |
[`1.6.7` ->
`1.6.8`](https://renovatebot.com/diffs/npm/@swc-node%2fregister/1.6.7/1.6.8)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/@swc-node%2fregister/1.6.8?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@swc-node%2fregister/1.6.8?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@swc-node%2fregister/1.6.7/1.6.8?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@swc-node%2fregister/1.6.7/1.6.8?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
| [@swc/core](https://swc.rs)
([source](https://togithub.com/swc-project/swc)) | [`1.3.88` ->
`1.3.90`](https://renovatebot.com/diffs/npm/@swc%2fcore/1.3.88/1.3.90) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/@swc%2fcore/1.3.90?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@swc%2fcore/1.3.90?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@swc%2fcore/1.3.88/1.3.90?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@swc%2fcore/1.3.88/1.3.90?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>swc-project/swc-node (@&#8203;swc-node/register)</summary>

###
[`v1.6.8`](https://togithub.com/swc-project/swc-node/releases/tag/%40swc-node/register%401.6.8)

[Compare
Source](https://togithub.com/swc-project/swc-node/compare/@swc-node/register@1.6.7...@swc-node/register@1.6.8)

#### What's Changed

- fix(core): remove `jsc.module.noInterop` when it's not supported by
[@&#8203;kdy1](https://togithub.com/kdy1) in
[swc-project/swc-node#733
- chore: fix README for install, build, and test by
[@&#8203;devjiwonchoi](https://togithub.com/devjiwonchoi) in
[swc-project/swc-node#728
- feat(register): @&#8203;swc-node/register/esm use TypeScript resolver
by [@&#8203;cm-ayf](https://togithub.com/cm-ayf) in
[swc-project/swc-node#727

#### New Contributors

- [@&#8203;devjiwonchoi](https://togithub.com/devjiwonchoi) made their
first contribution in
[swc-project/swc-node#728
- [@&#8203;cm-ayf](https://togithub.com/cm-ayf) made their first
contribution in
[swc-project/swc-node#727

**Full Changelog**:
https://github.com/swc-project/swc-node/compare/[@&#8203;swc-node/core](https://togithub.com/swc-node/core)[@&#8203;1](https://togithub.com/1).10.5...[@&#8203;swc-node/register](https://togithub.com/swc-node/register)[@&#8203;1](https://togithub.com/1).6.8

</details>

<details>
<summary>swc-project/swc (@&#8203;swc/core)</summary>

###
[`v1.3.90`](https://togithub.com/swc-project/swc/blob/HEAD/CHANGELOG.md#1390---2023-09-27)

[Compare
Source](https://togithub.com/swc-project/swc/compare/v1.3.89...v1.3.90)

##### Bug Fixes

- **(es/codegen)** Fix codegen of unicode surrogates
([#&#8203;7985](https://togithub.com/swc-project/swc/issues/7985))
([39c680d](https://togithub.com/swc-project/swc/commit/39c680d814644fbde50b0f73a29aebe791812eb2))

- **(es/codegen)** Fix codegen of optional chaining expr with a comment
([#&#8203;8005](https://togithub.com/swc-project/swc/issues/8005))
([f07bb48](https://togithub.com/swc-project/swc/commit/f07bb482e694a422591085621a38aff747294502))

- **(es/typescript)** Handle `DeclareCollect` correctly
([#&#8203;8008](https://togithub.com/swc-project/swc/issues/8008))
([9d08b8a](https://togithub.com/swc-project/swc/commit/9d08b8a6145d486e570a22563c62a69722cf8759))

- **(es/typescript)** Handle qualified access in assign pat
([#&#8203;8012](https://togithub.com/swc-project/swc/issues/8012))
([2f01aba](https://togithub.com/swc-project/swc/commit/2f01aba978415add87994f9b20e3e2db83fc48b6))

###
[`v1.3.89`](https://togithub.com/swc-project/swc/blob/HEAD/CHANGELOG.md#1389---2023-09-25)

[Compare
Source](https://togithub.com/swc-project/swc/compare/v1.3.88...v1.3.89)

##### Bug Fixes

- **(es)** Enable resolver if `jsc.baseUrl` is specified
([#&#8203;7998](https://togithub.com/swc-project/swc/issues/7998))
([f374ad9](https://togithub.com/swc-project/swc/commit/f374ad903a066ebf3a7e54a3656cb3fc44b37445))

- **(es/compat)** Skip function scope in `block-scoped-fns` pass
([#&#8203;7975](https://togithub.com/swc-project/swc/issues/7975))
([d91a554](https://togithub.com/swc-project/swc/commit/d91a554033c8ce0ea2b2538b0e86abf5a81eac76))

##### Refactor

- **(es/typescript)** Reimplement TypeScript pass
([#&#8203;7202](https://togithub.com/swc-project/swc/issues/7202))
([9c9a538](https://togithub.com/swc-project/swc/commit/9c9a538f23cb2bcd2f46e156babf64c7a81db245))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xLjMiLCJ1cGRhdGVkSW5WZXIiOiIzNy4xLjQiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Co-authored-by: tsang-bot[bot] <146107447+tsang-bot[bot]@users.noreply.github.com>
tsang-bot bot added a commit to tsangste/nx-package-test that referenced this pull request Feb 6, 2024
This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@swc-node/register](https://togithub.com/swc-project/swc-node) |
[`1.6.8` ->
`1.8.0`](https://renovatebot.com/diffs/npm/@swc-node%2fregister/1.6.8/1.8.0)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/@swc-node%2fregister/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@swc-node%2fregister/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@swc-node%2fregister/1.6.8/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@swc-node%2fregister/1.6.8/1.8.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
| [@swc/cli](https://togithub.com/swc-project/pkgs)
([source](https://togithub.com/node ./bin)) | [`0.3.5` ->
`0.3.6`](https://renovatebot.com/diffs/npm/@swc%2fcli/0.3.5/0.3.6) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/@swc%2fcli/0.3.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@swc%2fcli/0.3.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@swc%2fcli/0.3.5/0.3.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@swc%2fcli/0.3.5/0.3.6?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>swc-project/swc-node (@&#8203;swc-node/register)</summary>

###
[`v1.8.0`](https://togithub.com/swc-project/swc-node/releases/tag/%40swc-node/register%401.8.0)

[Compare
Source](https://togithub.com/swc-project/swc-node/compare/@swc-node/register@1.6.8...@swc-node/register@1.8.0)

#### What's Changed

- fix(core): remove `jsc.module.noInterop` when it's not supported by
[@&#8203;kdy1](https://togithub.com/kdy1) in
[swc-project/swc-node#733
- chore: fix README for install, build, and test by
[@&#8203;devjiwonchoi](https://togithub.com/devjiwonchoi) in
[swc-project/swc-node#728
- feat(register): @&#8203;swc-node/register/esm use TypeScript resolver
by [@&#8203;cm-ayf](https://togithub.com/cm-ayf) in
[swc-project/swc-node#727
- chore(deps-dev): bump
[@&#8203;babel/traverse](https://togithub.com/babel/traverse) from
7.22.10 to 7.23.2 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[swc-project/swc-node#735
- Fix
[#&#8203;716](https://togithub.com/swc-project/swc-node/issues/716) by
[@&#8203;eliellis](https://togithub.com/eliellis) in
[swc-project/swc-node#725
- chore(deps-dev): bump axios from 1.3.5 to 1.6.1 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[swc-project/swc-node#738
- fix(core): respect useDefineForClassFields tsconfig value by
[@&#8203;dsyddall](https://togithub.com/dsyddall) in
[swc-project/swc-node#740
- fix(core): respect sourceMaps swcrc value by
[@&#8203;dsyddall](https://togithub.com/dsyddall) in
[swc-project/swc-node#742
- feat: add esm-register for node>20.6 by
[@&#8203;yeliex](https://togithub.com/yeliex) in
[swc-project/swc-node#748
- chore(deps-dev): bump follow-redirects from 1.15.2 to 1.15.4 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[swc-project/swc-node#747
- feat(core): support swc's `ignoreDynamic` option by
[@&#8203;SukkaW](https://togithub.com/SukkaW) in
[swc-project/swc-node#745
- fix: fix import ts from node_modules by
[@&#8203;yeliex](https://togithub.com/yeliex) in
[swc-project/swc-node#744
- Mention --enable-source-maps in README.md for esm projects. by
[@&#8203;Xinkai](https://togithub.com/Xinkai) in
[swc-project/swc-node#737
- feat: upgrade dependencies by
[@&#8203;Brooooooklyn](https://togithub.com/Brooooooklyn) in
[swc-project/swc-node#751

#### New Contributors

- [@&#8203;devjiwonchoi](https://togithub.com/devjiwonchoi) made their
first contribution in
[swc-project/swc-node#728
- [@&#8203;cm-ayf](https://togithub.com/cm-ayf) made their first
contribution in
[swc-project/swc-node#727
- [@&#8203;eliellis](https://togithub.com/eliellis) made their first
contribution in
[swc-project/swc-node#725
- [@&#8203;dsyddall](https://togithub.com/dsyddall) made their first
contribution in
[swc-project/swc-node#740
- [@&#8203;SukkaW](https://togithub.com/SukkaW) made their first
contribution in
[swc-project/swc-node#745
- [@&#8203;Xinkai](https://togithub.com/Xinkai) made their first
contribution in
[swc-project/swc-node#737

**Full Changelog**:
https://github.com/swc-project/swc-node/compare/[@&#8203;swc-node/core](https://togithub.com/swc-node/core)[@&#8203;1](https://togithub.com/1).10.5...[@&#8203;swc-node/register](https://togithub.com/swc-node/register)[@&#8203;1](https://togithub.com/1).8.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xNzEuMCIsInVwZGF0ZWRJblZlciI6IjM3LjE3Mi4yIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCJ9-->

Co-authored-by: tsang-bot[bot] <146107447+tsang-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants