我怎样才能得到类型编译器输出编译后的 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 文件保持与。它的文件,我不想跟踪。Js 文件,因此我希望将所有 JavaScript 文件保存在一个单独的目录树中(然后可以添加到该目录树中)。就像这样:

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。

133216 次浏览

由于类型1.5,这也可以在 tsconfig.json文件中设置:

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

原始答案

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

来自命令行文档

--outDir DIRECTORY Redirect output structure to the directory.

如果你想在 js 中映射 app/script 文件夹的目录结构,我建议你对你的文件监视器使用以下设置:

Arguments: --sourcemap --outDir $ProjectFileDir$/js/$FileDirPathFromParent(scripts)$ $FileName$
Working Directory: $FileDir$
Output Paths To Refresh: $ProjectFileDir$/js/$FileDirPathFromParent(scripts)$/$FileNameWithoutExtension$.js:$ProjectFileDir$/js/$FileDirPathFromParent(scripts)$/$FileNameWithoutExtension$.js.map

Intellij 用户,将 Type 脚本编译到多个输出目录

对于 Intellij 用户,这可能是有用的。这就是我如何使用内置的类型脚本编译器使其工作。

环境资讯

  • 视窗7
  • 使用 NPM Version 1.7.3的类型脚本
  • 情报14
  • ES6
  • 使用 RequreJS

目录结构示例

BEFORE COMPILE
----------------------------------------
-> JS
-> app
-> config.js  //this is not generated
-> libs
-> jquery.js  //this is not generated
-> plugins
-> TS
-> app
-> main.ts
-> libs
-> jquery.d.ts
-> plugins
-> somePlugin.ts


AFTER COMPILE
----------------------------------------
-> JS
-> app
-> config.js  //this is not generated
-> main.js
-> libs
-> jquery.js  //this is not generated
-> plugins
somePlugin.ts
-> TS
-> app
-> main.ts
-> libs
-> jquery.d.ts    //this is where I kept my definition files
-> plugins
-> somePlugin.ts

情报设置

  • 文件-> 设置-> 打字稿
  • 节点解释器: 您的 NodeJS 安装路径
  • 编译器版本: 通常位于 C:\yourUserName\AppData\Roaming\npm\node_modules\typescript\lib
  • 命令行选项: -m amd -t ES6 -outDir E:\myapp\js
  • 只检查编译主文件并将其指向您的条目文件。如果没有选中这个选项,那么所有的文件都会尝试输出到 outDir 路径。

enter image description here

我像这样设置 package.json,以便键入 npm run start将所有内容输出到 build。源文件保存在 src中。输出文件由 --outDir build指定。

{
"name": "myapp",
"version": "0.0.1",
"scripts": {
"tsc": "tsc",
"tsc:w": "tsc -w --outDir build",
"lite": "lite-server",
"start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
},
"license": "private",
"dependencies": {
"angular2": "2.0.0-beta.0",
"systemjs": "0.19.6",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.0",
"zone.js": "0.5.10"
},
"devDependencies": {
"concurrently": "^1.0.0",
"lite-server": "^1.3.1",
"typescript": "^1.7.3"
}
}

您可以在 tsconfig.json 中排除构建目录,尽管这可能没有必要,因为那里只有 JS:

{
"compilerOptions": {
"target": "ES5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules",
"build"
]
}

或者,将 "outDir": "build"添加到 tsconfig.json 文件

我使用 Atom 和 Atom-typecript 扩展,我的 tsconfig.json 看起来像这样:

{
"compilerOptions": {
"outDir":"js"
}
}

虽然这些答案是正确的,但是你应该考虑你是否真的只是想要 在 IDE 中隐藏.js 文件

在 VisualStudio 代码中,转到 File > Preferences > Settings.vscode\settings.json文件并输入:

"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/*.js" : {
"when": "$(basename).ts"
},
"**/*.js.map": {
"when": "$(basename)"
}
}

上面隐藏了存在对应的.ts 文件的.js 文件。

关于 Grunt,当前要在生产环境中保存开发文件夹项目结构,并且在文件编译后不会得到意外的结果,请参考选项:

compilerOptions: {
rootDir: 'devFolder'
// ...
}

请参阅官方 grunt-ts 文档中的 选项。

我希望这将有助于某人,如果他们卡住了,得到一个奇怪的结果在生产。

下面的命令为我做到了这一点:
tsc index.ts --outDir .temp && node .temp/index.js && rm -rf .temp