简体   繁体   English

带有多个包和源映射的gulp-typescript

[英]gulp-typescript with multiple bundles and sourcemaps

I'm working on a typescript project where I need to create two different JS bundles, one bundle for admin and another bundle for everything else and generate source maps for each bundle. 我正在一个打字稿项目上,我需要创建两个不同的JS捆绑包,一个捆绑包用于admin,另一个捆绑包用于其他所有东西,并为每个捆绑包生成源映射。 I've been trying to clone the js stream from gulp-typescript then filter out the files that are relevant to a bundle and then merge the streams and write the source maps. 我一直在尝试从gulp-typescript克隆js流,然后过滤掉与包相关的文件,然后合并流并编写源映射。 However after running the task the bundles are not created. 但是,在运行任务之后,将不会创建捆绑包。

gulpfile.js gulpfile.js

var config = {
    src: {
        ts:['./Content/Scripts/TypeScript/**/*.ts']
    },
    bundles: {
        dashboard: [
             '**/Controller/Widgets/**/*.js',
             '**/Services/Widgets/**/*.js'
        ],
        core: [
             '**/Common/**/*.js',
             '**/Server/**/*.js',
             '**/Controller/Search/**/*.js'
        ]
    }
}
gulp.task('build-ts', function () {
    var tsResult = gulp.src(config.src.ts)
        .pipe(srcMap.init())
        .pipe(tsProj());

    var bundleStreams = [
        tsResult.js
            .pipe(clone())
            .pipe(filter(config.bundles.core))
            .pipe(concat(config.outFiles.bundles.core)),
        tsResult.js
            .pipe(clone())
            .pipe(filter(config.bundles.dashboard))
            .pipe(concat(config.outFiles.bundles.dashboard))
    ];
    return merge([            
        merge(bundleStreams)           
            .pipe(srcMap.write())
            .pipe(gulp.dest(config.dist.main)),       
        tsResult.dts
            .pipe(concat(config.outFiles.typeDef))
            .pipe(gulp.dest(config.dist.main))       
    ]);
}); 

package.json 的package.json

{ 
    "devDependencies": {
        "gulp": "^3.9.1",
        "gulp-clone": "^1.0.0",
        "gulp-concat": "^2.6.1",
        "gulp-filter": "^5.0.0",
        "gulp-sourcemaps": "^2.4.1",
        "gulp-typescript": "^3.1.4",
        "merge2": "^1.0.3",
        "typescript": "2.2.1"
    },
}

Note: the source typescript files are using namespaces not modules. 注意:源打字稿文件使用的是名称空间而不是模块。

A quick and dirt approach on this would be to compile each of the bundles independently, by providing to the gulp.src() only the files needed for each bundle. 一种快速有效的方法是通过向gulp.src()仅提供每个捆绑包所需的文件来独立地编译每个捆绑包。 The typescript compiler will do the rest by fetching all needed dependencies that you did not provided on gulp.src() . 类型脚本编译器将通过获取gulp.src()上未提供的所有必需依赖项来完成其余工作。

The approach may lead to duplicated code on each of the bundles. 该方法可能导致每个捆绑包上的代码重复。 If that is an issue for you, my approach would be to create two distinct projects (one for admin and another for the everything else). 如果这对您来说是个问题,我的方法是创建两个不同的项目(一个用于管理员,另一个用于其他所有项目)。 You probably may have shared code between the two bundles so, if that is the case, I would create a third project to have this shared code. 您可能在两个捆绑软件之间共享了代码,因此,如果是这样,我将创建第三个项目以拥有此共享代码。

Each project can have it's own bundle and respective sourcemaps. 每个项目都可以拥有自己的捆绑包和各自的源映射。 You can think of each project as a different npm package. 您可以将每个项目视为不同的npm软件包。

Hope that this helps you, Best regards, jpsfs 希望这对您有帮助,最好的问候,jpsfs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM