我非常困惑为什么在
components.js
(React 的挂载点)中需要 // require('');
。如果删除该行注释,则会出现 "SyntaxError: Unexpected token import" 错误。目前,我还不知道为什么一行注释会影响转译。我也不确定这是 React、babelify、browserify、browserify-rails 还是 Rails 资产管道的问题。
请参考 https://github.com/sidazhou/react_rails_skeleton/tree/v0.0.1 获取完整代码库。
components.js
// require(''); // somehow this is necessary, why?! Otherwise we get: "SyntaxError: Unexpected token import"
import React from 'react';
import ReactDOM from 'react-dom';
import Widgets from './components/Widgets.jsx';
ReactDOM.render( <Widgets />, document.getElementById('react_component') );
package.json
{
"name": "react-sample",
"dependencies": {
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babelify": "^7.2.0",
"browserify": "^12.0.1",
"browserify-incremental": "^3.0.1",
"history": "^1.13.1",
"material-ui": "^0.13.4",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"react-router": "^1.0.2",
"react-tap-event-plugin": "^0.2.1"
},
"engines": {
"node": ">= 0.10"
}
}
application.rb
config.browserify_rails.commandline_options = "-t [ babelify --presets [ es2015 react ] ]"
{"babel-preset-es2015": "^6.5.0", "babel-preset-react": "^6.5.0"}
并没有帮助解决问题。 - Sida Zhourequire('');
? - Jeremy Rodi// rquire('');
不起作用,// require()
起作用。感觉越来越像解析器在某个地方出错了。 - Sida Zhou