简体   繁体   English

Intellij Idea-将Typescript编译为Javascript并保留目录结构

[英]Intellij Idea - compiling Typescript to Javascript and keeping directory structure

I'm trying to get Intellij Idea to keep the directory structure when compiling Typescript to Javascript. 我正在尝试让Intellij Idea保留将Typescript编译为Javascript时的目录结构。 My directory structure is as follows: 我的目录结构如下:

root
- ts
  - SomeClass1.ts
  - SomeFolder
    - AwesomeClass2.ts
- tsc

I want the resulting compiled files to be: 我希望生成的编译文件为:

root
- tsc
  - SomeClass1.js
  - SomeFolder
    - AwesomeClass2.js

我希望它看起来像

This is my Typescript configuration: 这是我的Typescript配置:

打字稿配置

打字稿安装

However when trying to compile, I'm getting this error in the console: 但是,当尝试编译时,我在控制台中收到此错误:

打字稿编译错误

  • If I set "Use output path" to "tsc", it works, but the directory structure is gone and that leads to RequireJS to not find the files anymore. 如果将“使用输出路径”设置为“ tsc”,则可以使用,但是目录结构消失了,这导致RequireJS不再找到文件。
  • If I set "Use output path" to "tsc\\$FileDirRelativeToSourcepath$", I get the "cannot read the property 'replace' of undefined". 如果将“使用输出路径”设置为“ tsc \\ $ FileDirRelativeToSourcepath $”,则会得到“无法读取未定义的属性'replace'”。
  • If I disable "Use output path" and use "--outDir" with "Command line options", the files do compile into the right directory, but IntelliJ no longer recompiles files automatically if they've been changed. 如果禁用“使用输出路径”并将“ --outDir”与“命令行选项”一起使用,则文件确实会编译到正确的目录中,但是如果更改了IntelliJ,它们将不再自动重新编译文件。

UPDATE, solution by Nitzan Tomer: 更新,Nitzan Tomer解决方案:

Create tsconfig.json in the root directory: 在根目录中创建tsconfig.json:

{
  "compilerOptions": {
    "module": "amd",
    "target": "es5",
    "outDir": "tsc",
    "rootDir": "ts",
    "sourceMap": true,
    "declaration": true
  },
  "exclude": [
    "tsc"
  ]
}

and enable "use tsconfig.json" in IntelliJ. 并在IntelliJ中启用“ use tsconfig.json”。

If you use tsconfig.json you can specify the rootDir and outDir , if you do that then it will keep the directory structure of the rootDir in the compiled version inside the outDir . 如果你使用tsconfig.json可以指定rootDiroutDir ,如果你这样做,那么它将保持的目录结构rootDir在内部编译版本outDir

In your case: 在您的情况下:

{
    "compilerOptions": {
        "outDir": "tsc",
        "rootDir": "ts",
        "sourceMap": true,
        "declaration": true
    }
}

Place this file as tsconfig.json in your root directory and in intellij/webstorm Enable Typescript Compiler and then Use tsconfig.json . 将此文件作为tsconfig.json放在您的root目录中,并在intellij / webstorm Enable Typescript Compiler ,然后Use tsconfig.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM