小编典典

如何让 Typescript 编译器将编译后的 js 输出到不同的目录?

all

我对 TypeScript 还很陌生,现在我的项目结构中的几个地方都有 .ts 文件:

app/
 |-scripts/
    |-app.ts
    |
    |-classes/
    |  |-classA.ts
    |  |-classB.ts
    |  
    |-controllers/
    |  |-controllerA.ts
    |  |-controllerB.ts
    |  
    |-otherStuff/
       |-otherstuffA.ts

现在,当我的文件被编译时,它们被编译到 .ts 文件所在的同一目录中:

app/
 |-scripts/
    |-app.ts
    |-app.js
    |
    |-classes/
    |  |-classA.ts
    |  |-classB.ts
    |  |-classA.js
    |  |-classB.js
    |  
    |-controllers/
    |  |-controllerA.ts
    |  |-controllerB.ts
    |  |-controllerA.js
    |  |-controllerB.js
    |  
    |-otherStuff/
       |-otherstuffA.ts
       |-otherStuffA.js

虽然我喜欢 .js 文件与 .ts 文件保持相同目录结构的方式,但我不想在 VCS 中跟踪 .js 文件,所以我想将所有 JavaScript
文件保存在一个单独的目录树(然后我可以将其添加到 .gitignore),如下所示:

app/
 |-scripts/
 |  |-app.ts
 |  |
 |  |-classes/
 |  |  |-classA.ts
 |  |  |-classB.ts
 |  |  
 |  |-controllers/
 |  |  |-controllerA.ts
 |  |  |-controllerB.ts
 |  |  
 |  |-otherStuff/
 |     |-otherstuffA.ts
 |
 |-js/
    |-app.js
    |
    |-classes/
    |  |-classA.js
    |  |-classB.js
    |
    |-controllers/
    |  |-controllerA.js
    |  |-controllerB.js
    |
    |-otherStuff/
       |-otherstuffA.js

是否有某个设置或选项可以告诉 TypeScript 编译器执行此操作?另外,我不确定它是否相关,但我正在使用 WebStorm。


阅读 156

收藏
2022-08-29

共1个答案

小编典典

从 Typescript 1.5 开始,这也可以在tsconfig.json文件中设置:

"compilerOptions": {
    "outDir": "DIRECTORY"
    ...

原始答案

使用--outDirtsc 上的选项(在 IntelliJ 的文件观察器中配置)

从命令行文档

--outDir DIRECTORY Redirect output structure to the directory.

2022-08-29