Skip to content

fix: fix import ts from node_modules#744

Merged
Brooooooklyn merged 1 commit into
swc-project:masterfrom
yeliex:master
Feb 1, 2024
Merged

fix: fix import ts from node_modules#744
Brooooooklyn merged 1 commit into
swc-project:masterfrom
yeliex:master

Conversation

@yeliex

@yeliex yeliex commented Dec 2, 2023

Copy link
Copy Markdown
Contributor

as #727 (review) describes, when import .ts files from node_moduels, it throws error TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts".

it works well in 1.6.7

example scene: monorepo import .ts file in project, or server framework wants dynamic load routes in development

@yeliex

yeliex commented Dec 7, 2023

Copy link
Copy Markdown
Contributor Author

@Brooooooklyn please take a look

@weyert

weyert commented Jan 4, 2024

Copy link
Copy Markdown

Yes, would be nice if this change can be merged.

@Brooooooklyn Brooooooklyn merged commit c4485ca into swc-project:master Feb 1, 2024
tsang-bot Bot referenced this pull request in tsangste/nx-package-test 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/[email protected]...@swc-node/[email protected])

#### What's Changed

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

#### New Contributors

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

@charleslai2000 charleslai2000 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code can be changed into:

const TS_EXTENSIONS = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts];
const JS_EXTENSIONS = ["js", "jsx", "cjs", "mjs"];

export const resolve = async (specifier, context, nextResolve) => {
    const isTS = TS_EXTENSIONS.some((ext) => specifier.endsWith(ext));
    const isJS = JS_EXTENSIONS.some((ext) => specifier.endsWith(ext));
    // entrypoint
    if (!context.parentURL) {
        return {
            format: isTS ? 'ts' : undefined,
            url: specifier,
            shortCircuit: true,
        };
    }
    // import/require javascript module
    if (isJS) {
      // - local project non-TS file
      return nextResolve(specifier);
    }
    const { resolvedModule } = ts.resolveModuleName(specifier, fileURLToPath(context.parentURL), tsconfig, host, moduleResolutionCache);
    // import from local project to local project TS file
    if (resolvedModule && TS_EXTENSIONS.includes(resolvedModule.extension)) {
        return {
            format: 'ts',
            url: pathToFileURL(resolvedModule.resolvedFileName).href,
            shortCircuit: true,
        };
    }
    // import from local project to either:
    // - something TS couldn't resolve
    // - external library
    return nextResolve(specifier);
};

This code will deal with soft links of monorepo subprojects under node_modules, in this cache, the specifier has no extension at all.

@charleslai2000

Copy link
Copy Markdown

In order to load typescript file under node_modules, you also need change file register.js, remove the following codes:

    if (options.files && options.files.length) {
        if (PLATFORM === 'win32' &&
            options.files.every((file) => filename !== (0, path_1.resolve)(process.cwd(), file))) {
            return sourcecode;
        }
        if (PLATFORM !== 'win32' && options.files.every((file) => !filename.endsWith(file))) {
            return sourcecode;
        }
    }

These codes will limit only compile files in current project and all ts files under node_modules will be filtered out and emit to node. This is not suitable in this context.

@yeliex

yeliex commented Mar 2, 2024

Copy link
Copy Markdown
Contributor Author

I think this code can be changed into:

const TS_EXTENSIONS = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts];
const JS_EXTENSIONS = ["js", "jsx", "cjs", "mjs"];

export const resolve = async (specifier, context, nextResolve) => {
    const isTS = TS_EXTENSIONS.some((ext) => specifier.endsWith(ext));
    const isJS = JS_EXTENSIONS.some((ext) => specifier.endsWith(ext));
    // entrypoint
    if (!context.parentURL) {
        return {
            format: isTS ? 'ts' : undefined,
            url: specifier,
            shortCircuit: true,
        };
    }
    // import/require javascript module
    if (isJS) {
      // - local project non-TS file
      return nextResolve(specifier);
    }
    const { resolvedModule } = ts.resolveModuleName(specifier, fileURLToPath(context.parentURL), tsconfig, host, moduleResolutionCache);
    // import from local project to local project TS file
    if (resolvedModule && TS_EXTENSIONS.includes(resolvedModule.extension)) {
        return {
            format: 'ts',
            url: pathToFileURL(resolvedModule.resolvedFileName).href,
            shortCircuit: true,
        };
    }
    // import from local project to either:
    // - something TS couldn't resolve
    // - external library
    return nextResolve(specifier);
};

This code will deal with soft links of monorepo subprojects under node_modules, in this cache, the specifier has no extension at all.

add another condition to detect js files is unnecessary. these would be coved by final case.

@yeliex

yeliex commented Mar 2, 2024

Copy link
Copy Markdown
Contributor Author

In order to load typescript file under node_modules, you also need change file register.js, remove the following codes:

    if (options.files && options.files.length) {
        if (PLATFORM === 'win32' &&
            options.files.every((file) => filename !== (0, path_1.resolve)(process.cwd(), file))) {
            return sourcecode;
        }
        if (PLATFORM !== 'win32' && options.files.every((file) => !filename.endsWith(file))) {
            return sourcecode;
        }
    }

These codes will limit only compile files in current project and all ts files under node_modules will be filtered out and emit to node. This is not suitable in this context.

I think it works as expected. files should be added into options.files, or leaves the option empty. or please provide reproduction case it it has problem in your situation

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.

4 participants