Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ES6中使用Gulp编译React时出现语法错误_Javascript_Reactjs_Gulp_Ecmascript 6 - Fatal编程技术网

Javascript 在ES6中使用Gulp编译React时出现语法错误

Javascript 在ES6中使用Gulp编译React时出现语法错误,javascript,reactjs,gulp,ecmascript-6,Javascript,Reactjs,Gulp,Ecmascript 6,当我尝试使用React编译下面的代码时,我得到了下面的错误。在这样一个简单的程序中我看不到这个问题,当我克隆git repo时,示例代码可以正确编译 main.js: import React from 'react'; import HelloWorld from './components/helloworld'; //import HelloWorld from './hello-world-es5'; React.render( <HelloWorld phrase="E

当我尝试使用React编译下面的代码时,我得到了下面的错误。在这样一个简单的程序中我看不到这个问题,当我克隆git repo时,示例代码可以正确编译

main.js:

import React from 'react';
import HelloWorld from './components/helloworld';
//import HelloWorld from './hello-world-es5';

React.render(
    <HelloWorld phrase="ES6"/>,
    document.body
);
从“React”导入React;
从“./components/HelloWorld”导入HelloWorld;
//从“./hello-world-es5”导入HelloWorld;
反应(
,
文件正文
);
HelloWorld:

import React from 'react';


class HelloWorld extends React.Component {
    render() {
        return <h1>Hello from {this.props.phrase}!</h1>;
    }
}

export default HelloWorld;
从“React”导入React;
类HelloWorld扩展了React.Component{
render(){
从{this.props.phrase}返回Hello!;
}
}
导出默认HelloWorld;
错误:

SyntaxError: /Users/**/**/**/**/js/main.js: Unexpected token (7:4)
  5 | 
  6 | ReactDOM.render(
> 7 |     <HelloWorld phrase="ES6"/>,
    |     ^
  8 |     document.body
  9 | );
    at Parser.pp.raise 
Process finished with exit code 1
SyntaxError:/Users/**/**/**/**/js/main.js:意外标记(7:4)
5 | 
6 | ReactDOM.render(
> 7 |     ,
|     ^
8 |文件正文
9 | );
在Parser.pp.raise
进程已完成,退出代码为1

我在使用最新版本的babelify、browserify和react时遇到了这个问题。您需要为最新版本的babelify指定预设配置。es6吞咽任务可能如下所示:

'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));
});
请注意,正在向browserify传递一个配置对象,该配置对象包含transform属性,该属性用于包含同样使用对象配置的babelify变换。babelify配置对象包含预设。您还应安装您希望使用的预设。您可以在上阅读更多关于此的信息


我注意到的另一件事是,您没有提到您的Reactjs版本。最新版本为0.14.2,与0.13.3相比有一些重大变化。使用最新版本,您还需要react dom,并将其用作组件到dom的装载点。

您如何编译它?使用babelify吞咽。如有必要,我可以发布一饮而尽的dile。此时您如何配置
babelify
?如果您使用的是最新版本,则需要将
es2015
react
作为两个单独的预设启用。我建议您切换到高级网页包,除此之外,似乎您没有正确设置编译器来检测和编译jsxloganfsmyth,您是对的。这一要求似乎没有在一般的文档中体现出来。如果您愿意,您可以重新键入此答案,我将接受。