Typescript Tsconfig和outDir问题
我对tsconfig.json中的outDir有问题 我有以下tsconfig文件:Typescript Tsconfig和outDir问题,typescript,tsconfig,Typescript,Tsconfig,我对tsconfig.json中的outDir有问题 我有以下tsconfig文件: { "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "remo
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"outDir": "dist",
"outFile": "App.js"
},
"exclude": [
"node_modules",
"local_typings"
]
}
这些文件被编译为App.js,但不是在我指定的目录(dist)中,而是在tsconfig文件所在的目录中。有人找到了这个问题的解决办法吗?事实上,我发现了问题所在。我觉得这很有趣:在
outFile
中指定文件路径和文件名就足够了,而不需要使用outDir
。因此,在这种情况下,这将是:
"outFile":"dist/App.js"
祝你过得愉快 一个文件或多个文件使用“outDir”:“/dist”不幸的是,TypeScript(至少从3.3版开始)不同时支持outDir
和outFile
我也希望保持我的源目录中没有中间构建文件,同时仍然使用TypeScript将我的代码捆绑到单个文件中,而不需要向第三方实用程序(如webpack、parcel或rollup)提供支持,然后将其所有输出合并到单个文件中,但遗憾的是,(目前)不需要这样做
(不过,要简单地在不同的目录中生成捆绑输出,是的,outFile:“subdir/bundle.js”
会很好地工作。)对于任何想从模块而不是模块/dist/anywhere导入的人
我找到了一个相当简单的解决办法
首先,我的文件夹结构是这样的
- project
|- package.json
|- app
||- package.json
||- ...
|- api
||- package.json
||- ...
|- shared
||- package.json
||- tsconfig.json
||- index.ts
||- src
|||- MySharedResource.ts
|||- MySharedSomething.ts
...
(shared是在api和应用程序中本地安装的npm包)
我希望能够使用简单的
从“共享”导入{MySharedResource}代码>
方法是从MySharedResource.ts
和MySharedSomething.ts
export interface MySharedResource {
...
}
然后将其从index.ts
导出,如下所示:
export * from "./src/MySharedResource";
export * from "./src/MySharedSomething";
重要提示
您的/shared中的tsconfig.json
应该将outDir选项设置为/dist
(例如),以便index.ts
被编译为/shared/dist/index.js
,所有其他内容被编译为/shared/dist/src。
另外,在package.json
集中:
"main":"dist/index.js"
附注1
每个文件可以导出多个内容,但如果您希望像这样导入
import { Thing } from 'shared/Thing';
然后,您需要在index.ts
旁边有另一个文件(在本例中为Thing.ts
),其中包含以下内容:
export * from "./src/Task";
附注2
你也可以这样做
export * as Thing from "./src/Thing";
如果您有50个.ts
文件,outFile会将所有ts文件合并为一个文件