首页 > 解决方案 > Webpack 文件加载器提供图像但不提供音频 (mp3)

问题描述

我正在使用 Webpack 和文件加载器编写一个 TypeScript 应用程序来提供图像和音频。我无法让 webpack 配置处理音频文件。

我越来越GET http://localhost:8080/media/d.mp3 404 (Not Found)

这些图像在开发服务器上工作正常,并且通过下面的 webpack 设置也可以很好地构建。

文件夹结构:

|-public    
|---audio    
|---images    
|-src    
|---audio    
|---images

如果我将所有扩展名组合成一个字符串并使用单个文件夹,则结果相同。

我的webpack.config.js

const path = require('path');

module.exports = {
    entry: "./src/index.ts",
    output: {
        path: path.resolve(__dirname, 'public/scripts'),
        filename: 'bundle.js'
    },
    devtool: "source-map",
    module: {
        rules: [{
                test: /\.tsx?$/,
                loader: "awesome-typescript-loader"
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.(scss|sass|css)$/,
                exclude: /node_modules/,
                use: ['style-loader', 'css-loader', 'sass-loader']
            },
            {
                test: /\.(png|jpe?g|gif)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: '[name].[ext]',                            
                            outputPath: '../images/',
                            publicPath: 'images/',
                            useRelativePaths: true
                        }
                    }
                ]
            },
            {
                test: /\.(mp3|wav)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {                            
                            name: '[name].[ext]',                            
                            outputPath: '../audio/',
                            publicPath: 'audio/',
                            useRelativePaths: true
                        }
                    }
                ]
            },
        ]
    },
    resolve: {
        extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },
    devServer: {
        contentBase: path.resolve(__dirname, 'public'),
        publicPath: '/scripts/'
    },
    node: {
        fs: "empty"
    }
};

标签: javascriptwebpackwebpack-file-loader

解决方案


推荐阅读