typescript tsconfig.json - 仅从文件夹构建 ts 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34117191/
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
tsconfig.json - Only build ts files from folder
提问by Michael Crook
I am currently trying to build my ts files into a single ts files. The issue I'm getting is that my code below isn't doing what I thought it would. I used sourceRoot to attempt to set the only place it could get the source from but that didn't work. I have also tried putting a . infront of the directory but it still pulls from everywhere :(
我目前正在尝试将我的 ts 文件构建为单个 ts 文件。我遇到的问题是我下面的代码没有按照我的想法去做。我使用 sourceRoot 尝试设置它可以从中获取源的唯一位置,但这不起作用。我也试过把 . 在目录前面,但它仍然从任何地方拉:(
{
"compilerOptions": {
"target": "ES5",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"out": "www/js/app.js",
"sourceMap": true,
"sourceRoot": "www/app"
}
}
all files including those not inside of www/app build :(
所有文件,包括那些不在 www/app build 内的文件:(
for now I've moved back to manually specifying the files:
现在我已经回到手动指定文件:
{
"compilerOptions": {
"target": "ES5",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"out": "www/js/app.js",
"sourceMap": true
},
"files": [
"./www/app/app.ts",
"./www/app/menu/menuController.ts",
"./www/app/playlists/playlistsController.ts"
]
}
is it possible to restrict the source directories to be only www/app?
是否可以将源目录限制为仅 www/app?
Thanks
谢谢
采纳答案by Eugene Bichel
Yes,it is possible. Please use rootDir
like 'rootDir': 'app'
, if www
is your root dir of your application.
对的,这是可能的。请使用rootDir
like 'rootDir': 'app'
,如果www
是您的应用程序的根目录。
rootDir
description from typescript compiler options:
rootDir
来自打字稿编译器选项的描述:
Specifies the root directory of input files.
指定输入文件的根目录。
回答by TSV
According to the tsconfig schema:
根据 tsconfig架构:
"If no 'files' property is present in a tsconfig.json, the compiler defaults to including all files the containing directory and subdirectories. When a 'files' property is specified, only those files are included."
"If no 'files' property is present in a tsconfig.json, but an 'exclude' property is present, the compiler will exclude the files and folders specified in the 'exclude' property."
“如果 tsconfig.json 中不存在 'files' 属性,则编译器默认包含包含目录和子目录的所有文件。当指定了 'files' 属性时,仅包含这些文件。”
“如果 tsconfig.json 中不存在 'files' 属性,但存在 'exclude' 属性,则编译器将排除在 'exclude' 属性中指定的文件和文件夹。”
According to the description:
根据描述:
The "files" property cannot be used in conjunction with the "exclude" property. If both are specified then the "files" property takes precedence.
“files”属性不能与“exclude”属性一起使用。如果两者都指定,则“文件”属性优先。
The issue 'support globs in tsconfig.json files property (or just file/directory exclusions)'reflects current situation.
该问题“在tsconfig.json文件属性(或只是文件/目录排除)支持水珠”反映了当前的局面。
回答by ford04
This question is a bit older, I know. But some of the TypeScript compiler options mentioned here are not necessarily helpful to solve the question. For people searching rootDir
or similar (like me), it may be helpful to clarify the mentioned and the solution-relevant options.
这个问题有点老了,我知道。但是这里提到的一些 TypeScript 编译器选项不一定有助于解决问题。对于搜索rootDir
或类似的人(像我一样),澄清提到的和解决方案相关的选项可能会有所帮助。
Emit all files into one single file
将所有文件发送到一个文件中
? Use outFile
? 用outFile
? Don't use out
(deprecated)
? 不要使用out
(已弃用)
Background:
背景:
If you want to to build to a destination directory, choose outDir
. See compiler optionsfor further infos.
如果要构建到目标目录,请选择outDir
. 有关更多信息,请参阅编译器选项。
Only emit files from a certain directory
只从某个目录发出文件
? Use files
/include
/exclude
in tsconfig
? 使用files
/ include
/exclude
在tsconfig
? Don't use rootDir
? 不要使用rootDir
Explanation:
解释:
The compiler finds all input files by
编译器通过以下方式找到所有输入文件
- looking at
file
/include
/exclude
properties - following
import
statements - following
///<reference .. />
(should not matter so much anymore)
- 看着
file
/include
/exclude
性能 - 以下
import
陈述 - 关注
///<reference .. />
(应该不再那么重要了)
If those options are not specified, all files in your TypeScript project root (given by tsconfig.json) will be included. The import
ed modules are automaticallyincluded by the compiler, regardless of file
/include
/exclude
- have a look at their FAQ. All input files combined are the envelope of files it will build. How to configure file
/include
/exclude
, see tsconfig.json docs, consider also @TSV's answer.
如果未指定这些选项,则将包含 TypeScript 项目根目录(由 tsconfig.json 提供)中的所有文件。该import
编辑模块自动由编译器包括,无论file
/ include
/ exclude
-看看他们的常见问题。合并的所有输入文件是它将构建的文件的信封。如何配置file
/ include
/exclude
见tsconfig.json文档,也可以考虑@ TSV的答案。
rootDir
根目录
rootDir
controls the outputdirectory structure alongside with outDir
, it is notused to specify the compiler input. Its usage is (quote):
rootDir
与 一起控制输出目录结构outDir
,它不用于指定编译器输入。它的用法是(引用):
For every input file (i.e. a .ts/.tsx file) it needs to generate an matching output (a .js/.jsx file). To figure out the file path of the generated output file it will chop off the "rootDir" from the input, then prepend the "outDir" to it.
对于每个输入文件(即 .ts/.tsx 文件),它需要生成匹配的输出(.js/.jsx 文件)。要找出生成的输出文件的文件路径,它将从输入中切掉“rootDir”,然后在其前面加上“outDir”。
Consequently rootDir
needs a directory that includes all your input sources from above, otherwise you get
因此rootDir
需要一个包含上面所有输入源的目录,否则你会得到
error TS6059: File is not under 'rootDir' .. 'rootDir' is expected to contain all source files
错误 TS6059:文件不在“rootDir”下..“rootDir”应包含所有源文件
If rootDir
is omitted, the compiler automatically calculates a suitable directory by considering all input files at hand. So it doesn't necessarily have to be set.
如果rootDir
省略,编译器会通过考虑手头的所有输入文件自动计算合适的目录。所以不一定要设置。
sourceRoot
源根
This option is only relevant with sourcemaps/debugging and can be omitted for the scope of the question. It is used, when your sources files are at a different location at runtime than at design time. The compiler will adjust the paths to the sources in the sourcemap file to match the paths at runtime (compiler options).
此选项仅与源映射/调试相关,可以在问题范围内省略。当您的源文件在运行时与设计时位于不同位置时,将使用它。编译器将调整源映射文件中源的路径以匹配运行时的路径(编译器选项)。
Hope, that clarifies things a bit.
希望,这可以澄清一些事情。
回答by XQi
The rootDir
is just used to control the output directory structure.
的rootDir
只是用于控制输出的目录结构。
The output directory structure will be similar to your rootDir
's directory.
U can use the glob-like file patterns
to restrict your source directories:
输出目录结构将类似于您rootDir
的目录。你可以使用glob-like file patterns
来限制你的源目录:
* matches zero or more characters (excluding directory separators)
? matches any one character (excluding directory separators)
**/ recursively matches any subdirectory