4

I am using gulp to bundle browserify+babelify with uglifyjs. O However, the sourcemaps generated from my project only give me the bundled version, not the bundled version.

Here is my setup:

var gulp       = require('gulp'),
    source     = require('vinyl-source-stream'),
    browserify = require('browserify'),
    gutil      = require('gulp-util'),
    buffer     = require('vinyl-buffer'),
    sourcemaps = require('gulp-sourcemaps'),
    uglify     = require('gulp-uglify'),
    file       = 'index.js';

gulp.task('build', function(){
    return browserify({
        entries: [file],
        transform: ["babelify"]
      })
    .bundle()
    .pipe(source(file))
    .pipe(buffer())
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(uglify())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./public/js'))
    .pipe(gutil.noop())
})

Is there a way for me to create a build that bundles babel=>browserify=>uglify and still return me maps to my pre-babel files?

I also don't mind not using gulp (I actually prefer grunt but this setup used to work for me in the past).

3
  • 1
    Hi, Why a gutil.noop() at the end ?
    – JiDai
    Jan 5, 2016 at 10:58
  • The question states only give me the bundled version, not the bundled version. I'm assuming you mean only give me the bundled version and not the unbundled version. If so can you make the edit to update the question.
    – PatS
    Jan 8, 2020 at 16:08
  • My guess is that the gutil.noop() was added in an attempt to make another gulp task wait for the gulp.dest() to complete. But I don't think gulp works that way.
    – PatS
    Jan 8, 2020 at 16:10

1 Answer 1

11

First of all you need to set browserify to generate source maps by setting the debug option to true, then if you want the pre babelify source maps you need to configure babelify to generate source maps:

   var gulp       = require('gulp'),
    source     = require('vinyl-source-stream'),
    browserify = require('browserify'),
    gutil      = require('gulp-util'),
    buffer     = require('vinyl-buffer'),
    sourcemaps = require('gulp-sourcemaps'),
    uglify     = require('gulp-uglify'),
    babelify   = require('babelify')
    file       = 'index.js';

gulp.task('build', function(){
    return browserify(file,{debug:true}).transform(babelify, {presets: ["es2015", "react"],sourceMaps:true})
    .bundle()
    .pipe(source(file))
    .pipe(buffer())
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(uglify())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./build/js'))
    .pipe(gutil.noop())
})
3
  • 1
    I did this, but I am not able to put breakpoints in chrome debugger on some lines. Mostly on the lines that are not scoped in a function. Any tips for this?
    – Luka
    Mar 2, 2016 at 19:34
  • 3
    I've found that passing { compress: false } to uglify() fixes some odd breakpoint behavior in Chrome.
    – Arktronic
    Jan 9, 2017 at 17:50
  • I would not run uglify() at all when creating source maps.
    – PatS
    Jan 8, 2020 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.