什么webpack加载程序可以解析 html文件中的元素

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

我在尝试使用webpack构建的源代码中具有这种元素:

<svg
      version="1"
      xmlns="http://www.w3.org/2000/svg"
      width={size}
      height={size}
      viewBox={setView(options)}
      id={id}
    >
      {element}
</svg>

但是,当我尝试构建我的应用程序时,出现这种错误:

ERROR in ./src/lib/components/element.js 33:4
Module parse failed: Unexpected token (33:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 
|   return (
>     <svg
|       version="1"
|       xmlns="http://www.w3.org/2000/svg"
 @ ./src/lib/index.js 1:0-49 3:15-25
 @ ./src/components/Results.tsx
 @ ./src/components/PageContainer.tsx
 @ ./src/App.tsx
 @ ./src/index.tsx

我认为这是因为我没有包含svg标签的html文件的加载程序。我尝试安装svg-loader,但似乎只关心以.svg结尾的文件。为了使用webpack.config.js构建项目,我需要安装哪个模块?我正在使用webpack版本4.41.2

这是我的webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');


module.exports = {
  entry: {
    main: './src/index.tsx'
  },
  output: {
    path: path.join(__dirname, '../dist/'),
    filename: '[name].bundle.js',
  },
  watch: true,
  resolve: {
    extensions: ['.ts', '.tsx', '.js']
  },
  devServer: {
    contentBase: path.join(__dirname, '../dist/'),
    port: 2222
  },
  devtool: 'inline-source-map',
  module: {
    rules: [
      { test: /.tsx?$/, use: ['awesome-typescript-loader'] },
      { test: /.html$/, use: 'raw-loader' },
      { test:/\.(s*)css$/, use:['style-loader','css-loader', 'sass-loader'] },
      { test: /\.woff(\?.+)?$/, use: 'url-loader?limit=10000&mimetype=application/font-woff' },
      { test: /\.woff2(\?.+)?$/, use: 'url-loader?limit=10000&mimetype=application/font-woff' },
      { test: /\.ttf(\?.+)?$/, use: 'file-loader' },
      { test: /\.eot(\?.+)?$/, use: 'file-loader' },
      { test: /\.svg(\?.+)?$/, use: 'svg-inline-loader' },
      { test: /\.png$/, use: 'url-loader?mimetype=image/png' },
      { test: /\.gif$/, use: 'url-loader?mimetype=image/gif' }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'index.html',
      showErrors: true,
      title: 'React-TS-Webpack App',
      path: path.join(__dirname, '../dist/'),
      hash: true
    })
  ]

} 
javascript reactjs webpack-4
1个回答
0
投票

原来这与svg标签无关。我在webpack.config中缺少js文件的加载程序。一个简单的解决方法是添加一个像这样的babel-loader:

 { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/,
      query: {
          presets: ["@babel/env", "@babel/react"]
      } 
}
© www.soinside.com 2019 - 2024. All rights reserved.