简体   繁体   English

Webpack + React:模块解析失败:意外的令牌:您可能需要一个合适的加载器

[英]Webpack + React: Module parse failed: Unexpected token: You may need an appropriate loader

Stuck with simple ReactJs + Webpack structure initialization. 坚持简单的ReactJs + Webpack结构初始化。 Not sure where I missed but it should be something very silly. 不知道我错过了什么,但它应该是非常愚蠢的东西。

Can someone please spot where is the error or what I am doing wrong? 有人可以找出错误在哪里或我做错了什么?

Simple ReactJs Code: index.js 简单的ReactJs代码: index.js

import React from "react";
import render from "react-dom";

class App extends React.Component {
    render() {
        return <p>hello world</p>;
    }
}

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

Webpack Config Webpack配置

import ExtraneousFileCleanupPlugin from 'webpack-extraneous-file-cleanup-plugin';

import manifestPlugin from 'webpack-manifest-plugin';
import CleanWebpackPlugin from 'clean-webpack-plugin';
import webpack from 'webpack';
import path from 'path';

let pathsToClean = [   
    'sp/common/css/*.css',
    'sp/common/js/*.js',
];

// the clean options to use
let cleanOptions = {
    root:     path.resolve( __dirname, '../public'),
    exclude:  [],
    verbose:  false,
    dry:      false
};

module.exports = {

    entry: {
        'common/js/index' : [
            './src/js/index.jsx',
        ],
        'common/js/vendor': [
            'react', 'react-dom', 'react-router','axios',
            'redux', 'react-redux', 'redux-logger', 'redux-thunk', 'redux-promise-middleware'
        ],
    },
    output: {
        path: path.resolve(__dirname, '../public/'),
        filename: 'sp/[name].[chunkhash].js'
    },
    module: {
        rules: [
            {

                test: /\.js?$/,
                include: path.resolve(__dirname, './src/js'),
                exclude: [
                    path.resolve(__dirname, './build/'),
                    path.resolve(__dirname, './node_modules/'),
                ],
                loader: 'babel',
                options:{
                    babelrc: false,
                    presets: ['env', 'es2015', 'react', 'stage-2'],
                },
            },
            {
                enforce: "pre",
                test: /\.js?$/,
                include: path.resolve(__dirname, '/../src/js'),
                exclude: [
                    path.resolve(__dirname, '/../build/'),
                    path.resolve(__dirname, '/../node_modules/'),
                ],
                loader: 'eslint-loader',
            },    
        ],
    },

    plugins: [

        new webpack.optimize.CommonsChunkPlugin({
            name: "common/js/common",
            filename: "/sp/common/js/common.[chunkhash].js"
        }),
        new ExtraneousFileCleanupPlugin({
            extensions: ['.js'],
            minBytes: 1000,
            manifestJsonName: '/public/sp.manifest.json',
            paths: [
                'sp/common/css',
                'sp/common/js',
            ]
        }),
        new manifestPlugin({
            'options': {
                writeToFileEmit: true,
            },
            fileName: 'sp.manifest.json',
        }),
        new CleanWebpackPlugin(pathsToClean, cleanOptions), // clean the folders after generating new file
    ],

};

Error : 错误

RROR in ./src/js/index.js
Module parse failed: Unexpected token (13:15)
You may need an appropriate loader to handle this file type.
| class App extends React.Component {
|     render() {
|         return <p>hello world</p>;
|     }
| }
 @ multi ./src/js/index.js

Webpack Version: 3.10 React Version: 16.2 React-dom: 16.2 Webpack版本: 3.10 React版本: 16.2 React-dom: 16.2

Solved it. 解决了它。 My includ , exclude folder for babel-loader and eslint-loader was wrong. 我的includbabel-loadereslint-loader exclude文件夹是错误的。 Corrected it and its working fine. 纠正了它和它的工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模块解析失败:意外的令牌(7:5)您可能需要适当的加载程序来处理此文件类型。 :Webpack,Bootstrap - Module parse failed: Unexpected token (7:5) You may need an appropriate loader to handle this file type. : Webpack, Bootstrap React/Zusand - 模块解析失败:意外的令牌。 您可能需要适当的加载程序来处理此文件类型 - React/Zustand - Module parse failed: Unexpected token. You may need an appropriate loader to handle this file type React - 模块解析失败:意外的令牌。 您可能需要适当的加载程序来处理此文件类型 - React - Module parse failed: Unexpected Token. You may need an appropriate loader to handle this file type Webpack 4:模块解析失败:意外字符“@”。 你可能需要一个合适的加载器来处理这种文件类型…… - Webpack 4: Module parse failed: Unexpected character '@'. You may need an appropriate loader to handle this file type… 模块解析失败:意外令牌 (41:41) 您可能需要适当的加载程序来处理此文件类型 - Module parse failed: Unexpected token (41:41) You may need an appropriate loader to handle this file type Web3-react 错误模块解析失败:意外令牌您可能需要一个适当的加载程序来处理此文件类型。 web3-反应/钱包连接 - Web3-react Error Module parse failed: Unexpected token You may need an appropriate loader to handle this file type. web3-react/walletconnect webpack错误:模块解析失败您可能需要适当的加载程序来处理此文件类型 - webpack Error :Module parse failed You may need an appropriate loader to handle this file type Webpack 无法加载 pdf 文件:模块解析失败 您可能需要适当的加载程序来处理此文件类型 - Webpack Cannot load pdf file: Module parse failed You may need an appropriate loader to handle this file type Webpack 4 - 模块解析失败:您可能需要适当的加载程序来处理此文件类型 - Webpack 4 - Module parse failed: You may need an appropriate loader to handle this file type React-如何解决模块解析失败错误:您可能需要适当的加载器来处理此文件类型 - React - How to solve Module Parse Failed Error: You may need an appropriate loader to handle this file type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM