Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gulp, browserify, maps?

How do I enable source maps? I'm doing this:

  var browserify = require("gulp-browserify")

  gulp.task("compile:client", function() {
    gulp.src("src/client/app.coffee", {
      read: false
    })
    .pipe(browserify({
      debug: true // THIS DOES NOTHING :(
      transform: ['coffeeify'],
      extensions: ['.coffee']
    }))
    .pipe(rename('app.js'));
  });

Ouch... for some reason on the github page for gulp-browserify it says: PLUGIN IS BLACKLISTED.

I don't get it... how the heck I'm suppose to use browserify with my coffeescript files then?

UPD: Ha! I was wrong: debug option works. It just sticks source maps info right there into the output javascript file. Awesome. Still the question remains open: why this plugin in blacklisted?

like image 626
iLemming Avatar asked May 15 '14 00:05

iLemming


2 Answers

I found a solution, by crawling the web, and it looks like this:

var browserify = require('browserify');
var gulp = require('gulp');
var exorcist = require('exorcist');
var source = require('vinyl-source-stream');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps'); // https://www.npmjs.org/package/gulp-sourcemaps

gulp.task('browserify', function(){
    return browserify({
            entries: ['./file1.js'],
            debug: true
        })
        .bundle()
        .pipe(exorcist('./output.js.map'))
        .pipe(source('output.js'))
        .pipe(gulp.dest('./'));
});

gulp.task('together', ['browserify'], function() {
  return gulp.src('output.js')
    .pipe(sourcemaps.init({loadMaps: true}))
      .pipe(concat('all-with-maps.js'))
      .pipe(uglify())
    .pipe(sourcemaps.write('.', {addComment: true /* the default */, sourceRoot: '/src'}))
    .pipe(gulp.dest('dist'));
});

Make sure you have a recent version of browserify installed (I use 5.10.0 as of today).. You used to need to pass {debug: true} to the bundle() call .. but it has moved to browserify() directly.

Regarding the blacklisting: it is thought to be better to use browserify() directly, as we do here. There's no need for a plugin it would seem.

like image 109
abourget Avatar answered Nov 23 '22 02:11

abourget


Have a look over here:

https://github.com/gulpjs/plugins/issues/47

and here:

https://github.com/gulpjs/gulp/issues/369

UPDATE:

I don't think this below is "messy".

var source = require('vinyl-source-stream');
var browserify = require('browserify');

var bundler = browserify('./js/index.js');

gulp.task('compile', function(){
  return bundler.bundle({standalone: 'noscope'})
    .pipe(source('noscope.js'))
    .pipe(gulp.dest('./dist'));
});
like image 38
Mangled Deutz Avatar answered Nov 23 '22 02:11

Mangled Deutz