Webpack: cannot add audio (mp3) - javascript

I tried a lot to add audio to my webpack, so i can play sound every time user click on div.
Tried different ways, used loaders, but everything dont work.
JS:
const audio = new Audio('../../sounds/24.mp3')
audio.play()
Webpack config:
module: {
rules: [
{
test: /\.[tj]s$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.(?:ico|gif|png|jpg|jpeg|svg)$/i,
type: 'asset/resource',
},
{
test: /\.(woff(2)?|eot|ttf|otf)$/i,
type: 'asset/resource',
},
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
{
test: /\.s[ac]ss$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
},
{
test: /\.mp3$/i,
use: 'file-loader',
},
],
}
package.json:
"file-loader": "^6.2.0",
Every time i get this error:
GET http://localhost:8080/sounds/24.mp3 404 (Not Found)
I feel like I make a stupid mistake, but i cant realize where.

Related

Webpack - Include file multiple times

I want to include a file twice through two different loaders. The reasoning is I want to display code snippets in ES6 while allowing them to be run in browsers not supporting the syntax.
Effectively what I would like to achieve is the below but with both loaders results being included in the output -
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.(js|jsx)$/,
include: /app\/examples/,
use: [
{
loader: "file-loader",
options: {
regExp: /app\/examples\/([^\/]+)\/([^\.]+)+\.jsx?$/,
name: 'examples/[1]/[2].example',
}
}
]
}
With the above in my webpack config
import example from '../../examples/simple/ex1'
results in
Module {default: "examples/simple/ex1.example", __esModule: true, Symbol(Symbol.toStringTag): "Module"}
Rather than the code run through babel as I would have hoped for.
const multi = require('multi-loader');
const combineLoaders = require('webpack-combine-loaders');
module: {
loaders: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
include: /app\/examples/,
loader: multi(combineLoaders([
{ loader: 'file-loader' },
{ loader: 'babel-loader' },
]))
},
]
}
This should do the trick. you have to also use combineLoaders as you have to use options object. inside combine loaders array you can pass loader configuration also.
I couldn't manage to handle this with loaders in the end - although with further reading I don't believe this was the correct approach anyway. Instead i'm now using copy-webpack-plugin to copy the files -
plugins: [
new CopyWebpackPlugin([ {
from: path.join(rootDir, 'app', 'examples'),
to: path.join(outputDir, 'examples')
}])
],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: "babel-loader"
}
]
}

fontawesome-webfont and webpack.js configuration

I need to add the react-froala-wysiwyg editor to my ReactJs application. I installed the package and I insert this snippet in my component :
// Require Editor JS files.
import 'froala-editor/js/froala_editor.pkgd.min.js';
// Require Editor CSS files.
import 'froala-editor/css/froala_style.min.css';
import 'froala-editor/css/froala_editor.pkgd.min.css';
// Require Font Awesome.
import 'font-awesome/css/font-awesome.css';
import FroalaEditor from 'react-froala-wysiwyg';
<FroalaEditor model={this.state.infos} />
In the webpack.js :
module: {
rules: [
{ test: /\.tsx?$/, include: /ClientApp/, use: 'awesome-typescript-loader?silent=true' },
{ test: /\.css$/, use: isDevBuild ? ['style-loader', 'css-loader'] : ExtractTextPlugin.extract({ use: 'css-loader?minimize' }) },
{
test: /\.(png|jpg|jpeg|gif|svg)$/,
loader: 'url-loader?limit=1024&name=images/[name].[ext]',
exclude: /node_modules/
},
{
test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/font-woff"
}, {
test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/font-woff"
}, {
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=application/octet-stream"
}, {
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: "file"
}, {
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: "url?limit=10000&mimetype=image/svg+xml"
}
]
},
I get this error :
in ./~/font-awesome/fonts/fontawesome-webfont.woff2?v=4.7.0
in./~/font-awesome/fonts/fontawesome-webfont.eot?v=4.7.0
in ./~/font-awesome/fonts/fontawesome-webfont.eot in
in ./~/font-awesome/fonts/fontawesome-webfont.woff?v=4.7.0
in ./~/font-awesome/fonts/fontawesome-webfont.ttf?v=4.7.0
in ./~/font-awesome/fonts/fontawesome-webfont.svg?v=4.7.0
You may need an appropriate loader to handle this file type.
So I need to know how can I edit the webpack coonfig file to fix this issue ?
Thanks,
Looks like you could use file-loader -
Install with npm npm install file-loader
update your loaders:
{
test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/'
}
}]
}

