Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transpile TypeScript In Memory With Node

Is there a way to transpile TypeScript in memory with Node? I would like to be able to get the generated JavaScript in memory.

like image 844
A2MetalCore Avatar asked Feb 25 '15 21:02

A2MetalCore


2 Answers

Yes. TypeScript provides a a ts.transpileModule function:

const ts = require('typescript');
const source = "let x: string  = 'hello world'";
const result = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS }});
console.log(result.outputText); // var x = 'hello world';

More

  • From the TypeScript wiki : https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#a-simple-transform-function
like image 175
basarat Avatar answered Oct 02 '22 14:10

basarat


We ended up developing our own solution based on the native TypeScript transpileModule functionality.

FROM TYPESCRIPT DOCS

TranspileModule will compile source text from 'input' argument using specified compiler options. If no options are provided - it will use a set of default compiler options. Extra compiler options that will unconditionally be used by this function are:

  • isolatedModules = true
  • allowNonTsExtensions = true
  • noLib = true
  • noResolve = true

transpile() - This code will transpile TypeScript to JavaScript (typescriptServices.min.js from TypeScript is required):

export function transpile(tscode: string): TYPE.EVENT {

    interface TranspileOptions {
        compilerOptions?: any
        fileName?: string;
        reportDiagnostics?: boolean;
        moduleName?: string;
        renamedDependencies?: any;
    }

    interface TranspileOutput {
        outputText: string;
        diagnostics?: any[];
        sourceMapText?: string;
    }

    let compilerOptions: ts.CompilerOptions = {
        isolatedModules: false
    }

    let options: TranspileOptions = {
        compilerOptions: compilerOptions,
        reportDiagnostics: true
        // moduleName: modulename
    }

    let info: TYPE.EVENT;

    try {

        // Transpile the ts code to js.
        let ret: TranspileOutput = ts.transpileModule(tscode, options);

        // If diagnostics were returned.
        // NOTE: The transpiler is currently always return a message (code=5047) about 'isolatedModules', which
        // is not relavent for our use. If there is more than one row than there is in fact an error.
        if (ret.diagnostics && ret.diagnostics.length > 0) {

            let code = ret.diagnostics[0].code;

            if (code == 5047) {
                return (info = {
                    success: true,
                    returnvalue: ret.outputText,
                    caller: LibCore.getFunctionName(arguments)
                })
            } else {

                let text = ret.diagnostics[0].messageText;
                // let hint = ret.diagnostics[0].file.parseDiagnostics[0].file.nextContainer.symbol.name;

                return (info = {
                    success: false,
                    returnvalue: ret.diagnostics,
                    message: `Syntax error: ${text} Code: ${code}`,
                    caller: LibCore.getFunctionName(arguments)
                })
            }

        } else {
            return (info = {
                success: true,
                returnvalue: ret.outputText,
                caller: LibCore.getFunctionName(arguments)
            })
        }

    } catch (e) {
        return (info = {
            success: false,
            message: e.message,
            caller: LibCore.getFunctionName(arguments)
        })
    }
} 
like image 33
A2MetalCore Avatar answered Oct 02 '22 16:10

A2MetalCore