2016-09-12 10 views
0

ための "予期しないトークン" 私はtranspileしようとしたコードはこれです:ガルプ+ browserify + babelify:JSXコード

var gulp = require('gulp'); 
var browserify = require('browserify'); 
var babelify = require('babelify'); 
var source = require('vinyl-source-stream'); 

gulp.task('default', function() { 
    return browserify('./source/app.js') 
    .transform(babelify) 
    .bundle() 
    .pipe(source('snapterest.js')) 
    .pipe(gulp.dest('./build/')); 
}); 

しかし、私場合:

var React = require('react'); 
var ReactDOM = require('react-dom'); 

var listOfItems = <ul> 
        <li className="item-1">Item 1</li> 
        <li className="item-2">Item 2</li> 
        <li className="item-3">Item 3</li> 
        </ul>; 

ReactDOM.render(listOfItems, document.getElementById('react-application')); 

gulpfile.jsは次のようになりますgulpを起動しようと、それは私に、このエラーを与える:

[18:00:34] Using gulpfile ~/Documents/Esercitazioni/react/reactjs-essentials/snapterest/gulpfile.js 
[18:00:34] Starting 'default'... 
events.js:141 
     throw er; // Unhandled 'error' event 
    ^

SyntaxError: /Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/source/app.js: Unexpected token (4:18) 
    2 | var ReactDOM = require('react-dom'); 
    3 | 
> 4 | var listOfItems = <ul> 
    |     ^
    5 |      <li className="item-1">Item 1</li> 
    6 |      <li className="item-2">Item 2</li> 
    7 |      <li className="item-3">Item 3</li> 
    at Parser.pp.raise (/Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/location.js:22:13) 
    at Parser.pp.unexpected (/Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/util.js:91:8) 
    at Parser.pp.parseExprAtom (/Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:512:12) 
    at Parser.pp.parseExprSubscripts (/Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:270:19) 
    at Parser.pp.parseMaybeUnary (/Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:250:19) 
    at Parser.pp.parseExprOps (/Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:180:19) 
    at Parser.pp.parseMaybeConditional (/Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:157:19) 
    at Parser.pp.parseMaybeAssign (/Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:120:19) 
    at Parser.pp.parseVar (/Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/statement.js:585:24) 
    at Parser.pp.parseVarStatement (/Users/macbookpro/Documents/Esercitazioni/react/reactjs-essentials/snapterest/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/statement.js:416:8) 

答えて

0

は括弧のHELで<li>を包むいp? babelify docsから

var React = require('react'); 
var ReactDOM = require('react-dom'); 

var listOfItems = (
       <ul> 
       <li className="item-1">Item 1</li> 
       <li className="item-2">Item 2</li> 
       <li className="item-3">Item 3</li> 
       </ul> 
    ); 

ReactDOM.render(listOfItems, document.getElementById('react-application')); 
+0

いいえ! :[私に同じエラーを与える! –

+0

JSXを使わずにJavaScriptコードを使って同じコードを書くと、gulpはエラーを表示しません。 JSXのgulpのみを使用するとエラーになります。 –

+0

ああ、あなたのbabelifyが反応構文をサポートするように正しく構成されていないと思います。私はベルベリファの専門家ではないので、私はそれを最もよく修正する方法がわかりません –

5

$ npm install --save-dev babel-preset-es2015 babel-preset-react 

次に、あなたがそれらを使用するbabelifyを指示する必要があります:あなたはbabel-preset-es2015babel-preset-reactを必要があることを意味し、あなたの場合は

As of Babel 6.0.0 there are no plugins included by default. For babelify to be useful, you must also include some presets and/or plugins .

gulp.task('default', function() { 
    return browserify('./source/app.js') 
    .transform(babelify.configure({presets: ['es2015','react']})) 
    .bundle() 
    .pipe(source('snapterest.js')) 
    .pipe(gulp.dest('./build/')); 
}); 
+0

ありがとう、それは私のために働いた。 – user3181365