mcasimir / gulp-rollup

gulp plugin for Rollup ES6 module bundler
MIT License
93 stars 12 forks source link

fix(sourcemaps): not allowing plugins to create sourcemaps #66

Closed dfenstermaker closed 6 years ago

dfenstermaker commented 6 years ago

The issue here is that if you want to use a plugin like rollup-plugin-sourcemaps to generate a sourcemap from sourcemaps you cannot. Since it appears you are expecting the files that are read in to already contain a sourcemap. By allowing the flag set on the config to remain as is you allow plugins to work as expected.

lemmabit commented 6 years ago

Ha! Funny, it wasn't even possible for a plugin like rollup-plugin-sourcemaps to exist until I submitted this Rollup PR: https://github.com/rollup/rollup/pull/715

I implemented that feature because I needed it in gulp-rollup 2.0.0. Five days later, I guess maxdavidson saw the potential and made that plugin.

Now, the idea behind your PR is a perfectly good one from a user's perspective, but I'm afraid Rollup's internals make it impossible for it to be useful. gulp-rollup and rollup-plugin-sourcemaps both work by providing a load hook for Rollup (which is why they both depend on that one change). If one of their loads gets called, the other won't, so they can't be used together. Rollup isn't very good at making this sort of thing necessarily obvious to end users.

Any Rollup plugin that synthesizes sourcemaps out of nothing has to do so by providing a load hook. They are thus all incompatible with gulp-rollup. There are a few reasonable ways in which this could be fixed, but right now, that's how it is.

The good news is that what you're trying to do is actually really easy without any Rollup plugins. You just have to pass the loadMaps: true option to sourcemaps.init():

var gulp       = require('gulp'),
    rollup     = require('gulp-rollup'),
    sourcemaps = require('gulp-sourcemaps');

gulp.task('bundle', function() {
  gulp.src('./src/**/*.js')
    .pipe(sourcemaps.init({ loadMaps: true }))
      // transform the files here.
      .pipe(rollup({
        input: './src/main.js'
      }))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('./dist'));
});

I hope that helps.

dfenstermaker commented 6 years ago

Thanks! That works great. That said it's terribly confusing on how to get source maps to work with gulp-rollup. I think the snippet you pasted here would be quite useful on the readme. I am sure a lot of people would find this useful, I spent quite a few hours figuring out why I wasnt getting source maps.

What I did in this PR did allow for gulp-rollup-sourcemaps to work with gulp-rollup so hard to understand what you mean. Anyway thanks for the help and good luck!

lemmabit commented 6 years ago

@dfenstermaker If it worked, that means you're feeding the files directly from the filesystem to gulp-rollup without modifying them at all via gulp. That isn't gulp-rollup's intended purpose. I strongly recommend using rollup-stream instead—it's a lot simpler and less confusing, and you'd be able to use rollup-plugin-sourcemaps.

dfenstermaker commented 6 years ago

Will check it out thanks