npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rnx-kit/typescript-service

v2.0.0

Published

TypeScript language services with support for custom module resolution

Downloads

45,344

Readme

@rnx-kit/typescript-service

Build npm version

@rnx-kit/typescript-service gives you access to TypeScript's language services, and lets you customize how module resolution occurs.

Configuration

The starting point for working with TypeScript is reading configuration from the command line, or from a configuration file like tsconfig.json.

Both methods yeild a ParedCommandLine object, offering the same level of control over how TypeScript behaves.

import ts from "typescript";

// Read configuration from a NodeJS command-line
const cmdLine = ts.parseCommandLine(process.argv.slice(2));

// Read configuration from a project file (parsed into a TypeScript command-line object)
const configFileName = findConfigFile(searchPath);
if (!configFileName) {
  throw new Error(`Failed to find config file under ${searchPath}`);
}
const cmdLine = readConfigFile(configFileName);
if (!cmdLine) {
  throw new Error(`Failed to read config file ${configFileName}`);
}

// For either method, handle errors
if (cmdLine.errors.length > 0) {
  ...
}

Language Services

TypeScript's language service allows you to work with source code continuously, unlike the TypeScript compiler, which makes a single pass through the code. The language service tends to load only what is needed to fulfill the current request, such as getting diagnostics for a particular source file, or re-loading a changed file being watched. This saves time and memory, when full source validation isn't needed.

The language service is accessible through the Service and Project classes. Service manages shared state across all projects, and is meant to be a singleton. Project contains a TypeScript configuration, which includes a list of source files. TypeScript configuration comes from either the command line or a file like tsconfig.json.

You can use a Project to validate code, and emit transpiled JavaScript:

const service = new Service();
const project = service.openProject(cmdLine);

// validate
const fileHasErrors = project.validateFile(fileName);
const projectHasErrors = project.validate();

// emit
const fileEmitted = project.emitFile(fileName);
const projectEmitted = project.emit();

You can also change which files are in a project. This is typically done in response to an external event, like a callback notifying you that a file has been added, updated or removed:

import ts from "typescript";

function onFileEvent(eventType: string, fileName: string, payload?: string) {
  if (eventType === "add") {
    project.addFile(fileName);
  } else if (eventType === "modify") {
    project.updateFile(
      fileName,
      payload && ts.ScriptSnapshot.fromString(payload)
    );
  } else if (eventType === "delete") {
    project.deleteFile(fileName);
  }
}

When you're finished working with a Project, you must dispose of it to properly release all internal resources:

project.dispose();

Customizing the Language Service

The language service is initialized using a host interface. You can customize the host interface to change the way TypeScript works:

const enhanceLanguageServiceHost = (host: ts.LanguageServiceHost): void => {
  // change host functions in here
};

const service = new Service();
const project = service.openProject(cmdLine, enhanceLanguageServiceHost);

For example, you can replace the functions which control how modules and type references are resolved to files:

function resolveModuleNames(
  moduleNames: string[],
  containingFile: string,
  reusedNames: string[] | undefined,
  redirectedReference: ResolvedProjectReference | undefined,
  options: CompilerOptions
): (ResolvedModule | undefined)[] {
  /* ... */
}

function resolveTypeReferenceDirectives(
  typeDirectiveNames: string[],
  containingFile: string,
  redirectedReference: ResolvedProjectReference | undefined,
  options: CompilerOptions
): (ResolvedTypeReferenceDirective | undefined)[] {
  /* ... */
}

const enhanceLanguageServiceHost = (host: ts.LanguageServiceHost): void => {
  host.resolveModuleNames = resolveModuleNames;
  host.resolveTypeReferenceDirectives = resolveTypeReferenceDirectives;
};