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

Directory import error on compiled output #8782

Closed
stelescuraul opened this issue Mar 26, 2024 · 3 comments · Fixed by #8784
Closed

Directory import error on compiled output #8782

stelescuraul opened this issue Mar 26, 2024 · 3 comments · Fixed by #8784
Assignees
Labels
Milestone

Comments

@stelescuraul
Copy link

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

@kdy1
Copy link
Member

kdy1 commented Mar 27, 2024

I can't run npm ci from the repository you provided.

npm ERR! code E403
npm ERR! 403 403 Forbidden - GET https://artifactory.workday.com/artifactory/api/npm/npm-virtual/source-map/-/source-map-0.6.1.tgz
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy, or
npm ERR! 403 on a server you do not have access to.

@stelescuraul
Copy link
Author

@kdy1 I removed package lock, you should be able to run npm i instead.

kdy1 added a commit that referenced this issue Mar 28, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.4.12 Apr 4, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented May 4, 2024

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 May 4, 2024
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