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

Can't use ESM with jsc.paths #7829

Closed
Nickersoft opened this issue Aug 17, 2023 · 7 comments · Fixed by #7860
Closed

Can't use ESM with jsc.paths #7829

Nickersoft opened this issue Aug 17, 2023 · 7 comments · Fixed by #7860
Assignees
Labels
Milestone

Comments

@Nickersoft
Copy link

Nickersoft commented Aug 17, 2023

Describe the bug

Hey folks,

I've come into a bit of a pickle. Node 19 drops support for es-module-specifier-resolution, which I have been using to support extension-less imports in my ESM codebase.

I realized this makes it impossible for me to continue using jsc.paths in my TypeScript project, as ts files automatically have their extensions removed, yet if I use js as an extension, SWC can't find the path and doesn't transform the import.

Example

"paths": {
    "@my/pkg": ["libs/pkg/src/index.ts"]
}

results in:

import { fn } from 'libs/pkg/src/index';

and

"paths": {
    "@my/pkg": ["libs/pkg/src/index.js"]
}

results in:

import { fn } from '@my/pkg';

Input code

import { fn } from '@my/pkg';

Config

{
  "module": {
    "type": "es6"
  },
  "jsc": {
    "baseUrl": ".",
    "target": "es2020",
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "importMeta": true
    },
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": true
    },
    "keepClassNames": true,
    "paths": {
      "@my/pkg": ["libs/pkg/src/index.ts"],
    }
  }
}

Playground link

No response

Expected behavior

I think that SWC should provide a similar mechanism to how TypeScript supports ESM, where TS files can be imported as JS (i.e. jsc.paths should find and transform the path appropriately).

Better yet, now that you can import ts files in TS, what I really wish is that you could just always use ts extensions for everything and SWC would be smart enough to know to replace them with js in the transpiled code instead of just removing the extension.

Actual behavior

Right now, SWC defaults to removing TS extensions unless you manually import TS files as JS (which luckily TS supports), however, this breaks down when using jsc.paths as described above. Starting in Node 19, this will result in un-runnable code without a custom loader.

Version

1.3.77

Additional context

No response

@kdy1 kdy1 added this to the Planned milestone Aug 17, 2023
@kdy1 kdy1 self-assigned this Aug 17, 2023
@Nickersoft
Copy link
Author

Hey @kdy1! Excited to see this marked as planned. Curious out of the suggestions above, what approach do you think the SWC project will take here? Or are you still unsure?

@kdy1
Copy link
Member

kdy1 commented Aug 18, 2023

I prefer following the direction of tsc, but I'm not sure if tsc has one. Mainly because I didn't read your comment yet... I need some time 😄

@Nickersoft
Copy link
Author

Haha sure thing – regardless, super happy this is getting looked at and let me know if I can help in any way :)

@kdy1
Copy link
Member

kdy1 commented Aug 25, 2023

I think

{
  "module": {
    "type": "es6"
  },
  "jsc": {
    "baseUrl": ".",
    "target": "es2020",
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "importMeta": true
    },
    "paths": {
      "@my/pkg": ["libs/pkg/src/index.js"],
    }
  }
}

is a better API

@kdy1
Copy link
Member

kdy1 commented Aug 25, 2023

But maybe we need an option to fully qualify index.js even if we can remove it from the import.

@kdy1
Copy link
Member

kdy1 commented Aug 25, 2023

Filed #7861

kdy1 added a commit that referenced this issue Aug 25, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.80 Aug 25, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Sep 25, 2023

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 Sep 25, 2023
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