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

virtual-tsc

v0.6.2

Published

Provides means to compile TypeScript code in memory

Downloads

5,099

Readme

virtual-tsc

Provides means to compile TypeScript code to JavaScript in memory. Requires typescript >= v2.0 and @types/node as peer dependencies, where @types/node should match your NodeJS runtime.

Usage

import { compile } from "virtual-tsc";
import * as ts from "typescript";
const result: CompileResult = compile(sourceCode: string, compilerOptions?: ts.CompilerOptions, declarations?);

where CompileResult looks as follows:

export interface CompileResult {
    success: boolean;
    diagnostics: Diagnostic[];
    result?: string;
    declarations?: string;
}

export interface Diagnostic {
    type: "error" | "warning" | "message";
    lineNr: number;
    charNr: number;
    sourceLine: string;
    description: string;
    annotatedSource: string;
}

Ambient declarations

declarations is an object of the type:

{
    "filename1.d.ts": "file contents 1",
    // ...
}

and is used to specify ambient declarations. Filenames must end in .d.ts. For instance you can declare a function log that exists in the global scope by providing a file like the following:

import * as fs from "fs"; // dummy import
declare global {
    function log(text: string);
}

To support augmentation of the global scope (like in the above file), you must force TypeScript to treat the file as a module. This can be done by a dummy import of a core NodeJS module.

Faster compilation with the Language Service API

As of version 0.3.0, this library supports incremental compilation with the TypeScript Language Service API. In simple tests, compile times for recurring compilations could be reduced by at least 99%. The usage changes slightly:

import { Server as TSServer } from "virtual-tsc";

// Create a new instance of the compiler with optional compiler options
const tsserver = new TSServer(options?: ts.CompilerOptions);

// optionally provide ambient declarations
tsserver.provideAmbientDeclarations(declarations);

// compile whenever the source file changes:
const result = tsserver.compile(
	filename /* string */,
	source /* string */
);

By providing a filename for the source, it is possible to compile multiple scripts on one instance of the compiler.

Error-tolerant compilation

By specifying noEmitOnError: false on the compilerOptions object, you can get a compiled result even if there were build errors. For example, the code

const test: string = 1

then compiles to the valid JavaScript

var test = 1

but you get the additional error message

const test: string = 1
      ^
ERROR: Type '1' is not assignable to type 'string'.

Changelog

0.6.2 (2022-01-10)

  • (AlCalzone) Replaced corrupted colors dependency with picocolors

0.6.1 (2020-07-05)

  • (AlCalzone) Allow package.json as ambient declarations, use "" as the current directory

0.6.0 (2020-06-09)

  • (AlCalzone) Expose setTypeScriptResolveOptions to set the options for resolving TypeScript and its lib files.

0.5.0 (2020-01-28)

  • (AlCalzone) Passing false as the 2nd parameter to the Server constructor disables logging.

0.4.6 (2018-08-03)

  • (AlCalzone) Allow TypeScript v3+ as a peer dependency

0.4.5 (2018-05-30)

  • (AlCalzone) Fixed performance issues when declaration and noEmitOnError are both true

0.4.1 (2018-05-23)

  • (AlCalzone) Allow emitting only declaration files

0.4.0 (2018-05-23)

  • (AlCalzone) Emit declaration files (*.d.ts), enabled by default

0.3.4 (2017-11-26)

  • (AlCalzone) Added a custom logger output

0.3.3 (2017-11-14)

  • (AlCalzone) Fixed lib resolution for the LanguageServiceAPI

0.3.2 (2017-11-09)

  • (AlCalzone) Use the LanguageServiceAPI to speed up multiple compilations

0.2.3 (2017-10-13)

  • (AlCalzone) Fixed module resolution on Linux
  • (AlCalzone) Added async compile method

0.2.2 (2017-10-13)

  • (AlCalzone) support NodeJS 4

0.2.1 (2017-10-13)

  • (AlCalzone) support output of builds with errors

0.2.0 (2017-10-13)

  • (AlCalzone) support ambient declarations

0.1.0 (2017-10-13)

  • (AlCalzone) initial release.

License

The MIT License (MIT)

Copyright (c) 2017 AlCalzone [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.