使用 Typescript 3 构建到 dist/ 文件夹时保持 src/ 文件夹结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52121725/
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
Maintain src/ folder structure when building to dist/ folder with Typescript 3
提问by nfadili
I have a typescript nodejs server with this structure:
我有一个具有以下结构的打字稿 nodejs 服务器:
tsconfig.json
package.json
src/
middleware/
utils/
index.ts
dist/
middleware/
utils/
index.ts
When using Typescript 2, I was able to transpile my project from the src/ to a dist/ folder and have a mirror image of my directory structure to work with.
使用 Typescript 2 时,我能够将我的项目从 src/ 转换到 dist/ 文件夹,并有我的目录结构的镜像可以使用。
With the release of Typescript 3 they have introduced project referencesand changed the way code is transpiled into an output directory. Now tsc
outputs to the dist/ folder in a nested way like this:
随着 Typescript 3 的发布,他们引入了项目引用并改变了代码转换到输出目录的方式。现在tsc
以嵌套的方式输出到 dist/ 文件夹,如下所示:
dist/
src/
middleware/
utils/
index.ts
My tsconfig.json is:
我的 tsconfig.json 是:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"allowJs": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"declaration": false,
"outDir": "dist/",
"lib": [
"es7",
"dom"
]
},
"include": [
"src/"
]
}
How can I configure Typescript to output my src/ folder as a mirror image into a dist/ folder?
如何配置 Typescript 将我的 src/ 文件夹作为镜像输出到 dist/ 文件夹中?
采纳答案by Luke W
I had a similar problem when initially converting to a Typescript project. I also set resolveJsonModule: true
and the src
directory was copied to the output dist
directory.
最初转换为 Typescript 项目时,我遇到了类似的问题。我还设置resolveJsonModule: true
并将src
目录复制到输出dist
目录。
The underlying reason is that one of my source files require
d package.json at the root of the project. Once i removed that, tsc no longer added src to the dist directory.
根本原因是我的源文件require
之一是项目根目录下的 package.json。一旦我删除了它,tsc 就不再将 src 添加到 dist 目录中。
In short, make sure you are not requiring files outside of your src directory.
简而言之,请确保您不需要 src 目录之外的文件。
Explanatory FAQ here: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-does---outdir-moves-output-after-adding-a-new-file
解释性常见问题解答:https: //github.com/Microsoft/TypeScript/wiki/FAQ#why-does---outdir-moves-output-after-adding-a-new-file
回答by Matt McCutchen
The upgrade from TypeScript 2 to 3 by itself shouldn't have changed the behavior; if we can confirm that it did, that may be a bug. In any case, check that the rootDir
compiler option points to your src
directory and not to the parent directory, because the structure under the rootDir
is what is mirrored under the outDir
.
从 TypeScript 2 升级到 3 本身不应该改变行为;如果我们可以确认它确实如此,那可能是一个错误。在任何情况下,检查rootDir
编译器选项指向你的src
目录,而不是父目录,因为下的结构rootDir
是什么是下的镜像outDir
。
回答by Omega Cube
The problem appears after adding the resolveJsonModule: true
to tsconfig.json
添加resolveJsonModule: true
到tsconfig.json后出现问题
回答by Morlo Mbakop
The structure of the output directory is controlled by the rootDir
of the compilerOptions
. See documentation here, setting it to ./src
should solve the issue.
输出目录的结构是由受控rootDir
的compilerOptions
。请参阅此处的文档,将其设置为./src
应该可以解决问题。
{
"compilerOptions": {
"rootDir": "src",
...
},
"include": [
"src/"
]
}