Babel无法解析js文件中的语法

时间:2019-09-06 00:09:54

标签: reactjs webpack babel babel-loader

我的index.js文件的try-catch语句中出现SyntaxError: /django/src/static/index.js: Unexpected token, expected "}"。我正在使用babel-loader尝试使它加载我的各种语法,但是我没有运气。我感觉自己在.babelrc文件中尝试了1000种组合,但无济于事。我的问题之一是,我不知道我使用的语法是什么,因此不知道如何配置babel来正确加载它。任何帮助表示赞赏。

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import createHistory from 'history/createBrowserHistory';

import { authLoginUserSuccess } from './actions/auth';
import Root from './containers/Root/Root';
import configureStore from './store/configureStore';



const initialState = {};
const target = document.getElementById('root');

const history = createHistory();
const store = configureStore(initialState, history);

const node = (
    <Provider>
        <Root store={store} history={history} />
    <Provider/>
);

const token = sessionStorage.getItem('token');
let user = {};
try {
    user = JSON.parse(sessionStorage.getItem('user'));
} catch (e) {
    // Failed to parse
}

if (token !== null) {
    store.dispatch(authLoginUserSuccess(token, user));
}

ReactDOM.render(node, target);

.babelrc

{
    "presets": [
        "@babel/preset-react",
        "@babel/preset-env",
        "@babel/preset-flow", 
    ],
    "plugins": [
        "@babel/plugin-proposal-logical-assignment-operators",
        ["@babel/plugin-proposal-optional-chaining", { "loose": false }],
        ["@babel/plugin-proposal-pipeline-operator", { "proposal": "minimal" }],
        ["@babel/plugin-proposal-nullish-coalescing-operator", { "loose": false }],
        "@babel/plugin-proposal-do-expressions",
        "@babel/plugin-proposal-export-default-from",
        "@babel/plugin-syntax-dynamic-import",
        ["@babel/plugin-proposal-class-properties", {loose: true}],
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ]
}

webpack.config.js

 resolve: {
        extensions: ['.jsx', '.js', '.json', '.scss', '.css'],
        modules: ['node_modules']
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                resolve: {
                  extensions: [".js", ".jsx"]
                },
                exclude: /node_modules/,
                use:[
                {
                    loader: 'babel-loader',
                    options: {
                        babelrc: true,
                 }
             ]
       }

0 个答案:

没有答案