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

新手上路,请多包涵

我对 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。

原文由 TheGuyWithTheFace 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 699
2 个回答

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

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

原始答案

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

从命令行文档

--outDir DIRECTORY Redirect output structure to the directory.

原文由 Bruno Grieder 发布,翻译遵循 CC BY-SA 4.0 许可协议

以下命令为我完成了它:

tsc index.ts --outDir .temp && node .temp/index.js && rm -rf .temp

原文由 M Omer Sharif Bhatti 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题