Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

vue-tsc can not find vue files if module resolution is node12 or nodenext #1394

Closed
kalvenschraut opened this issue Jun 5, 2022 · 11 comments · Fixed by #1543
Closed

vue-tsc can not find vue files if module resolution is node12 or nodenext #1394

kalvenschraut opened this issue Jun 5, 2022 · 11 comments · Fixed by #1543
Labels
enhancement New feature or request

Comments

@kalvenschraut
Copy link
Contributor

Reproduction can be found here

I have been trying to migrate my codebase to use typescripts new esm modules and for that I need to use node12 or nodenext module resolution. Everything seems to work fine, but due to typescript needing extensions to correctly resolve files now, it appears vue-tsc is unable to find the vue files anymore. I did notice having vue.js did fix the issue for vue-tsc, but then I can't build with vite.

I can't seem to find any other alternative solutions to this problem, maybe I have to change something in my code base?

A possible solution to the problem maybe changing the file import path to vue.js with vue-tsc before calling tsc? If this is the desired direction, and if pointed in the right direction I can take a look at doing a PR.

@kalvenschraut
Copy link
Contributor Author

This seems to affect the module resolution in my editor also.

@kalvenschraut
Copy link
Contributor Author

kalvenschraut commented Jun 5, 2022

Found a workaround, which is using .vue.js everywhere and added the below to my vite config

{
    resolve: {
      // seems first matched alias is used, so handle case where using @ alias and vue file has a .vue.js extension
      alias: [{
        find: /^@\/(.*)\.vue\.js$/,
        replacement: `${resolve('src')}/$1.vue`
      }, {
        find: /^(.*)\.vue\.js$/, 
        replacement: `$1.vue`
      }, {
        find: '@',
        replacement: resolve('src')
      }]
    }
}

I would still like to either see volar rewrite the .vue imports to .vue.js so typescript can lookup the types automatically for the user or for vite to automatically add this kind of alias, not sure which direction preferred at this point.

@pwang2
Copy link

pwang2 commented Dec 5, 2022

using latest volar after merge #1542 , everything works fine but language server will complain. Have I missed anything?

image

@pwang2
Copy link

pwang2 commented Dec 5, 2022

still quite confusing but fixed by adding env.d.ts with a generic vue module definition and include in tsconfig.json

/// <reference types="vite/client" />
/// <reference types="vue/macros-global" />

declare module "*.vue" {
  import { DefineComponent } from "vue"
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
  const component: DefineComponent<{}, {}, any>
  export default component
}

@pwang2
Copy link

pwang2 commented Dec 7, 2022

After adding the dumb module hack, in main.ts, if you go to definition of App.vue import, it takes you to the env.d.ts dumb module.

In App.vue, if you go to definition of HelloWorld.vue, it takes you to the actual file which is more desired.

So still, it is not quite a complete solution.

@johnsoncodehk
Copy link
Member

@pwang2 see #1897 (comment)

@pwang2
Copy link

pwang2 commented Dec 9, 2022

@johnsoncodehk , Takeover mode works for me in Neovim. Thanks! Great work on volar to make everyone's life better and better. 🙌

@AlansCodeLog
Copy link

Is nodenext resolution supposed to work with just the .vue extension (I can't seem to get that working, and takeover is enabled) or do we have to specify .vue.js?

@kalvenschraut
Copy link
Contributor Author

it is suppose to but @johnsoncodehk it appears since typescript 5.0 I have to refer to ,vue.js again.
Update my original repo to typescript 5.0 and can be used to recreate.

image

@johnsoncodehk
Copy link
Member

It should be fixed by #2555

@kalvenschraut
Copy link
Contributor Author

looks like #2555 has been out already? In my repo it is still a problem even with vue-tsc 1.4.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants