我一直在关注这个guide,将ES6代码转换为PhpStorm中的ES5代码.
我的问题是当脚本有一个import语句时:
import TestingView from 'component/TestingView';
class ItemPage extends React.Component {
render () {
return <TestingView>
}
}
TestingView.js
export default class TestingView extends React.Component{
render(){
return <div>Hello!!!!</div>
}
}
文件监视器将其转换为以下形式:
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _TestingView = require('component/TestingView');
var _TestingView2 = _interopRequireDefault(_TestingView);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
* Created by laukaichung on 7/10/16.
*/
var ItemPage = function (_React$Component) {
_inherits(ItemPage, _React$Component);
function ItemPage() {
_classCallCheck(this, ItemPage);
return _possibleConstructorReturn(this, Object.getPrototypeOf(ItemPage).apply(this, arguments));
}
_createClass(ItemPage, [{
key: 'render',
value: function render() {
return React.createElement(_TestingView2.default, null);
}
}]);
return ItemPage;
}(React.Component);
//# sourceMappingURL=item_page.js.map
显然,构建的脚本不能在客户端工作,因为有一个必需的语句.它会抛出我未捕获的ReferenceError:require未定义.
有没有解决方案在工作脚本中编译它?我知道browserify但我找不到任何关于它如何被集成到babel文件观察器的信息.
观察者配置:
Program : /home/something/node_modules/.bin/babel
Arguments :$FileDir$--source-maps --out-dir /something/js/build
Working directory: $ProjectFileDir$
解决方法:
请参阅require is not defined:您正在将ES6模块编译为CommonJS格式,因此问题.您需要使用Browserify或WebPack捆绑您的模块,或者编译为AMD格式(transform-es2015-modules-amd)并在yoiur应用程序中包含Require.js