1

I have two gulp tasks: the first handles caching + babel + sourcemaps, the other one concatenates the translated files into one single JavaScript file + sourcemaps.

gulp.task('prepare:js', () => {
  const babelOptions = {
    "presets": [
      [
        "env",
        { "targets": { "browsers": ["firefox >= 24"]} }
      ]
    ]
  }
  return gulp.src('app/scripts')
    .pipe(changed('.tmp/scripts'))
    .pipe(sourcemaps.init())
    .pipe(babel(babelOptions))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('.tmp/scripts'))
})

gulp.task('package:js', ['prepapre:js'], () => {
  return gulp.src(['.tmp/scripts/*.js', '.tmp/scripts/**/*.js'])
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(concat('app.bundle.js'))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('dist/scripts))
})

It almost works as I want. The remaining issue is with the sourcemaps. Indeed, it generates a single sourcemap with duplicate informations:

  • One mapping between my original code (located in app/scripts) and the concatenated file thanks to loadMaps: true (that's the one I want to keep)
  • Another mapping between the babelified code (located in .tmp/scripts) and the concatenated code (that's the one I want to get rid of).

The thing is, if I remove the instruction .pipe(sourcemaps.init({loadMaps: true})), the first sourcemaps will no longer be included. I could also merge these two tasks but I would not be able to use gulp-changed anymore (since any change to a file located in app/scripts would trigger a concatenation and refresh the cache).

How can I get rid of the second mapping?

Simon
  • 1,679
  • 4
  • 21
  • 37
  • Wouldn't you have to remove both the `sourcemaps.init(...)` _and_ the `sourcemaps.write('.')` under the task `package:js`? Or is that what you were saying you already tried? – Patrick Roberts Jul 25 '17 at 19:25
  • Also, a side note, I think your `gulp.src()` is redundant for `package:js`. I'm pretty sure you can just do `gulp.src('.tmp/scripts/**/*.js')`. – Patrick Roberts Jul 25 '17 at 19:27
  • Removing the sourcemaps instructions in the `package:js` task would prevent any sourcemaps from being generated in `dist/scripts` (since they would not be migrated from `.tmp` to `dist`). Thanks for the `gulp.src` tip, though, you are definitely right. – Simon Jul 26 '17 at 08:31
  • Ah okay. Good luck with the problem then, I'm not sure how to approach that. – Patrick Roberts Jul 26 '17 at 08:37

0 Answers0