为什么 TypeScript 编译器会忽略 tsconfig.json? [英] Why is the TypeScript compiler ignoring tsconfig.json?

查看:18
本文介绍了为什么 TypeScript 编译器会忽略 tsconfig.json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个文件,是从教程中粘贴的(让我们面对现实吧,文档、教程和示例之间的差异令人震惊):

I have this file, pasted from a tutorial (and let's face it, the disparity between docs, tuts, and examples is astounding):

/scripts/tsconfig.json:

/scripts/tsconfig.json:

{
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "module": "commonjs",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "outDir": "../wwwroot/appScripts/",
        "removeComments": false,
        "sourceMap": true,
        "target": "es5",
        "moduleResolution": "node"
    },
    "exclude": [
        "node_modules",
        "typings/index",
        "typings/index.d.ts"
    ]
}

选项设置为在保存时编译,但每当我保存 TypeScript 文件时,JavaScript 输出都会在源文件下方"或附加到"源文件中结束:

Options are set to compile on save, but whenever I save a TypeScript file, the JavaScript output ends up 'under', or 'attached to', the source file:

TypeScript
|
--test.ts 
    |
    --test.js

,它在物理上与源代码 /TypeScript 位于同一目录中.如果 tsconfig.json 丢失,编译器会抱怨,但是当它存在时,编译器会忽略 "outDir": "../wwwroot/appScripts/"设置.

, and that is physically in the same directory as the source, /TypeScript. If tsconfig.json is missing, the compiler complains, but when it's present, and it definitely is, the compiler ignores the "outDir": "../wwwroot/appScripts/" setting.

我真的是 Gulp 的新手,但 Gulp 任务对我来说看起来不错:

I am really to new to Gulp, but the Gulp task looks OK to me:

var tsProject = ts.createProject('scripts/tsconfig.json');
gulp.task('ts', function (done) {
    //var tsResult = tsProject.src()
    var tsResult = gulp.src([
            "scripts/*.ts"
    ])
        .pipe(ts(tsProject), undefined, ts.reporter.fullReporter());
    return tsResult.js.pipe(gulp.dest('./wwwroot/appScripts'));
});

推荐答案

选项设置为保存时编译

Options are set to compile on save

当您保存文件时,它会自动编译该单个文件和导入该文件的文件.关闭 IDE 中的自动编译选项,因此编译器会考虑 tsconfig.json 文件.

When you save a file it is automatically compiling that single file and files imported on that file. Turn off auto compile option from your IDE, so compiler will consider tsconfig.json file.

在命令行中指定输入文件时,tsconfig.json 文件被忽略.

目录中存在 tsconfig.json 文件表明该目录是 TypeScript 项目的根目录.tsconfig.json 文件指定编译项目所需的根文件和编译器选项.项目通过以下方式之一编译:

The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project. A project is compiled in one of the following ways:

使用 tsconfig.json

Using tsconfig.json

  1. 通过在没有输入文件的情况下调用 tsc,在这种情况下,编译器会从当前目录开始搜索 tsconfig.json 文件并继续沿父目录链向上.

  1. By invoking tsc with no input files, in which case the compiler searches for the tsconfig.json file starting in the current directory and continuing up the parent directory chain.

通过在没有输入文件和 --project(或仅 -p)命令行选项的情况下调用 tsc,该选项指定包含 tsconfig.json 文件的目录的路径.

By invoking tsc with no input files and a --project (or just -p) command line option that specifies the path of a directory containing a tsconfig.json file.

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

这篇关于为什么 TypeScript 编译器会忽略 tsconfig.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