andywang425 / vscode-scriptmonkey

Userscript language support extension for vscode.
https://marketplace.visualstudio.com/items?itemName=andywang.vscode-scriptmonkey
MIT License
23 stars 3 forks source link

Support for libraries from `@require` #16

Closed rybak closed 6 months ago

rybak commented 1 year ago

Browser extensions for userscripts support loading of additional JavaScript source files to the scope of the userscripts, i.e. libraries. This is done using @require entries in metadata.

It would be nice if Scriptmonkey, the Visual Studio Code extension, supported loading such libraries into scope when editing userscripts.

I'm not familiar with JavaScript development in general, but I assume that for editing JavaScript source code that isn't a userscript, such loading of symbols from libraries (functions, constants, variables, classes) is supported. That is, developers, when editing a JavaScript file, where a library is used, can enjoy the auto-completion, documentation lookup, and other niceties when working with APIs of the library.

Possible workaround?

Because my userscripts are relatively simple, I haven't yet moved to "building" the userscript using the JavaScript build tools. I assume, this would solve this "access to library in the currently edited file" problem through support of these build tools in VS Code. Maybe I can hack together the package.json file (or something, I don't know how build scripts for JS work) which would make VS Code load the libraries into scope?

Reference documentation about @require

andywang425 commented 1 year ago

It would be nice if Scriptmonkey, the Visual Studio Code extension, supported loading such libraries into scope when editing userscripts.

I've had that thought before. However with my current knowledge of vscode extension development, I have no idea how to implement this feature. Maybe I'll find out a way after I learn more.

Because my userscripts are relatively simple, I haven't yet moved to "building" the userscript using the JavaScript build tools.

Actually I think using a userscript development framework is the simplest solution. Just install some npm packages and import them, then you can enjoy the auto-completion, type annotation and documentation lookup. I'm using vite-plugin-monkey in one of my userscript projects and it works well for me. Though using build tools for simple userscripts may seem unnecessary, you can give it a try.

I assume, this would solve this "access to library in the currently edited file" problem through support of these build tools in VS Code. Maybe I can hack together the package.json file (or something, I don't know how build scripts for JS work) which would make VS Code load the libraries into scope?

Don't know how to do this in a vscode extension too.

rybak commented 1 year ago

After poking at VS Code for a bit, I found a workaround that works for my simple use-case. I use TamperDAV for syncing userscripts between the browser and the directory which is opened in VS Code. I also have a local clone of the repository with the libraries used by the userscripts I am editing. Simply creating a symlink to the library's .js file in the directory, which is opened in VS Code, is enough for VS Code to pick it up.