使用Typescript 3构建到dist/文件夹时,保持src/文件夹结构 [英] Maintain src/ folder structure when building to dist/ folder with Typescript 3
问题描述
我有一个具有以下结构的Typescript Node.js服务器:
I have a typescript nodejs server with this structure:
tsconfig.json
package.json
src/
middleware/
utils/
index.ts
dist/
middleware/
utils/
index.ts
使用Typescript 2时,我可以将项目从src/转换为dist/文件夹,并可以使用我的目录结构的镜像.
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 3的发布,他们引入了项目引用,并更改了将代码转换为输出目录的方式.现在tsc
以这样的嵌套方式输出到dist/文件夹:
With the release of Typescript 3 they have introduced project references and changed the way code is transpiled into an output directory. Now tsc
outputs to the dist/ folder in a nested way like this:
dist/
src/
middleware/
utils/
index.ts
我的tsconfig.json是:
My tsconfig.json is:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"allowJs": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"declaration": false,
"outDir": "dist/",
"lib": [
"es7",
"dom"
]
},
"include": [
"src/"
]
}
如何配置Typescript将src/文件夹作为镜像输出到dist/文件夹中?
How can I configure Typescript to output my src/ folder as a mirror image into a dist/ folder?
推荐答案
最初转换为Typescript项目时,我遇到了类似的问题.我还设置了resolveJsonModule: true
,并将src
目录复制到了输出dist
目录.
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.
根本原因是我的源文件之一require
d package.json位于项目的根目录.一旦我删除了该文件,TSC便不再将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.
简而言之,请确保您不需要src目录之外的文件.
此处的解释性常见问题解答: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-does---outdir-moves-output-after-adding-a-new-file
Explanatory FAQ here: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-does---outdir-moves-output-after-adding-a-new-file
这篇关于使用Typescript 3构建到dist/文件夹时,保持src/文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!