简体   繁体   English

Gulp中的TypeScript + Browserify + SourceMaps?

[英]TypeScript + Browserify + SourceMaps in Gulp?

Hi this issue is stumping me. 嗨,这个问题让我很沮丧。

I want to know if, after compiling from TS and using Browserify, I can get my SourceMaps (from gulp-sourcemaps) to point all the way back to my TS files. 我想知道从TS编译并使用Browserify后,是否可以获取SourceMaps(来自gulp-sourcemaps),一直指向我的TS文件。

Currently I have it working so that I use tsify to compile the TS then I bundle it all into an all.js then uglify (minify) it into an all.min.js. 目前,我正在使用它,以便我使用tsify来编译TS,然后将其全部捆绑到all.js中,然后将其uglify(缩小)到all.min.js中。 I also have the SourceMaps but only to map from the minified version to the all.js. 我也有SourceMaps,但仅用于从缩小版本映射到all.js。

I have searched quite a bit for this. 我已经搜索了很多。 I have also done the SourceMaps before from JS minified to my TS but I was not using Browserify in that case. 从JS缩小到TS之前,我还完成了SourceMaps,但在这种情况下我没有使用Browserify。

My current working Gulp task: 我当前的工作Gulp任务:

gulp.task('scripts', function(){
    return browserify(paths.mainJs)
        .plugin(tsify)
        .bundle()
        .on('error',console.error.bind(console))
        .pipe(source('all.js'))
        .pipe(buffer())
        .pipe(sourcemaps.init())
        .pipe(gulp.dest(paths.outscripts))
        .pipe(rename('all.min.js'))
        .pipe(uglify())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(paths.outscripts));
});

Note that a big issue here is that everything between the sourcemaps calls need to have support for gulp-sourcemaps, which Browserify does not. 请注意,这里的一个大问题是调用sourcemaps之间的所有内容都需要支持gulp-sourcemaps,而Browserify则不需要。 Gulp does have a Typescript compiler as well but then how would I use Browserify? Gulp也有Typescript编译器,但是我将如何使用Browserify?

Thanks! 谢谢!

I figured it out!! 我想到了!! After hours.... 下班后....

This is it: 就是这个:

    gulp.task('scripts', function(){
    return browserify(paths.mainTs,{debug: true})
        .on('error',console.error.bind(console))
        .plugin(tsify)
        .bundle()
        .pipe(source('all.js'))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(gulp.dest(paths.outscripts))
        .pipe(rename('all.min.js'))
        .pipe(uglify())
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(paths.outscripts));
});

paths.mainTs is the entry TS file (main.ts, index.ts, app.ts, whatever). path.mainTs是条目TS文件(main.ts,index.ts,app.ts等)。

The debug flag tell Browserify to write source maps. 调试标志告诉Browserify编写源映射。

I then load those maps before minifying and then write them on the min version. 然后,在缩小之前加载这些地图,然后在最小版本上编写它们。

Note that if you're using the full js, rather than minified, while debugging then you can just skip the two lines with sourcemaps. 请注意,如果您在调试时使用的是完整的js(而不是最小化的js),则只需跳过带有源图的两行。

EDIT: To avoid a large JS file at the end (since it includes inline sourcemaps), just sourcemaps.write('/somePath'). 编辑:为了避免最后出现一个较大的JS文件(因为它包括内联源地图),只需sourcemaps.write('/ somePath')。

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

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