代码之家  ›  专栏  ›  技术社区  ›  Doua Beri

用于在其他目录中编译的typescript API

  •  1
  • Doua Beri  · 技术社区  · 6 年前

    我有一个包含多个 .ts 文件夹。 我想编译那些 TS .js 并将它们放在不同的目录中。

    我受到本页示例的启发: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API

    function compile(fileNames, options) {
        let program = ts.createProgram(fileNames, options);
        let emitResult = program.emit();
    
        let allDiagnostics = ts
            .getPreEmitDiagnostics(program)
            .concat(emitResult.diagnostics);
    
        allDiagnostics.forEach(diagnostic => {
            if (diagnostic.file) {
                let {line, character} = diagnostic.file.getLineAndCharacterOfPosition(
                    diagnostic.start
                );
                let message = ts.flattenDiagnosticMessageText(
                    diagnostic.messageText,
                    "\n"
                );
                console.log(
                    `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`
                );
            } else {
                console.log(
                    `${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`
                );
            }
        });
    
        let exitCode = emitResult.emitSkipped ? 1 : 0;
        console.log(`Process exiting with code '${exitCode}'.`);
        process.exit(exitCode);
    }
    

    然后我用 outDir 在编译器选项对象中

    compile(["test.ts"], {
        noEmitOnError: false,
        noImplicitAny: false,
        target: ts.ScriptTarget.ES5,
        module: ts.ModuleKind.ESNext,
        outDir: "output" // files are placed in a directory called output
    });
    

    不过,我很好奇是否有不同的方法来决定在哪里写 JS 不涉及的文件 Outdir 编译器选项的属性。

    注: 我在找一个使用typescript API而不是命令行的答案 tsc

    1 回复  |  直到 6 年前
        1
  •  1
  •   artem    6 年前

    第二个论点 emit() writeFile?: WriteFileCallback . 您可以尝试使用它,并以您需要的任何方式自己编写文件:

    program.emit(undefined, yourOwnWriteFileFunction);
    

    此外,根据评论和规范, ts.createProgram() 接受的自定义实现 CompilerHost 也可以覆盖 writeFile :

    export interface CompilerHost extends ModuleResolutionHost {
        ....
        writeFile: WriteFileCallback;
    

    Relevant part of Program interface

    export interface Program extends ScriptReferenceHost {
    
        /**
         * Emits the JavaScript and declaration files.  If targetSourceFile is not specified, then
         * the JavaScript and declaration files will be produced for all the files in this program.
         * If targetSourceFile is specified, then only the JavaScript and declaration for that
         * specific file will be generated.
         *
         * If writeFile is not specified then the writeFile callback from the compiler host will be
         * used for writing the JavaScript and declaration files.  Otherwise, the writeFile parameter
         * will be invoked when writing the JavaScript and declaration files.
         */
        emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
    

    请注意,API的这些部分没有记录在typescript wiki上,因此如果您决定使用它们,应该密切关注 breaking API changes page .

    推荐文章