How can I add style tags to single file components in vue-jekyll?

I've stumbled upon this repo vue-jekyll which is so close to my needs..
It's missing one thing though, the ability to add <style lang="scss" scoped>...</style> to the single file components, example here.
It seems that the original dev is MIA on this project - so I'm reaching out to anyone that might know how to add this functionality to Webpack. The current config is as follows:
module.exports = {
entry: {
// 🎌 OUR SOURCE FILE 🎌
jsSource: './js/source.js'
},
output: {
// 🎌 OUR DESTINATION 🎌
filename: './js/bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
},
{
test: /\.json$/,
use: 'json-loader'
},
{
test: /\.vue$/,
use: 'vue-loader'
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js' // 'vue/dist/vue.common.js' for webpack 1
}
}
}
Any ideas? :/

Webpack Exclude a specific file

I have this code In my webpack.config.prod.js and I was wondering how do I exclude all json except one in a specific path like src/configs/configs
exclude: [
/\.html$/,
/\.(js|jsx)$/,
/\.css$/,
/\.json$/,
/\.bmp$/,
/\.gif$/,
/\.jpe?g$/,
/\.png$/,
],
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
}
...
According to the Webpack documentation, you can do something like this.
exclude: {
test: [
/\.html$/,
/\.(js|jsx)$/,
/\.css$/,
/\.json$/,
/\.bmp$/,
/\.gif$/,
/\.jpe?g$/,
/\.png$/,
],
exclude: [
'src/configs/configs/your.json'
]
}
To make exclude work I had to escape the dot in the specific file I wanted to exclude. Here's an example of excluding favicon.ico from a general rule and adding a special rule for it:
{
test: /\.(ico|jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
exclude: /favicon\.ico$/,
loader: 'file-loader',
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
// A special rule for favicon.ico to place it into build root directory.
{
test: /favicon\.ico$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash:8]',
},
},
For Webpack 5:
{
test: /\.(png|svg|jpg|jpeg|gif)$/,
type: 'asset/resource',
},
{
test: /favicon\.ico$/,
type: 'asset/resource',
generator: {
filename: '[name][ext]',
},
},

Using loaders in webpack got error

What's wrong with my code below? I got error bundling less files, for example
ERROR in ./~/less-loader!./resources/assets/js/bundle/components/widget/clock.less
Below is my webpack.config.js
module: {
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loaders: ["react-hot", "babel-loader","babel?presets[]=react,presets[]=es2015,presets[]=stage-0"],
},
{
test: /\.less$/,
loaders: ['style-loader','less-loader']
}
]
}
I'm trying to implement hot reloading.
If you're using webpack v1:
module: {
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loaders: ["babel-loader","babel?presets[]=react,presets[]=es2015,presets[]=stage-0"],
},
{
test: /\.less$/,
loaders: ['style-loader','css-loader', 'less-loader']
}
]
}
or if you're using webpack v2:
module: {
rules: [
{
test: /\.js?$/,
exclude: /node_modules/,
use: ["babel-loader","babel?presets[]=react,presets[]=es2015,presets[]=stage-0"],
},
{
test: /\.less$/,
use: ['style-loader','css-loader', 'less-loader']
}
]
}
As your webpack is reaching the less-loader, I don't see any issue in configuration. Otherwise it prompts issue with webpack configuration.
Now you need to check your file here resources/assets/js/bundle/components/widget/clock.less
It is the place which is having issues.
If not please share complete error, this is just the first line of error.

Categories

Resources