javascript - Webpack 编译 React.js 项目失败

标签 javascript reactjs webpack

我尝试使用 webpack v2.2.1 和 web pack-dev-sever v2.4.1 构建我的 React 模板项目。当我运行“webpack-dev-server --progress --hot --inline --colors”时,web pack 报告“编译失败”。

我检查了我的 .js 文件和配置文件,但找不到错误。谁能帮我解决这个问题?谢谢。

错误信息

ERROR in ./src/app.js
Module parse failed: /Users/liszt/Develop/react-project/src/app.js Unexpected token (7:9)
You may need an appropriate loader to handle this file type.
| const App = {
|   render() {
|       return <div>
|           Hi
|       </div>
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/app.js
webpack: Failed to compile.

这些是我的 app.js 文件和 web pack 配置

应用程序.js

import React from 'react'
import ReactDOM from 'react-dom'

const App = {
    render() {
        return <div>
            Hi
        </div>
    }
}

ReactDOM.render(
    <App />,
    document.getElementById('app')
)

webpack.config.js

const Webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin'); // For css bundle

// For minifying the js. The config is for ignoring the warning msgs
const uglifyJsCfg = new Webpack.optimize.UglifyJsPlugin({
    compress: {
        warnings: false
    }
})

const extractCommonsCfg = new Webpack.optimize.CommonsChunkPlugin({
    name: 'vender',
    filename: 'vender.js'
})

const extraCssCfg = new ExtractTextPlugin('style.css')

module.exports = {
    entry: {
        app: './src/app.js',
        vender: [
            'react',
            'react-dom',
        ]
    },

    output: {
        path: `${__dirname}/dist`,
        filename: "app.js"
    },

    module: {
        rules: [
            {
                test: /src\/\.jsx?$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader'
                    }
                ]
            },
            {
                test: /assets\/less\/\.less$/,
                loader: ExtractTextPlugin
            },
            {
                test: /assets\/img\/\.(png|jpe?g|gif|svg)(\?.*)?$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 10000
                        }
                    }
                ]
            }
        ]
    }, 

    plugins: [
        extractCommonsCfg,
        extraCssCfg,
        uglifyJsCfg,
        new Webpack.NamedModulesPlugin()
    ]
}

我的.babelrc

{
    presets: ['es2015', 'react'],
    plugins: ["transform-object-rest-spread"],
    env: {
        "production": {
            "plugins": ["transform-react-remove-prop-types"]
        }
    }
}

最佳答案

这个测试正则表达式 test:/src\/\.jsx?$/,

可能需要一个 * 在里面:test:/src\/\*.jsx?$/,

因为您已经声明了入口点,所以您不一定需要在test 中指定目录src。所以这是更可取的:

测试:/\.jsx?$/,

关于javascript - Webpack 编译 React.js 项目失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42609567/

相关文章:

javascript - 高效获取下拉列表中的选定选项(XHTML Select 元素)

html - NextJS html [lang] 默认缺失

node.js - 如何通过 configureWebpack 向 Vuepress 组件公开 Node 环境变量?

javascript - React 身份验证 session 管理

reactjs - React.memo 和 withRouter

reactjs - 用于生产的 Preact 和 Webpack

webpack - 即使使用 babel 插件,解构赋值在 IE 11 中也不起作用

javascript - jquery window.onscroll 监听器显示 :none and display:inline not appearing in Safari

javascript - 调整窗口大小时 HTML/CSS 导航栏和图像移动

javascript - 如何确定网站是在 iPhone/Android 浏览器上打开还是从 iPhone/Android 应用程序网页 View 打开?