2015-12-26 69 views
29

我正在使用TS 1.7,並且正在嘗試將我的項目編譯爲一個可以包含在我的html文件中的大文件。Typescript編譯爲單個文件

我的項目結構如下:

-build // Build directory 
-src // source root 
--main.ts // my "Main" file that uses the imports my outer files 
--subDirectories with more ts files. 
-package.json 
-tsconfig.json 

我tsconfig文件是:

{ 
    "compilerOptions": { 
    "module":"amd", 
    "target": "ES5", 
    "removeComments": true, 
    "preserveConstEnums": true, 
    "outDir": "./build", 
    "outFile":"./build/build.js", 
    "sourceRoot": "./src/", 
    "rootDir": "./src/", 
    "sourceMap": true 
    } 
} 

當我建立我的項目,我期待build.js文件是一個大文件從我的編譯資源。 但是,build.js文件是空的,我得到我所有的文件編譯ojs文件。

我的每一個TS的文件看起來有點像這樣

import {blabla} from "../../bla/blas"; 

export default class bar implements someThing { 
    private variable : string; 
} 

我在做什麼錯?

回答

29

這將實施在TypeSript 1.8。對於該版本,outFile選項在模塊系統時有效。

此時該功能可用於Typescript的開發版本。 要安裝運行:

$ npm install -g [email protected] 

因爲即使它不是模塊明顯,不過outFile選項不能一起工作以前的版本。

您可以查看this問題了解更多詳情。


如果你想輸出,低於1.8版本的一個文件,你不能使用模塊選項tsconfig.json。相反,您必須使用模塊關鍵字來製作命名空間。

tsconfig.json文件應該是這樣的:

{ 
    "compilerOptions": { 
    "target": "ES5", 
    "removeComments": true, 
    "preserveConstEnums": true, 
    "outFile": "./build/build.js", 
    "sourceRoot": "./src/", 
    "rootDir": "./src/", 
    "sourceMap": true 
    } 
} 

而且您的TS文件應該是這樣的:

module SomeModule { 
    export class RaceTrack { 
    constructor(private host: Element) { 
     host.appendChild(document.createElement("canvas")); 
    } 
    } 
} 

,而不是和使用import語句你必須通過命名空間來引用導入。

window.addEventListener("load", (ev: Event) => { 
    var racetrack = new SomeModule.RaceTrack(document.getElementById("content")); 
}); 
+0

你指出,這個問題似乎解決了,也https://github.com/Microsoft/TypeScript/wiki/Compiler-Options 表明--outfile標誌作爲與使用 - 模塊標誌。 –

+0

確實,但它似乎並沒有工作。 :(也許在github上發佈一個問題? – toskv

+0

順便說一下,github上的問題的解決方案似乎並沒有指向代碼庫的任何實際更改,他們總是關閉這是默認行爲。 – toskv

相關問題