转换从es5到es6的反应

时间:2016-02-08 22:44:30

标签: reactjs gulp babeljs

我正在将现有的反应项目从es5转换为es6。我有babel-preset-react。当我将es6添加到myFile.js时,我收到错误:

  

解析文件时出现意外的令牌。

代码:

var React = require('react');
var Input = require('../common/textInput')

var ReviewTimeOffRequestPage = React.createClass({
  let m = 0;//add this and I get an error
  render: function() {

babel-preset-react是否需要单独的es6模块?

3 个答案:

答案 0 :(得分:1)

这就是预设提供的https://babeljs.io/docs/plugins/preset-react/

  • 语法流
  • 语法JSX
  • 变换流条类型
  • 变换反应的-JSX
  • 变换反应的显示名称

所以,基本上那些是jsx和特定于流的变换器。这意味着如果您使用上述5个变压器未涵盖的任何其他语言功能 - 您也必须将其插入。

PS:在你的情况下,对象文字中的语句无论如何都在语法上是不正确的。

{
    let m = 0;
}

上面的代码没有意义,因为那些必须是键值对(或函数表达式),而不是语句。

答案 1 :(得分:1)

babel-preset-reac不包含ES6功能,要启用ES6,您应添加babel-preset-es2015

答案 2 :(得分:0)

这就是我如何解决将反应从es5转换为es6的问题:

  

npm install babel-preset-react

然后配置.babelrc以使用es2015:

{
  "presets": ["react", "es2015"]
}
  linting(gulp-eslint)。在eslint.config.json中设置es6:true

{
  "ecmaFeatures": {
    "jsx": true
  },
  "env": {
    "browser": true,
    "node": true,
    "jquery": true,
    "es6": true
  },
  "rules": {
    "quotes": 0,
    "no-trailing-spaces": 0,
    "eol-last": 0,
    "no-unused-vars": 0,
    "no-underscore-dangle": 0,
    "no-alert": 0,
    "no-lone-blocks": 0
  },
  "globals": {
    "jQuery": true,
    "$": true
  }
}

从es5切换到babel-preset-react后,我遇到了jQuery无法找到的问题。奇怪的是,我将jQuery导入移到了入口点main.js的顶部,并使用了es6导入语法。

  

main.js

//For some reason when using babel-preset-es2015 the import needs to be
//defined on two lines using the import syntax.
//It also needs to be defined at the top of this file  
//$ = jQuery = require('jquery');//didnt work
//$ = require('jquery');//didn't work
//jQuery = require('jquery');//didnt work
import $ from 'jquery';
import jQuery from 'jquery';

var React = require('react');
//...
  

gulpfile食谱

   //Dependencies for reference
    var gulp = require('gulp');
    var connect = require('gulp-connect');//Runs a local dev server
    var open = require('gulp-open');//Open a URL in a web browser
    var browserify = require('browserify');//bundles js
    var reactify = require('reactify');//transforms react jsx to js
    var babel = require('babelify');;//transforms es6 to es5
    var sourcemaps = require('gulp-sourcemaps');//sourcemaps for debugging
    var source = require('vinyl-source-stream');//use conventional text streams with gulp
    var buffer = require('vinyl-buffer');//buffer to allow sourcemaps to work
    var concat = require('gulp-concat');//concatenates files
    var lint = require('gulp-eslint');//Lint js files, including JSX
    var uglify = require('gulp-uglify');

//....
gulp.task('js', function(){
  browserify(config.paths.mainJs,  { debug: true })
      .transform(babel)
      .bundle()
      .on('error', console.error.bind(console))
      .pipe(source('bundle.js'))
      .pipe(buffer())
      .pipe(sourcemaps.init({ loadMaps: true }))
          // Add transformation tasks to the pipeline here.
          .pipe(uglify())
      .pipe(sourcemaps.write('./'))
      .pipe(gulp.dest(config.paths.dist + '/scripts'))
      .pipe(connect.reload())
});

gulp.task('lint', function(){
  return gulp.src(config.paths.js)
            .pipe(lint({config: 'eslint.config.json'}))
            .pipe(lint.format());
});