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

Support supplying a watchFactory option that resolves the plugin and uses it for watching file or directory #51074

Closed
wants to merge 13 commits into from

Conversation

sheetalkamat
Copy link
Member

@sheetalkamat sheetalkamat commented Oct 5, 2022

With this change we allow users to use custom watchFactory plugin either on command line or in editor
For plugin writers the plugin is expected to be module that returns the factory with methods to watchFile and watchDirectory

export type UserWatchFactoryModule = (mod: { typescript: typeof ts, options: WatchOptions, config: any }) => UserWatchFactory;
export interface UserWatchFactory {
    watchFile?(fileName: string, callback: FileWatcherCallback, pollingInterval: number, options: WatchOptions | undefined): FileWatcher;
    watchDirectory?(fileName: string, callback: DirectoryWatcherCallback, recursive: boolean, options: WatchOptions | undefined): FileWatcher;
    onConfigurationChanged?(config: any): void;
}

This behaves similar to language service plugin as in user can have this set in tsconfig.json in watchOptions as watchFactory, pass it on command line during batch compilation or set in vscode's global watchOptions

watchFactory option can either be module name to look for or it can be PluginImport which is object literal with property name that is resolved as name and the object literal is passed on to the factory.

The sample watchFactory using @parcel/watcher is at https://github.com/sheetalkamat/typescript-parcel-watcher

The changes in PR include:

  • Adding option watchFactory in watchOptions that takes in either string or object literal. The name of the factory needs to be package name to resolve (just like our LS plugins`
  • During watchFile or watchDirectory we look for this resolved module and use the specific function if available.
  • On command line we look for the module next to typescript we are running from
  • In editor it looks for paths just like it would find LS plugins from (eg. depending on options, custom locations, executing typescript peer module or next to tsconfig.json)
  • In editor we also support calling onConfigurationChanged similar to we call for LS plugins when editor uses protocol to do so.

Eg plugin using parcel watcher is at https://github.com/sheetalkamat/typescript-parcel-watcher

Installing:

git clone https://github.com/sheetalkamat/typescript-parcel-watcher.git
cd typescript-parcel-watcher
npm i
npm run build

Usage:

npm link
cd repoWithTypeScriptCode
npm link typescript-parcel-watcher
# install typescript from this PR

At this point you can pass --watchFactory typescript-parcel-watcher to tsc or set it in watchOptions of config file and/or vscode settings.

@typescript-bot
Copy link
Collaborator

Thanks for the PR! It looks like you've changed the TSServer protocol in some way. Please ensure that any changes here don't break consumers of the current TSServer API. For some extra review, we'll ping @sheetalkamat, @amcasey, @mjbvz, @minestarks for you. Feel free to loop in other consumers/maintainers if necessary

@sheetalkamat sheetalkamat added Experiment A fork with an experimental idea which might not make it into master Experience Enhancement Noncontroversial enhancements labels Oct 5, 2022
@sheetalkamat sheetalkamat force-pushed the watchPlugins branch 10 times, most recently from d1d7469 to 2e5534d Compare October 10, 2022 23:06
@sheetalkamat sheetalkamat force-pushed the watchPlugins branch 8 times, most recently from 43a3958 to 83a0fa2 Compare November 10, 2022 03:08
@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 10, 2022
@sheetalkamat sheetalkamat changed the title [WIP] Watch plugin Support supplying watchFactory option that resolves the plugin and uses it for watching file or directory Nov 10, 2022
@sheetalkamat sheetalkamat force-pushed the watchPlugins branch 5 times, most recently from 8bcf7de to 18bf8ce Compare March 13, 2023 21:20
@sheetalkamat sheetalkamat force-pushed the watchPlugins branch 2 times, most recently from 52da5a5 to 129896e Compare March 21, 2023 19:01
@sheetalkamat sheetalkamat marked this pull request as draft April 10, 2023 22:27
@sheetalkamat sheetalkamat force-pushed the watchPlugins branch 3 times, most recently from f660f6b to 5d652ba Compare April 10, 2023 23:04
@sheetalkamat
Copy link
Member Author

Close in favor of #54012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team Experience Enhancement Noncontroversial enhancements Experiment A fork with an experimental idea which might not make it into master For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants