Skip to content

Relative paths without file extensions in TS code should have .js extension appended when compiled #5346

Closed as not planned
@Jacob-Lockwood

Description

@Jacob-Lockwood

Describe the bug

In TypeScript code, import statements of other .ts files are written without the .ts file extension, but in node (or browser) with ESM, a path including file extension is used. SWC should add a .js file extension to relative imports from TS.

Input code

// foo.ts
const foo: string = "foo"
export { foo }

// index.ts
import someLib from "somLib"
import { foo } from "./foo"
console.log(foo)

Config

{
  "$schema": "https://json.schemastore.org/swcrc",
  "module": {
    "type": "es6"
  },
  "jsc": {
    "target": "es2022",
    "parser": {
      "syntax": "typescript"
    }
  }
}

Playground link

https://play.swc.rs/?version=1.2.220&code=H4sIAAAAAAAAA8vMLcgvKlFIy89XSCvKz1VQ0tMHspW4gISGJpe%2BvkJqRUFqcklqipVCJlalelnFSgA5CE0GRQAAAA%3D%3D&config=H4sIAAAAAAAAA0WNSwrDMAxE76J1FsWLLnKHHsK4SnDxD40CDcZ3rx1cspNmnp4qfeBorVSsgGVMOJPaL62kZ2E48UVpIUWPNhvArS9WdtaOMMzDmF6HnMETWCj65LdzyFyORRi4K5v28Cdbd8X8PkZQr3%2BX80ntdsw7j9cEVQ5uPx%2Bqcl%2B4AAAA

Expected behavior

SWC would transform relative imports without file extension from TypeScript to imports with .js extension.

Actual behavior

SWC does not transform these import paths, so Node fails when running them.

Version

"@swc/core": "^1.2.220"

Additional context

I have to use ESM with Node because some of my dependencies only work with it.

May be related to #1934, not sure

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions