How convert .jsx to .js with Gulp and Babel?

前端 未结 5 928
余生分开走
余生分开走 2021-01-01 16:38

I need to convert all /src/.jsx files to /src/.js

Before I used gulp-react:

var react = require(\'gulp-react\');

gulp.task(\'jsx\', functio         


        
5条回答
  •  说谎
    说谎 (楼主)
    2021-01-01 17:09

    For latest React v16.8.x using Babel 7, use @babel/preset-env and @babel/preset-react modules.

    npm i -D @babel/preset-env @babel/preset-react
    

    Then in your gulpfile.js:

    var gulp = require('gulp');
    var babel = require('gulp-babel');
    
    gulp.task("whatever_task_name_you_prefer", function(){
        return gulp.src("src/jsx/*.jsx")
            .pipe(babel({
                presets: ["@babel/preset-env", @babel/preset-react"]
            }))
            .pipe(gulp.dest("dist"));
    });
    

提交回复
热议问题