I have the following gulpfile.js:
var gulp = require('gulp');
var browserify = require('gulp-browserify');
var concat = require('gulp-concat');
gulp.task('browserify', function() {
gulp.src('js/ScheduleMain.js')
.pipe(browserify({transform:'reactify'}))
.pipe(concat('ScheduleMain.js'))
.pipe(gulp.dest('static/dist/js'));
gulp.src('js/ConfidenceMain.js')
.pipe(browserify({transform:'reactify'}))
.pipe(concat('ConfidenceMain.js'))
.pipe(gulp.dest('static/dist/js'));
});
gulp.task('default',['browserify']);
gulp.task('watch', function() {
gulp.watch('src/**/*.*', ['default']);
});
As you can see I have two source files that need transforming. ScheduleMain.js is written in es5 and builds fine. I want to write my new application (ConfidenceMain.js) in es6 and possible transform it to es5 for build. I am a bit confused on how to do this (or rather if it is at all remended).
Bottom line: Can I move forward with new react projects written in es6 syntax despite having es5 code previously for other projects in the same code base?
I have the following gulpfile.js:
var gulp = require('gulp');
var browserify = require('gulp-browserify');
var concat = require('gulp-concat');
gulp.task('browserify', function() {
gulp.src('js/ScheduleMain.js')
.pipe(browserify({transform:'reactify'}))
.pipe(concat('ScheduleMain.js'))
.pipe(gulp.dest('static/dist/js'));
gulp.src('js/ConfidenceMain.js')
.pipe(browserify({transform:'reactify'}))
.pipe(concat('ConfidenceMain.js'))
.pipe(gulp.dest('static/dist/js'));
});
gulp.task('default',['browserify']);
gulp.task('watch', function() {
gulp.watch('src/**/*.*', ['default']);
});
As you can see I have two source files that need transforming. ScheduleMain.js is written in es5 and builds fine. I want to write my new application (ConfidenceMain.js) in es6 and possible transform it to es5 for build. I am a bit confused on how to do this (or rather if it is at all remended).
Bottom line: Can I move forward with new react projects written in es6 syntax despite having es5 code previously for other projects in the same code base?
Share Improve this question edited May 19, 2016 at 17:04 Luke Bennett 32.9k3 gold badges32 silver badges57 bronze badges asked May 19, 2016 at 16:27 ApathyBearApathyBear 9,63515 gold badges59 silver badges90 bronze badges 1- Your bottom line conflicts with your title question :/ – Washington Guedes Commented May 19, 2016 at 16:38
1 Answer
Reset to default 4Yes, you can mix both ES6 and ES5 - ES6 is fully backwards patible, so essentially you could think of your entire app as ES6, but only use the new syntax and functionality in new code.
You would need to add a transpilation step to your gulp pipeline to pass your code through babel and pile it down to ES5. Something like this:
var gulp = require('gulp');
var browserify = require('gulp-browserify');
var concat = require('gulp-concat');
var babel = require('gulp-babel');
gulp.task('browserify', function() {
gulp.src('js/ScheduleMain.js')
.pipe(browserify({transform:'reactify'}))
.pipe(concat('ScheduleMain.js'))
.pipe(gulp.dest('static/dist/js'));
gulp.src('js/ConfidenceMain.js')
.pipe(babel())
.pipe(browserify({transform:'reactify'}))
.pipe(concat('ConfidenceMain.js'))
.pipe(gulp.dest('static/dist/js'));
});
gulp.task('default',['browserify']);
gulp.task('watch', function() {
gulp.watch('src/**/*.*', ['default']);
});
Note that the code above wouldn't transpile ScheduleMain.js but you could easily do that if you wanted, to enable the use of ES6 features going forwards - just pipe it through babel()
in the same way.
Note that babel will require some configuration - the documentation will guide you through this. You'll want the es2015 and react presets.
Edit: Given your use of browserify, a cleaner approach might be to use the babelify transform instead:
gulp.src('js/ConfidenceMain.js')
.pipe(browserify({transform:'babelify'}))
.pipe(concat('ConfidenceMain.js'))
.pipe(gulp.dest('static/dist/js'));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745613603a4636098.html
评论列表(0条)