moxicat
moxicat

Reputation: 91

VSCODE Javascript ----how to Implicit import library

I want to hide "import" when referencing the library, that is, local/global import, and use the contents of the library normally Can it be achieved, or is it already achieved?

I use these codes in a plug-in loader, so I don’t need to import it. It’s just a tool for prompting to view annotations.

I am using VSCODE and its built-in JavaScript

example:

import { mc } from "./Libary/Game/Player";

mc.runcmd('kill u m')

want to use:

mc.runcmd('kill u m')

And you can also see comments and function types, etc.

Upvotes: 7

Views: 570

Answers (3)

Vineet Singh
Vineet Singh

Reputation: 347

It is not possible in vscode, this feature was introduced in one of the versions of IntelliJ IDEA but they had to roll back to the previous version within 2 weeks. It causes too much trouble for programmers. It became really hard to separate two programs from each other and apart from that the size of the build file keeps on increasing with every new project.
If you still want to do that use the idea shared by @David shortman

Upvotes: 0

Taghi Khavari
Taghi Khavari

Reputation: 6582

I don't think this is achievable at the moment nor do I think it should be, because:

  1. The imports are related to how modules in javascript language works

  2. It may cause a lot of problems.

for example, imagine that you have two games like Game1 and Game2 which both of them has a mc method:

import { mc } from "./Libary/Game1/Player";
import { mc } from "./Libary/Game2/Player";

How should VScode decide what to import here?

but when you're using normal javascript imports it's just a matter of changing name imports like this:

import { mc as mc1 } from "./Libary/Game1/Player";
import { mc as mc2 } from "./Libary/Game2/Player";

Upvotes: 4

David Shortman
David Shortman

Reputation: 992

By declaring the API for your libraries in a .d.ts file and setting up a jsconfig.json in the root of your project, VSCode will automatically include those definitions in the global scope.

Here's an example repository with a functioning usage of mc.runcmd: https://github.dev/david-shortman/vscode-javascript-how-to-implicit-import-library

More information on declaration files- https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html

More on jsconfig.json- https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#using-tsconfigjson-or-jsconfigjson

Upvotes: 0

Related Questions