program tip

TypeScript-컴파일 된 파일을 별도의 디렉토리에 보관하는 방법은 무엇입니까?

radiobox 2020. 8. 22. 08:30
반응형

TypeScript-컴파일 된 파일을 별도의 디렉토리에 보관하는 방법은 무엇입니까?


저는 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을 사용하고 있습니다.


--outDirtsc 의 옵션 사용 (IntelliJ의 파일 감시자 내에서 구성됨)

명령 줄 문서에서

--outDir DIRECTORY Redirect output structure to the directory.

편집하다

Since Typescript 1.5, this can also be set in the tsconfig.json file:

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

Or, add "outDir": "build"to tsconfig.json file


Though these answers are correct you should consider whether you actually just want to hide your .js files from your IDE.

In Visual Studio Code, go to File > Preferences > Settings or your .vscode\settings.json file and enter:

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

The above hides .js files where a corresponding .ts file exists.


If you like to map the directory structure of the app/scripts folder in js, I'd suggest using the following settings for your 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

I setup package.json like this so that typing npm run start outputs everything to build. The source files are kept in src. The outfile is specified by --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"
  }
}

You can exclude your build directory in tsconfig.json, though it probably isn't necessary, since there is only JS there:

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

Intellij Users, compile Typescript to multiple output directories

For Intellij users this may be useful. This was how I got this to work using the built in Typescript Compiler.

Environment Info

Example Directory Structure

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

  • File -> Settings -> Typescript
  • Node Interpreter: Your NodeJS Install Path
  • Compiler Version: typically located at C:\yourUserName\AppData\Roaming\npm\node_modules\typescript\lib
  • Command Line Options: -m amd -t ES6 -outDir E:\myapp\js
  • Check compile main file only and point it to your entry file. E:\myapp\ts\main.ts If this is not checked then all of your files will try to output to your outDir path.

enter image description here


Im using Atom with the atom-typescript extension and my tsconfig.json looks like this:

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

Regarding Grunt, currently to save your dev folder project structure in production and not get an unexpected result after files have compiled, please refer to the option:

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

See the rootDir option in official grunt-ts docs.

I hope it will help someone if they get stuck and get a weird result in production.

참고URL : https://stackoverflow.com/questions/24454371/typescript-how-to-keep-compiled-files-in-a-separate-directory

반응형