首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React.js - babel-loader -意外标记

React.js - babel-loader -意外标记
EN

Stack Overflow用户
提问于 2016-07-25 08:34:32
回答 1查看 717关注 0票数 1

我有我不能解决的问题。对于类似的问题,有很多答案,但它们并不适用(我认为)。

我有一些文件。让我们从package.json开始:

代码语言:javascript
复制
{
  "name": "react-tut",
  "version": "0.0.0",
  "description": "",
  "main": "webpack.config.js",
  "dependencies": {
    "babel-core": "^6.11.4",
    "babel-loader": "^6.2.4",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-plugin-react-html-attrs": "^2.0.0",
    "babel-plugin-transform-class-properties": "^6.11.5",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-0": "^6.5.0",
    "react": "^15.2.1",
    "react-dom": "^15.2.1",
    "webpack": "^1.13.1",
    "webpack-dev-server": "^1.14.1"
  },
  "devDependencies": {},
  "scripts": {
    "dev": "./node_modules/.bin/webpack-dev-server --content-base src --inline --hot",
    "setDebug": "set NODE_ENV=debug",
    "setProduction": "set NODE_ENV=production",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

webpack.config.js:

代码语言:javascript
复制
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
const path = require("path");

module.exports = {
    context: path.join(__dirname, "src"),
    devtool: debug ? "inline-sourcemap" : null,
    entry: "./js/client.js",
    module: {
        loaders: [
            {
                test: /\.js?$/,
                exclude: /(node_modules|bower|components)/,
                loader: 'babel-loader',
                query: {
                    presets: ['react', 'es2015', 'stage-0'],
                    plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy']
                }
            }
        ]
    },
    output: {
        path: path.join(__dirname, "src"),
        filename: "client.min.js"
    },
    plugins: debug ? [] : [
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin({mangle: false, sourcemap: false}),
    ],
};

client.js:

代码语言:javascript
复制
import React from "react";
import ReactDOM from "react-dom";

import Layout from "./components/Layout";

const app = document.getElementById('app');
ReactDOM.render(<Layout/>, app);

Layout.js:

代码语言:javascript
复制
import React from "react";

export default class Layout extends React.Component {
    render() {
        return <h1>Wololo?</h1>;
    }
}

当我运行webpack时,我得到这个错误:

代码语言:javascript
复制
ERROR in ./js/components/Layout.js
Module parse failed: C:\Users\onlin\WebstormProjects\react-tut\src\js\components\Layout.js Unexpected token (5:15)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (5:15)
    at Parser.pp.raise (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:923:13)
    at Parser.pp.unexpected (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:1490:8)
    at Parser.pp.parseExprAtom (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:333:12)
    at Parser.pp.parseExprSubscripts (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:228:19)
    at Parser.pp.parseMaybeUnary (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:207:17)
    at Parser.pp.parseExprOps (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:154:19)
    at Parser.pp.parseMaybeConditional (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:136:19)
    at Parser.pp.parseMaybeAssign (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:112:19)
    at Parser.pp.parseExpression (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:88:19)
    at Parser.pp.parseReturnStatement (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:1872:26)
    at Parser.pp.parseStatement (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:1737:19)
    at Parser.pp.parseBlock (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:2009:21)
    at Parser.pp.parseFunctionBody (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:610:22)
    at Parser.pp.parseMethod (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:579:8)
    at Parser.pp.parseClassMethod (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:2155:23)
    at Parser.pp.parseClass (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:2140:10)
    at Parser.pp.parseExprAtom (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:324:19)
    at Parser.pp.parseExprSubscripts (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:228:19)
    at Parser.pp.parseMaybeUnary (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:207:17)
    at Parser.pp.parseExprOps (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:154:19)
    at Parser.pp.parseMaybeConditional (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:136:19)
    at Parser.pp.parseMaybeAssign (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:112:19)
    at Parser.pp.parseExport (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:2181:21)
    at Parser.pp.parseStatement (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:1762:85)
    at Parser.pp.parseTopLevel (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:1666:21)
    at Parser.parse (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:1632:17)
    at Object.parse (C:\Users\onlin\WebstormProjects\react-tut\node_modules\acorn\dist\acorn.js:885:44)
    at Parser.parse (C:\Users\onlin\WebstormProjects\react-tut\node_modules\webpack\lib\Parser.js:902:15)
    at DependenciesBlock.<anonymous> (C:\Users\onlin\WebstormProjects\react-tut\node_modules\webpack\lib\NormalModule.js:104:16)
    at DependenciesBlock.onModuleBuild (C:\Users\onlin\WebstormProjects\react-tut\node_modules\webpack-core\lib\NormalModuleMixin.js:310:10)
 @ ./js/client.js 11:14-44

错误指向Layout.js中的<字符。我正在遵循this tutorial,在我将布局类移动到分离文件(从client.js)之后,这个错误开始出现。我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-25 10:11:25

在您的webpack配置文件中,components文件夹被设置为从babel中排除。

代码语言:javascript
复制
module: {
    loaders: [
        {
            test: /\.js?$/,
            exclude: /(node_modules|bower|components)/,
            loader: 'babel-loader',
            query: {
                presets: ['react', 'es2015', 'stage-0'],
                plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy']
            }
        }
    ]
}

因此,JSX结构中的组件<Layout>无法识别。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38558234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档