首页 > 解决方案 > Webpack:无法使用文件加载器显示 SVG 图像(svg 解析错误)

问题描述

我使用的 SVGrequire不会显示。

在我的终端中,发出了 svg 资产,并且在我的html.

<img src="/images/brand-illustration.f20767e375d3094978f5cc5c9726d0a6.svg">

但是,当 jpg 或 png 等其他格式有效时,SVG 不会显示。我尝试打开原始 svg 文件,它确实有效。当我检查发出的 svg 资产并将其加载到浏览器中时,如下所示:localhost:8080/images/brand-illustration.f20767e375d3094978f5cc5c9726d0a6.svg,我得到了parse error在此处输入图像描述

这就是我在pug文件中使用 svg 的方式

img(src=require('../../assets/img/brand-illustration.svg').default)

网络包配置

module.exports = {
    entry: {
        main: "./src/index.js"
    },
    output: {
        path: path.join(__dirname, "../build"),
        filename: "[name].bundle.js",
        publicPath: '/'
    },
    mode: "development",
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.s?css$/,
                use: [{
                    loader: MiniCssExtractPlugin.loader
                }, {
                    loader: 'css-loader',
                    options: {
                        sourceMap: true
                    }
                }, {
                    loader: 'resolve-url-loader',
                    options: {
                        sourceMap: true
                    }
                }, {
                    loader: 'sass-loader',
                    options: {
                        sourceMap: true
                    }
                }]
            }, {
                test: /\.pug$/,
                use: ["pug-loader"]
            },
            {
                test: /\.(png|svg|jpe?g|gif|webp)$/,
                use: [
                    {
                        loader: "file-loader",
                        options: {
                            limit: 10000,
                            outputPath: 'images',
                            name: '[name].[hash].[ext]'
                        }
                    },
                ]
            },
            {
                test: /\.(woff(2)?|ttf|otf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        limit: 10000,
                        name: '[name].[hash].[ext]',
                        outputPath: 'fonts'
                    }
                }]
            },
            {
                test: /\.html$/,
                use: {
                    loader: "html-loader?interpolate",
                    options: {
                        attrs: ["img:src", ":data-src"],
                        minimize: true
                    }
                }
            }
        ]
    },
    plugins: [
        // CleanWebpackPlugin will do some clean up/remove folder before build
        // In this case, this plugin will remove 'dist' and 'build' folder before re-build again
        new MiniCssExtractPlugin({
            filename: 'css/[name].css',
            chunkFilename: 'css/[name].[contenthash]_[id].css'
        }),
        new CleanWebpackPlugin(),
        // The plugin will generate an HTML5 file for you that includes all your webpack bundles in the body using script tags
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: path.join(__dirname, '..', '/src/index.pug'),
            inject: true,
            publicPath: '/'
        }),
        new HtmlWebpackPlugin({
            filename: 'about.html',
            template: path.join(__dirname, '..', '/src/about.pug'),
            inject: true,
            publicPath: '/'
        }),
        new HtmlWebpackPlugin({
            filename: 'works.html',
            template: path.join(__dirname, '..', '/src/works.pug'),
            inject: true,
            publicPath: '/'
        }),
        new HtmlWebpackPlugin({
            filename: 'contact.html',
            template: path.join(__dirname, '..', '/src/contact.pug'),
            inject: true,
            publicPath: '/'
        }),
        new HtmlWebpackPlugin({
            filename: '404.html',
            template: path.join(__dirname, '..', '/src/404.pug'),
            inject: true,
            publicPath: '/'
        })
    ].concat(htmlPlugins)
};

开发依赖

    "@babel/core": "^7.7.7",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/preset-env": "^7.7.7",
    "babel-loader": "^8.0.6",
    "brotli-webpack-plugin": "^1.1.0",
    "clean-webpack-plugin": "^3.0.0",
    "compression-webpack-plugin": "^3.0.1",
    "css-loader": "^3.4.1",
    "cssnano": "^4.1.0",
    "file-loader": "^5.0.2",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.9.0",
    "node-sass": "^4.13.0",
    "optimize-css-assets-webpack-plugin": "^5.0.3",
    "postcss-loader": "^3.0.0",
    "postcss-preset-env": "^6.7.0",
    "pug-html-loader": "^1.1.5",
    "pug-loader": "^2.4.0",
    "purgecss-webpack-plugin": "^1.6.0",
    "resolve-url-loader": "^3.1.2",
    "sass-loader": "^8.0.0",
    "style-loader": "^1.1.2",
    "terser-webpack-plugin": "^2.3.1",
    "url-loader": "^4.1.1",
    "webpack": "^4.41.5",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.10.1",
    "webpack-merge": "^5.7.0"

标签: svgwebpackwebpack-config

解决方案


删除file-loader使用并添加type: 'asset/resource'到 Webpack 配置中


推荐阅读