How do I generate a sourcemap with babel using thi

2019-08-10 03:49发布

问题:

gulp.task('build:server:js', function(){
  return gulp.src("server/**/*.js")
    .pipe(sourcemaps.init())
    .pipe(babel({
      "presets": ["es2015", "react", "stage-0"]
    }))
    .pipe(sourcemaps.write('.', {
      includeContent: false,
      sourceRoot: function(file) {
        var from = file.path;
        var to = path.resolve(__dirname+'/../server');
        var dest = path.relative(from, to) + '/../server';

        console.log("from %s\nto: %s\ndest: %s\n", from, to, dest);
        return dest;
      }
    }))
    .pipe(gulp.dest("dist/server/"));
});

gulp tasks are in ./tasks folder, not ./ -- the build is generated in ./dist folder and should point back to ./path/to/src.js.

The dist folder has same structure as root project folder. (ie: ./server/core/routes.js for example ends up in ./dist/server/core/routes.js when transpiled along with the .maps file.