将 TypeScript 与 tsconfig.json 文件一起使用时,是否可以包含 json 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42421392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:17:04  来源:igfitidea点击:

Is it possible to include a json file when using TypeScript with a tsconfig.json file?

jsontypescript

提问by Brian Vallelunga

Is there any way to have the TypeScript compiler also copy content files that don't have a ts or tsx extension to the output directory?

有没有办法让 TypeScript 编译器也将没有 ts 或 tsx 扩展名的内容文件复制到输出目录?

For example, my tsconfig.json looks like:

例如,我的 tsconfig.json 看起来像:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "../../dist",
        "types": [
        ]
    },
    "include": [
        "file1.ts",
        "config.json"
    ],
    "exclude": [
        "../../node_modules"
    ]
}

I would like the config.jsonto end up in my ../../dist directory, but this isn't happening. Is there no concept of a content file for TypeScript?

我希望config.json最终出现在我的 ../../dist 目录中,但这并没有发生。TypeScript 没有内容文件的概念吗?

采纳答案by vogtb

No, the tsccommand's job is only to compile .ts files. It doesn't have the capabilities to do build-related tasks that a build system like gulp, gruntor webpackwould handle.

不,该tsc命令的工作只是编译 .ts 文件。它没有能力做构建相关的任务,像构建系统一饮而尽咕噜的WebPack会处理。

For more information, see this similar question: Angular 2 + Typescript compiler copy html and css files

有关更多信息,请参阅此类似问题:Angular 2 + Typescript compiler copy html and css files

回答by Arun Reddy

This can be achieved by setting "resolveJsonModule": truein the compiler options and adding "source-path/**/*.json"to the includearray in the tsconfig.json

这可以通过"resolveJsonModule": true在编译器选项中设置并添加"source-path/**/*.json"include数组中来实现tsconfig.json

回答by Alex Sharp

One workaround is to make the json file a normal js file, require/import it normally, and add "allowJs" : trueto "compilerOptions" in tsconfig.json.

一种解决方法是将 json 文件设为普通的 js 文件,正常 require/import,并添加"allowJs" : true到 tsconfig.json 中的“compilerOptions”。