![](/img/trans.png)
[英]Visual Studio 2012: compile more TypeScript files to one JavaScript file
[英]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.