【问题标题】:My webpack.config.js cant read any image file - Angular 8我的 webpack.config.js 无法读取任何图像文件 - Angular 8
【发布时间】:2020-05-05 13:53:24
【问题描述】:

我正在构建一个 Angular 8 应用程序,但在构建它时出现错误。我不知道如何在 webpack 中加载图像。我已经尝试了一些方法,但没有任何效果。我收到此错误:

ERROR in ./src/assets/images/icon_transparent.png 1:0
Module parse failed: Unexpected character '�' (1:0)
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
(Source code omitted for this binary file)
 @ ./src/app/shared/navigation/navigation.component.html 1:265-326
 @ ./src/app/shared/navigation/navigation.component.ts
 @ ./src/app/app.module.ts
 @ ./src/main.ts

我使用的 webpack 版本会影响这个吗?

我的 webpack.config.js 就是这个:

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

module.exports = {
    entry: './src/main.ts',
    resolve: {
        extensions: ['.ts', '.js'],
        alias: {
            '@': path.resolve(__dirname, 'src/app/'),
        }
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: ['ts-loader', 'angular2-template-loader']
            },
            {
                test: /\.html$/,
                use: 'html-loader'
            },
            {
                test: /\.less$/,
                use: ['style-loader', 'css-loader', 'less-loader']
            },

            // workaround for warning: System.import() is deprecated and will be removed soon. Use import() instead.
            {
                test: /[\/\\]@angular[\/\\].+\.js$/,
                parser: { system: true }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({ template: './src/index.html' }),
        new webpack.DefinePlugin({
            // global app config object
            config: JSON.stringify({
                apiUrl: 'http://localhost:4000'
            })
        }),

        // workaround for warning: Critical dependency: the request of a dependency is an expression
        new webpack.ContextReplacementPlugin(
            /\@angular(\\|\/)core(\\|\/)fesm5/,
            path.resolve(__dirname, 'src')
        )
    ],
    optimization: {
        splitChunks: {
            chunks: 'all',
        },
        runtimeChunk: true
    },
    devServer: {
        historyApiFallback: true
    }
}

【问题讨论】:

  • 您是否阅读了错误消息提示的页面?您确实在规则列表中没有任何 .png 文件的加载器。

标签: angular webpack webpack-style-loader


【解决方案1】:

应该用作外部资源(如图像、字体等)而不是捆绑到 javascript 代码中的文件必须使用适当的webpack loader 加载。通常是 webpack 原生的file-loader。

// add this to your rules array
{
  test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
  use: 'file-loader'
}

More about loading images with webpack.

还可以考虑使用 angular-cli 代替 webpack。它通常工作得更快,而且你不会浪费时间编写 webpack 配置。

【讨论】:

    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 2023-04-06
    相关资源
    最近更新 更多