Serving mp3 files using the webpack file loader

后端 未结 5 504
名媛妹妹
名媛妹妹 2020-12-31 01:57

I have a problem with getting my mp3 files to work using the webpack file loader.

This is the issue:

I have a mp3 file on my harddisk, that if I open using c

相关标签:
5条回答
  • 2020-12-31 02:14

    https://stackoverflow.com/a/41158166/11878375 - it's correctly answer, but define SRC like this:

    var path = require('path');
    
    var SRC = path.resolve(__dirname, 'src/main/js');
    

    For example, I am using react-chat-ui with webpack and this is my webpack.config.js:

    const path = require('path');
    
    const SRC = path.resolve(__dirname, 'node_modules');
    
    module.exports = {
      entry: './jsx/App.jsx',
      mode: "development",
      output: {
        path:
            '/java/helios-backend/src/main/resources/static/js'
            // __dirname + '/js/'
        ,
        filename: 'bundle.js'
      },
      devtool: '#sourcemap',
      stats: {
       colors: true,
       reasons: true
      },
      module: {
        rules: [
          { test: /\.css$/, loader: 'style-loader!css-loader' },
          {
            test: /\.jsx?$/,
            exclude: /(node_modules)/,
            loaders: ['babel-loader']
          },
          {
            test: /\.(gif|png|jpe?g|svg)$/i,
            use: [
              'file-loader',
              {
                loader: 'image-webpack-loader',
                options: {
                  bypassOnDebug: true,
                  disable: true, 
                },
              },
            ]},
          {
            test: /\.mp3$/,
            include: SRC,
            loader: 'file-loader'
          }
    
        ]
      }
    };
    

    And do not forget to install the file-loader before:

    npm install file-loader --save-dev
    
    0 讨论(0)
  • 2020-12-31 02:30

    Check the version of the file-loader, I solved this by upgrading the file-loader from 4.x.x to 6.x.x, then add mp3 to the normal webpack config plugin settings for files =>

    // webpack.config.js
    
          { 
            test: /\.(gif|png|jpe?g|svg|xml|mp3)?$/i,
            use: "file-loader"
          }
          // OR
          {
            test: /\(mp3|wav|mpe?g|ogg)?$/i,
            use: 'file-loader'
          }
          
          // OR BOTH

    0 讨论(0)
  • 2020-12-31 02:31

    This is how I process mp3 files using Webpack in Nuxt 2:

      build: {
      
        loaders: {
          vue: {
            transformAssetUrls: {
              audio: 'src',
            },
          },
        },
    
        extend(config, ctx) {
          config.module.rules.push({
            test: /\.mp3$/,
            loader: 'file-loader',
            options: {
              name: '[path][name].[ext]',
            },
          })
        },
      },

    Now you can write <audio src="@/assets/water.mp3"></audio> in your templates and it should work.

    0 讨论(0)
  • 2020-12-31 02:36

    My solution: install the file-loader. Update webpack.config.js:

    // webpack.config.js
    rules: [
                {
                    test: /\.mp3$/,
                    loader: 'file-loader',
                    options: {
                        name: '[path][name].[ext]'
                    }
                }
    

    Project.js :

    const smthn = require('../sound.mp3');
    const sound = new Audio('./sound.mp3');
    (() => sound.play())();
    
    0 讨论(0)
  • 2020-12-31 02:37

    Use file-loader like this:

    {
        test: /\.mp3$/,
        include: SRC,
        loader: 'file-loader'
    }
    
    0 讨论(0)
提交回复
热议问题