Skip to content

Directory import error on compiled output #8782

@stelescuraul

Description

@stelescuraul

Describe the bug

Hi everyone,

I have created a small reproducible example of the error which you can find here.

Using path aliases in tsconfig and swcrc, the index.js filename is stripped from the end of the import path when using ESM modules.

> npx swc -d ./dist -D --delete-dir-on-start --only="**/*.js,**/*.ts,**/*.cjs,**/*.mjs" --ignore ./node_modules,./dist .
> node dist/projects/api/main.js

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import 'workspaces/dist/libs/math' is not supported resolving ES modules imported from workspaces/dist/projects/api/main.js

The output of in the dist/projects/api/main.js looks like this:

import { sum } from "../../libs/math"; <<-- should be /math/index.js

Renaming the file "index.js" to anything else resolves the issue and the path is added correctly. EG renaming it to main.js:

import { sum } from "../../libs/math/main.js"; <<-- main.js is correctly appended

This issue seems to have been introduced in 1.3.106. last version that worked with this setup was 1.3.105

Input code

// projects/api/main.ts

import { sum } from "@foo/math";

console.log(sum(1, 2));

Config

{
  "$schema": "https://json.schemastore.org/swcrc",
  "isModule": true,
  "jsc": {
    "target": "es2022",
    "keepClassNames": true,
    "loose": false,
    "baseUrl": ".",
    "paths": {
      "@foo/math": ["libs/math/index.js"],
      "@foo/math/*": ["libs/math/*"],
      "@foo/common": ["libs/common/main.js"]
    },
    "parser": {
      "syntax": "typescript"
    }
  },
  "minify": false,
  "module": {
    "type": "es6",
    "resolveFully": true
  }
}

Playground link (or link to the minimal reproduction)

https://github.com/stelescuraul/dep-cruiser-issue

SWC Info output

No response

Expected behavior

Correctly append /index.js for path aliases.

Actual behavior

No response

Version

1.3.105

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions