用Node将TypeScript移植到内存中

问题描述 投票:6回答:2

有没有办法用Node将TypeScript转置到内存中?我想能够在内存中得到生成的JavaScript。

javascript node.js typescript code-translation
2个回答
8
投票

是的,TypeScript提供了一个 ts.transpileModule 的功能。

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';

更多


3
投票

我们最终基于原生的TypeScript开发了自己的解决方案。转桩模块 功能。

从TYPESCRIPT DOCS

TranspileModule将使用指定的编译器选项从'input'参数编译源文本。如果没有提供任何选项,它将使用一组默认的编译器选项。本函数将无条件地使用额外的编译器选项。

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

转堆() - 这段代码将把TypeScript移植到JavaScript中(typecriptServices.min.js from TypeScript 是必须的)。)

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)
        })
    }
} 
© www.soinside.com 2019 - 2024. All rights reserved.