2015-10-16 138 views
2

我安装了最新的ASP.NET5 beta8并创建了新项目。在项目内部,我创建了一个新文件夹,添加了tsproject.json文件并编写了一些TypeScript文件。我想要自动编译我的TypeScript子项目。我该怎么办?我应该写些什么样的咕噜任务,在VS中启用自动的TypeScript文件编译(但它会使用tsconfig.json设置来编译文件夹中的所有文件?)还是其他?编译ASP.NET5项目中的TypeScript项目

+0

你有很多的可能性。我不亲自使用视觉工作室,但我曾与grunt合作过。在个人的基础上,我更喜欢这个。我运行一个** gulp手表**并保存它会将我的ts传输给js。我已经建立了它,所以它会保持相同的文件夹结构(而不​​是将所有内容编译成一个js文件)。如果你想在visual studio + typescript + angular(或不是)上提供一些建议,我强烈建议观看这个https://www.youtube.com/watch?v=Fw3ihDINdbk并检查频道。干杯。 – rottenoats

+1

这是令人难以置信的视频!感谢您的分享! – Rem

+0

是的,它回答了我的很多问题。这个也很好。 https://www.youtube.com/watch?v=OZxnFB0yQHs。如果你找到了你的问题的答案,请记住在这篇文章中添加答案。我相信有很多人需要澄清这个问题。 – rottenoats

回答

1

一个tsconfig.json文件添加到您的项目的根:

{ 
    // TypeScript to JavaScript compilation options. Add what you want here. 
    "compilerOptions": { 
    "noImplicitAny": true, // Do not allow implicit any variables. 
    "noEmitOnError": true, // Stop processing on error. 
    "removeComments": false, // Do not remove comments. 
    "sourceMap": false,  // Do not create source map files. 
    "module": "commonjs", // Use the Common JS modules or some other format. 
    "target": "es5"   // Compile to ECMAScript 5. 
    }, 
    // Exclude the bower_components, node_modules and wwwroot folders from 
    // being scanned for TypeScript (.ts) or TypeScript definition (.d.ts) files. 
    "exclude": [ 
    "bower_components", 
    "node_modules", 
    "wwwroot" 
    ] 
} 

添加gulp-typescript编译.ts文件,gulp-concat来连接文件和merge-stream合并咕嘟咕嘟流至package.json

{ 
    // Omitted... 
    "devDependencies": { 
     // Omitted... 
     "gulp-concat": "2.6.0", 
     "gulp-typescript": "2.9.2", 
     "merge-stream": "1.0.0" 
    } 
} 

在你的gulpfile.js文件中,添加一个新的构建TypeScript任务(我展示了如何进行增量构建以缩短构建时间,所以这有点高级,你可以查看文档

var gulp = require("gulp"); 
var concat = require("gulp-concat"); 
var typescript = require("gulp-typescript"); 
var merge = require("merge-stream"); 

// A TypeScript project is used to enable faster incremental compilation, 
// rather than recompiling everything from scratch each time. Each 
// resulting compiled file has it's own project which is stored in 
// the typeScriptProjects array. 
var typeScriptProjects = []; 
function getTypeScriptProject(name) { 
    var item; 
    for (var i = 0; i < typeScriptProjects.length; ++i) { 
     if (typeScriptProjects[i].name === name) { 
      item = typeScriptProjects[i]; 
     } 
    } 

    if (item === undefined) { 
     // Use the tsconfig.json file to specify how TypeScript (.ts) 
     // files should be compiled to JavaScript (.js). 
     var project = typescript.createProject("tsconfig.json"); 
     item = { name: name, project: project }; 
     typeScriptProjects.push(item); 
    } 

    return item.project; 
} 

var sources = { 
    // An array containing objects required to build a single JavaScript file. 
    ts: [ 
     { 
      // name - The name of the final JavaScript file to build. 
      name: "main.js", 
      // paths - A single or array of paths to TypeScript files. 
      paths: [ 
       "Folder1/one.ts", 
       "Folder1/two.ts", 
       "Folder2/**/*.ts" 
      ] 
     } 
    ] 
}; 

gulp.task("build-ts", function() { 
    var tasks = sources.ts.map(function (source) { // For each set of source files in the sources. 
     return gulp        // Return the stream. 
      .src(source.paths)      // Start with the source paths. 
      .pipe(typescript(getTypeScriptProject(source))) // Compile TypeScript (.ts) to JavaScript (.js) using the specified options. 
      .pipe(concat(source.name))    // Concatenate JavaScript files into a single file with the specified name. 
      .pipe(gulp.dest("./wwwroot/js/"));  // Saves the JavaScript file to the specified destination path. 
    }); 
    return merge(tasks);       // Combine multiple streams to one and return it so the task can be chained. 
}); 
+1

感谢您的回答。并且对于优秀的ASP.NET 5 Boilerplate项目也是如此!顺便说一句我可以使用几个tsconfig.json并将它们放在子文件夹内? – Rem

+0

很酷,你用过它!我认为上面的代码已经在样板文件中,或者将在下一个版本中,所以你可以在那里看到它。我相信你可以,但我没有尝试过。你的情况是什么,需要你有多个? –

+1

我想将我的项目的不同大型模块编译为单独的js文件,并使用像AMD这样的smth按需下载它们。我认为最好在每个tsconfig.json中描述我的意图,然后将代码隐藏在gulpfile.js中的代码中 – Rem

0

BTW我可以用几个tsconfig.json,并把它们里面的子文件夹:如果你想要看的东西简单)吞掉,打字稿?

TSC -p ./subfolder