我一直在按照这个指南使用PhpStorm将ES6代码转换为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
显然,由于存在所需的语句,构建脚本在客户端上无法工作。它会抛出
Uncaught ReferenceError: require is not defined
。是否有任何解决方案可以编译为工作脚本?我知道browserify,但我找不到任何关于如何将其集成到babel文件监听器中的信息。
监听器配置:
Program : /home/something/node_modules/.bin/babel
Arguments :$FileDir$ --source-maps --out-dir /something/js/build
Working directory: $ProjectFileDir$