Babel与SyntaxError:意外令牌

时间:2018-11-14 13:55:22

标签: webpack babel webpack-dev-server webpack-2 babel-loader

我想使用babel在React中处理JSX,但是我总是收到一些错误的警报,这是我的webpack.config.js文件:

const path = require('path');

module.exports = {
    entry: {
        main: './src/index.js'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                loader: 'babel-loader'
            }
        ]
    },
    mode: "development"
}

这是我的package.json文件:

{
  "name": "Pro-React",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "watch": "webpack --watch",
    "start": "webpack-dev-server --open"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.7.0",
    "webpack": "^4.25.1",
    "webpack-cli": "^3.1.2",
    "webpack-dev-server": "^3.1.10"
  },
  "dependencies": {
    "react": "^16.6.3"
  }
}

这是.babelrc文件:

{
    "presets": ["env"]
  }

这是index.js文件:

import React from 'react';
class Hello extends Component {
    render() {
        return (
            <h1>Hello World</h1>
        );
    }
}

React.render(<Hello />, document.getElementById('root'));

它总是提醒我标题标签之间有意外令牌,谁能帮助我解决这个问题?

0 个答案:

没有答案