-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Go to Definition for Non JS/TS File Imports #15146
Comments
They're a couple of great VS Code plugins that do this at the moment. Because of this, they suffer from the following problem: Say I have a declaration file, let's call it declare module '*.css' { export default '' as string; }
declare module '*.scss' { export default '' as string; }
declare module '*.html' { export default '' as string; } in this case, despite of the plugins efforts, the user always sees a list of two definitions to go to instead of being able to jump directly to the file. That is really the only drawback to the current plugins. Having said that, I always thought it would be nice if TypeScript could validate the existence of these asset files but i think that ship has long ago sailed. |
My main concern is that this is not ES6 compliant. ES6 does not talk about importing non-code modules. So this code is not really valid ES6 and will not work with a native ES6 module implementation. So we can pretend that it will work, and just let it pass. or leave it the way it is and be spec-compliant. |
I'm OK with not including this as builtin TS functionality given the compliance concerns. Is there any way we can leverage TSServer for these scenarios however? Perhaps with a TSServer plugin? |
This is part of the module resolution logic, so we have to add it in the core. |
Since this is specific to Webpack's implementation of |
@thw0rted how is this specific to Webpack? |
I should have said, since it's specific to the loader / bundler's implementation of |
It is correct that TypeScript does not provide this capability, but Webpack is just one of many tools that do. |
I think my point stands, in that case -- nothing in core TypeScript provides for |
Yes you are quite correct. I didn't mean to suggest otherwise I was just mentioning that the notion is more general than Webpack. |
Additionally TypeScript should be checking the path of EVERY import that occurs (module or otherwise) and raise an appropriate error. A hacky workaround silences an error that an html file isn't a module, but an incorrect relative path doesn't re-flag an error. So I'm stuck guessing that my import paths are hopefully correct. microsoft/vscode#48025 (comment) I don't get the argument regarding strict ES6 spec compliance. This is something webpack, fusebox and pretty much every bundler out there allows and encourages. I'd imagine this has plenty of devs out there using it. Widespread real-world use cases matter, so the meaning of what is and isn't 'compliant' needs to be re-evaluated in this case. Besides if this needs to be supported by the core, and maybe just have an appropriate .tsconfig flag turned off by default, why not just do it? |
I think the argument is that Typescript never has to actually load the file contents -- that's handled by your bundler, after transpilation. I think it's important to address exactly what we're trying to solve. Is it edit-time path checking, so you get a red squiggle in VS Code when the file doesn't exist? Or is it transpile-time checking so that |
Because Typescript doesn't need to load the file contents of non-code imports, a simple path check would be trivial to implement... otherwise every person that uses Typescript + a bundler would need an extra plugin for smooth usage. Considering how ubiquitous bundlers have become, I still think this would be a trivial addition to the Typescript functionality set... As for a plugin that does the above, I'm having a hard time finding one. Can someone suggest one? |
VSCode never supports? Or, I want plugin or I'll develop myself |
Just a quick note here: I implemented CSS/SCSS's |
Note that this doesn't work even for importing |
@dwelle See the |
@mjbvz can't seem to make it work, using this {
"compilerOptions": {
"rootDir": ".",
"module": "system",
"resolveJsonModule": true
},
/* ... */
} |
Ok, it seems that So I guess I can either choose between absolute paths, or |
This issue has been bugging me a lot. I'm glad someone else brought it up first. |
Currently, what's the best workaround? |
The problems my team and I currently have are:
Would something like a (Presumably the typescript compiler would emit an error if asked to bundle a file with a non-js import) |
I had read this issue earlier today, before I filed issue 90155 in vscode . This does not appear to me to address my problem. But if, as you said earlier in this thread, there are extensions that workaround this I'd sure like to know of one. I've been scouring the extensions gallery trying to find it. Do you know any by name that I could try? |
I found a vscode plugin called |
When can we deal with this problem?@mjbvz |
It is very important to autocomplete files other than JS or TS and check the existence of a file. Due to its absence, typos often appear and, in general, work becomes less comfortable. Please make this opportunity. |
Bump. It would be nice to have a way to do file path resolution with wild cards and extrapolate the beginning path and resolve the file using node.
The above would both declare the typing and return the file it should be resolving. Of course there would need to be escape hatches for bad paths etc. |
In the meantine, use can leverage CSS Navigation extension className resolution. It planned to support |
I think this can be closed since this landed in TS 4.3 https://devblogs.microsoft.com/typescript/announcing-typescript-4-3/#go-to-def-non-js |
Yep. I've just rechecked. Looks lite it works now |
Yes, go to definition works. But how can I get rid of the warning "Cannot find module '../img/bla.png' or its corresponding type declarations.ts(2307)" while preserving this feature? If I |
@chkpnt A workaround; not too good but it works Removes the warning and also go-to-definition |
Unfortunately, adding |
Here's an extension that implements the requested feature: |
It’s not solving the '.scss' navigation to file and style class. |
From microsoft/vscode#24276
Issue
Users of tools such as webpack often load non js/ts assets, such as css or images, using
require
orimport
:Operations such as
go to definition
currently do not work on these pathsProposal
go to definition
on these resource paths should try to open the corresponding resource in the editor.The text was updated successfully, but these errors were encountered: