SyntaxError:意外的令牌导入-reactjs

时间:2019-04-10 21:27:40

标签: javascript reactjs syntax-error babeljs

在我们的应用程序中,我们使用了由第一批开发人员创建的图标库,而新版本的react16.0 and higher)不再支持该图标

我们决定保留他们以前的外观,只是稍稍更改代码以使其工作并使其成为Component

这是现在的样子:

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import * as icons from 'icons';

class Icon extends Component {
    static propTypes = {
        type: PropTypes.oneOf(Object.keys(icons)).isRequired
    };

    render() {
        const {type, ...other} = this.props;
        const IconComponent = icons[type];

        return IconComponent ? (
            <IconComponent {...other}/>
        ) : null;
    }
}

export * from '/icons';
export default Icon;

理论上,一切都应该正常工作,因为此目录中的其他组件是以类似的方式编写的。

但是有一个我从未遇到过的错误:

00:14:58 App /home/cpt/Desktop/prod/local/app/components/Base/Elements/IconSocial/index.js:3
00:14:58 App import _Object$defineProperty from 'babel-runtime/core-js/object/define-property';
00:14:58 App ^^^^^^
00:14:58 App SyntaxError: Unexpected token import

错误指向此行:import * as icons from 'icons';

我们使用babel-core": "6.26.3"

请告诉我,可能是什么?预先感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

这是通天塔问题。在这里您可以了解到以下信息:https://github.com/babel/babel/issues/2877

可能的解决方案是将其添加到插件中:

"plugins": [
    ["transform-runtime", { "polyfill": false }]
]
相关问题