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.
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;
}
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.
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.
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'.
- (AlCalzone) Replaced corrupted
colors
dependency withpicocolors
- (AlCalzone) Allow
package.json
as ambient declarations, use "" as the current directory
- (AlCalzone) Expose
setTypeScriptResolveOptions
to set the options for resolving TypeScript and its lib files.
- (AlCalzone) Passing
false
as the 2nd parameter to the Server constructor disables logging.
- (AlCalzone) Allow TypeScript v3+ as a peer dependency
- (AlCalzone) Fixed performance issues when
declaration
andnoEmitOnError
are bothtrue
- (AlCalzone) Allow emitting only declaration files
- (AlCalzone) Emit declaration files (*.d.ts), enabled by default
- (AlCalzone) Added a custom logger output
- (AlCalzone) Fixed lib resolution for the LanguageServiceAPI
- (AlCalzone) Use the LanguageServiceAPI to speed up multiple compilations
- (AlCalzone) Fixed module resolution on Linux
- (AlCalzone) Added async compile method
- (AlCalzone) support NodeJS 4
- (AlCalzone) support output of builds with errors
- (AlCalzone) support ambient declarations
- (AlCalzone) initial release.
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.