首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在TypeScript项目中编译ASP.NET5项目

在TypeScript项目中编译ASP.NET5项目
EN

Stack Overflow用户
提问于 2015-10-16 08:39:59
回答 2查看 415关注 0票数 2

我安装了最新的ASP.NET5 beta8并创建了新项目。在项目内部,我创建了一个新文件夹,添加了tsproject.json文件,并编写了一些TypeScript文件.ts。

我想要自动编译我的TypeScript子项目.我该怎么办?我是否应该编写某种普通任务,在VS中启用自动TypeScript文件编译(但它会使用tsconfig.json设置来编译文件夹中的所有文件吗??)?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-16 11:29:47

tsconfig.json文件添加到项目的根目录中:

代码语言:javascript
复制
{
  // 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

代码语言:javascript
复制
{
    // Omitted...
    "devDependencies": {
        // Omitted...
        "gulp-concat": "2.6.0",
        "gulp-typescript": "2.9.2",
        "merge-stream": "1.0.0"
    }
}

在您的gulpfile.js文件中,添加一个新的build TypeScript任务(我向您展示了如何进行增量构建,以加快构建时间,因此这有点高级,如果您想查看更简单的内容,可以查看gulpfile.js文档):

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-10-17 19:14:10

顺便说一句,我可以使用几个tsconfig.json并将它们放在子文件夹中吗?

-p ./子文件夹

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33165998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档