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

Native support for tsconfig's paths resolution #6828

Open
4 tasks done
Exelord opened this issue Feb 9, 2022 · 13 comments
Open
4 tasks done

Native support for tsconfig's paths resolution #6828

Exelord opened this issue Feb 9, 2022 · 13 comments
Labels
documentation Improvements or additions to documentation

Comments

@Exelord
Copy link

Exelord commented Feb 9, 2022

Clear and concise description of the problem

By having Typescript integration in Vite, I kind of assumed all tsconfig features would work out-of-the-box. While migrating from Next.js I discovered that Vite is lacking support for paths resolving. I noticed that many of my friends did that as well, although I would be in favour of adding a full typescript features support.

Suggested solution

Add native support for paths resolutions similar to: https://www.npmjs.com/package/vite-tsconfig-paths

Alternative

Document lack of this feature in Typescript section and recommend using: https://www.npmjs.com/package/vite-tsconfig-paths

Additional context

No response

Validations

@Niputi Niputi added the p2-nice-to-have Not breaking anything but nice to have (priority) label Feb 9, 2022
@Shinigami92
Copy link
Member

I have to say, I also would like to have this 🤔

So I would not need this anymore in all my projects
https://github.com/Shinigami92/quasar-vite/blob/d76b2e7a85a6ada9d0735fbc44931757af86df68/vite.config.ts#L23-L27

@haoqunjiang
Copy link
Member

haoqunjiang commented Feb 10, 2022

TSConfig paths only works for TypeScript / JavaScript modules (semantically). We need the alias for all kinds of resources.
So I think it would be confusing to use paths exclusively.

@Menci
Copy link
Contributor

Menci commented Mar 5, 2022

My solution:

import tsconfig from "./tsconfig.json";

const tsconfigPathAliases = Object.fromEntries(
  Object.entries(tsconfig.compilerOptions.paths).map(([key, values]) => {
    let value = values[0];
    if (key.endsWith("/*")) {
      key = key.slice(0, -2);
      value = value.slice(0, -2);
    }

    const nodeModulesPrefix = "node_modules/";
    if (value.startsWith(nodeModulesPrefix)) {
      value = value.replace(nodeModulesPrefix, "");
    } else {
      value = path.join(__dirname, value);
    }

    return [key, value];
  })
);

Works with my tsconfig.json:

// ...
    "paths": {
      "@/*": ["src/*"],
      "semantic-ui-css": ["node_modules/fomantic-ui-css"],
      "mobx-react": ["node_modules/mobx-react-lite"],
      "lodash": ["node_modules/lodash-es"],
      "markdown-it": ["node_modules/@esm-bundle/markdown-it"]
    }
// ...

@drush
Copy link

drush commented Jun 15, 2022

The feature should explicitly support (and validate it works with) a tsconfig.json with the extends keyword to import a tsconfig.base.json that has paths defined in the base file.

@Enteleform
Copy link

Adding some notes from #10063: tsconfig paths do not work in vite.config.ts, which was closed in favor of this issue.

In addition to the recommended functionality of vite-tsconfig-paths, which only works for the project being built, it would also be ideal to have paths aliases accessible within the execution of vite.config.ts itself.

@wvhulle
Copy link

wvhulle commented Feb 6, 2023

My solution:

import tsconfig from "./tsconfig.json";

const tsconfigPathAliases = Object.fromEntries(
  Object.entries(tsconfig.compilerOptions.paths).map(([key, values]) => {
    let value = values[0];
    if (key.endsWith("/*")) {
      key = key.slice(0, -2);
      value = value.slice(0, -2);
    }

    const nodeModulesPrefix = "node_modules/";
    if (value.startsWith(nodeModulesPrefix)) {
      value = value.replace(nodeModulesPrefix, "");
    } else {
      value = path.join(__dirname, value);
    }

    return [key, value];
  })
);

Works with my tsconfig.json:

// ...
    "paths": {
      "@/*": ["src/*"],
      "semantic-ui-css": ["node_modules/fomantic-ui-css"],
      "mobx-react": ["node_modules/mobx-react-lite"],
      "lodash": ["node_modules/lodash-es"],
      "markdown-it": ["node_modules/@esm-bundle/markdown-it"]
    }
// ...

How do you apply this exactly?

@bluwy
Copy link
Member

bluwy commented Jul 14, 2023

While I'd like to see first-class support for this as well, unfortunately tsconfig paths and resolve.alias are not a one-to-one behaviour.

  • tsconfig paths are a lot more lenient and involves multiple resolves / filesystem access to match the resolved path
  • resolve.alias is stricter where the first regex match will immediately replace and return the resolved path (only string manipulation)

Because of that it's not great to support this ootb and potentially slowing down more projects. vite-tsconfig-paths would still be the recommended way if you prefer the define-once experience. In the last meeting, we're open to documenting this as well.

@bluwy bluwy added documentation Improvements or additions to documentation and removed enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority) labels Jul 14, 2023
@alamhubb
Copy link

alamhubb commented Oct 4, 2024

I am looking forward to this feature, it will be very useful in the development phase, I need to write some plug-ins in the project, I will use the @ symbol in the plug-in development phase, but vite.config.ts does not support it, typescript supports it, vite app supports it. However, the reference code in vite.config.ts is not supported, which is sad

@haoqunjiang
Copy link
Member

How about subpath imports like #dep? It's natively supported by both Node.js and TypeScript 5.4+

@silvenon
Copy link

silvenon commented Oct 4, 2024

@alamhubb and using vite-tsconfig-paths plugin in the meantime doesn't work for you in plugin development?

@alamhubb
Copy link

alamhubb commented Oct 4, 2024

@alamhubb and using vite-tsconfig-paths plugin in the meantime doesn't work for you in plugin development?

Thanks, I temporarily solved this problem using tsx

@raveclassic
Copy link

@alamhubb would you mind sharing your configuration with tsx pls?

@alamhubb
Copy link

@alamhubb would you mind sharing your configuration with tsx pls?

image

  "scripts": {
    "test": "tsx test/HelloWorldGrammarParser.ts",
    "build": "tsc"
  },

tsconfig.json

{

  "compilerOptions": {
    "allowImportingTsExtensions": true,
    "moduleResolution": "Node",
    "module": "ES2022",
    "target": "ES5",
    "rootDir": "./src",
    "outDir": "./lib",
    "baseUrl": ".",
    "lib": [
      "ES2020"
    ],
    "types": [
      "node"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "esModuleInterop": true
  }
}

i use @ in HelloWorldGrammarParser.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests