【问题标题】:Module parse failed: Unexpected token. Cannot load img tag模块解析失败:意外的令牌。无法加载 img 标签
【发布时间】:2019-04-01 08:08:08
【问题描述】:

我正在使用 react-table 以及作为 HOC 的 FoldableTable 的附加功能。运行构建脚本时出现以下错误

ERROR in ./node_modules/react-table/src/hoc/foldableTable/index.js 10:24
Module parse failed: Unexpected token (10:24)
You may need an appropriate loader to handle this file type.
|   const style = { width: 25 }
| 
>   if (collapsed) return <img src={right} style={style} alt="right" />
|   return <img src={left} style={style} alt="left" />

我的webpack.config.jsnpm run build 脚本如下:

const path = require("path");
const webpack = require("webpack");
const BundleTracker = require('webpack-bundle-tracker');

module.exports = {
  context: __dirname,

  entry: [
    'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:3000',
    'webpack/hot/only-dev-server',
    './src/index.js',
  ],

  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/app/track_dashboard/dist/',
  },

  devtool: 'source-map',

  devServer: {
    hot: true,
    contentBase: path.resolve(__dirname, 'dist'),
    publicPath: '/',
  },

  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },
      {
        test: /\.js?$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },

      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader?modules'],
      },
      {
        test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
        loader: 'url-loader?limit=100000'
      },
    ],
  },

  plugins: [
    new BundleTracker({filename: './webpack-stats.json'}),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(),
  ],
}

我的.babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}

我提供了url-loader 来处理svg 图像,但我想它仍然需要处理img 标记。这里有什么不同的做法?

我可以在这里提供一些上下文。 HOC有以下sn-p:

import React from 'react'
import left from './left.svg'
import right from './right.svg'

const defaultFoldIconComponent = ({ collapsed }) => {
  const style = { width: 25 }

  if (collapsed) return <img src={right} style={style} alt="right" />
  return <img src={left} style={style} alt="left" />
}

【问题讨论】:

    标签: javascript reactjs webpack webpack-dev-server react-table


    【解决方案1】:
    return (<img src={right} style={style} alt="right" />)
    

    use应该把它包装起来然后return()

    【讨论】:

      猜你喜欢
      • 2019-02-15
      • 2023-03-27
      • 2021-04-26
      • 2021-05-01
      • 2020-11-16
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多