Debug NodeJS + ES6 app (Webstorm)

后端 未结 4 1095
别跟我提以往
别跟我提以往 2020-12-28 20:09

I want to use ES6 at both: client and server side. Of course, I can launch my NodeJS server from terminal like babel-node src/app.js, but it makes it impossible

4条回答
  •  不知归路
    2020-12-28 20:57

    You can use the below gulp babel task to compile your es6 files into es5. The generated files will be saved in dist directory. Put a breakpoint in the original es6 file eg. app.js and start a debug session for the generated file ie. dist/app.js (since node can’t run es6 files). The breakpoint in the original file will be hit.

    var gulp = require('gulp'),
        sourcemaps = require('gulp-sourcemaps'),
        babel = require('gulp-babel'),
        path = require('path'),
        gutil = require('gulp-util');
    
    // Compile ES6 to ES5
    gulp.task("babel", function () {
        return gulp.src('**/*.js')
            .pipe(sourcemaps.init())
            .pipe(babel())
            .on('error', gutil.log)
            .pipe(sourcemaps.write('.', {
                includeContent: false,
                sourceRoot: function(file) {
                    return path.relative(file.path, __dirname);
                }
            }))
            .pipe(gulp.dest('dist'));
    });
    

提交回复
热议问题