Syntax Error when using Gulp to compile React in ES6

别等时光非礼了梦想. 提交于 2019-12-06 08:37:28

I ran into this problem while using the latest versions of babelify, browserify, and react. You need to specify a presets configuration for the latest versions of babelify. An es6 gulp task might look something like this:

'use strict';

import browserify from 'browserify';

import babelify from 'babelify';
import reactPreset from 'babel-preset-react';
import es2015Preset from 'babel-preset-es2015';

import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';

import gulp from 'gulp';
import gutil from 'gulp-util';
import uglify from 'gulp-uglify';
import sourcemaps from 'gulp-sourcemaps';

import config from '../config'; //externalized json config file

gulp.task('compile', function () {
    var b = browserify({
        entries: config.entries,
        debug: true,
        transform: [babelify.configure({'presets': [reactPreset, es2015Preset]})]
    });

    return b.bundle()
        .pipe(source(config.source))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(uglify())
        .on('error', gutil.log)
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(config.dest));
});

Note that the browserify is being passed a configuration object that contains the transform property for containing the babelify transform that is also being configured with a object. The babelify configuration object contains the presets. You should also npm install the presets that you wish to use. You can read more about this at https://github.com/babel/babelify#options.

One other thing that I noticed is that you don't mention your Reactjs version. The latest is 0.14.2 which made some significant changes over 0.13.3. Using the latest you would also need react-dom and use that as the mount point for your component to the DOM.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!