TypeScript - ¿Cómo mantener los archivos compilados en un directorio separado?


Soy bastante nuevo en TypeScript, y ahora mismo lo he hecho .archivos ts en varios lugares a través de la estructura de mi proyecto:

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

Ahora mismo, cuando se compilan mis archivos, se compilan en el mismo directorio que el .ts fles están en:

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

Mientras que me gusta la forma en que el .los archivos js mantienen la misma estructura de directorios que el .archivos ts, no quiero rastrear el .archivos js en mi VCS, así que me gustaría mantener todos mis archivos JavaScript en un árbol de directorios separado (que luego puedo añadir a .gitignore), así:

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

¿Hay una configuración u opción en algún lugar que le diga al compilador de TypeScript que haga esto? Además, no estoy seguro de si es relevante, pero estoy usando WebStorm.

Author: TheGuyWithTheFace, 2014-06-27

7 answers

Utilice la opción --outDir en tsc (configurado dentro del File Watcher en IntelliJ)

Desde la documentación de la línea de comandos

--outDir DIRECTORY Redirect output structure to the directory.

Editar

Desde Typescript 1.5, esto también se puede establecer en el archivo tsconfig.json:

"compilerOptions": {
    "outDir": "DIRECTORY"
    ...
 80
Author: Bruno Grieder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-19 07:51:37

O bien, agregue "outDir": "build" a tsconfig.json file

 28
Author: Qingshan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-13 13:11:09

Si desea mapear la estructura de directorios de la carpeta app/scripts en js, le sugiero que use la siguiente configuración para su file watcher:

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
 3
Author: lena,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-27 19:03:36

I setup package.json como este para que escribiendo npm run start sale todo a build. Los archivos fuente se guardan en src. El outfile está especificado por --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"
  }
}

Puede excluir su directorio de compilación en tsconfig.json, aunque probablemente no sea necesario, ya que solo hay JS:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "build"
  ]
}
 3
Author: Josh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-21 22:10:24

Aunque estas respuestas son correctas, debe considerar si realmente solo desea ocultar su .archivos js de su IDE .

En el código de Visual Studio, vaya a File > Preferences > Settings o a su archivo .vscode\settings.json e ingrese:

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

Lo anterior se esconde .js archivos donde un correspondiente .el archivo ts existe.

 3
Author: Dave Clark,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-18 12:29:26

Usuarios de Intellij, compilan Typescript en múltiples directorios de salida

Para los usuarios de Intellij esto puede ser útil. Así fue como conseguí que esto funcionara usando el compilador de Typescript incorporado.

Medio Ambiente

Ejemplo Estructura de directorios

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

Intellij Setup

  • Archivo - > Configuración - > Typescript
  • Intérprete de nodos: La ruta de instalación de NodeJS
  • Versión del compilador: normalmente se encuentra en C:\yourUserName\AppData\Roaming\npm\node_modules\typescript\lib
  • Opciones de la Línea de Comandos: -m amd -t ES6 -outDir E:\myapp\js
  • Compruebe compilar solo el archivo principal y apunte a su archivo de entrada. E:\myapp\ts\main.ts Si esto no está marcado, todos sus archivos intentarán generar la salida a su ruta OutDir.

introduzca la descripción de la imagen aquí

 2
Author: Kris Hollenbeck,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-15 21:44:31

Estoy usando Atom con la extensión atom-typescript y mi tsconfig.json se ve así:

{
  "compilerOptions": {
    "outDir":"js"
  }
}
 1
Author: Daveman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-07 09:49:40