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

tsc-ls

v0.0.6

Published

TypeScript compiler using language service plugins

Downloads

5

Readme

tsc-ls

TypeScript compiler overlay using language service plugins.

npm license CI - CD

One of TypeScript compiler limitation is its inability to use language services plugins during compilation. By design, language services are meant to be used by editors only.

This limitation makes it impossible to type-check typescript plugins in CLI environments, like with CIs.

A good example would be with typescript-plugin-css-modules plugin, which allows to add CSS classes typing:

// tsconfig.json
{
  "compilerOptions": {
    "composite": true,
    "plugins": [
      {
        "name": "typescript-plugin-css-modules"
      }
    ]
  },
  "include": ["."]
}
/* toto.module.css */

.button {
  color: red;
}
import styles from './toto.module.css';

styles.button; // OK
styles.toto; // Editor shows type error

With this example, editors like VSCode shows correctly the error. But if you use tsc to compile your file, it will emit no error.

This is why tsc-ls was made, to make compilation using language service plugins. So it shows errors in the console like it should.

Limitations

This tool is young, tested but only for some cases, nothing is guaranteed.

This inconsistency is due to the TypeScript environment, which does not allow it to easily work with its compiler. As mentioned above, language services were not meant to be used outside of editors. Like with some popular plugins, multiple globals may be mutated during the process. tsc-ls cannot know what plugins will do, so it's not possible to offer a totally generic solution that works with every plugin.

For now it was tested with these plugins:

Using other plugins may not work. In this case, consider opening an issue, I would be happy to help.

Also, only some TypeScript project configurations are handled, it may not work with your projects - again here, consider opening an issue.

Currently only TypeScript build mode is handled (with --build or -b).

You may see some performance impact, depending on plugins you are using.

Benchmark

You can see performance impact using tsc-ls -b instead of tsc -b using no-plugins: https://chnapy.github.io/tsc-ls/dev/bench

Benchmark is done without plugin to keep a consistant comparison basis. Also plugin performance impact depends entirely of plugin itself.

If you have performance issues please first check plugins you are using.

Quick start

yarn add -D tsc-ls

You can use tsc-ls like tsc, using same compiler options.

yarn tsc-ls -b .

Because of configuration limitation, you must use build mode (--build or -b). Check tsc CLI documentation for more.

You can now use this command into your package.json.

{
  "scripts": {
    "type-check": "tsc-ls -b"
  }
}

Use into scripts

You can use this tsc-ls functions into your own scripts.

#!/usr/bin/env node

import ts from 'typescript';
import { compile, workarounds } from 'tsc-ls';

const parsedCommandLine = workarounds.parsePatchedCommandLine(
  ts.sys.args,
  ts.sys.readFile
);

compile({
  parsedCommandLine,
}).then(({ hasErrors, writeDiagnostics }) => {
  writeDiagnostics();

  if (hasErrors) {
    process.exit(1);
  }
});

For more check source files, like bin.ts.

Use by plugins

tsc-ls pass to plugins a language service typed as LanguageServiceWithDiagnostics which has an optional pluginsDiagnostics property. It allows plugins to put diagnostics directly to compiler result. It can be useful when you want to pass errors outside of sourceFile creation/update process and trigger compilation error.

import { LanguageServiceWithDiagnostics, PluginInit } from 'tsc-ls';

const init: PluginInit = (modules) => {
  const ts = modules.typescript;

  const create = (info) => {
    const languageServiceWithDiagnostics: LanguageServiceWithDiagnostics = info.languageService;

    const { pluginsDiagnostics } = languageServiceWithDiagnostics;

    // plugin logic...

    if(pluginsDiagnostics) {
      const fileDiagnostics = pluginsDiagnostics.get(fileName) ?? [];

      fileDiagnostics.push(
        ...// put your errors & other diagnostics
      );

      pluginsDiagnostics.set(fileName, fileDiagnostics);
    }

    return languageServiceWithDiagnostics;
  };

  return { create };
};

export = init;