簡體   English   中英

Typescript:在Visual Studio 2013中編譯為多個.js文件

[英]Typescript: compile to more than one .js file in Visual Studio 2013

我有一些用TypeScript編寫的大型代碼庫項目。 由於多種原因,我想將同一項目的.ts文件編譯成幾個.js文件。 我找不到方法,只能找到2個著名的項目設置:將每個.ts文件編譯為一個.js文件,並將所有.ts文件編譯為單個.js文件。

例如,假設我有A.ts,B.ts,C.ts,D.ts,並且想要生成ABC.js和CD.js

有沒有辦法使用VS 2013或任何附加工具來做到這一點?

使用Visual Studio選項或現有外接程序無法實現。 您可以在外部使用grunt之類的東西來做到這一點: https : //github.com/TypeStrong/grunt-ts具有2個單獨的構建目標。

我掙扎了很久。 我的解決方案是刪除所有

/// <reference path="sometsfile.ts">
形成文件(這樣,項目中的所有打字稿文件都可以互相看到)。

然后啟用選項以分別編譯每個打字稿。 下一步使用grunt-concat(不幸的是,您必須提供文件夾的順序才能起作用)並將模塊文件中的每個js文件合並為一個。

如果文件發生更改,您可以使用grunt watch來連接每個模塊,這樣就不必再編譯所有項目了:)

此外,Visual Studio Typescript工具還將直接在Visual Studio錯誤列表中顯示任何錯誤。

我的示例grunt配置:

 module.exports = function (grunt) { grunt.initConfig({ concat: { options: { separator: '\\n', }, app: { src: [ 'Src/Application/Modules/**/*.js', 'Src/Application/Config/*.js', 'Src/Application/*.js' ], dest: 'Scripts/App/App.js' }, ui: { src: ['Src/UI/**/*.js'], dest: 'Scripts/App/UI.js' } }, watch: { app: { files: ['Src/Application/**/*.js'], tasks: ['concat:app'], options: { nospawn: true } }, ui: { files: ['Src/UI/**/*.js'], tasks: ['concat:ui'], options: { nospawn: true } } } }); grunt.loadNpmTasks('grunt-contrib-concat'); grunt.loadNpmTasks('grunt-contrib-watch'); }; 

關於basarat響應,我想指出的是,如果您使用grunt-ts,則必須在文件中提供引用,並且引用將自動解析 這意味着,如果您的模塊A引用了模塊B中的文件,則該文件將包含在您的out文件A.js中

這是我的做法

1) create a _references.ts in your project root folder 
2) include references of your ts files in the order you want compilation by the ///<reference syntax
3) go to your typescript project properties -> typescript build option and check the combine javascript output file and mention the file name 
4) done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM