如何使用内联 带有webpack的React应用程序中的标签

问题描述 投票:0回答:1

我在使用webpack编译React应用程序时遇到错误:

You may need an appropriate loader to handle this file type. <svg

这起初似乎是一个容易解决的问题,但我的问题是我找不到与我的用例匹配的任何答案。我没有从svg扩展名的文件中导入.svg

相反,我的svgs在React组件中:

例:

import React from 'react';
import PropTypes from 'prop-types';

const SvgIcon = ({ icon: Icon, ...rest }) => (
  <span>
    <Icon { ...rest } />
  </span>
);

图标:

import React from 'react';

const TickIcon = (props) => (
  <svg { ...props }>
    <path
      d="M7.8,15.5c-0.3,0-0.7-0.1-0.9-0.3l-5-4.6c-0.5-0.5-0.5-1.2,0-1.7c0.5-0.5,1.3-0.5,1.8,0l4.1,3.8l8.5-7.8  c0.5-0.5,1.3-0.5,1.8,0c0.5,0.5,0.5,1.2,0,1.7l-9.4,8.6C8.5,15.4,8.2,15.5,7.8,15.5"
    />
  </svg>
);

export default TickIcon;

像这样使用:

<SvgIcon icon={ TickIcon } />

我找不到任何方法来完成这项工作,我安装了大量不同的svg加载器,并在我的webpack配置中实现它们,但由于没有实际的svg文件,它们都没有工作?

这是我的webpack.config

const webpack = require('webpack');
const path = require('path');

const HtmlWebPackPlugin = require('html-webpack-plugin');

const htmlPlugin = new HtmlWebPackPlugin({
  template: './source/index.html',
  filename: 'index.html',
});

const buildPlugin = new webpack.DefinePlugin({
  BUILD_INFO: JSON.stringify('BUILD-000'),
});

module.exports = {
  entry: './source/client.js',
  output: {
    path: path.resolve('build'),
    filename: 'bundled.js',
    publicPath: '/',
  },
  module: {
    rules: [
      {
        test: /\.js$|\.jsx$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
    ],
  },
  resolve: {
    alias: {
      api: path.resolve(__dirname, './source/js/api'),
      config: path.resolve(__dirname, './source/js/config'),
      components: path.resolve(__dirname, './source/js/components'),
      init: path.resolve(__dirname, './source/js/init'),
      context: path.resolve(__dirname, './source/js/context'),
      views: path.resolve(__dirname, './source/js/views'),
      utilities: path.resolve(__dirname, './source/js/utilities'),
      helpers: path.resolve(__dirname, './source/js/helpers'),
      store: path.resolve(__dirname, './source/js/store'),
      styles: path.resolve(__dirname, './source/styles'),
    },
    extensions: ['.js', '.jsx'],
  },
  plugins: [htmlPlugin, buildPlugin],
  watchOptions: {
    aggregateTimeout: 300,
    poll: 1000,
  },
  devServer: {
    disableHostCheck: true,
    historyApiFallback: true,
    port: 80,
    hot: false,
    host: '0.0.0.0',
    stats: {
      assets: true,
      children: false,
      chunks: false,
      hash: false,
      modules: false,
      publicPath: false,
      timings: true,
      version: false,
      warnings: true,
      colors: true,
    },
  },
};

谢谢

svg webpack webpack-loader
1个回答
0
投票

您显示的代码不会导入ReactDOM

import ReactDOM from 'react-dom'

如果您使用的是JSX语法,那肯定是必需的。

此外,没有为babel-loader说明的选项。您至少需要将@babel/preset-react作为预设来编译它:

module: {
  rules: [
    {
      test: /\.js$|\.jsx$/,
      exclude: /node_modules/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env', '@babel/preset-react']
        }
      }
    }
  ]
}

@babel/preset-env不是严格需要的,但会进行下编译,这样您就不必担心浏览器的兼容性。

© www.soinside.com 2019 - 2024. All rights reserved.